1212import java .net .URISyntaxException ;
1313import java .net .URL ;
1414import java .util .List ;
15+ import java .util .Optional ;
16+ import java .util .concurrent .ExecutionException ;
1517import org .junit .After ;
1618import org .junit .Assert ;
1719import org .junit .Before ;
1820import org .junit .BeforeClass ;
1921import org .junit .Rule ;
2022import org .junit .Test ;
2123import org .jvnet .hudson .test .JenkinsRule ;
24+ import com .gargoylesoftware .htmlunit .WebAssert ;
25+ import com .gargoylesoftware .htmlunit .html .HtmlCheckBoxInput ;
26+ import com .gargoylesoftware .htmlunit .html .HtmlPage ;
2227import com .mathworks .ci .MatlabBuilder .RunTestsAutomaticallyOption ;
2328import com .mathworks .ci .MatlabBuilder .RunTestsWithCustomCommandOption ;
2429
@@ -38,6 +43,7 @@ public class MatlabBuilderTest {
3843 private MatlabBuilder matlabBuilder ;
3944 private static URL url ;
4045 private static String FileSeperator ;
46+ private static String VERSION_INFO_XML_FILE = "VersionInfo.xml" ;
4147
4248 @ Rule
4349 public JenkinsRule jenkins = new JenkinsRule ();
@@ -83,12 +89,13 @@ public void testTearDown() {
8389 }
8490
8591 private String getMatlabroot (String version ) throws URISyntaxException {
86- ClassLoader classLoader = MatlabBuilderTest . class . getClassLoader () ;
87- String matlabRoot = new File (
88- classLoader . getResource ("versioninfo/" + version + "/VersionInfo.xml" ). toURI ())
89- . getAbsolutePath (). replace ( FileSeperator + "VersionInfo.xml" , "" );
90- return matlabRoot ;
92+ String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE ;
93+ String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE ;
94+ URL matlabRootURL = Optional . ofNullable ( getResource (userVersionInfo )). orElseGet (() -> getResource ( defaultVersionInfo ));
95+ File matlabRoot = new File ( matlabRootURL . toURI () );
96+ return matlabRoot . getAbsolutePath (). replace ( FileSeperator + VERSION_INFO_XML_FILE , "" ). replace ( "R2017a" , version ) ;
9197 }
98+
9299
93100 /*
94101 * Test Case to verify if Build step contains "Run MATLAB Tests" option.
@@ -279,4 +286,141 @@ public void verifyCustomCommandInvokedForBatchMode() throws Exception {
279286 jenkins .assertLogContains ("-batch" , build );
280287 jenkins .assertLogContains ("runtests" , build );
281288 }
289+
290+ /*
291+ * Test to verify if Automatic option passes appropriate test atrtifact values.
292+ */
293+
294+ @ Test
295+ public void verifyRunTestAutomaticallyIsDefault () throws Exception {
296+ this .matlabBuilder .setLocalMatlab (getMatlabroot ("R2018b" ));
297+ FreeStyleBuild build = getBuildforRunTestAutomatically ();
298+ jenkins .assertLogContains ("-batch" , build );
299+ jenkins .assertLogContains ("true,true,true" , build );
300+ }
301+
302+ /*
303+ * Test to verify default value of getStringByName() when Automatic test mode.
304+ */
305+
306+ @ Test
307+ public void verifyDefaultValueOfgetStringByName () throws Exception {
308+ this .matlabBuilder .setLocalMatlab (getMatlabroot ("R2018b" ));
309+ RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption ();
310+ Assert .assertNull (runOption .getStringByName ("fakeChkBox" ));
311+ }
312+
313+ /*
314+ * Test to verify default value of getBooleanByName() when Custom test mode.
315+ */
316+
317+ @ Test
318+ public void verifyDefaultValueOfgetBooleanByName () throws Exception {
319+ this .matlabBuilder .setLocalMatlab (getMatlabroot ("R2018b" ));
320+ RunTestsWithCustomCommandOption runOption = new RunTestsWithCustomCommandOption ();
321+ Assert .assertFalse (runOption .getBooleanByName ("fakeCommand" ));
322+ }
323+
324+ /*
325+ * Test to verify when MATLAB version is older the R2017a
326+ */
327+
328+ @ Test
329+ public void verifyMatlabVersionOlderThanR17a () throws Exception {
330+ this .matlabBuilder .setLocalMatlab (getMatlabroot ("R2016b" ));
331+ FreeStyleBuild build = getBuildforRunTestAutomatically ();
332+ jenkins .assertLogContains ("-r" , build );
333+ jenkins .assertLogContains ("try,exit(" , build );
334+ }
335+
336+ /*
337+ * Test To verify if UI throws an error when MATLAB root is empty.
338+ *
339+ */
340+
341+ @ Test
342+ public void verifyEmptyMatlabRootError () throws Exception {
343+ project .getBuildersList ().add (this .matlabBuilder );
344+ HtmlPage page = jenkins .createWebClient ().goTo ("job/test0/configure" );
345+ WebAssert .assertTextPresent (page , TestMessage .getValue ("Builder.matlab.root.empty.error" ));
346+ }
347+
348+ /*
349+ * Test To verify UI does throw error when in valid MATLAB root entered
350+ *
351+ */
352+
353+ @ Test
354+ public void verifyInvalidMatlabRootDisplaysError () throws Exception {
355+ project .getBuildersList ().add (this .matlabBuilder );
356+ this .matlabBuilder .setLocalMatlab ("/fake/matlab/path" );
357+ HtmlPage page = jenkins .createWebClient ().goTo ("job/test0/configure" );
358+ WebAssert .assertTextPresent (page , TestMessage .getValue ("Builder.invalid.matlab.root.error" ));
359+ }
360+
361+ /*
362+ * Test To verify UI does not throw any error when valid MATLAB root entered
363+ *
364+ */
365+
366+ @ Test
367+ public void verifyValidMatlabRootDoesntDisplayError () throws Exception {
368+ project .getBuildersList ().add (this .matlabBuilder );
369+ this .matlabBuilder .setLocalMatlab (getMatlabroot ("R2018b" ));
370+ HtmlPage page = jenkins .createWebClient ().goTo ("job/test0/configure" );
371+ WebAssert .assertTextNotPresent (page , TestMessage .getValue ("Builder.invalid.matlab.root.error" ));
372+ }
373+
374+ /*
375+ * Test To verify UI displays Cobertura Warning message when unsupported MATLAB version used.
376+ *
377+ */
378+
379+ @ Test
380+ public void verifyCoberturaWarning () throws Exception {
381+ project .getBuildersList ().add (this .matlabBuilder );
382+ this .matlabBuilder .setLocalMatlab (getMatlabroot ("R2017a" ));
383+ HtmlPage page = jenkins .createWebClient ().goTo ("job/test0/configure" );
384+ HtmlCheckBoxInput coberturaChkBx = page .getElementByName ("taCoberturaChkBx" );
385+ coberturaChkBx .setChecked (true );
386+ Thread .sleep (2000 );
387+ WebAssert .assertTextPresent (page , TestMessage .getValue ("Builder.matlab.cobertura.support.warning" ));
388+ }
389+
390+ /*
391+ * Test To verify UI displays Cobertura Error message when invalid MATLAB root entered.
392+ *
393+ */
394+
395+ @ Test
396+ public void verifyCoberturaError () throws Exception {
397+ project .getBuildersList ().add (this .matlabBuilder );
398+ this .matlabBuilder .setLocalMatlab ("/fake/matlab/path" );
399+ HtmlPage page = jenkins .createWebClient ().goTo ("job/test0/configure" );
400+ HtmlCheckBoxInput coberturaChkBx = page .getElementByName ("taCoberturaChkBx" );
401+ coberturaChkBx .setChecked (true );
402+ Thread .sleep (2000 );
403+ String pageText = page .asText ();
404+ String filteredPageText = pageText .replaceFirst (TestMessage .getValue ("Builder.invalid.matlab.root.error" ), "" );
405+ Assert .assertTrue (filteredPageText .contains (TestMessage .getValue ("Builder.invalid.matlab.root.error" )));
406+ }
407+
408+ /*
409+ * Private helper methods for tests
410+ */
411+
412+ private FreeStyleBuild getBuildforRunTestAutomatically () throws InterruptedException , ExecutionException {
413+ RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption ();
414+ runOption .setTaCoberturaChkBx (true );
415+ runOption .setTaJunitChkBx (true );
416+ runOption .setTatapChkBx (true );
417+ this .matlabBuilder .setTestRunTypeList (runOption );
418+ project .getBuildersList ().add (this .matlabBuilder );
419+ FreeStyleBuild build = project .scheduleBuild2 (0 ).get ();
420+ return build ;
421+ }
422+
423+ private URL getResource (String resource ) {
424+ return MatlabBuilderTest .class .getClassLoader ().getResource (resource );
425+ }
282426}
0 commit comments