Skip to content
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
1 change: 1 addition & 0 deletions org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.m2e.core,
org.eclipse.m2e.core.ui,
org.eclipse.jdt.launching,
org.eclipse.jdt.junit,
org.eclipse.debug.core,
org.eclipse.ui.tests.harness,
org.eclipse.ui,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
Expand Down Expand Up @@ -150,7 +151,8 @@ public void test_configuration_must_be_updated_with_surefire_config()

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=surefireArgLineValue"));
assertThat(argLine, Matchers.containsString("--argLineItem=surefireArgLineValue --undefinedArgLineItem="));
assertThat(argLine, Matchers.not(Matchers.containsString("${undefinedProperty}")));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
Expand Down Expand Up @@ -209,7 +211,8 @@ public void test_configuration_must_be_updated_with_failsafe_config()

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue"));
assertThat(argLine, Matchers.containsString("--argLineItem=failsafeArgLineValue --undefinedArgLineItem="));
assertThat(argLine, Matchers.not(Matchers.containsString("${undefinedProperty}")));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
Expand Down Expand Up @@ -261,7 +264,8 @@ public void test_configuration_must_be_updated_with_surefire_config_when_created

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=surefireArgLineValue"));
assertThat(argLine, Matchers.containsString("--argLineItem=surefireArgLineValue --undefinedArgLineItem="));
assertThat(argLine, Matchers.not(Matchers.containsString("${undefinedProperty}")));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
Expand Down Expand Up @@ -310,7 +314,8 @@ public void test_configuration_must_be_updated_with_failSafe_config_when_created

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue"));
assertThat(argLine, Matchers.containsString("--argLineItem=failsafeArgLineValue --undefinedArgLineItem="));
assertThat(argLine, Matchers.not(Matchers.containsString("${undefinedProperty}")));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
Expand Down Expand Up @@ -355,7 +360,7 @@ public void test_deferred_variable_are_resolved() throws CoreException, IOExcept
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
assertFalse(argLine.contains("@{titi.tata}")); // unresolved property is removed
}

private void updateProject(IProject project) throws CoreException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,15 @@ public class UnitTestSupport {
private static final String FAILSAFE_PLUGIN_ARTIFACT_ID = "maven-failsafe-plugin";

/**
* deffered variable pattern
* deferred variable pattern
*/
private static final Pattern DEFERRED_VAR_PATTERN = Pattern.compile("@\\{(.*?)\\}");

/**
* standard variable pattern
*/
private static final Pattern STANDARD_VAR_PATTERN = Pattern.compile("\\$\\{(.*?)\\}");

/**
* maven group id for the maven plugins
*/
Expand Down Expand Up @@ -280,7 +285,8 @@ private void defineConfigurationValues(IProject project, ILaunchConfiguration co
}
if(args.systemPropertyVariables() != null) {
args.systemPropertyVariables().entrySet().stream() //
.filter(e -> e.getKey() != null && e.getValue() != null)
.filter(e -> e.getKey() != null && e.getValue() != null
&& !removeStandardVariablePlaceholders(e.getValue()).isEmpty())
.forEach(e -> launchArguments.add("-D" + e.getKey() + "=" + escapeValue(e.getValue())));
}
copy.setAttribute(LAUNCH_CONFIG_VM_ARGUMENTS, launchArguments.toString());
Expand All @@ -298,7 +304,8 @@ private void defineConfigurationValues(IProject project, ILaunchConfiguration co

if(args.environmentVariables() != null) {
Map<String, String> filteredMap = args.environmentVariables().entrySet().stream()
.filter(entry -> entry.getKey() != null && entry.getValue() != null)
.filter(entry -> entry.getKey() != null && entry.getValue() != null
&& !removeStandardVariablePlaceholders(entry.getValue()).isEmpty())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
copy.setAttribute(LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, filteredMap);
}
Expand Down Expand Up @@ -421,6 +428,8 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo

String argLine = maven.getMojoParameterValue(mavenProject, execution, PLUGIN_ARGLINE, String.class, monitor);
argLine = resolveDeferredVariables(mavenProject, argLine);
// resolve all placeholders which were not resolved previously by the empty string
argLine = removeStandardVariablePlaceholders(argLine);

return new TestLaunchArguments(argLine,
maven.getMojoParameterValue(mavenProject, execution, PLUGIN_SYSPROP_VARIABLES, Map.class, monitor),
Expand All @@ -433,29 +442,43 @@ private TestLaunchArguments getTestLaunchArguments(MavenProject mavenProject, Mo
return null;
}

}
/**
* 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 full placeholder is replaced by the empty string.
*
* @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 (or empty
* strings)
*/
private static String resolveDeferredVariables(MavenProject mavenProject, String value) {
Properties properties = mavenProject.getProperties();
if(properties.isEmpty() || value == null) {
return "";
}
return DEFERRED_VAR_PATTERN.matcher(value).replaceAll(match -> {
String key = match.group(1);
String replacement = properties.getProperty(key);
return replacement != null ? replacement : "";
});
}

/**
* 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;
/**
* This method is used to remove all standard variable placeholders in the format ${...} from a given string value.
* All placeholders are replaced with empty strings.
*
* @param value the string containing the placeholders to be removed
* @return the string with all placeholders removed
*/
private static String removeStandardVariablePlaceholders(String value) {
if(value == null) {
return value;
}
return STANDARD_VAR_PATTERN.matcher(value).replaceAll("");
}
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;
});

}

/**
Expand Down
Loading