Skip to content

Commit c06c4ef

Browse files
trancexpressHannesWell
authored andcommitted
Support JUnit 5 and JUnit 6 for org.eclipse.pde.core.requiredPlugins
When a plug-in requires the JUnit bundle junit-jupiter-api, if the target platform contains both JUnit 5 and JUnit 6, RequiredPluginsClasspathContainer will put JUnit 6 on the JDT classpath. This is problematic, since a plug-in cannot then specify JUnit 5, even when constraining the version of junit-jupiter-api. This change checks what version of junit-jupiter-api is required, then adds only that version of JUnit bundles to the container. Fixes: #2007
1 parent d0405a5 commit c06c4ef

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/RequiredPluginsClasspathContainer.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
import org.eclipse.pde.internal.core.bnd.BndProjectManager;
6666
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
6767
import org.eclipse.pde.internal.core.natures.BndProject;
68+
import org.osgi.framework.Version;
69+
import org.osgi.framework.VersionRange;
6870
import org.osgi.resource.Resource;
6971

7072
import aQute.bnd.build.Container;
@@ -74,19 +76,26 @@
7476

7577
class RequiredPluginsClasspathContainer {
7678

79+
private static final Version JUNIT_5_9 = new Version(5, 9, 0);
80+
private static final VersionRange BELOW_JUNIT_5_9 = new VersionRange("[1.0,5.9)"); //$NON-NLS-1$
81+
7782
@SuppressWarnings("nls")
7883
private static final Set<String> JUNIT5_RUNTIME_PLUGINS = Set.of("org.junit", //
84+
"junit-platform-launcher",
85+
"org.junit.platform.launcher",
7986
"junit-jupiter-engine", // BSN of the bundle from Maven-Central
8087
"org.junit.jupiter.engine"); // BSN of the bundle from Eclipse-Orbit
8188
@SuppressWarnings("nls")
82-
private static final Set<String> JUNIT5_API_PLUGINS = Set.of( //
89+
private static final Set<String> JUNIT_JUPITER_API_BUNDLES = Set.of( //
8390
"junit-jupiter-api", // BSN of the bundle from Maven-Central
8491
"org.junit.jupiter.api"); // BSN of the bundle from Eclipse-Orbit
8592

93+
private static final Comparator<BundleDescription> BUNDLE_VERSION = Comparator
94+
.comparing(BundleDescription::getVersion);
95+
8696
private final IPluginModelBase fModel;
8797
private final IBuild fBuild;
8898

89-
private List<BundleDescription> junit5RuntimeClosure;
9099
private IClasspathEntry[] fEntries;
91100
private boolean addImportedPackages;
92101

@@ -575,20 +584,28 @@ protected void addExtraClasspathEntries(List<IClasspathEntry> entries, String[]
575584
*/
576585
private void addJunit5RuntimeDependencies(Set<BundleDescription> added, List<IClasspathEntry> entries)
577586
throws CoreException {
578-
if (!containsJunit5Dependency(added)) {
587+
Optional<BundleDescription> highestJunitBundle = getHighestJunitBundle(added);
588+
if (highestJunitBundle.isEmpty()) {
579589
return;
580590
}
581-
582-
if (junit5RuntimeClosure == null) {
583-
junit5RuntimeClosure = collectJunit5RuntimeRequirements();
591+
BundleDescription junitBundle = highestJunitBundle.get();
592+
Collection<BundleDescription> junitRequirements;
593+
if (junitBundle.getVersion().compareTo(JUNIT_5_9) < 0) {
594+
// JUnit 5.8 and below bundles don't have specific version requirements that we can use
595+
junitRequirements = collectRuntimeRequirementsBelowJunit5_9();
596+
} else {
597+
junitRequirements = DependencyManager.findRequirementsClosure(List.of(junitBundle));
598+
}
599+
if (junitRequirements.isEmpty()) {
600+
return;
584601
}
585602

586603
String id = fModel.getPluginBase().getId();
587-
if (id != null && junit5RuntimeClosure.stream().map(BundleDescription::getSymbolicName).anyMatch(id::equals)) {
604+
if (id != null && junitRequirements.stream().map(BundleDescription::getSymbolicName).anyMatch(id::equals)) {
588605
return; // never extend the classpath of a junit bundle
589606
}
590607

591-
for (BundleDescription desc : junit5RuntimeClosure) {
608+
for (BundleDescription desc : junitRequirements) {
592609
if (added.contains(desc)) {
593610
continue; // bundle has explicit dependency
594611
}
@@ -599,14 +616,10 @@ private void addJunit5RuntimeDependencies(Set<BundleDescription> added, List<ICl
599616
}
600617
}
601618

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();
619+
private static List<BundleDescription> collectRuntimeRequirementsBelowJunit5_9() {
620+
List<BundleDescription> roots = JUNIT5_RUNTIME_PLUGINS.stream()
621+
.map(id -> PluginRegistry.findModel(id, BELOW_JUNIT_5_9)).filter(Objects::nonNull)
622+
.filter(IPluginModelBase::isEnabled).map(IPluginModelBase::getBundleDescription).toList();
610623
Set<BundleDescription> closure = DependencyManager.findRequirementsClosure(roots,
611624
INCLUDE_OPTIONAL_DEPENDENCIES);
612625
String systemBundleBSN = TargetPlatformHelper.getPDEState().getSystemBundle();
@@ -716,4 +729,7 @@ private void addExtraLibrary(IPath path, IPluginModelBase model, List<IClasspath
716729
}
717730
}
718731

732+
private static Optional<BundleDescription> getHighestJunitBundle(Collection<BundleDescription> bundles) {
733+
return bundles.stream().filter(b -> JUNIT_JUPITER_API_BUNDLES.contains(b.getSymbolicName())).max(BUNDLE_VERSION);
734+
}
719735
}

0 commit comments

Comments
 (0)