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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -279,6 +279,33 @@ 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, maybe removing the placeholder would be
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say yes - removing it would probably be better ;)

Although I believe that in a CLI build, it would also not be resolved and stay as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hesitated but in the end I preferred a quick failure in the case of an undefined variable than spending time looking for where the problem comes from

@HannesWell, @laeubi, @akurtakov what are your thought on this ?

// better
}

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.Matcher;
import java.util.regex.Pattern;

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 @@ -398,8 +406,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 +423,33 @@ 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();
Matcher matcher = DEFERRED_VAR_PATTERN.matcher(value);

while(matcher.find()) {
String match = matcher.group();
String key = matcher.group(1);
String replacement = properties.getProperty(key);

if(replacement != null) {
value = value.replace(match, replacement);
}
}

return value;
}

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