|
| 1 | +package org.eclipse.pde.launching; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 10 | +import org.eclipse.pde.core.plugin.IPluginModelBase; |
| 11 | +import org.eclipse.pde.internal.launching.launcher.EclipsePluginValidationOperation; |
| 12 | +import org.osgi.framework.Version; |
| 13 | +import org.osgi.framework.hooks.resolver.ResolverHook; |
| 14 | +import org.osgi.framework.wiring.BundleCapability; |
| 15 | +import org.osgi.framework.wiring.BundleRequirement; |
| 16 | +import org.osgi.framework.wiring.BundleRevision; |
| 17 | + |
| 18 | +final class JUnitEclipsePluginValidationOperation extends EclipsePluginValidationOperation { |
| 19 | + |
| 20 | + private final JunitValidationHook junitValidationHook; |
| 21 | + |
| 22 | + public JUnitEclipsePluginValidationOperation(ILaunchConfiguration configuration, Set<IPluginModelBase> models) { |
| 23 | + super(configuration, models); |
| 24 | + junitValidationHook = new JunitValidationHook(configuration); |
| 25 | + if (junitValidationHook.junitVersion == 5) { |
| 26 | + setResolverHook(junitValidationHook); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean hasErrors() { |
| 32 | + return super.hasErrors() || !junitValidationHook.errors.isEmpty(); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public Map<Object, Object[]> getInput() { |
| 37 | + Map<Object, Object[]> map = super.getInput(); |
| 38 | + map.putAll(junitValidationHook.errors); |
| 39 | + return map; |
| 40 | + } |
| 41 | + |
| 42 | + private static class JunitValidationHook implements ResolverHook { |
| 43 | + |
| 44 | + private final Map<Object, Object[]> errors; |
| 45 | + private final int junitVersion; |
| 46 | + |
| 47 | + @SuppressWarnings("restriction") |
| 48 | + private JunitValidationHook(ILaunchConfiguration configuration) { |
| 49 | + errors = new HashMap<>(2); |
| 50 | + org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration); |
| 51 | + if (org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID.equals(testKind.getId())) { |
| 52 | + junitVersion = 5; |
| 53 | + } else { |
| 54 | + junitVersion = -1; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) { |
| 60 | + Map<String, String> directives = requirement.getDirectives(); |
| 61 | + if (!"optional".equals(directives.get("resolution"))) { //$NON-NLS-1$ //$NON-NLS-2$ |
| 62 | + String requirementName = requirement.getRevision().getSymbolicName(); |
| 63 | + if (!requirementName.startsWith("junit")) { //$NON-NLS-1$ |
| 64 | + Iterator<BundleCapability> iterator = candidates.iterator(); |
| 65 | + while (iterator.hasNext()) { |
| 66 | + BundleCapability candidate = iterator.next(); |
| 67 | + BundleRevision candidateRevision = candidate.getRevision(); |
| 68 | + String name = candidateRevision.getSymbolicName(); |
| 69 | + Version version = candidateRevision.getVersion(); |
| 70 | + if (version.getMajor() > junitVersion && name.startsWith("junit")) { //$NON-NLS-1$ |
| 71 | + // TODO: externalize |
| 72 | + String error = requirementName + " depends on " + name + "_" + version; //$NON-NLS-1$ //$NON-NLS-2$ |
| 73 | + errors.put(requirement, new Object[] {error}); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void filterResolvable(Collection<BundleRevision> candidates) { |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) { |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void end() { |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments