Skip to content

Commit 6638c00

Browse files
committed
Validate that a JUnit 5 launch has no dependencies to JUnit 6 bundles
This change introduces support for custom OSGi ResolverHooks in the PDE launch and validation workflow, specifically enabling exclusion of JUnit 6 bundles when running JUnit 5 tests. The changes add a mechanism to inject a ResolverHook into the bundle validation process and implement a hook that filters out JUnit 6 bundles, improving compatibility and reliability of JUnit 5 plug-in test launches. Fixes: #2045
1 parent 4fe820b commit 6638c00

File tree

6 files changed

+123
-3
lines changed

6 files changed

+123
-3
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
import org.eclipse.osgi.util.NLS;
3232
import org.eclipse.pde.core.plugin.IPluginModelBase;
3333
import org.eclipse.pde.internal.build.BundleHelper;
34+
import org.osgi.framework.hooks.resolver.ResolverHook;
3435

3536
public class BundleValidationOperation implements IWorkspaceRunnable {
3637

3738
private static StateObjectFactory FACTORY;
3839

3940
private final Set<IPluginModelBase> fModels;
4041
private final Dictionary<String, String>[] fProperties;
42+
private final ResolverHook fResolverHook;
4143
private State fState;
4244

4345
@SuppressWarnings("unchecked")
@@ -46,8 +48,13 @@ public BundleValidationOperation(Set<IPluginModelBase> models) {
4648
}
4749

4850
public BundleValidationOperation(Set<IPluginModelBase> models, Dictionary<String, String>[] properties) {
51+
this(models, properties, null);
52+
}
53+
54+
public BundleValidationOperation(Set<IPluginModelBase> models, Dictionary<String, String>[] properties, ResolverHook resolverHook) {
4955
fModels = models;
5056
fProperties = properties;
57+
fResolverHook = resolverHook;
5158
}
5259

5360
@Override
@@ -57,6 +64,9 @@ public void run(IProgressMonitor monitor) throws CoreException {
5764
}
5865
SubMonitor subMonitor = SubMonitor.convert(monitor, fModels.size() + 1);
5966
fState = FACTORY.createState(true);
67+
if (fResolverHook != null) {
68+
fState.setResolverHookFactory(c -> fResolverHook);
69+
}
6070
long id = 1;
6171
for (IPluginModelBase fModel : fModels) {
6272
BundleDescription bundle = fModel.getBundleDescription();

ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/PDEMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class PDEMessages extends NLS {
3636
public static String WorkbenchLauncherConfigurationDelegate_noStartup;
3737
public static String JUnitLaunchConfiguration_error_notaplugin;
3838
public static String JUnitLaunchConfiguration_error_missingPlugin;
39+
public static String JUnitLaunchConfiguration_error_invalidJunitVersion;
3940

4041
public static String OSGiLaunchConfiguration_cannotFindLaunchConfiguration;
4142
public static String OSGiLaunchConfiguration_selected;

ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/LaunchValidationOperation.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ public LaunchValidationOperation(ILaunchConfiguration configuration, Set<IPlugin
5858

5959
@Override
6060
public void run(IProgressMonitor monitor) throws CoreException {
61-
fOperation = new BundleValidationOperation(fModels, getPlatformProperties());
61+
fOperation = createOperation(getPlatformProperties());
6262
fOperation.run(monitor);
6363
}
6464

65+
protected BundleValidationOperation createOperation(Dictionary<String, String>[] properties) throws CoreException {
66+
return new BundleValidationOperation(fModels, properties);
67+
}
68+
6569
@SuppressWarnings("unchecked")
6670
protected Dictionary<String, String>[] getPlatformProperties() throws CoreException {
6771
IExecutionEnvironment[] envs = getMatchingEnvironments();

ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/pderesources.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ WorkbenchLauncherConfigurationDelegate_jrePathNotFound = The installation path t
2525
WorkbenchLauncherConfigurationDelegate_noStartup = Launching failed. Bootstrap code cannot be found.
2626
JUnitLaunchConfiguration_error_notaplugin = Could not launch the JUnit plug-in tests because project ''{0}'' is not a plug-in project.
2727
JUnitLaunchConfiguration_error_missingPlugin = Required plug-in ''{0}'' could not be found.
28+
JUnitLaunchConfiguration_error_invalidJunitVersion = JUnit {0} launch may not function with dependency to {1} with version {2}.
2829

2930
OSGiLaunchConfiguration_cannotFindLaunchConfiguration=Cannot find the {0} OSGi framework.
3031
OSGiLaunchConfiguration_selected=selected
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.eclipse.pde.launching;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Dictionary;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
import org.eclipse.core.runtime.CoreException;
12+
import org.eclipse.core.runtime.IStatus;
13+
import org.eclipse.core.runtime.Status;
14+
import org.eclipse.debug.core.ILaunchConfiguration;
15+
import org.eclipse.osgi.service.resolver.BundleDescription;
16+
import org.eclipse.osgi.util.NLS;
17+
import org.eclipse.pde.core.plugin.IPluginModelBase;
18+
import org.eclipse.pde.internal.core.BundleValidationOperation;
19+
import org.eclipse.pde.internal.launching.PDEMessages;
20+
import org.eclipse.pde.internal.launching.launcher.EclipsePluginValidationOperation;
21+
import org.osgi.framework.Constants;
22+
import org.osgi.framework.Version;
23+
import org.osgi.framework.hooks.resolver.ResolverHook;
24+
import org.osgi.framework.wiring.BundleCapability;
25+
import org.osgi.framework.wiring.BundleRequirement;
26+
import org.osgi.framework.wiring.BundleRevision;
27+
28+
final class JUnitEclipsePluginValidationOperation extends EclipsePluginValidationOperation implements ResolverHook {
29+
30+
private static final String JUNIT_BUNDLE_PREFIX = "junit"; //$NON-NLS-1$
31+
private final Map<BundleDescription, IStatus[]> errors;
32+
private final int junitVersion;
33+
34+
@SuppressWarnings("restriction")
35+
public JUnitEclipsePluginValidationOperation(ILaunchConfiguration configuration, Set<IPluginModelBase> models) {
36+
super(configuration, models);
37+
errors = new HashMap<>(2);
38+
org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration);
39+
junitVersion = switch (testKind.getId()) {
40+
case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT3_TEST_KIND_ID -> 3;
41+
case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT4_TEST_KIND_ID -> 4;
42+
case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID -> 5;
43+
default -> throw new IllegalArgumentException("Unknown JUnit launch type: " + testKind.getDisplayName()); //$NON-NLS-1$
44+
};
45+
}
46+
47+
protected BundleValidationOperation createOperation(Dictionary<String, String>[] properties) throws CoreException {
48+
BundleValidationOperation op = new BundleValidationOperation(fModels, properties, this);
49+
return op;
50+
}
51+
52+
@Override
53+
public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
54+
if (!Constants.RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(Constants.RESOLUTION_DIRECTIVE))) {
55+
String requirementName = requirement.getRevision().getSymbolicName();
56+
if (!requirementName.startsWith(JUNIT_BUNDLE_PREFIX)) {
57+
Iterator<BundleCapability> iterator = candidates.iterator();
58+
while (iterator.hasNext()) {
59+
BundleCapability candidate = iterator.next();
60+
BundleRevision candidateRevision = candidate.getRevision();
61+
String name = candidateRevision.getSymbolicName();
62+
Version version = candidateRevision.getVersion();
63+
if (version.getMajor() > junitVersion && name.startsWith(JUNIT_BUNDLE_PREFIX)) {
64+
BundleDescription bundle = getState().getBundle(requirementName, requirement.getRevision().getVersion());
65+
if (bundle != null) {
66+
String error = NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_invalidJunitVersion, new Object[] {Integer.valueOf(junitVersion), name, version});
67+
IStatus[] bundleErrors = errors.computeIfAbsent(bundle, b -> new Status[0]);
68+
if (!Arrays.stream(bundleErrors).map(IStatus::getMessage).anyMatch(m -> error.equals(m))) {
69+
IStatus[] newBundleErrors = Arrays.copyOf(bundleErrors, bundleErrors.length + 1);
70+
newBundleErrors[bundleErrors.length] = Status.error(error);
71+
errors.put(bundle, newBundleErrors);
72+
}
73+
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
@Override
82+
public void filterResolvable(Collection<BundleRevision> candidates) {
83+
}
84+
85+
@Override
86+
public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
87+
}
88+
89+
@Override
90+
public void end() {
91+
}
92+
93+
@Override
94+
public boolean hasErrors() {
95+
return super.hasErrors() || !errors.isEmpty();
96+
}
97+
98+
@Override
99+
public Map<Object, Object[]> getInput() {
100+
Map<Object, Object[]> map = super.getInput();
101+
map.putAll(errors);
102+
return map;
103+
}
104+
105+
}

ui/org.eclipse.pde.launching/src/org/eclipse/pde/launching/JUnitLaunchConfigurationDelegate.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
import org.eclipse.pde.internal.launching.PDELaunchingPlugin;
7575
import org.eclipse.pde.internal.launching.PDEMessages;
7676
import org.eclipse.pde.internal.launching.launcher.BundleLauncherHelper;
77-
import org.eclipse.pde.internal.launching.launcher.EclipsePluginValidationOperation;
7877
import org.eclipse.pde.internal.launching.launcher.LaunchArgumentsHelper;
7978
import org.eclipse.pde.internal.launching.launcher.LaunchConfigurationHelper;
8079
import org.eclipse.pde.internal.launching.launcher.LaunchPluginValidator;
@@ -610,7 +609,7 @@ protected void validateProjectDependencies(ILaunchConfiguration configuration, I
610609
* a progress monitor
611610
*/
612611
protected void validatePluginDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
613-
EclipsePluginValidationOperation op = new EclipsePluginValidationOperation(configuration, fModels.keySet(), launchMode);
612+
JUnitEclipsePluginValidationOperation op = new JUnitEclipsePluginValidationOperation(configuration, fModels.keySet());
614613
LaunchPluginValidator.runValidationOperation(op, monitor);
615614
}
616615
}

0 commit comments

Comments
 (0)