Skip to content

Commit 38dc2bc

Browse files
committed
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 df089ec commit 38dc2bc

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@
1414
*******************************************************************************/
1515
package org.eclipse.pde.internal.core;
1616

17-
import static org.eclipse.pde.internal.core.DependencyManager.Options.INCLUDE_OPTIONAL_DEPENDENCIES;
18-
1917
import java.io.File;
2018
import java.util.ArrayDeque;
2119
import java.util.ArrayList;
2220
import java.util.Arrays;
2321
import java.util.Collection;
22+
import java.util.Collections;
2423
import java.util.Comparator;
2524
import java.util.HashMap;
2625
import java.util.HashSet;
2726
import java.util.List;
2827
import java.util.Map;
29-
import java.util.Objects;
3028
import java.util.Optional;
3129
import java.util.Queue;
3230
import java.util.Set;
@@ -65,6 +63,7 @@
6563
import org.eclipse.pde.internal.core.bnd.BndProjectManager;
6664
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
6765
import org.eclipse.pde.internal.core.natures.BndProject;
66+
import org.osgi.framework.Version;
6867
import org.osgi.resource.Resource;
6968

7069
import aQute.bnd.build.Container;
@@ -75,18 +74,13 @@
7574
class 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

Comments
 (0)