|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Pascal Treilhes |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *******************************************************************************/ |
| 10 | + |
| 11 | +package org.eclipse.m2e.jdt.tests; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNotNull; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | +import static org.junit.Assume.assumeTrue; |
| 17 | + |
| 18 | +import java.io.ByteArrayInputStream; |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.eclipse.core.resources.IFile; |
| 26 | +import org.eclipse.core.resources.IProject; |
| 27 | +import org.eclipse.core.runtime.CoreException; |
| 28 | +import org.eclipse.core.runtime.FileLocator; |
| 29 | +import org.eclipse.debug.core.DebugPlugin; |
| 30 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 31 | +import org.eclipse.debug.core.ILaunchConfigurationType; |
| 32 | +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
| 33 | +import org.eclipse.debug.core.ILaunchManager; |
| 34 | +import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; |
| 35 | +import org.eclipse.m2e.core.MavenPlugin; |
| 36 | +import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl; |
| 37 | +import org.eclipse.m2e.jdt.internal.UnitTestSupport; |
| 38 | +import org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider; |
| 39 | +import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | +import org.junit.runners.Parameterized; |
| 44 | +import org.junit.runners.Parameterized.Parameter; |
| 45 | +import org.junit.runners.Parameterized.Parameters; |
| 46 | + |
| 47 | +@SuppressWarnings("restriction") |
| 48 | +@RunWith(Parameterized.class) |
| 49 | +public class UnitTestLaunchConfigConfigurationTest extends AbstractMavenProjectTestCase { |
| 50 | + |
| 51 | + private static final String REPLACED_SUREFIRE_POM_STRING = "<!-- surefireArgs: replacedByArgsSets -->"; |
| 52 | + private static final String REPLACED_FAILSAFE_POM_STRING = "<!-- failsafeArgs: replacedByArgsSets -->"; |
| 53 | + private static final String ROOT_PATH = "/projects/surefireFailsafeToTestLaunchSettings"; |
| 54 | + private static ILaunchManager LAUNCH_MANAGER = DebugPlugin.getDefault().getLaunchManager(); |
| 55 | + |
| 56 | + private static final String SUREFIRE_ARGS_SET = """ |
| 57 | + <configuration> |
| 58 | + <argLine> |
| 59 | + --argLineItem=surefireArgLineValue |
| 60 | + </argLine> |
| 61 | + <systemPropertyVariables> |
| 62 | + <surefireProp1>surefireProp1Value</surefireProp1> |
| 63 | + </systemPropertyVariables> |
| 64 | + <environmentVariables> |
| 65 | + <surefireEnvironmentVariables1>surefireEnvironmentVariables1Value</surefireEnvironmentVariables1> |
| 66 | + </environmentVariables> |
| 67 | + </configuration> |
| 68 | + """; |
| 69 | + private static final String FAILSAFE_ARGS_SET = """ |
| 70 | + <configuration> |
| 71 | + <argLine> |
| 72 | + --argLineItem=failsafeArgLineValue |
| 73 | + </argLine> |
| 74 | + <systemPropertyVariables> |
| 75 | + <failsafeProp1>failsafeProp1Value</failsafeProp1> |
| 76 | + </systemPropertyVariables> |
| 77 | + <environmentVariables> |
| 78 | + <failsafeEnvironmentVariables1>failsafeEnvironmentVariables1Value</failsafeEnvironmentVariables1> |
| 79 | + </environmentVariables> |
| 80 | + </configuration> |
| 81 | + """; |
| 82 | + |
| 83 | + // Define the parameters to be used in the test |
| 84 | + @Parameters |
| 85 | + public static Collection<Object> data() { |
| 86 | + return List.of(MavenRuntimeClasspathProvider.JDT_TESTNG_TEST, MavenRuntimeClasspathProvider.JDT_JUNIT_TEST); |
| 87 | + } |
| 88 | + |
| 89 | + @Parameter(0) |
| 90 | + public String testType; |
| 91 | + |
| 92 | + @Override |
| 93 | + @Before |
| 94 | + public void setUp() throws Exception { |
| 95 | + super.setUp(); |
| 96 | + ((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true); |
| 97 | + setAutoBuilding(true); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void test_configuration_must_be_updated_with_surefire_config() |
| 102 | + throws CoreException, IOException, InterruptedException { |
| 103 | + // Get launch type |
| 104 | + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); |
| 105 | + |
| 106 | + assumeTrue(testType + " support not available", type != null); |
| 107 | + |
| 108 | + File pomFile = getTestFile("argumentsAreSet/pom.xml"); |
| 109 | + String surefireConf = SUREFIRE_ARGS_SET; |
| 110 | + String failsafeConf = null; |
| 111 | + |
| 112 | + IProject project = importProject(pomFile.getAbsolutePath()); |
| 113 | + |
| 114 | + // create basic unit test |
| 115 | + createDefaultTest(project, type, "test.SomeTest"); |
| 116 | + |
| 117 | + updateProject(project); |
| 118 | + waitForJobsToComplete(); |
| 119 | + |
| 120 | + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); |
| 121 | + assertTrue(updatedConfigurations.length == 1); |
| 122 | + |
| 123 | + mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); |
| 124 | + updateProject(project); |
| 125 | + waitForJobsToComplete(); |
| 126 | + |
| 127 | + updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); |
| 128 | + assertTrue(updatedConfigurations.length == 1); |
| 129 | + |
| 130 | + ILaunchConfiguration config = updatedConfigurations[0]; |
| 131 | + |
| 132 | + // check argLine |
| 133 | + String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, ""); |
| 134 | + assertTrue(argLine.contains("--argLineItem=surefireArgLineValue")); |
| 135 | + |
| 136 | + // check environmentVariables |
| 137 | + Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, |
| 138 | + (Map<String, String>) null); |
| 139 | + |
| 140 | + assertNotNull(envVars); |
| 141 | + assertTrue(envVars.size() == 1); |
| 142 | + assertTrue(envVars.containsKey("surefireEnvironmentVariables1")); |
| 143 | + assertEquals("surefireEnvironmentVariables1Value", envVars.get("surefireEnvironmentVariables1")); |
| 144 | + |
| 145 | + // check systemPropertyVariables |
| 146 | + assertTrue(argLine.contains("-DsurefireProp1=surefireProp1Value")); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void test_configuration_must_be_updated_with_failsafe_config() |
| 151 | + throws CoreException, IOException, InterruptedException { |
| 152 | + // Get launch type |
| 153 | + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); |
| 154 | + |
| 155 | + assumeTrue(testType + " support not available", type != null); |
| 156 | + |
| 157 | + File pomFile = getTestFile("argumentsAreSet/pom.xml"); |
| 158 | + String surefireConf = null; |
| 159 | + String failsafeConf = FAILSAFE_ARGS_SET; |
| 160 | + |
| 161 | + IProject project = importProject(pomFile.getAbsolutePath()); |
| 162 | + // waitForJobsToComplete(); |
| 163 | + |
| 164 | + // create basic unit test |
| 165 | + createDefaultTest(project, type, "test.SomeTestIT"); |
| 166 | + |
| 167 | + updateProject(project); |
| 168 | + waitForJobsToComplete(); |
| 169 | + |
| 170 | + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); |
| 171 | + assertTrue(updatedConfigurations.length == 1); |
| 172 | + |
| 173 | + mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); |
| 174 | + updateProject(project); |
| 175 | + |
| 176 | + updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); |
| 177 | + assertTrue(updatedConfigurations.length == 1); |
| 178 | + |
| 179 | + ILaunchConfiguration config = updatedConfigurations[0]; |
| 180 | + |
| 181 | + // check argLine |
| 182 | + String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, ""); |
| 183 | + assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue")); |
| 184 | + |
| 185 | + // check environmentVariables |
| 186 | + Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, |
| 187 | + (Map<String, String>) null); |
| 188 | + |
| 189 | + assertNotNull(envVars); |
| 190 | + assertTrue(envVars.size() == 1); |
| 191 | + assertTrue(envVars.containsKey("failsafeEnvironmentVariables1")); |
| 192 | + assertEquals("failsafeEnvironmentVariables1Value", envVars.get("failsafeEnvironmentVariables1")); |
| 193 | + |
| 194 | + // check systemPropertyVariables |
| 195 | + assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value")); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + public void test_configuration_must_be_updated_with_surefire_config_when_created() |
| 200 | + throws CoreException, IOException, InterruptedException { |
| 201 | + // Get launch type |
| 202 | + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); |
| 203 | + |
| 204 | + assumeTrue(testType + " support not available", type != null); |
| 205 | + |
| 206 | + File pomFile = getTestFile("argumentsAreSet/pom.xml"); |
| 207 | + String surefireConf = SUREFIRE_ARGS_SET; |
| 208 | + String failsafeConf = null; |
| 209 | + |
| 210 | + IProject project = importProject(pomFile.getAbsolutePath()); |
| 211 | + mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); |
| 212 | + updateProject(project); |
| 213 | + waitForJobsToComplete(); |
| 214 | + |
| 215 | + // create basic unit test |
| 216 | + createDefaultTest(project, type, "test.SomeTest"); |
| 217 | + |
| 218 | + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); |
| 219 | + assertTrue(updatedConfigurations.length == 1); |
| 220 | + |
| 221 | + ILaunchConfiguration config = updatedConfigurations[0]; |
| 222 | + |
| 223 | + // check argLine |
| 224 | + String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, ""); |
| 225 | + assertTrue(argLine.contains("--argLineItem=surefireArgLineValue")); |
| 226 | + |
| 227 | + // check environmentVariables |
| 228 | + Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, |
| 229 | + (Map<String, String>) null); |
| 230 | + |
| 231 | + assertNotNull(envVars); |
| 232 | + assertTrue(envVars.size() == 1); |
| 233 | + assertTrue(envVars.containsKey("surefireEnvironmentVariables1")); |
| 234 | + assertEquals("surefireEnvironmentVariables1Value", envVars.get("surefireEnvironmentVariables1")); |
| 235 | + |
| 236 | + // check systemPropertyVariables |
| 237 | + assertTrue(argLine.contains("-DsurefireProp1=surefireProp1Value")); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void test_configuration_must_be_updated_with_failSafe_config_when_created() |
| 242 | + throws CoreException, IOException, InterruptedException { |
| 243 | + // Get launch type |
| 244 | + ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType); |
| 245 | + |
| 246 | + assumeTrue(testType + " support not available", type != null); |
| 247 | + |
| 248 | + File pomFile = getTestFile("argumentsAreSet/pom.xml"); |
| 249 | + String surefireConf = null; |
| 250 | + String failsafeConf = FAILSAFE_ARGS_SET; |
| 251 | + |
| 252 | + IProject project = importProject(pomFile.getAbsolutePath()); |
| 253 | + mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf); |
| 254 | + updateProject(project); |
| 255 | + waitForJobsToComplete(); |
| 256 | + |
| 257 | + // create basic unit test |
| 258 | + createDefaultTest(project, type, "test.SomeTestIT"); |
| 259 | + |
| 260 | + ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type); |
| 261 | + assertTrue(updatedConfigurations.length == 1); |
| 262 | + |
| 263 | + ILaunchConfiguration config = updatedConfigurations[0]; |
| 264 | + |
| 265 | + // check argLine |
| 266 | + String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, ""); |
| 267 | + assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue")); |
| 268 | + |
| 269 | + // check environmentVariables |
| 270 | + Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES, |
| 271 | + (Map<String, String>) null); |
| 272 | + |
| 273 | + assertNotNull(envVars); |
| 274 | + assertTrue(envVars.size() == 1); |
| 275 | + assertTrue(envVars.containsKey("failsafeEnvironmentVariables1")); |
| 276 | + assertEquals("failsafeEnvironmentVariables1Value", envVars.get("failsafeEnvironmentVariables1")); |
| 277 | + |
| 278 | + // check systemPropertyVariables |
| 279 | + assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value")); |
| 280 | + } |
| 281 | + |
| 282 | + private void updateProject(IProject project) throws CoreException, InterruptedException { |
| 283 | + MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project, monitor); |
| 284 | + waitForJobsToComplete(); |
| 285 | + } |
| 286 | + |
| 287 | + // Create a default test |
| 288 | + private void createDefaultTest(IProject project, ILaunchConfigurationType type, String testClassName) |
| 289 | + throws CoreException { |
| 290 | + // create basic unit test |
| 291 | + ILaunchConfigurationWorkingCopy launchConfig = type.newInstance(project, "sampleTest"); |
| 292 | + launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getName()); |
| 293 | + launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, testClassName); |
| 294 | + launchConfig.setAttribute(DebugPlugin.ATTR_WORKING_DIRECTORY, project.getLocation().toString()); |
| 295 | + launchConfig.doSave(); |
| 296 | + } |
| 297 | + |
| 298 | + // Merge the pom and plugins configuration into the project |
| 299 | + private void mergePomAndPluginConfigIntoProject(IProject project, File pomTemplate, String surefireConfiguration, |
| 300 | + String failsafeConfiguration) throws IOException, CoreException { |
| 301 | + String pom = Utils.read(project, pomTemplate); |
| 302 | + IFile pomFileWS = project.getFile(pomTemplate.getName()); |
| 303 | + String newContent = pom; |
| 304 | + |
| 305 | + if (surefireConfiguration != null) { |
| 306 | + newContent = newContent.replace(REPLACED_SUREFIRE_POM_STRING, surefireConfiguration); |
| 307 | + } |
| 308 | + |
| 309 | + if (failsafeConfiguration != null) { |
| 310 | + newContent = newContent.replace(REPLACED_FAILSAFE_POM_STRING, failsafeConfiguration); |
| 311 | + } |
| 312 | + |
| 313 | + pomFileWS.setContents(new ByteArrayInputStream(newContent.getBytes()), true, false, null); |
| 314 | + } |
| 315 | + |
| 316 | + private File getTestFile(String filename) throws IOException { |
| 317 | + return new File(FileLocator.toFileURL(getClass().getResource(ROOT_PATH + "/" + filename)).getFile()); |
| 318 | + } |
| 319 | +} |
0 commit comments