Skip to content

Commit 8bcf724

Browse files
committed
Add test
1 parent a37fc0f commit 8bcf724

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>foo.bar</groupId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<artifactId>project.a</artifactId>
9+
10+
<properties>
11+
<version.surefire>3.0.0</version.surefire>
12+
<version.junit.jupiter>5.14.1</version.junit.jupiter>
13+
</properties>
14+
<build>
15+
<pluginManagement>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-surefire-plugin</artifactId>
20+
<version>${version.surefire}</version>
21+
<dependencies>
22+
<!-- add JUnit4 vintage engine only via plugin classpath -->
23+
<dependency>
24+
<groupId>org.junit.vintage</groupId>
25+
<artifactId>junit-vintage-engine</artifactId>
26+
<version>${version.junit.jupiter}</version>
27+
</dependency>
28+
</dependencies>
29+
</plugin>
30+
</plugins>
31+
</pluginManagement>
32+
</build>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-api</artifactId>
37+
<version>${version.junit.jupiter}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.13.2</version>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package testMvn;
2+
3+
import java.io.StringWriter;
4+
5+
import org.junit.Test;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class JUnit4Test {
9+
10+
@Test
11+
public void test1() {
12+
assertTrue(true);
13+
}
14+
15+
}

org.eclipse.m2e.jdt.tests/src/org/eclipse/m2e/jdt/tests/UnitTestLaunchConfigConfigurationTest.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.ByteArrayInputStream;
2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.util.Arrays;
2324
import java.util.Collection;
2425
import java.util.List;
2526
import java.util.Map;
@@ -34,12 +35,15 @@
3435
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
3536
import org.eclipse.debug.core.ILaunchManager;
3637
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
38+
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
3739
import org.eclipse.m2e.core.MavenPlugin;
3840
import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
3941
import org.eclipse.m2e.jdt.internal.UnitTestSupport;
4042
import org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider;
4143
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
44+
import org.hamcrest.Description;
4245
import org.hamcrest.Matchers;
46+
import org.hamcrest.TypeSafeMatcher;
4347
import org.junit.Before;
4448
import org.junit.Test;
4549
import org.junit.runner.RunWith;
@@ -54,7 +58,7 @@ public class UnitTestLaunchConfigConfigurationTest extends AbstractMavenProjectT
5458
private static final String REPLACED_SUREFIRE_POM_STRING = "<!-- surefireArgs: replacedByArgsSets -->";
5559
private static final String REPLACED_FAILSAFE_POM_STRING = "<!-- failsafeArgs: replacedByArgsSets -->";
5660
private static final String ROOT_PATH = "/projects/surefireFailsafeToTestLaunchSettings";
57-
private static ILaunchManager LAUNCH_MANAGER = DebugPlugin.getDefault().getLaunchManager();
61+
private static final ILaunchManager LAUNCH_MANAGER = DebugPlugin.getDefault().getLaunchManager();
5862

5963
/*
6064
* XML allows encoding set of control characters: space (U+0020), carriage
@@ -101,7 +105,7 @@ public class UnitTestLaunchConfigConfigurationTest extends AbstractMavenProjectT
101105
""";
102106

103107
// Define the parameters to be used in the test
104-
@Parameters
108+
@Parameters(name = "{0}")
105109
public static Collection<Object> data() {
106110
return List.of(MavenRuntimeClasspathProvider.JDT_TESTNG_TEST, MavenRuntimeClasspathProvider.JDT_JUNIT_TEST);
107111
}
@@ -363,6 +367,64 @@ public void test_deferred_variable_are_resolved() throws CoreException, IOExcept
363367
assertFalse(argLine.contains("@{titi.tata}")); // unresolved property is removed
364368
}
365369

370+
@Test
371+
public void testJUnit4TestWithJUnit5Dependency() throws CoreException, IOException, InterruptedException {
372+
assumeTrue("Only relevant for JUnit", MavenRuntimeClasspathProvider.JDT_JUNIT_TEST.equals(testType));
373+
374+
// Get launch type
375+
ILaunchConfigurationType type = LAUNCH_MANAGER.getLaunchConfigurationType(testType);
376+
377+
assumeTrue(testType + " support not available", type != null);
378+
379+
File pomFile = getTestFile("junit5TestProject/pom.xml");
380+
381+
IProject project = importProject(pomFile.getAbsolutePath());
382+
383+
// create basic unit test
384+
createDefaultTest(project, type, "testMvn.JUnit4Test");
385+
386+
updateProject(project);
387+
waitForJobsToComplete();
388+
389+
ILaunchConfiguration[] updatedConfigurations = LAUNCH_MANAGER.getLaunchConfigurations(type);
390+
assertTrue(updatedConfigurations.length == 1);
391+
392+
ILaunchConfiguration config = updatedConfigurations[0];
393+
MavenRuntimeClasspathProvider provider = new MavenRuntimeClasspathProvider();
394+
IRuntimeClasspathEntry[] classpathEntries = provider.computeUnresolvedClasspath(config); // to trigger classpath
395+
// computation
396+
IRuntimeClasspathEntry[] resolvedClasspathEntries = provider.resolveClasspath(classpathEntries, config);
397+
// make sure that vintage engine is on the classpath (as being part of the
398+
// m-surefire-p classpath)
399+
assertThat(Arrays.asList(resolvedClasspathEntries),
400+
Matchers.hasItem(new IRuntimeClasspathEntryMatcherByFilename("junit-vintage-engine-5.14.1.jar")));
401+
}
402+
403+
static final class IRuntimeClasspathEntryMatcherByFilename extends TypeSafeMatcher<IRuntimeClasspathEntry> {
404+
405+
private final String filename;
406+
407+
public IRuntimeClasspathEntryMatcherByFilename(String filename) {
408+
this.filename = filename;
409+
}
410+
411+
@Override
412+
public void describeTo(Description description) {
413+
description.appendText("IRuntimeClasspathEntry with location ending with ").appendValue(filename);
414+
}
415+
416+
@Override
417+
protected void describeMismatchSafely(IRuntimeClasspathEntry item, Description mismatchDescription) {
418+
mismatchDescription.appendText("was IRuntimeClasspathEntry ").appendValue(item)
419+
.appendText(" with location ").appendValue(item.getLocation());
420+
}
421+
422+
@Override
423+
protected boolean matchesSafely(IRuntimeClasspathEntry item) {
424+
return item.getLocation() != null && item.getLocation().endsWith(filename);
425+
}
426+
}
427+
366428
private void updateProject(IProject project) throws CoreException, InterruptedException {
367429
MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project, monitor);
368430
waitForJobsToComplete();
@@ -400,4 +462,5 @@ private void mergePomAndPluginConfigIntoProject(IProject project, File pomTempla
400462
private File getTestFile(String filename) throws IOException {
401463
return new File(FileLocator.toFileURL(getClass().getResource(ROOT_PATH + "/" + filename)).getFile());
402464
}
465+
403466
}

0 commit comments

Comments
 (0)