diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java index d469c15d09..d78f6807d6 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java @@ -157,6 +157,9 @@ public Commandline createCommandLine( for (Entry entry : getEnvironmentVariables().entrySet()) { String value = entry.getValue(); + if (value != null) { + value = replaceThreadNumberPlaceholders(value, forkNumber); + } cli.addEnvironment(entry.getKey(), value == null ? "" : value); } diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java index 708ec12608..2209a808df 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java @@ -146,6 +146,68 @@ protected void resolveClasspath( .doesNotHaveDuplicates(); } + @Test + public void testEnvInterpolateForkNumber() throws Exception { + Map env = new HashMap<>(); + env.put("FORK_ID", "${surefire.forkNumber}"); + String[] exclEnv = {"PATH"}; + + String jvm = new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath(); + Platform platform = new Platform().withJdkExecAttributesForTests(new JdkAttributes(jvm, false)); + + ForkConfiguration config = + new DefaultForkConfiguration( + emptyClasspath(), + basedir, + "", + basedir, + new Properties(), + "", + env, + exclEnv, + false, + 2, + true, + platform, + new NullConsoleLogger(), + mock(ForkNodeFactory.class)) { + + @Override + protected void resolveClasspath( + @Nonnull Commandline cli, + @Nonnull String booterThatHasMainMethod, + @Nonnull StartupConfiguration config, + @Nonnull File dumpLogDirectory) {} + }; + + List providerJpmsArgs = new ArrayList<>(); + providerJpmsArgs.add(new String[] {"arg2", "arg3"}); + + File cpElement = getTempClasspathFile(); + List cp = singletonList(cpElement.getAbsolutePath()); + + ClasspathConfiguration cpConfig = + new ClasspathConfiguration(new Classpath(cp), emptyClasspath(), emptyClasspath(), true, true); + ClassLoaderConfiguration clc = new ClassLoaderConfiguration(true, true); + StartupConfiguration startup = new StartupConfiguration("cls", cpConfig, clc, ALL, providerJpmsArgs); + + org.apache.maven.surefire.shared.utils.cli.Commandline cliFork1 = + config.createCommandLine(startup, 1, getTempDirectory()); + + assertThat(cliFork1.getEnvironmentVariables()) + .contains("FORK_ID=1") + .doesNotContain("PATH=") + .doesNotHaveDuplicates(); + + org.apache.maven.surefire.shared.utils.cli.Commandline cliFork2 = + config.createCommandLine(startup, 2, getTempDirectory()); + + assertThat(cliFork2.getEnvironmentVariables()) + .contains("FORK_ID=2") + .doesNotContain("PATH=") + .doesNotHaveDuplicates(); + } + @Test public void testCliArgs() throws Exception { String jvm = new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath(); diff --git a/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm b/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm index 79b821a57e..7cdbf4a36e 100644 --- a/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm +++ b/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm @@ -281,16 +281,17 @@ public class TestSuite { specify variables and values to be added to the system properties during the test execution. - You can use the place holder <<<$\{surefire.forkNumber\}>>> within - <<>>, or within the system properties (both those specified via + You can use the placeholder <<<$\{surefire.forkNumber\}>>> within <<>>, + <<>> (since ${project.artifactId}:3.2.0), + or within the system properties (both those specified via <<>> and via <<>>). Before executing - the tests, the ${thisPlugin.toLowerCase()} plugin replaces that place holder + the tests, the ${thisPlugin.toLowerCase()} plugin replaces that placeholder by the number of the actually executing process, counting from 1 to the effective value of <<>> times the maximum number of parallel executions in Maven parallel builds, i.e. the effective value of the <<<-T>>> command line argument of Maven core. - In case of disabled forking (<<>>), the place holder will be + In case of disabled forking (<<>>), the placeholder will be replaced with <1>. The following is an example configuration that makes use of up to three forked