Skip to content

Commit 70cf76f

Browse files
treilhesHannesWell
authored andcommitted
Handle deferred var @{x} from surefire/failsafe in test launch-configs
Fixes #1824
1 parent 3af1fc5 commit 70cf76f

File tree

3 files changed

+166
-2
lines changed

3 files changed

+166
-2
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>TMP</groupId>
6+
<artifactId>TMP</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-surefire-plugin</artifactId>
14+
<version>3.5.0</version>
15+
<executions>
16+
<execution>
17+
<id>default-test</id>
18+
<phase>test</phase>
19+
<goals>
20+
<goal>test</goal>
21+
</goals>
22+
<configuration>
23+
<!-- @formatter:off Avoid newlines inside argLine! -->
24+
<!-- Sets the VM argument line used when unit tests
25+
are run. See jacoco plugin -->
26+
<argLine>-Xshare:off
27+
-Djdk.net.URLClassPath.disableClassPathURLCheck=true
28+
@{jacoco.surefireArgLine} @{titi.tata}
29+
</argLine>
30+
31+
</configuration>
32+
</execution>
33+
</executions>
34+
</plugin>
35+
36+
<plugin>
37+
<groupId>org.jacoco</groupId>
38+
<artifactId>jacoco-maven-plugin</artifactId>
39+
<version>0.8.12</version>
40+
<executions>
41+
<execution>
42+
<id>coverage-prepare-agent</id>
43+
<goals>
44+
<goal>prepare-agent</goal>
45+
</goals>
46+
<configuration>
47+
<!-- Sets the name of the property containing the
48+
settings for JaCoCo runtime agent. -->
49+
<propertyName>jacoco.surefireArgLine</propertyName>
50+
</configuration>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
<pluginManagement>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.eclipse.m2e</groupId>
59+
<artifactId>lifecycle-mapping</artifactId>
60+
<version>1.0.0</version>
61+
<configuration>
62+
<lifecycleMappingMetadata>
63+
<pluginExecutions>
64+
<pluginExecution>
65+
<pluginExecutionFilter>
66+
<groupId>org.jacoco</groupId>
67+
<artifactId>jacoco-maven-plugin</artifactId>
68+
<versionRange>[0.0.0,)</versionRange>
69+
<goals>
70+
<goal>prepare-agent</goal>
71+
</goals>
72+
</pluginExecutionFilter>
73+
<action>
74+
<execute>
75+
<runOnConfiguration>true</runOnConfiguration>
76+
</execute>
77+
</action>
78+
</pluginExecution>
79+
</pluginExecutions>
80+
</lifecycleMappingMetadata>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</pluginManagement>
85+
</build>
86+
<dependencyManagement>
87+
<dependencies>
88+
<dependency>
89+
<groupId>org.junit</groupId>
90+
<artifactId>junit-bom</artifactId>
91+
<version>5.11.0</version>
92+
<type>pom</type>
93+
<scope>import</scope>
94+
</dependency>
95+
</dependencies>
96+
</dependencyManagement>
97+
98+
<dependencies>
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
</dependencies>
105+
</project>

org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,32 @@ public void test_configuration_must_be_updated_with_failSafe_config_when_created
279279
assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value"));
280280
}
281281

282+
@Test
283+
public void test_deferred_variable_are_resolved() throws CoreException, IOException, InterruptedException {
284+
// Get launch type
285+
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);
286+
287+
assumeTrue(testType + " support not available", type != null);
288+
289+
File pomFile = getTestFile("deferredVariables/pom.xml");
290+
291+
IProject project = importProject(pomFile.getAbsolutePath());
292+
293+
// create basic unit test
294+
createDefaultTest(project, type, "test.SomeTest");
295+
296+
updateProject(project);
297+
waitForJobsToComplete();
298+
299+
ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
300+
assertTrue(updatedConfigurations.length == 1);
301+
302+
ILaunchConfiguration config = updatedConfigurations[0];
303+
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
304+
assertTrue(argLine.contains("-javaagent")); // resolved jacoco agent
305+
assertTrue(argLine.contains("@{titi.tata}")); // unresolved property is unchanged as in CLI
306+
}
307+
282308
private void updateProject(IProject project) throws CoreException, InterruptedException {
283309
MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project, monitor);
284310
waitForJobsToComplete();

org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Optional;
27+
import java.util.Properties;
2728
import java.util.Set;
2829
import java.util.StringJoiner;
30+
import java.util.regex.Pattern;
2931

3032
import org.slf4j.Logger;
3133
import org.slf4j.LoggerFactory;
@@ -143,6 +145,11 @@ public class UnitTestSupport {
143145
*/
144146
private static final String FAILSAFE_PLUGIN_ARTIFACT_ID = "maven-failsafe-plugin";
145147

148+
/**
149+
* deffered variable pattern
150+
*/
151+
private static final Pattern DEFERRED_VAR_PATTERN = Pattern.compile("@\\{(.*?)\\}");
152+
146153
/**
147154
* maven group id for the maven plugins
148155
*/
@@ -398,8 +405,11 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo
398405
IProgressMonitor monitor) {
399406
try {
400407
IMaven maven = MavenPlugin.getMaven();
401-
return new TestLaunchArguments(
402-
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ARGLINE, String.class, monitor),
408+
409+
String argLine = maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ARGLINE, String.class, monitor);
410+
argLine = resolveDeferredVariables(mavenProject, argLine);
411+
412+
return new TestLaunchArguments(argLine,
403413
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_SYSPROP_VARIABLES, Map.class, monitor),
404414
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ENVIRONMENT_VARIABLES, Map.class, monitor),
405415
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_WORKING_DIRECTORY, File.class, monitor),
@@ -412,6 +422,29 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo
412422

413423
}
414424

425+
/**
426+
* This method is used to resolve deferred variables introduced by failsafe/surefire plugins in a given string value.
427+
* Deferred variables are placeholders in the string that are replaced with actual values from the Maven project's
428+
* properties. The placeholders are in the format @{...}, where ... is the key of the property. If a placeholder's
429+
* corresponding property does not exist, the placeholder is left as is.
430+
*
431+
* @param mavenProject the Maven project from which to retrieve the properties
432+
* @param value the string containing the placeholders to be replaced
433+
* @return the string with all resolvable placeholders replaced with their corresponding property values
434+
*/
435+
private static String resolveDeferredVariables(MavenProject mavenProject, String value) {
436+
Properties properties = mavenProject.getProperties();
437+
if(properties.isEmpty() || value == null) {
438+
return value;
439+
}
440+
return DEFERRED_VAR_PATTERN.matcher(value).replaceAll(match -> {
441+
String placeholder = match.group();
442+
String key = match.group(1);
443+
String replacement = properties.getProperty(key);
444+
return replacement != null ? replacement : placeholder;
445+
});
446+
}
447+
415448
/**
416449
* Holder for the surefire/failsafe launch arguments
417450
*/

0 commit comments

Comments
 (0)