|
| 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 | +} |
0 commit comments