1414 *******************************************************************************/
1515package org .eclipse .pde .internal .core ;
1616
17- import static org .eclipse .pde .internal .core .DependencyManager .Options .INCLUDE_OPTIONAL_DEPENDENCIES ;
18-
1917import java .io .File ;
2018import java .util .ArrayDeque ;
2119import java .util .ArrayList ;
2220import java .util .Arrays ;
2321import java .util .Collection ;
22+ import java .util .Collections ;
2423import java .util .Comparator ;
2524import java .util .HashMap ;
2625import java .util .HashSet ;
2726import java .util .List ;
2827import java .util .Map ;
29- import java .util .Objects ;
3028import java .util .Optional ;
3129import java .util .Queue ;
3230import java .util .Set ;
6563import org .eclipse .pde .internal .core .bnd .BndProjectManager ;
6664import org .eclipse .pde .internal .core .ibundle .IBundlePluginModelBase ;
6765import org .eclipse .pde .internal .core .natures .BndProject ;
66+ import org .osgi .framework .Version ;
6867import org .osgi .resource .Resource ;
6968
7069import aQute .bnd .build .Container ;
7574class RequiredPluginsClasspathContainer {
7675
7776 @ SuppressWarnings ("nls" )
78- private static final Set <String > JUNIT5_RUNTIME_PLUGINS = Set .of ("org.junit" , //
79- "junit-jupiter-engine" , // BSN of the bundle from Maven-Central
80- "org.junit.jupiter.engine" ); // BSN of the bundle from Eclipse-Orbit
81- @ SuppressWarnings ("nls" )
8277 private static final Set <String > JUNIT5_API_PLUGINS = Set .of ( //
8378 "junit-jupiter-api" , // BSN of the bundle from Maven-Central
8479 "org.junit.jupiter.api" ); // BSN of the bundle from Eclipse-Orbit
8580
8681 private final IPluginModelBase fModel ;
8782 private final IBuild fBuild ;
8883
89- private List <BundleDescription > junit5RuntimeClosure ;
9084 private IClasspathEntry [] fEntries ;
9185 private boolean addImportedPackages ;
9286
@@ -575,20 +569,22 @@ protected void addExtraClasspathEntries(List<IClasspathEntry> entries, String[]
575569 */
576570 private void addJunit5RuntimeDependencies (Set <BundleDescription > added , List <IClasspathEntry > entries )
577571 throws CoreException {
578- if (!containsJunit5Dependency (added )) {
572+ Optional <BundleDescription > highesJunitBundle = getHighestJunitBundle (added );
573+ if (highesJunitBundle .isEmpty ()) {
579574 return ;
580575 }
581-
582- if (junit5RuntimeClosure == null ) {
583- junit5RuntimeClosure = collectJunit5RuntimeRequirements ();
576+ Set <BundleDescription > junitRequirements = DependencyManager
577+ .findRequirementsClosure (Collections .singleton (highesJunitBundle .get ()));
578+ if (junitRequirements .isEmpty ()) {
579+ return ;
584580 }
585581
586582 String id = fModel .getPluginBase ().getId ();
587- if (id != null && junit5RuntimeClosure .stream ().map (BundleDescription ::getSymbolicName ).anyMatch (id ::equals )) {
583+ if (id != null && junitRequirements .stream ().map (BundleDescription ::getSymbolicName ).anyMatch (id ::equals )) {
588584 return ; // never extend the classpath of a junit bundle
589585 }
590586
591- for (BundleDescription desc : junit5RuntimeClosure ) {
587+ for (BundleDescription desc : junitRequirements ) {
592588 if (added .contains (desc )) {
593589 continue ; // bundle has explicit dependency
594590 }
@@ -599,21 +595,6 @@ private void addJunit5RuntimeDependencies(Set<BundleDescription> added, List<ICl
599595 }
600596 }
601597
602- private boolean containsJunit5Dependency (Collection <BundleDescription > dependencies ) {
603- return dependencies .stream ().map (BundleDescription ::getSymbolicName ).anyMatch (JUNIT5_API_PLUGINS ::contains );
604- }
605-
606- private static List <BundleDescription > collectJunit5RuntimeRequirements () {
607- List <BundleDescription > roots = JUNIT5_RUNTIME_PLUGINS .stream ().map (PluginRegistry ::findModel )
608- .filter (Objects ::nonNull ).filter (IPluginModelBase ::isEnabled )
609- .map (IPluginModelBase ::getBundleDescription ).toList ();
610- Set <BundleDescription > closure = DependencyManager .findRequirementsClosure (roots ,
611- INCLUDE_OPTIONAL_DEPENDENCIES );
612- String systemBundleBSN = TargetPlatformHelper .getPDEState ().getSystemBundle ();
613- return closure .stream ().filter (b -> !b .getSymbolicName ().equals (systemBundleBSN ))
614- .sorted (Comparator .comparing (BundleDescription ::getSymbolicName )).toList ();
615- }
616-
617598 private void addSecondaryDependencies (BundleDescription desc , Set <BundleDescription > added ,
618599 List <IClasspathEntry > entries ) {
619600 try {
@@ -716,4 +697,18 @@ private void addExtraLibrary(IPath path, IPluginModelBase model, List<IClasspath
716697 }
717698 }
718699
700+ private static Optional <BundleDescription > getHighestJunitBundle (Collection <BundleDescription > bundleDescriptions ) {
701+ return bundleDescriptions .stream ().filter (RequiredPluginsClasspathContainer ::isJunit5ApiBundle )
702+ .max (RequiredPluginsClasspathContainer ::bundleVersionsCompare );
703+ }
704+
705+ private static int bundleVersionsCompare (BundleDescription bd1 , BundleDescription bd2 ) {
706+ Version v1 = bd1 .getVersion ();
707+ Version v2 = bd2 .getVersion ();
708+ return v1 .compareTo (v2 );
709+ }
710+
711+ private static boolean isJunit5ApiBundle (BundleDescription bundleDescription ) {
712+ return JUNIT5_API_PLUGINS .contains (bundleDescription .getSymbolicName ());
713+ }
719714}
0 commit comments