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

Commit c7b7a91

Browse files
committed
Added unit tests to improve code coverage
1 parent 466c0d4 commit c7b7a91

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import org.junit.Rule;
2020
import org.junit.Test;
2121
import org.jvnet.hudson.test.JenkinsRule;
22+
import com.gargoylesoftware.htmlunit.WebAssert;
23+
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
24+
import com.gargoylesoftware.htmlunit.html.HtmlPage;
2225
import com.mathworks.ci.MatlabBuilder.RunTestsAutomaticallyOption;
2326
import com.mathworks.ci.MatlabBuilder.RunTestsWithCustomCommandOption;
2427

@@ -279,4 +282,132 @@ public void verifyCustomCommandInvokedForBatchMode() throws Exception {
279282
jenkins.assertLogContains("-batch", build);
280283
jenkins.assertLogContains("runtests", build);
281284
}
285+
286+
/*
287+
* Test to verify if Automatic option passes appropriate test atrtifact values.
288+
*/
289+
290+
@Test
291+
public void verifyRunTestAutomaticallyIsDefault() throws Exception {
292+
this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b"));
293+
RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption();
294+
runOption.setTaCoberturaChkBx(true);
295+
runOption.setTaJunitChkBx(true);
296+
runOption.setTatapChkBx(true);
297+
this.matlabBuilder.setTestRunTypeList(runOption);
298+
project.getBuildersList().add(this.matlabBuilder);
299+
FreeStyleBuild build = project.scheduleBuild2(0).get();
300+
jenkins.assertLogContains("-batch", build);
301+
jenkins.assertLogContains("true,true,true", build);
302+
}
303+
304+
/*
305+
* Test to verify default value of getStringByName() when Automatic test mode.
306+
*/
307+
308+
@Test
309+
public void verifyDefaultValueOfgetStringByName() throws Exception {
310+
this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b"));
311+
RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption();
312+
Assert.assertNull(runOption.getStringByName("fakeChkBox"));
313+
}
314+
315+
/*
316+
* Test to verify default value of getBooleanByName() when Custom test mode.
317+
*/
318+
319+
@Test
320+
public void verifyDefaultValueOfgetBooleanByName() throws Exception {
321+
this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b"));
322+
RunTestsWithCustomCommandOption runOption = new RunTestsWithCustomCommandOption();
323+
Assert.assertFalse(runOption.getBooleanByName("fakeCommand"));
324+
}
325+
326+
/*
327+
* Test to verify when MATLAB version is older the R2017a
328+
*/
329+
330+
@Test
331+
public void verifyMatlabVersionOlderThanR17a() throws Exception {
332+
this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b"));
333+
RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption();
334+
runOption.setTaCoberturaChkBx(true);
335+
runOption.setTaJunitChkBx(true);
336+
runOption.setTatapChkBx(true);
337+
this.matlabBuilder.setTestRunTypeList(runOption);
338+
project.getBuildersList().add(this.matlabBuilder);
339+
FreeStyleBuild build = project.scheduleBuild2(0).get();
340+
jenkins.assertLogContains("-batch", build);
341+
jenkins.assertLogContains("true,true,true", build);
342+
}
343+
344+
/*
345+
* Test To verify if UI throws an error when MATLAB root is empty.
346+
*
347+
*/
348+
349+
@Test
350+
public void verifyEmptyMatlabRootError() throws Exception {
351+
project.getBuildersList().add(this.matlabBuilder);
352+
HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure");
353+
WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.root.empty.error"));
354+
}
355+
356+
/*
357+
* Test To verify UI does not throw any error when valid MATLAB root entered
358+
*
359+
*/
360+
361+
@Test
362+
public void verifyInvalidValidMatlabRoot() throws Exception {
363+
project.getBuildersList().add(this.matlabBuilder);
364+
this.matlabBuilder.setLocalMatlab("/fake/matlab/path");
365+
HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure");
366+
WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.error"));
367+
}
368+
369+
/*
370+
* Test To verify UI does not throw any error when valid MATLAB root entered
371+
*
372+
*/
373+
374+
@Test
375+
public void verifyValidMatlabRoot() throws Exception {
376+
project.getBuildersList().add(this.matlabBuilder);
377+
this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b"));
378+
HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure");
379+
WebAssert.assertTextNotPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.error"));
380+
}
381+
382+
/*
383+
* Test To verify UI displays Cobertura Warning message when unsupported MATLAB version used.
384+
*
385+
*/
386+
387+
@Test
388+
public void verifyCoberturaWarning() throws Exception {
389+
project.getBuildersList().add(this.matlabBuilder);
390+
this.matlabBuilder.setLocalMatlab(getMatlabroot("R2017a"));
391+
HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure");
392+
HtmlCheckBoxInput coberturaChkBx = page.getElementByName("taCoberturaChkBx");
393+
coberturaChkBx.setChecked(true);
394+
WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.cobertura.support.warning"));
395+
}
396+
397+
/*
398+
* Test To verify UI displays Cobertura Error message when invalid MATLAB root entered.
399+
*
400+
*/
401+
402+
@Test
403+
public void verifyCoberturaError() throws Exception {
404+
project.getBuildersList().add(this.matlabBuilder);
405+
this.matlabBuilder.setLocalMatlab("/fake/matlab/path");
406+
HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure");
407+
HtmlCheckBoxInput coberturaChkBx = page.getElementByName("taCoberturaChkBx");
408+
coberturaChkBx.setChecked(true);
409+
String pageText = page.asText();
410+
String filteredPageText = pageText.replaceFirst(TestMessage.getValue("Builder.invalid.matlab.root.error"), "");
411+
Assert.assertTrue(filteredPageText.contains(TestMessage.getValue("Builder.invalid.matlab.root.error")));
412+
}
282413
}

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.

0 commit comments

Comments
 (0)