Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: unit test configuration handles deferred var @{x} from surefire/failsafe #1827

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TMP</groupId>
<artifactId>TMP</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- @formatter:off Avoid newlines inside argLine! -->
<!-- Sets the VM argument line used when unit tests
are run. See jacoco plugin -->
<argLine>-Xshare:off
-Djdk.net.URLClassPath.disableClassPathURLCheck=true
@{jacoco.surefireArgLine} @{titi.tata}
</argLine>

</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>coverage-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the name of the property containing the
settings for JaCoCo runtime agent. -->
<propertyName>jacoco.surefireArgLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>[0.0.0,)</versionRange>
<goals>
<goal>prepare-agent</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnConfiguration>true</runOnConfiguration>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,30 @@ public class UnitTestLaunchConfigConfigurationTest extends AbstractMavenProjectT
private static final String SUREFIRE_ARGS_SET = """
<configuration>
<argLine>
--argLineItem=surefireArgLineValue
--argLineItem=surefireArgLineValue --undefinedArgLineItem=${undefinedProperty}
</argLine>
<systemPropertyVariables>
<surefireProp1>surefireProp1Value</surefireProp1>
<surefireEmptyProp>${undefinedProperty}</surefireEmptyProp>
</systemPropertyVariables>
<environmentVariables>
<surefireEnvironmentVariables1>surefireEnvironmentVariables1Value</surefireEnvironmentVariables1>
<surefireEmptyEnvironmentVariables1>${undefinedProperty}</surefireEmptyEnvironmentVariables1>
</environmentVariables>
</configuration>
""";
private static final String FAILSAFE_ARGS_SET = """
<configuration>
<argLine>
--argLineItem=failsafeArgLineValue
--argLineItem=failsafeArgLineValue --undefinedArgLineItem=${undefinedProperty}
</argLine>
<systemPropertyVariables>
<failsafeProp1>failsafeProp1Value</failsafeProp1>
<failsafeEmptyProp>${undefiniedProperty}</failsafeEmptyProp>
</systemPropertyVariables>
<environmentVariables>
<failsafeEnvironmentVariables1>failsafeEnvironmentVariables1Value</failsafeEnvironmentVariables1>
<failsafeEmptyEnvironmentVariables1>${undefinedProperty}</failsafeEmptyEnvironmentVariables1>
</environmentVariables>
</configuration>
""";
Expand Down Expand Up @@ -144,6 +148,10 @@ public void test_configuration_must_be_updated_with_surefire_config()

// check systemPropertyVariables
assertTrue(argLine.contains("-DsurefireProp1=surefireProp1Value"));

// check systemPropertyVariables with null value aren't set
assertTrue(!argLine.contains("-DsurefireEmptyProp="));

}

@Test
Expand Down Expand Up @@ -193,6 +201,9 @@ public void test_configuration_must_be_updated_with_failsafe_config()

// check systemPropertyVariables
assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value"));

// check systemPropertyVariables with null value aren't set
assertTrue(!argLine.contains("-DfailsafeEmptyProp="));
}

@Test
Expand Down Expand Up @@ -279,6 +290,32 @@ public void test_configuration_must_be_updated_with_failSafe_config_when_created
assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value"));
}

@Test
public void test_deferred_variable_are_resolved() throws CoreException, IOException, InterruptedException {
// Get launch type
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);

assumeTrue(testType + " support not available", type != null);

File pomFile = getTestFile("deferredVariables/pom.xml");

IProject project = importProject(pomFile.getAbsolutePath());

// create basic unit test
createDefaultTest(project, type, "test.SomeTest");

updateProject(project);
waitForJobsToComplete();

ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
assertTrue(updatedConfigurations.length == 1);

ILaunchConfiguration config = updatedConfigurations[0];
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("-javaagent")); // resolved jacoco agent
assertTrue(argLine.contains("@{titi.tata}")); // unresolved property is unchanged as in CLI
}

private void updateProject(IProject project) throws CoreException, InterruptedException {
MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project, monitor);
waitForJobsToComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -143,6 +146,11 @@ public class UnitTestSupport {
*/
private static final String FAILSAFE_PLUGIN_ARTIFACT_ID = "maven-failsafe-plugin";

/**
* deffered variable pattern
*/
private static final Pattern DEFERRED_VAR_PATTERN = Pattern.compile("@\\{(.*?)\\}");

/**
* maven group id for the maven plugins
*/
Expand Down Expand Up @@ -271,7 +279,9 @@ private void defineConfigurationValues(IProject project, ILaunchConfiguration co
launchArguments.add(args.argLine());
}
if(args.systemPropertyVariables() != null) {
args.systemPropertyVariables().forEach((key, value) -> launchArguments.add("-D" + key + "=" + value));
args.systemPropertyVariables().entrySet().stream() //
.filter(e -> e.getKey() != null && e.getValue() != null)
.forEach(e -> launchArguments.add("-D" + e.getKey() + "=" + e.getValue()));
}
copy.setAttribute(LAUNCH_CONFIG_VM_ARGUMENTS, launchArguments.toString());

Expand All @@ -287,7 +297,10 @@ private void defineConfigurationValues(IProject project, ILaunchConfiguration co
}

if(args.environmentVariables() != null) {
copy.setAttribute(LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, args.environmentVariables());
Map<String, String> filteredMap = args.environmentVariables().entrySet().stream()
.filter(entry -> entry.getKey() != null && entry.getValue() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
copy.setAttribute(LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, filteredMap);
}

copy.doSave();
Expand Down Expand Up @@ -398,8 +411,11 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo
IProgressMonitor monitor) {
try {
IMaven maven = MavenPlugin.getMaven();
return new TestLaunchArguments(
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ARGLINE, String.class, monitor),

String argLine = maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ARGLINE, String.class, monitor);
argLine = resolveDeferredVariables(mavenProject, argLine);

return new TestLaunchArguments(argLine,
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_SYSPROP_VARIABLES, Map.class, monitor),
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ENVIRONMENT_VARIABLES, Map.class, monitor),
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_WORKING_DIRECTORY, File.class, monitor),
Expand All @@ -412,6 +428,29 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo

}

/**
* This method is used to resolve deferred variables introduced by failsafe/surefire plugins in a given string value.
* Deferred variables are placeholders in the string that are replaced with actual values from the Maven project's
* properties. The placeholders are in the format @{...}, where ... is the key of the property. If a placeholder's
* corresponding property does not exist, the placeholder is left as is.
*
* @param mavenProject the Maven project from which to retrieve the properties
* @param value the string containing the placeholders to be replaced
* @return the string with all resolvable placeholders replaced with their corresponding property values
*/
private static String resolveDeferredVariables(MavenProject mavenProject, String value) {
Properties properties = mavenProject.getProperties();
if(properties.isEmpty() || value == null) {
return value;
}
return DEFERRED_VAR_PATTERN.matcher(value).replaceAll(match -> {
String placeholder = match.group();
String key = match.group(1);
String replacement = properties.getProperty(key);
return replacement != null ? replacement : placeholder;
});
}

/**
* Holder for the surefire/failsafe launch arguments
*/
Expand Down
Loading