Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 149 additions & 5 deletions src/test/java/com/mathworks/ci/MatlabBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
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;
import org.junit.BeforeClass;
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;

Expand All @@ -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();
Expand Down Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of a constant here! Perhaps could do the same for "versioninfo/" in case that path changes?

Also, this might not be called often enough to really matter, but maybe a StringBuilder in here? If you only call this once or twice, it probably doesn't matter that much and it is pretty readable as is, so I don't think it is a blocker.

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.
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/mathworks/ci/TestMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/testconfig.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.
2 changes: 2 additions & 0 deletions src/test/resources/versioninfo/R2016b/READ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2019 The MathWorks, Inc.
#This is dummy file created as resource