Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 8cd9ec0

Browse files
authored
Merge pull request #19 from mathworks/code_coverage
Added unit tests to improve code coverage
2 parents f4a5fba + ea257c8 commit 8cd9ec0

File tree

4 files changed

+156
-7
lines changed

4 files changed

+156
-7
lines changed

src/test/java/com/mathworks/ci/MatlabBuilderTest.java

Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
import java.net.URISyntaxException;
1313
import java.net.URL;
1414
import java.util.List;
15+
import java.util.Optional;
16+
import java.util.concurrent.ExecutionException;
1517
import org.junit.After;
1618
import org.junit.Assert;
1719
import org.junit.Before;
1820
import org.junit.BeforeClass;
1921
import org.junit.Rule;
2022
import org.junit.Test;
2123
import 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;
2227
import com.mathworks.ci.MatlabBuilder.RunTestsAutomaticallyOption;
2328
import 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
}

src/test/java/com/mathworks/ci/TestMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public String getBuildIgnoresTestFailure() {
2525
return rb.getString(VERIFY_BUILD_IGNORES_TEST_FAILURE);
2626
}
2727

28-
public String getValue(String key) {
28+
public static String getValue(String key) {
2929

3030
return rb.getString(key);
3131
}

src/test/resources/testconfig.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
#This is the test properties file for all static values used across test classes.
33

44
Verify.matlab.invokes.positive = MATLAB is invoking positive tests
5-
Verify.build.ignore.test.failure = Build Ignored test failure
5+
Verify.build.ignore.test.failure = Build Ignored test failure
6+
Builder.matlab.cobertura.support.warning = To generate a Cobertura report, use MATLAB R2017b or a newer version.
7+
Builder.invalid.matlab.root.error = Unable to launch MATLAB from the specified location. Verify the MATLAB root folder path.
8+
Builder.matlab.root.empty.error = Full path to the MATLAB root folder is required.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright 2019 The MathWorks, Inc.
2+
#This is dummy file created as resource

0 commit comments

Comments
 (0)