2424import java .nio .file .Path ;
2525import java .util .ArrayList ;
2626import java .util .Arrays ;
27- import java .util .Collection ;
2827import java .util .Collections ;
2928import java .util .Comparator ;
3029import java .util .LinkedHashMap ;
31- import java .util .LinkedHashSet ;
3230import java .util .List ;
3331import java .util .Map ;
3432import java .util .Properties ;
35- import java .util .Set ;
36- import java .util .function .Supplier ;
3733import java .util .jar .JarOutputStream ;
3834import java .util .jar .Manifest ;
3935import java .util .stream .Collectors ;
5854import org .eclipse .jdt .launching .IVMInstall ;
5955import org .eclipse .jdt .launching .IVMRunner ;
6056import org .eclipse .jdt .launching .JavaRuntime ;
61- import org .eclipse .osgi .service .resolver .BundleDescription ;
6257import org .eclipse .osgi .util .ManifestElement ;
6358import org .eclipse .osgi .util .NLS ;
6459import org .eclipse .pde .core .plugin .IFragment ;
6964import org .eclipse .pde .core .plugin .TargetPlatform ;
7065import org .eclipse .pde .internal .build .IPDEBuildConstants ;
7166import org .eclipse .pde .internal .core .ClasspathHelper ;
72- import org .eclipse .pde .internal .core .DependencyManager ;
7367import org .eclipse .pde .internal .core .ICoreConstants ;
74- import org .eclipse .pde .internal .core .PDECore ;
7568import org .eclipse .pde .internal .core .TargetPlatformHelper ;
7669import org .eclipse .pde .internal .core .bnd .PdeProjectAnalyzer ;
7770import org .eclipse .pde .internal .core .util .CoreUtility ;
7871import org .eclipse .pde .internal .core .util .VersionUtil ;
7972import org .eclipse .pde .internal .launching .IPDEConstants ;
73+ import org .eclipse .pde .internal .launching .JUnitLaunchRequirements ;
8074import org .eclipse .pde .internal .launching .PDELaunchingPlugin ;
8175import org .eclipse .pde .internal .launching .PDEMessages ;
8276import org .eclipse .pde .internal .launching .launcher .BundleLauncherHelper ;
8882import org .eclipse .pde .internal .launching .launcher .RequirementHelper ;
8983import org .eclipse .pde .internal .launching .launcher .VMHelper ;
9084import org .osgi .framework .Constants ;
91- import org .osgi .framework .VersionRange ;
9285
9386/**
9487 * A launch delegate for launching JUnit Plug-in tests.
10093 */
10194public class JUnitLaunchConfigurationDelegate extends org .eclipse .jdt .junit .launcher .JUnitLaunchConfigurationDelegate {
10295
103- public static final String JUNIT4_RUNTIME_PLUGIN = "org.eclipse.jdt.junit4.runtime" ; //$NON-NLS-1$
104- public static final String JUNIT5_RUNTIME_PLUGIN = "org.eclipse.jdt.junit5.runtime" ; //$NON-NLS-1$
105-
106- private static final VersionRange JUNIT5_VERSIONS = new VersionRange ("[1, 5)" ); //$NON-NLS-1$
107-
10896 static {
10997 RequirementHelper .registerLaunchTypeRequirements ("org.eclipse.pde.ui.JunitLaunchConfig" , lc -> { //$NON-NLS-1$
11098 // Junit launch configs can have the core test application set in either the 'app to test' or the 'application' attribute
@@ -399,36 +387,6 @@ protected String getApplication(ILaunchConfiguration configuration) {
399387 return application ;
400388 }
401389
402- private static IPluginModelBase findRequiredPluginInTargetOrHost (String id ) throws CoreException {
403- IPluginModelBase model = findRequiredPluginInTargetOrHost (id , () -> PluginRegistry .findModel (id ));
404- return model ;
405- }
406-
407- private static IPluginModelBase findRequiredPluginInTargetOrHost (String id , VersionRange versionRange ) throws CoreException {
408- IPluginModelBase model = findRequiredPluginInTargetOrHost (id , () -> PluginRegistry .findModel (id , versionRange ));
409- return model ;
410- }
411-
412- private static IPluginModelBase findRequiredPluginInTargetOrHost (BundleDescription bundleDescription ) throws CoreException {
413- String id = bundleDescription .getSymbolicName ();
414- IPluginModelBase model = findRequiredPluginInTargetOrHost (id , () -> PluginRegistry .findModel ((org .osgi .resource .Resource ) bundleDescription ));
415- return model ;
416- }
417-
418- private static IPluginModelBase findRequiredPluginInTargetOrHost (String id , Supplier <IPluginModelBase > findModel ) throws CoreException {
419- IPluginModelBase model = findModel .get ();
420- if (model == null || !model .getBundleDescription ().isResolved ()) {
421- // prefer bundle from host over unresolved bundle from target
422- model = PDECore .getDefault ().findPluginInHost (id );
423- }
424- if (model == null ) {
425- String message = NLS .bind (PDEMessages .JUnitLaunchConfiguration_error_missingPlugin , id );
426- Status error = new Status (IStatus .ERROR , IPDEConstants .PLUGIN_ID , IStatus .OK , message , null );
427- throw new CoreException (error );
428- }
429- return model ;
430- }
431-
432390 @ Override
433391 public String getProgramArguments (ILaunchConfiguration configuration ) throws CoreException {
434392 return LaunchArgumentsHelper .getUserProgramArguments (configuration );
@@ -561,7 +519,7 @@ protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch
561519 launchMode = launch .getLaunchMode ();
562520
563521 // implicitly add the plug-ins required for JUnit testing if necessary
564- addRequiredJunitRuntimePlugins (configuration );
522+ JUnitLaunchRequirements . addRequiredJunitRuntimePlugins (configuration , fAllBundles , fModels );
565523
566524 String attribute = launch .getAttribute (PDE_JUNIT_SHOW_COMMAND );
567525 boolean isShowCommand = false ;
@@ -582,96 +540,6 @@ protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch
582540 synchronizeManifests (configuration , subMonitor .split (1 ));
583541 }
584542
585- private void addRequiredJunitRuntimePlugins (ILaunchConfiguration configuration ) throws CoreException {
586- Collection <String > plugins = getRequiredJunitRuntimePlugins (configuration );
587- addPlugins (plugins );
588- if (plugins .contains (JUNIT5_RUNTIME_PLUGIN ) &&
589- (fAllBundles .containsKey ("junit-platform-runner" ) || fAllBundles .containsKey ("org.junit.platform.runner" ))) { //$NON-NLS-1$ //$NON-NLS-2$
590- Set <BundleDescription > descriptions = junit5PlatformRequirements ();
591- Set <BundleDescription > junitRquirements = DependencyManager .findRequirementsClosure (descriptions );
592- addAbsentRequirements (junitRquirements );
593- }
594- }
595-
596- /**
597- * @noreference This method is not intended to be referenced by clients.
598- * @return plugins required by JUnit 5 platform bundle
599- */
600- public static Set <BundleDescription > junit5PlatformRequirements () throws CoreException {
601- // add launcher and jupiter.engine to support @RunWith(JUnitPlatform.class)
602- String [] requiredPlugins = {
603- "junit-platform-launcher" , //$NON-NLS-1$
604- "junit-jupiter-engine" , //$NON-NLS-1$
605- };
606- Set <BundleDescription > descriptions = new LinkedHashSet <>();
607- for (String id : requiredPlugins ) {
608- IPluginModelBase model = findRequiredPluginInTargetOrHost (id , JUNIT5_VERSIONS );
609- if (model != null ) {
610- BundleDescription description = model .getBundleDescription ();
611- descriptions .add (description );
612- }
613- }
614- return descriptions ;
615- }
616-
617- private void addPlugins (Collection <String > plugins ) throws CoreException {
618- Set <String > requiredPlugins = new LinkedHashSet <>(plugins );
619-
620- Set <BundleDescription > addedRequirements = new LinkedHashSet <>();
621- addAbsentRequirements (requiredPlugins , addedRequirements );
622-
623- Set <BundleDescription > requirementsOfRequirements = DependencyManager .findRequirementsClosure (addedRequirements );
624- addAbsentRequirements (requirementsOfRequirements );
625- }
626-
627- private void addAbsentRequirements (Collection <String > requirements , Set <BundleDescription > addedRequirements ) throws CoreException {
628- for (String id : requirements ) {
629- List <IPluginModelBase > models = fAllBundles .computeIfAbsent (id , k -> new ArrayList <>());
630- if (models .stream ().noneMatch (m -> m .getBundleDescription ().isResolved ())) {
631- IPluginModelBase model = findRequiredPluginInTargetOrHost (id );
632- models .add (model );
633- BundleLauncherHelper .addDefaultStartingBundle (fModels , model );
634- if (addedRequirements != null ) {
635- addedRequirements .add (model .getBundleDescription ());
636- }
637- }
638- }
639- }
640-
641- private void addAbsentRequirements (Set <BundleDescription > toAdd ) throws CoreException {
642- for (BundleDescription requirement : toAdd ) {
643- String id = requirement .getSymbolicName ();
644- List <IPluginModelBase > models = fAllBundles .computeIfAbsent (id , k -> new ArrayList <>());
645- if (models .stream ().noneMatch (m -> m .getBundleDescription ().isResolved ())) {
646- IPluginModelBase model = findRequiredPluginInTargetOrHost (requirement );
647- models .add (model );
648- BundleLauncherHelper .addDefaultStartingBundle (fModels , model );
649- }
650- }
651- }
652-
653- /**
654- * @noreference This method is not intended to be referenced by clients.
655- * @param configuration non null config
656- * @return required plugins
657- */
658- @ SuppressWarnings ("restriction" )
659- public static Collection <String > getRequiredJunitRuntimePlugins (ILaunchConfiguration configuration ) {
660- org .eclipse .jdt .internal .junit .launcher .ITestKind testKind = org .eclipse .jdt .internal .junit .launcher .JUnitLaunchConfigurationConstants .getTestRunnerKind (configuration );
661- if (testKind .isNull ()) {
662- return Collections .emptyList ();
663- }
664- List <String > plugins = new ArrayList <>();
665- plugins .add ("org.eclipse.pde.junit.runtime" ); //$NON-NLS-1$
666-
667- if (org .eclipse .jdt .internal .junit .launcher .TestKindRegistry .JUNIT4_TEST_KIND_ID .equals (testKind .getId ())) {
668- plugins .add (JUNIT4_RUNTIME_PLUGIN );
669- } else if (org .eclipse .jdt .internal .junit .launcher .TestKindRegistry .JUNIT5_TEST_KIND_ID .equals (testKind .getId ())) {
670- plugins .add (JUNIT5_RUNTIME_PLUGIN );
671- }
672- return plugins ;
673- }
674-
675543 /**
676544 * Checks for old-style plugin.xml files that have become stale since the last launch.
677545 * For any stale plugin.xml files found, the corresponding MANIFEST.MF is deleted
0 commit comments