From c7b7a91f28c59a5b03f5298bd7d0b3b0d820eb27 Mon Sep 17 00:00:00 2001 From: nbhoski Date: Tue, 30 Apr 2019 14:54:47 +0530 Subject: [PATCH 1/4] Added unit tests to improve code coverage --- .../com/mathworks/ci/MatlabBuilderTest.java | 131 ++++++++++++++++++ .../java/com/mathworks/ci/TestMessage.java | 2 +- src/test/resources/testconfig.properties | 5 +- 3 files changed, 136 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java index b18fdccf..89c97930 100644 --- a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java +++ b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java @@ -19,6 +19,9 @@ import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; +import com.gargoylesoftware.htmlunit.WebAssert; +import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; +import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.mathworks.ci.MatlabBuilder.RunTestsAutomaticallyOption; import com.mathworks.ci.MatlabBuilder.RunTestsWithCustomCommandOption; @@ -279,4 +282,132 @@ public void verifyCustomCommandInvokedForBatchMode() throws Exception { jenkins.assertLogContains("-batch", build); jenkins.assertLogContains("runtests", build); } + + /* + * Test to verify if Automatic option passes appropriate test atrtifact values. + */ + + @Test + public void verifyRunTestAutomaticallyIsDefault() throws Exception { + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); + RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption(); + runOption.setTaCoberturaChkBx(true); + runOption.setTaJunitChkBx(true); + runOption.setTatapChkBx(true); + this.matlabBuilder.setTestRunTypeList(runOption); + project.getBuildersList().add(this.matlabBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("-batch", build); + jenkins.assertLogContains("true,true,true", build); + } + + /* + * Test to verify default value of getStringByName() when Automatic test mode. + */ + + @Test + public void verifyDefaultValueOfgetStringByName() throws Exception { + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); + RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption(); + Assert.assertNull(runOption.getStringByName("fakeChkBox")); + } + + /* + * Test to verify default value of getBooleanByName() when Custom test mode. + */ + + @Test + public void verifyDefaultValueOfgetBooleanByName() throws Exception { + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); + RunTestsWithCustomCommandOption runOption = new RunTestsWithCustomCommandOption(); + Assert.assertFalse(runOption.getBooleanByName("fakeCommand")); + } + + /* + * Test to verify when MATLAB version is older the R2017a + */ + + @Test + public void verifyMatlabVersionOlderThanR17a() throws Exception { + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); + RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption(); + runOption.setTaCoberturaChkBx(true); + runOption.setTaJunitChkBx(true); + runOption.setTatapChkBx(true); + this.matlabBuilder.setTestRunTypeList(runOption); + project.getBuildersList().add(this.matlabBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("-batch", build); + jenkins.assertLogContains("true,true,true", build); + } + + /* + * Test To verify if UI throws an error when MATLAB root is empty. + * + */ + + @Test + public void verifyEmptyMatlabRootError() throws Exception { + project.getBuildersList().add(this.matlabBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.root.empty.error")); + } + + /* + * Test To verify UI does not throw any error when valid MATLAB root entered + * + */ + + @Test + public void verifyInvalidValidMatlabRoot() throws Exception { + project.getBuildersList().add(this.matlabBuilder); + this.matlabBuilder.setLocalMatlab("/fake/matlab/path"); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.error")); + } + + /* + * Test To verify UI does not throw any error when valid MATLAB root entered + * + */ + + @Test + public void verifyValidMatlabRoot() throws Exception { + project.getBuildersList().add(this.matlabBuilder); + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertTextNotPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.error")); + } + + /* + * Test To verify UI displays Cobertura Warning message when unsupported MATLAB version used. + * + */ + + @Test + public void verifyCoberturaWarning() throws Exception { + project.getBuildersList().add(this.matlabBuilder); + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2017a")); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput coberturaChkBx = page.getElementByName("taCoberturaChkBx"); + coberturaChkBx.setChecked(true); + WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.cobertura.support.warning")); + } + + /* + * Test To verify UI displays Cobertura Error message when invalid MATLAB root entered. + * + */ + + @Test + public void verifyCoberturaError() throws Exception { + project.getBuildersList().add(this.matlabBuilder); + this.matlabBuilder.setLocalMatlab("/fake/matlab/path"); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput coberturaChkBx = page.getElementByName("taCoberturaChkBx"); + coberturaChkBx.setChecked(true); + String pageText = page.asText(); + String filteredPageText = pageText.replaceFirst(TestMessage.getValue("Builder.invalid.matlab.root.error"), ""); + Assert.assertTrue(filteredPageText.contains(TestMessage.getValue("Builder.invalid.matlab.root.error"))); + } } diff --git a/src/test/java/com/mathworks/ci/TestMessage.java b/src/test/java/com/mathworks/ci/TestMessage.java index 24972e6a..f8842832 100644 --- a/src/test/java/com/mathworks/ci/TestMessage.java +++ b/src/test/java/com/mathworks/ci/TestMessage.java @@ -25,7 +25,7 @@ public String getBuildIgnoresTestFailure() { return rb.getString(VERIFY_BUILD_IGNORES_TEST_FAILURE); } - public String getValue(String key) { + public static String getValue(String key) { return rb.getString(key); } diff --git a/src/test/resources/testconfig.properties b/src/test/resources/testconfig.properties index 089d9485..603cb72e 100644 --- a/src/test/resources/testconfig.properties +++ b/src/test/resources/testconfig.properties @@ -2,4 +2,7 @@ #This is the test properties file for all static values used across test classes. Verify.matlab.invokes.positive = MATLAB is invoking positive tests -Verify.build.ignore.test.failure = Build Ignored test failure \ No newline at end of file +Verify.build.ignore.test.failure = Build Ignored test failure +Builder.matlab.cobertura.support.warning = To generate a Cobertura report, use MATLAB R2017b or a newer version. +Builder.invalid.matlab.root.error = Unable to launch MATLAB from the specified location. Verify the MATLAB root folder path. +Builder.matlab.root.empty.error = Full path to the MATLAB root folder is required. \ No newline at end of file From 74da9a451fb132d23f82e841b7aac7335eba5883 Mon Sep 17 00:00:00 2001 From: nbhoski Date: Tue, 30 Apr 2019 15:58:03 +0530 Subject: [PATCH 2/4] Added sleep to allow rendering error/warning on UI --- src/test/java/com/mathworks/ci/MatlabBuilderTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java index 89c97930..4d19693a 100644 --- a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java +++ b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java @@ -391,6 +391,7 @@ public void verifyCoberturaWarning() throws Exception { HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); HtmlCheckBoxInput coberturaChkBx = page.getElementByName("taCoberturaChkBx"); coberturaChkBx.setChecked(true); + Thread.sleep(2000); WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.cobertura.support.warning")); } @@ -406,6 +407,7 @@ public void verifyCoberturaError() throws Exception { HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); HtmlCheckBoxInput coberturaChkBx = page.getElementByName("taCoberturaChkBx"); coberturaChkBx.setChecked(true); + Thread.sleep(2000); String pageText = page.asText(); String filteredPageText = pageText.replaceFirst(TestMessage.getValue("Builder.invalid.matlab.root.error"), ""); Assert.assertTrue(filteredPageText.contains(TestMessage.getValue("Builder.invalid.matlab.root.error"))); From 5965ea26339c136ad29b1e67925a3a4b527361e0 Mon Sep 17 00:00:00 2001 From: nbhoski Date: Thu, 2 May 2019 16:44:58 +0530 Subject: [PATCH 3/4] Updated as per review comments --- .../com/mathworks/ci/MatlabBuilderTest.java | 58 +++++++++++-------- .../resources/versioninfo/R2016b/READ.txt | 2 + 2 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 src/test/resources/versioninfo/R2016b/READ.txt diff --git a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java index 4d19693a..09b1cd00 100644 --- a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java +++ b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java @@ -12,6 +12,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.List; +import java.util.concurrent.ExecutionException; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -87,9 +88,17 @@ public void testTearDown() { private String getMatlabroot(String version) throws URISyntaxException { ClassLoader classLoader = MatlabBuilderTest.class.getClassLoader(); - String matlabRoot = new File( - classLoader.getResource("versioninfo/" + version + "/VersionInfo.xml").toURI()) - .getAbsolutePath().replace(FileSeperator + "VersionInfo.xml", ""); + String matlabRoot; + if (classLoader.getResource("versioninfo/" + version + "/VersionInfo.xml") != null) { + matlabRoot = new File( + classLoader.getResource("versioninfo/" + version + "/VersionInfo.xml").toURI()) + .getAbsolutePath().replace(FileSeperator + "VersionInfo.xml", ""); + } else { + matlabRoot = new File( + classLoader.getResource("versioninfo/" + "R2017a" + "/VersionInfo.xml").toURI()) + .getAbsolutePath().replace(FileSeperator + "VersionInfo.xml", "") + .replace("R2017a", version); + } return matlabRoot; } @@ -290,13 +299,7 @@ public void verifyCustomCommandInvokedForBatchMode() throws Exception { @Test public void verifyRunTestAutomaticallyIsDefault() throws Exception { this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); - RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption(); - runOption.setTaCoberturaChkBx(true); - runOption.setTaJunitChkBx(true); - runOption.setTatapChkBx(true); - this.matlabBuilder.setTestRunTypeList(runOption); - project.getBuildersList().add(this.matlabBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); + FreeStyleBuild build = getBuildforRunTestAutomatically(); jenkins.assertLogContains("-batch", build); jenkins.assertLogContains("true,true,true", build); } @@ -329,16 +332,10 @@ public void verifyDefaultValueOfgetBooleanByName() throws Exception { @Test public void verifyMatlabVersionOlderThanR17a() throws Exception { - this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); - RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption(); - runOption.setTaCoberturaChkBx(true); - runOption.setTaJunitChkBx(true); - runOption.setTatapChkBx(true); - this.matlabBuilder.setTestRunTypeList(runOption); - project.getBuildersList().add(this.matlabBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("-batch", build); - jenkins.assertLogContains("true,true,true", build); + this.matlabBuilder.setLocalMatlab(getMatlabroot("R2016b")); + FreeStyleBuild build = getBuildforRunTestAutomatically(); + jenkins.assertLogContains("-r", build); + jenkins.assertLogContains("try,exit(", build); } /* @@ -354,12 +351,12 @@ public void verifyEmptyMatlabRootError() throws Exception { } /* - * Test To verify UI does not throw any error when valid MATLAB root entered + * Test To verify UI does throw error when in valid MATLAB root entered * */ @Test - public void verifyInvalidValidMatlabRoot() throws Exception { + public void verifyInvalidMatlabRootDisplaysError() throws Exception { project.getBuildersList().add(this.matlabBuilder); this.matlabBuilder.setLocalMatlab("/fake/matlab/path"); HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); @@ -372,7 +369,7 @@ public void verifyInvalidValidMatlabRoot() throws Exception { */ @Test - public void verifyValidMatlabRoot() throws Exception { + public void verifyValidMatlabRootDoesntDisplayError() throws Exception { project.getBuildersList().add(this.matlabBuilder); this.matlabBuilder.setLocalMatlab(getMatlabroot("R2018b")); HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); @@ -412,4 +409,19 @@ public void verifyCoberturaError() throws Exception { String filteredPageText = pageText.replaceFirst(TestMessage.getValue("Builder.invalid.matlab.root.error"), ""); Assert.assertTrue(filteredPageText.contains(TestMessage.getValue("Builder.invalid.matlab.root.error"))); } + + /* + * Private helper methods for tests + */ + + private FreeStyleBuild getBuildforRunTestAutomatically() throws InterruptedException, ExecutionException { + RunTestsAutomaticallyOption runOption = new RunTestsAutomaticallyOption(); + runOption.setTaCoberturaChkBx(true); + runOption.setTaJunitChkBx(true); + runOption.setTatapChkBx(true); + this.matlabBuilder.setTestRunTypeList(runOption); + project.getBuildersList().add(this.matlabBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + return build; + } } diff --git a/src/test/resources/versioninfo/R2016b/READ.txt b/src/test/resources/versioninfo/R2016b/READ.txt new file mode 100644 index 00000000..2261121d --- /dev/null +++ b/src/test/resources/versioninfo/R2016b/READ.txt @@ -0,0 +1,2 @@ +# Copyright 2019 The MathWorks, Inc. +#This is dummy file created as resource \ No newline at end of file From ea257c8dddcd7e6a1e03b7ce484910ce011c8c85 Mon Sep 17 00:00:00 2001 From: nbhoski Date: Tue, 7 May 2019 15:19:23 +0530 Subject: [PATCH 4/4] Changed getResource() per review comments. --- .../com/mathworks/ci/MatlabBuilderTest.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java index 09b1cd00..baf72a99 100644 --- a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java +++ b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java @@ -12,6 +12,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.List; +import java.util.Optional; import java.util.concurrent.ExecutionException; import org.junit.After; import org.junit.Assert; @@ -42,6 +43,7 @@ public class MatlabBuilderTest { private MatlabBuilder matlabBuilder; private static URL url; private static String FileSeperator; + private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; @Rule public JenkinsRule jenkins = new JenkinsRule(); @@ -87,20 +89,13 @@ public void testTearDown() { } private String getMatlabroot(String version) throws URISyntaxException { - ClassLoader classLoader = MatlabBuilderTest.class.getClassLoader(); - String matlabRoot; - if (classLoader.getResource("versioninfo/" + version + "/VersionInfo.xml") != null) { - matlabRoot = new File( - classLoader.getResource("versioninfo/" + version + "/VersionInfo.xml").toURI()) - .getAbsolutePath().replace(FileSeperator + "VersionInfo.xml", ""); - } else { - matlabRoot = new File( - classLoader.getResource("versioninfo/" + "R2017a" + "/VersionInfo.xml").toURI()) - .getAbsolutePath().replace(FileSeperator + "VersionInfo.xml", "") - .replace("R2017a", version); - } - return matlabRoot; + String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; + String userVersionInfo = "versioninfo/"+version+"/" + VERSION_INFO_XML_FILE; + URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)).orElseGet(() -> getResource(defaultVersionInfo)); + File matlabRoot = new File(matlabRootURL.toURI()); + return matlabRoot.getAbsolutePath().replace(FileSeperator + VERSION_INFO_XML_FILE,"").replace("R2017a",version); } + /* * Test Case to verify if Build step contains "Run MATLAB Tests" option. @@ -424,4 +419,8 @@ private FreeStyleBuild getBuildforRunTestAutomatically() throws InterruptedExcep FreeStyleBuild build = project.scheduleBuild2(0).get(); return build; } + + private URL getResource(String resource) { + return MatlabBuilderTest.class.getClassLoader().getResource(resource); + } }