From 62a9da3acfac56242ae08ad2cdf4a80f5282dae1 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Tue, 21 Oct 2025 15:26:57 +0200 Subject: [PATCH 1/2] Add failing tests for unresolvable properties This relates to #1916 --- org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF | 1 + .../UnitTestLaunchConfigConfigurationTest.java | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF b/org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF index e9a7a03c0..bea0f6763 100644 --- a/org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.jdt.tests/META-INF/MANIFEST.MF @@ -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, diff --git a/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java b/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java index 29b76fd84..f8126458f 100644 --- a/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java +++ b/org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java @@ -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; @@ -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 envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, @@ -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 envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, @@ -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 envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, @@ -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 envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, @@ -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 { From 7d856d88d07f088ec79c4afb6a4c98905d4e01ad Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 23 Oct 2025 12:58:37 +0200 Subject: [PATCH 2/2] Remove unresolved placeholders from test parameters This closes #1916 --- .../m2e/jdt/internal/UnitTestSupport.java | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java index 1be57c009..b88756c5b 100644 --- a/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java +++ b/org.eclipse.m2e.jdt/src/org/eclipse/m2e/jdt/internal/UnitTestSupport.java @@ -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 */ @@ -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()); @@ -298,7 +304,8 @@ private void defineConfigurationValues(IProject project, ILaunchConfiguration co if(args.environmentVariables() != null) { Map 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); } @@ -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), @@ -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; - }); + } /**