2020import java .io .ByteArrayInputStream ;
2121import java .io .File ;
2222import java .io .IOException ;
23+ import java .util .Arrays ;
2324import java .util .Collection ;
2425import java .util .List ;
2526import java .util .Map ;
3435import org .eclipse .debug .core .ILaunchConfigurationWorkingCopy ;
3536import org .eclipse .debug .core .ILaunchManager ;
3637import org .eclipse .jdt .launching .IJavaLaunchConfigurationConstants ;
38+ import org .eclipse .jdt .launching .IRuntimeClasspathEntry ;
3739import org .eclipse .m2e .core .MavenPlugin ;
3840import org .eclipse .m2e .core .internal .preferences .MavenConfigurationImpl ;
3941import org .eclipse .m2e .jdt .internal .UnitTestSupport ;
4042import org .eclipse .m2e .jdt .internal .launch .MavenRuntimeClasspathProvider ;
4143import org .eclipse .m2e .tests .common .AbstractMavenProjectTestCase ;
44+ import org .hamcrest .Description ;
4245import org .hamcrest .Matchers ;
46+ import org .hamcrest .TypeSafeMatcher ;
4347import org .junit .Before ;
4448import org .junit .Test ;
4549import 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