Skip to content

Commit

Permalink
SOAtestMojoTest - assert on arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
benken-parasoft committed Nov 2, 2023
1 parent e30aa68 commit e3dcc01
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
20 changes: 13 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</prerequisites>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.version>3.9.0</maven.version>
Expand All @@ -36,9 +37,9 @@
<maven-site-plugin.version>3.12.1</maven-site-plugin.version>
<maven-surefire-plugin.version>3.2.1</maven-surefire-plugin.version>
<byte-buddy.version>1.14.9</byte-buddy.version>
<hamcrest.version>2.2</hamcrest.version>
<junit.version>4.13.2</junit.version>
<mockito.version>4.11.0</mockito.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<developers>
Expand Down Expand Up @@ -103,16 +104,21 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-bom</artifactId>
<version>${mockito.version}</version>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-parent</artifactId>
<version>${byte-buddy.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-parent</artifactId>
<version>${byte-buddy.version}</version>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-bom</artifactId>
<version>${mockito.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/parasoft/soatest/maven/plugin/SOAtestMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public class SOAtestMojo extends AbstractMojo {
* run.
*/
@Parameter(property = "soatest.datagroupconfig")
private String dataGroupConfig;
private File dataGroupConfig;

/**
* Specifies the name of the data source associated with the test(s) you
Expand Down Expand Up @@ -334,8 +334,8 @@ public class SOAtestMojo extends AbstractMojo {
private File report;

/**
* An absolute or relative path to the .properties file that includes custom
* configuration settings.
* Specifies the path to the .properties file that includes custom configuration
* settings.
*/
@Parameter(property = "soatest.settings")
private File settings;
Expand Down Expand Up @@ -512,10 +512,7 @@ public void execute() throws MojoExecutionException {
private List<String> getBaseCommand(Log log, String soatestcli, Path workspace) {
List<String> baseCommand = new LinkedList<>();
baseCommand.add(soatestcli);
if (javaHome != null) {
baseCommand.add("-Zjava_home"); //$NON-NLS-1$
baseCommand.add(javaHome.getAbsolutePath());
}
addOptionalCommand("-Zjava_home", javaHome, baseCommand); //$NON-NLS-1$
if (vmArgs != null) {
for (String vmArg : vmArgs) {
baseCommand.add("-J" + vmArg); //$NON-NLS-1$
Expand Down Expand Up @@ -613,8 +610,7 @@ private static void addOptionalCommand(String name, File value, List<String> com
private static void addOptionalCommand(String name, List<String> parameters, List<String> command) {
if (parameters != null) {
for (String parameter : parameters) {
command.add(name);
command.add(parameter);
addOptionalCommand(name, parameter, command);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package com.parasoft.soatest.maven.plugin;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -28,6 +33,7 @@
import java.io.File;
import java.util.List;

import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.WithoutMojo;
import org.junit.Rule;
Expand All @@ -46,25 +52,60 @@ protected void after() {
}
};

/**
* @throws Exception if any
*/
@Test
public void testSomething() throws Exception {
public void testExecute() throws Exception {
File pom = new File("target/test-classes/project-to-test/");
assertNotNull(pom);
assertTrue(pom.exists());

SOAtestMojo soatestMojo = (SOAtestMojo) rule.lookupConfiguredMojo(pom, "soatest");
assertNotNull(soatestMojo);
Process process = mock(Process.class);
List<String> importCommand;
List<String> testConfigCommand;
try (MockedConstruction<ProcessBuilder> processBuilder = mockConstruction(ProcessBuilder.class,
withSettings().defaultAnswer(CALLS_REAL_METHODS),
(mock, context) -> doReturn(process).when(mock).start())) {
withSettings().defaultAnswer(CALLS_REAL_METHODS), (mock, context) -> {
doReturn(process).when(mock).start();
doReturn(context.arguments().get(0)).when(mock).command();
})) {
soatestMojo.execute();
List<ProcessBuilder> constructed = processBuilder.constructed();
assertEquals(2, constructed.size());
importCommand = constructed.get(0).command();
testConfigCommand = constructed.get(1).command();
}
assertEquals(7, importCommand.size());
checkBaseCommand(pom, importCommand);
assertEquals("-import", importCommand.get(5));
assertEquals(pom.getAbsolutePath(), importCommand.get(6));

assertEquals(28, testConfigCommand.size());
checkBaseCommand(pom, testConfigCommand);
assertThat(testConfigCommand.subList(5, testConfigCommand.size()), contains(
"-config", "soatest.builtin://Demo Configuration",
"-publish",
"-dataGroupConfig", new File(pom, "dataconfig.xml").getAbsolutePath(),
"-dataSourceRow", "1",
"-dataSourceName", "data source name",
"-fail",
"-environment", "test environment",
"-environmentConfig", new File(pom, "environments.xml").getAbsolutePath(),
"-showsettings",
"-prefs", "prefs.properties",
"-report", new File(pom, "myreport.xml").getAbsolutePath(),
"-resource", "testProject",
"-property", "techsupport.auto_creation=true"
));
}

private static void checkBaseCommand(File baseDir, List<String> command) {
assertThat(command.size(), greaterThan(5));
List<String> baseCommand = command.subList(0, 5);
assertThat(baseCommand.get(0), endsWith("parasoft/soatest/soatestcli" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "")));
assertEquals("-data", baseCommand.get(1));
assertThat(baseCommand.get(2), startsWith(new File(System.getProperty("java.io.tmpdir"), "soatest.workspace").getAbsolutePath()));
assertEquals("-settings", baseCommand.get(3));
assertEquals(new File(baseDir, "settings.properties").getAbsolutePath(), baseCommand.get(4));
}

/** Do not need the MojoRule. */
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/project-to-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<artifactId>soatest-maven-plugin</artifactId>
<configuration>
<soatestHome>parasoft/soatest</soatestHome>
<config>soatest.builtin://Demo Configuration</config>
<settings>${project.basedir}/settings.properties</settings>
<dataGroupConfig>dataconfig.xml</dataGroupConfig>
<dataSourceName>data source name</dataSourceName>
Expand Down

0 comments on commit e3dcc01

Please sign in to comment.