diff --git a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java index b18fdccf..baf72a99 100644 --- a/src/test/java/com/mathworks/ci/MatlabBuilderTest.java +++ b/src/test/java/com/mathworks/ci/MatlabBuilderTest.java @@ -12,6 +12,8 @@ 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; import org.junit.Before; @@ -19,6 +21,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; @@ -38,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(); @@ -83,12 +89,13 @@ 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", ""); - 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. @@ -279,4 +286,141 @@ 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")); + FreeStyleBuild build = getBuildforRunTestAutomatically(); + 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("R2016b")); + FreeStyleBuild build = getBuildforRunTestAutomatically(); + jenkins.assertLogContains("-r", build); + jenkins.assertLogContains("try,exit(", 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 throw error when in valid MATLAB root entered + * + */ + + @Test + public void verifyInvalidMatlabRootDisplaysError() 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 verifyValidMatlabRootDoesntDisplayError() 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); + Thread.sleep(2000); + 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); + 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"))); + } + + /* + * 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; + } + + private URL getResource(String resource) { + return MatlabBuilderTest.class.getClassLoader().getResource(resource); + } } 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 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