|
| 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.PDELaunchingPlugin; |
| 20 | +import org.eclipse.pde.internal.launching.PDEMessages; |
| 21 | +import org.eclipse.pde.internal.launching.launcher.EclipsePluginValidationOperation; |
| 22 | +import org.osgi.framework.Constants; |
| 23 | +import org.osgi.framework.Version; |
| 24 | +import org.osgi.framework.VersionRange; |
| 25 | +import org.osgi.framework.hooks.resolver.ResolverHook; |
| 26 | +import org.osgi.framework.wiring.BundleCapability; |
| 27 | +import org.osgi.framework.wiring.BundleRequirement; |
| 28 | +import org.osgi.framework.wiring.BundleRevision; |
| 29 | + |
| 30 | +public final class JUnitEclipsePluginValidationOperation extends EclipsePluginValidationOperation implements ResolverHook { |
| 31 | + |
| 32 | + private static final VersionRange JUNIT5_VERSION_RANGE = new VersionRange("[1.0.0,6.0.0)"); //$NON-NLS-1$ |
| 33 | + |
| 34 | + private static final String JUNIT_BUNDLE_PREFIX = "junit"; //$NON-NLS-1$ |
| 35 | + |
| 36 | + private final Map<BundleDescription, IStatus[]> errors; |
| 37 | + private final VersionRange junitVersionRange; |
| 38 | + private final int junitVersion; |
| 39 | + |
| 40 | + @SuppressWarnings("restriction") |
| 41 | + public JUnitEclipsePluginValidationOperation(ILaunchConfiguration configuration, Set<IPluginModelBase> models, String launchMode) { |
| 42 | + super(configuration, models, launchMode); |
| 43 | + errors = new HashMap<>(2); |
| 44 | + org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration); |
| 45 | + switch (testKind.getId()) { |
| 46 | + case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID -> { |
| 47 | + junitVersionRange = JUNIT5_VERSION_RANGE; |
| 48 | + junitVersion = 5; |
| 49 | + } |
| 50 | + default -> { |
| 51 | + junitVersionRange = null; |
| 52 | + junitVersion = -1; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + protected BundleValidationOperation createOperation(Dictionary<String, String>[] properties) throws CoreException { |
| 58 | + ResolverHook hook = junitVersionRange != null ? this : null; |
| 59 | + BundleValidationOperation op = new BundleValidationOperation(fModels, properties, hook); |
| 60 | + return op; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) { |
| 65 | + if (!isOptional(requirement)) { |
| 66 | + BundleRevision requirementRevision = requirement.getRevision(); |
| 67 | + String requirementName = requirementRevision.getSymbolicName(); |
| 68 | + if (!requirementName.startsWith(JUNIT_BUNDLE_PREFIX)) { |
| 69 | + Iterator<BundleCapability> iterator = candidates.iterator(); |
| 70 | + while (iterator.hasNext()) { |
| 71 | + BundleCapability candidate = iterator.next(); |
| 72 | + BundleRevision candidateRevision = candidate.getRevision(); |
| 73 | + String name = candidateRevision.getSymbolicName(); |
| 74 | + Version version = candidateRevision.getVersion(); |
| 75 | + if (!junitVersionRange.includes(version) && name.startsWith(JUNIT_BUNDLE_PREFIX)) { |
| 76 | + Version requirementVersion = requirementRevision.getVersion(); |
| 77 | + BundleDescription bundle = getState().getBundle(requirementName, requirementVersion); |
| 78 | + if (bundle != null) { |
| 79 | + String error = NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_invalidJunitVersion, new Object[] {name, version, Integer.valueOf(junitVersion)}); |
| 80 | + IStatus[] bundleErrors = errors.computeIfAbsent(bundle, b -> new Status[0]); |
| 81 | + if (!Arrays.stream(bundleErrors).map(IStatus::getMessage).anyMatch(m -> error.equals(m))) { |
| 82 | + IStatus[] newBundleErrors = Arrays.copyOf(bundleErrors, bundleErrors.length + 1); |
| 83 | + newBundleErrors[bundleErrors.length] = Status.error(error); |
| 84 | + errors.put(bundle, newBundleErrors); |
| 85 | + } |
| 86 | + } else { |
| 87 | + PDELaunchingPlugin.log(Status.error("Bundle not found: " + requirementName + " " + requirementVersion, new IllegalStateException())); //$NON-NLS-1$ //$NON-NLS-2$ |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private boolean isOptional(BundleRequirement requirement) { |
| 96 | + return Constants.RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(Constants.RESOLUTION_DIRECTIVE)); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void filterResolvable(Collection<BundleRevision> candidates) { |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) { |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void end() { |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean hasErrors() { |
| 113 | + return super.hasErrors() || !errors.isEmpty(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Map<Object, Object[]> getInput() { |
| 118 | + Map<Object, Object[]> map = super.getInput(); |
| 119 | + map.putAll(errors); |
| 120 | + return map; |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments