Skip to content

Commit

Permalink
Propagate surefire and failsafe plugin configurations to launch config
Browse files Browse the repository at this point in the history
  • Loading branch information
treilhes authored and HannesWell committed Aug 27, 2024
1 parent 64e54d0 commit c8bcaaa
Show file tree
Hide file tree
Showing 7 changed files with 812 additions and 1 deletion.
11 changes: 11 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

Updated the embedded Maven from version 3.9.7 to 3.9.9; [Maven 3.9.9 Release Notes](https://maven.apache.org/docs/3.9.9/release-notes.html).

### Surefire/Failsafe plugin configuration propagated to Junit/TestNG launch configuration

The following arguments are supported: <br/>
`<argLine>`, <br/>
`<environmentVariables>`, <br/>
`<systemPropertyVariables>`, <br/>
`<workingDirectory>`,<br/>
`<enableAssertions>`,<br/>

Configuration is propagated on unit test launch configuration creation and also when executing `maven > update project`

## 2.6.1

* 📅 Release Date: 04th June 2024
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>foo.bar</groupId>
<artifactId>demo-test-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<configuration.prerequisites>
org.codehaus.mojo:properties-maven-plugin:read-project-properties</configuration.prerequisites>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- surefireArgs: replacedByArgsSets -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<!-- failsafeArgs: replacedByArgsSets -->
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
/*******************************************************************************
* Copyright (c) 2024 Pascal Treilhes
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.m2e.jdt.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
import org.eclipse.m2e.jdt.internal.UnitTestSupport;
import org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider;
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@SuppressWarnings("restriction")
@RunWith(Parameterized.class)
public class UnitTestLaunchConfigConfigurationTest extends AbstractMavenProjectTestCase {

private static final String REPLACED_SUREFIRE_POM_STRING = "<!-- surefireArgs: replacedByArgsSets -->";
private static final String REPLACED_FAILSAFE_POM_STRING = "<!-- failsafeArgs: replacedByArgsSets -->";
private static final String ROOT_PATH = "/projects/surefireFailsafeToTestLaunchSettings";
private static ILaunchManager LAUNCH_MANAGER = DebugPlugin.getDefault().getLaunchManager();

private static final String SUREFIRE_ARGS_SET = """
<configuration>
<argLine>
--argLineItem=surefireArgLineValue
</argLine>
<systemPropertyVariables>
<surefireProp1>surefireProp1Value</surefireProp1>
</systemPropertyVariables>
<environmentVariables>
<surefireEnvironmentVariables1>surefireEnvironmentVariables1Value</surefireEnvironmentVariables1>
</environmentVariables>
</configuration>
""";
private static final String FAILSAFE_ARGS_SET = """
<configuration>
<argLine>
--argLineItem=failsafeArgLineValue
</argLine>
<systemPropertyVariables>
<failsafeProp1>failsafeProp1Value</failsafeProp1>
</systemPropertyVariables>
<environmentVariables>
<failsafeEnvironmentVariables1>failsafeEnvironmentVariables1Value</failsafeEnvironmentVariables1>
</environmentVariables>
</configuration>
""";

// Define the parameters to be used in the test
@Parameters
public static Collection<Object> data() {
return List.of(MavenRuntimeClasspathProvider.JDT_TESTNG_TEST, MavenRuntimeClasspathProvider.JDT_JUNIT_TEST);
}

@Parameter(0)
public String testType;

@Override
@Before
public void setUp() throws Exception {
super.setUp();
((MavenConfigurationImpl) MavenPlugin.getMavenConfiguration()).setAutomaticallyUpdateConfiguration(true);
setAutoBuilding(true);
}

@Test
public void test_configuration_must_be_updated_with_surefire_config()
throws CoreException, IOException, InterruptedException {
// Get launch type
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);

assumeTrue(testType + " support not available", type != null);

File pomFile = getTestFile("argumentsAreSet/pom.xml");
String surefireConf = SUREFIRE_ARGS_SET;
String failsafeConf = null;

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);

mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf);
updateProject(project);
waitForJobsToComplete();

updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
assertTrue(updatedConfigurations.length == 1);

ILaunchConfiguration config = updatedConfigurations[0];

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=surefireArgLineValue"));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
(Map<String, String>) null);

assertNotNull(envVars);
assertTrue(envVars.size() == 1);
assertTrue(envVars.containsKey("surefireEnvironmentVariables1"));
assertEquals("surefireEnvironmentVariables1Value", envVars.get("surefireEnvironmentVariables1"));

// check systemPropertyVariables
assertTrue(argLine.contains("-DsurefireProp1=surefireProp1Value"));
}

@Test
public void test_configuration_must_be_updated_with_failsafe_config()
throws CoreException, IOException, InterruptedException {
// Get launch type
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);

assumeTrue(testType + " support not available", type != null);

File pomFile = getTestFile("argumentsAreSet/pom.xml");
String surefireConf = null;
String failsafeConf = FAILSAFE_ARGS_SET;

IProject project = importProject(pomFile.getAbsolutePath());
// waitForJobsToComplete();

// create basic unit test
createDefaultTest(project, type, "test.SomeTestIT");

updateProject(project);
waitForJobsToComplete();

ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
assertTrue(updatedConfigurations.length == 1);

mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf);
updateProject(project);

updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
assertTrue(updatedConfigurations.length == 1);

ILaunchConfiguration config = updatedConfigurations[0];

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue"));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
(Map<String, String>) null);

assertNotNull(envVars);
assertTrue(envVars.size() == 1);
assertTrue(envVars.containsKey("failsafeEnvironmentVariables1"));
assertEquals("failsafeEnvironmentVariables1Value", envVars.get("failsafeEnvironmentVariables1"));

// check systemPropertyVariables
assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value"));
}

@Test
public void test_configuration_must_be_updated_with_surefire_config_when_created()
throws CoreException, IOException, InterruptedException {
// Get launch type
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);

assumeTrue(testType + " support not available", type != null);

File pomFile = getTestFile("argumentsAreSet/pom.xml");
String surefireConf = SUREFIRE_ARGS_SET;
String failsafeConf = null;

IProject project = importProject(pomFile.getAbsolutePath());
mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf);
updateProject(project);
waitForJobsToComplete();

// create basic unit test
createDefaultTest(project, type, "test.SomeTest");

ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
assertTrue(updatedConfigurations.length == 1);

ILaunchConfiguration config = updatedConfigurations[0];

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=surefireArgLineValue"));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
(Map<String, String>) null);

assertNotNull(envVars);
assertTrue(envVars.size() == 1);
assertTrue(envVars.containsKey("surefireEnvironmentVariables1"));
assertEquals("surefireEnvironmentVariables1Value", envVars.get("surefireEnvironmentVariables1"));

// check systemPropertyVariables
assertTrue(argLine.contains("-DsurefireProp1=surefireProp1Value"));
}

@Test
public void test_configuration_must_be_updated_with_failSafe_config_when_created()
throws CoreException, IOException, InterruptedException {
// Get launch type
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);

assumeTrue(testType + " support not available", type != null);

File pomFile = getTestFile("argumentsAreSet/pom.xml");
String surefireConf = null;
String failsafeConf = FAILSAFE_ARGS_SET;

IProject project = importProject(pomFile.getAbsolutePath());
mergePomAndPluginConfigIntoProject(project, pomFile, surefireConf, failsafeConf);
updateProject(project);
waitForJobsToComplete();

// create basic unit test
createDefaultTest(project, type, "test.SomeTestIT");

ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
assertTrue(updatedConfigurations.length == 1);

ILaunchConfiguration config = updatedConfigurations[0];

// check argLine
String argLine = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_VM_ARGUMENTS, "");
assertTrue(argLine.contains("--argLineItem=failsafeArgLineValue"));

// check environmentVariables
Map<String, String> envVars = config.getAttribute(UnitTestSupport.LAUNCH_CONFIG_ENVIRONMENT_VARIABLES,
(Map<String, String>) null);

assertNotNull(envVars);
assertTrue(envVars.size() == 1);
assertTrue(envVars.containsKey("failsafeEnvironmentVariables1"));
assertEquals("failsafeEnvironmentVariables1Value", envVars.get("failsafeEnvironmentVariables1"));

// check systemPropertyVariables
assertTrue(argLine.contains("-DfailsafeProp1=failsafeProp1Value"));
}

private void updateProject(IProject project) throws CoreException, InterruptedException {
MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project, monitor);
waitForJobsToComplete();
}

// Create a default test
private void createDefaultTest(IProject project, ILaunchConfigurationType type, String testClassName)
throws CoreException {
// create basic unit test
ILaunchConfigurationWorkingCopy launchConfig = type.newInstance(project, "sampleTest");
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getName());
launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, testClassName);
launchConfig.setAttribute(DebugPlugin.ATTR_WORKING_DIRECTORY, project.getLocation().toString());
launchConfig.doSave();
}

// Merge the pom and plugins configuration into the project
private void mergePomAndPluginConfigIntoProject(IProject project, File pomTemplate, String surefireConfiguration,
String failsafeConfiguration) throws IOException, CoreException {
String pom = Utils.read(project, pomTemplate);
IFile pomFileWS = project.getFile(pomTemplate.getName());
String newContent = pom;

if (surefireConfiguration != null) {
newContent = newContent.replace(REPLACED_SUREFIRE_POM_STRING, surefireConfiguration);
}

if (failsafeConfiguration != null) {
newContent = newContent.replace(REPLACED_FAILSAFE_POM_STRING, failsafeConfiguration);
}

pomFileWS.setContents(new ByteArrayInputStream(newContent.getBytes()), true, false, null);
}

private File getTestFile(String filename) throws IOException {
return new File(FileLocator.toFileURL(getClass().getResource(ROOT_PATH + "/" + filename)).getFile());
}
}
2 changes: 1 addition & 1 deletion org.eclipse.m2e.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.m2e.jdt;singleton:=true
Bundle-Version: 2.3.500.qualifier
Bundle-Version: 2.3.600.qualifier
Bundle-Localization: plugin
Export-Package: org.eclipse.m2e.jdt,
org.eclipse.m2e.jdt.internal;x-friends:="org.eclipse.m2e.jdt.ui",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni
javaProject.setRawClasspath(classpath.getEntries(), classesFolder.getFullPath(), monitor);

MavenJdtPlugin.getDefault().getBuildpathManager().updateClasspath(project, monitor);

UnitTestSupport.resetLaunchConfigurations(project);
}

@SuppressWarnings("unused")
Expand Down
Loading

0 comments on commit c8bcaaa

Please sign in to comment.