Skip to content

Commit

Permalink
Handle deferred var @{x} from surefire/failsafe in test launch-configs
Browse files Browse the repository at this point in the history
Fixes #1824
  • Loading branch information
treilhes authored and HannesWell committed Nov 26, 2024
1 parent 3af1fc5 commit af446b2
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 2 deletions.
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 @@ -279,6 +279,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,10 @@
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -143,6 +145,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 @@ -398,8 +405,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 +422,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

0 comments on commit af446b2

Please sign in to comment.