Skip to content

Commit

Permalink
support surefire late evaluation syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Coles committed Jan 4, 2023
1 parent 82d509f commit 0c95ffb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Expand Up @@ -36,6 +36,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -120,7 +121,7 @@ private ReportOptions parseReportOptions(final List<String> classPath) {
data.addChildJVMArgs(this.mojo.getJvmArgs());
}
if (this.mojo.getArgLine() != null) {
data.setArgLine(this.mojo.getArgLine());
data.setArgLine(replacePropertyExpressions(this.mojo.getArgLine()));
}

data.setMutators(determineMutators());
Expand Down Expand Up @@ -370,4 +371,26 @@ private Properties createPluginProperties() {
return p;
}


/**
* This method taken from surefire under Apache 2 licence.
*
* Replaces expressions <pre>@{property-name}</pre> with the corresponding properties
* from the model. This allows late evaluation of property values when the plugin is executed (as compared
* to evaluation when the pom is parsed as is done with <pre>${property-name}</pre> expressions).
*
* This allows other plugins to modify or set properties with the changes getting picked up by surefire.
*/
private String replacePropertyExpressions(String argLine) {
for (Enumeration<?> e = mojo.getProject().getProperties().propertyNames(); e.hasMoreElements();) {
String key = e.nextElement().toString();
String field = "@{" + key + "}";
if (argLine.contains(field)) {
argLine = argLine.replace(field, mojo.getProject().getProperties().getProperty(key, ""));
}
}

return argLine;
}

}
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Predicate;

import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -55,12 +56,15 @@ public abstract class BasePitMojoTest extends AbstractMojoTestCase {
@Mock
protected PluginServices plugins;

Properties properties = new Properties();

@Override
protected void setUp() throws Exception {
super.setUp();
MockitoAnnotations.openMocks(this);
this.classPath = new ArrayList<>(FCollection.map(
ClassPath.getClassPathElementsAsFiles(), File::getAbsolutePath));
when(this.project.getProperties()).thenReturn(properties);
when(this.project.getTestClasspathElements()).thenReturn(this.classPath);
when(this.project.getPackaging()).thenReturn("jar");

Expand Down
Expand Up @@ -431,6 +431,19 @@ public void testParsesOutputEncoding() {
assertThat(actual.getOutputEncoding()).isEqualTo(StandardCharsets.US_ASCII);
}

public void testParsesArgline() {
ReportOptions actual = parseConfig("<argLine>foo</argLine>");
assertThat(actual.getArgLine()).isEqualTo("foo");
}

public void testEvaluatesArgLineProperties() {
properties.setProperty("FOO", "fooValue");
properties.setProperty("BAR", "barValue");
properties.setProperty("UNUSED", "unusedValue");
ReportOptions actual = parseConfig("<argLine>@{FOO} @{BAR}</argLine>");
assertThat(actual.getArgLine()).isEqualTo("fooValue barValue");
}

private ReportOptions parseConfig(final String xml) {
try {
final String pom = createPomWithConfiguration(xml);
Expand Down

0 comments on commit 0c95ffb

Please sign in to comment.