|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2006, 2025 IBM Corporation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + * Advantest - GH issue 2006 - JUnit 5 bundle clashes with JUnit 6 |
| 14 | + *******************************************************************************/ |
| 15 | +package org.eclipse.pde.internal.launching; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.LinkedHashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.eclipse.core.runtime.CoreException; |
| 26 | +import org.eclipse.core.runtime.IStatus; |
| 27 | +import org.eclipse.core.runtime.Status; |
| 28 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 29 | +import org.eclipse.osgi.service.resolver.BundleDescription; |
| 30 | +import org.eclipse.osgi.util.NLS; |
| 31 | +import org.eclipse.pde.core.plugin.IPluginModelBase; |
| 32 | +import org.eclipse.pde.core.plugin.PluginRegistry; |
| 33 | +import org.eclipse.pde.internal.core.DependencyManager; |
| 34 | +import org.eclipse.pde.internal.core.PDECore; |
| 35 | +import org.eclipse.pde.internal.launching.launcher.BundleLauncherHelper; |
| 36 | +import org.osgi.framework.VersionRange; |
| 37 | +import org.osgi.framework.wiring.BundleRevision; |
| 38 | + |
| 39 | +public class JUnitLaunchRequirements { |
| 40 | + |
| 41 | + public static final String JUNIT4_RUNTIME_PLUGIN = "org.eclipse.jdt.junit4.runtime"; //$NON-NLS-1$ |
| 42 | + public static final String JUNIT5_RUNTIME_PLUGIN = "org.eclipse.jdt.junit5.runtime"; //$NON-NLS-1$ |
| 43 | + |
| 44 | + private static final VersionRange JUNIT5_VERSIONS = new VersionRange("[1, 5)"); //$NON-NLS-1$ |
| 45 | + |
| 46 | + // we add launcher and jupiter.engine to support @RunWith(JUnitPlatform.class) |
| 47 | + private static final String[] JUNIT5_RUN_WITH_PLUGINS = {"junit-platform-launcher", //$NON-NLS-1$ |
| 48 | + "junit-jupiter-engine", //$NON-NLS-1$ |
| 49 | + }; |
| 50 | + |
| 51 | + public static void addRequiredJunitRuntimePlugins(ILaunchConfiguration configuration, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 52 | + Collection<String> plugins = getRequiredJunitRuntimePlugins(configuration); |
| 53 | + addPlugins(plugins, allBundles, allModels); |
| 54 | + if (plugins.contains(JUNIT5_RUNTIME_PLUGIN) && (allBundles.containsKey("junit-platform-runner") || allBundles.containsKey("org.junit.platform.runner"))) { //$NON-NLS-1$ //$NON-NLS-2$ |
| 55 | + Set<BundleDescription> descriptions = JUnitLaunchRequirements.junit5PlatformRequirements(); |
| 56 | + Set<BundleDescription> junitRquirements = DependencyManager.findRequirementsClosure(descriptions); |
| 57 | + addAbsentRequirements(junitRquirements, allBundles, allModels); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @SuppressWarnings("restriction") |
| 62 | + public static Collection<String> getRequiredJunitRuntimePlugins(ILaunchConfiguration configuration) { |
| 63 | + org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration); |
| 64 | + if (testKind.isNull()) { |
| 65 | + return Collections.emptyList(); |
| 66 | + } |
| 67 | + List<String> plugins = new ArrayList<>(); |
| 68 | + plugins.add("org.eclipse.pde.junit.runtime"); //$NON-NLS-1$ |
| 69 | + |
| 70 | + if (org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT4_TEST_KIND_ID.equals(testKind.getId())) { |
| 71 | + plugins.add(JUNIT4_RUNTIME_PLUGIN); |
| 72 | + } else if (org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID.equals(testKind.getId())) { |
| 73 | + plugins.add(JUNIT5_RUNTIME_PLUGIN); |
| 74 | + } |
| 75 | + return plugins; |
| 76 | + } |
| 77 | + |
| 78 | + private static void addPlugins(Collection<String> plugins, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 79 | + Set<String> requiredPlugins = new LinkedHashSet<>(plugins); |
| 80 | + |
| 81 | + Set<BundleDescription> addedRequirements = new LinkedHashSet<>(); |
| 82 | + addAbsentRequirements(requiredPlugins, addedRequirements, allBundles, allModels); |
| 83 | + |
| 84 | + Set<BundleDescription> requirementsOfRequirements = DependencyManager.findRequirementsClosure(addedRequirements); |
| 85 | + addAbsentRequirements(requirementsOfRequirements, allBundles, allModels); |
| 86 | + } |
| 87 | + |
| 88 | + private static void addAbsentRequirements(Collection<String> requirements, Set<BundleDescription> addedRequirements, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 89 | + for (String id : requirements) { |
| 90 | + List<IPluginModelBase> models = allBundles.computeIfAbsent(id, k -> new ArrayList<>()); |
| 91 | + if (models.stream().noneMatch(m -> m.getBundleDescription().isResolved())) { |
| 92 | + IPluginModelBase model = JUnitLaunchRequirements.findRequiredPluginInTargetOrHost(id); |
| 93 | + models.add(model); |
| 94 | + BundleLauncherHelper.addDefaultStartingBundle(allModels, model); |
| 95 | + if (addedRequirements != null) { |
| 96 | + addedRequirements.add(model.getBundleDescription()); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static void addAbsentRequirements(Set<BundleDescription> toAdd, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 103 | + for (BundleDescription requirement : toAdd) { |
| 104 | + String id = requirement.getSymbolicName(); |
| 105 | + List<IPluginModelBase> models = allBundles.computeIfAbsent(id, k -> new ArrayList<>()); |
| 106 | + boolean replace = !models.isEmpty() && models.stream().noneMatch(m -> m.getBundleDescription().getVersion().equals(requirement.getVersion())); |
| 107 | + if (replace || models.stream().noneMatch(m -> m.getBundleDescription().isResolved())) { |
| 108 | + IPluginModelBase model = JUnitLaunchRequirements.findRequiredPluginInTargetOrHost(requirement); |
| 109 | + if (replace) { |
| 110 | + String startLevel = null; |
| 111 | + for (IPluginModelBase m : models) { |
| 112 | + startLevel = allModels.remove(m); |
| 113 | + } |
| 114 | + models.clear(); |
| 115 | + allModels.put(model, startLevel); |
| 116 | + } |
| 117 | + models.add(model); |
| 118 | + BundleLauncherHelper.addDefaultStartingBundle(allModels, model); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static Set<BundleDescription> junit5PlatformRequirements() throws CoreException { |
| 124 | + Set<BundleDescription> descriptions = new LinkedHashSet<>(); |
| 125 | + for (String id : JUNIT5_RUN_WITH_PLUGINS) { |
| 126 | + IPluginModelBase model = findRequiredPluginInTargetOrHost(id, JUNIT5_VERSIONS); |
| 127 | + if (model != null) { |
| 128 | + BundleDescription description = model.getBundleDescription(); |
| 129 | + descriptions.add(description); |
| 130 | + } |
| 131 | + } |
| 132 | + return descriptions; |
| 133 | + } |
| 134 | + |
| 135 | + public static IPluginModelBase findRequiredPluginInTargetOrHost(String id) throws CoreException { |
| 136 | + return findRequiredPluginInTargetOrHost(id, PluginRegistry.findModel(id)); |
| 137 | + } |
| 138 | + |
| 139 | + private static IPluginModelBase findRequiredPluginInTargetOrHost(String id, VersionRange versionRange) throws CoreException { |
| 140 | + return findRequiredPluginInTargetOrHost(id, PluginRegistry.findModel(id, versionRange)); |
| 141 | + } |
| 142 | + |
| 143 | + public static IPluginModelBase findRequiredPluginInTargetOrHost(BundleRevision bundleRevision) throws CoreException { |
| 144 | + String id = bundleRevision.getSymbolicName(); |
| 145 | + return findRequiredPluginInTargetOrHost(id, PluginRegistry.findModel(bundleRevision)); |
| 146 | + } |
| 147 | + |
| 148 | + private static IPluginModelBase findRequiredPluginInTargetOrHost(String id, IPluginModelBase model) throws CoreException { |
| 149 | + if (model == null || !model.getBundleDescription().isResolved()) { |
| 150 | + // prefer bundle from host over unresolved bundle from target |
| 151 | + model = PDECore.getDefault().findPluginInHost(id); |
| 152 | + } |
| 153 | + if (model == null) { |
| 154 | + String message = NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_missingPlugin, id); |
| 155 | + Status error = new Status(IStatus.ERROR, IPDEConstants.PLUGIN_ID, IStatus.OK, message, null); |
| 156 | + throw new CoreException(error); |
| 157 | + } |
| 158 | + return model; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments