|
15 | 15 |
|
16 | 16 | import java.util.ArrayList; |
17 | 17 | import java.util.Collection; |
18 | | -import java.util.Collections; |
19 | 18 | import java.util.Comparator; |
20 | | -import java.util.HashSet; |
21 | 19 | import java.util.LinkedHashSet; |
22 | 20 | import java.util.List; |
23 | 21 | import java.util.Map; |
| 22 | +import java.util.Optional; |
24 | 23 | import java.util.Set; |
25 | | -import java.util.stream.Collectors; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Stream; |
26 | 26 |
|
27 | 27 | import org.eclipse.core.runtime.CoreException; |
28 | 28 | import org.eclipse.core.runtime.Status; |
|
34 | 34 | import org.eclipse.pde.internal.core.DependencyManager; |
35 | 35 | import org.eclipse.pde.internal.core.PDECore; |
36 | 36 | import org.eclipse.pde.internal.launching.launcher.BundleLauncherHelper; |
| 37 | +import org.osgi.framework.wiring.BundleRevision; |
37 | 38 |
|
38 | 39 | public class JUnitLaunchRequirements { |
39 | 40 |
|
40 | 41 | public static final String JUNIT4_JDT_RUNTIME_PLUGIN = "org.eclipse.jdt.junit4.runtime"; //$NON-NLS-1$ |
41 | 42 | public static final String JUNIT5_JDT_RUNTIME_PLUGIN = "org.eclipse.jdt.junit5.runtime"; //$NON-NLS-1$ |
| 43 | + private static final Comparator<IPluginModelBase> VERSION = Comparator.comparing(p -> p.getBundleDescription().getVersion()); |
42 | 44 |
|
43 | 45 | public static void addRequiredJunitRuntimePlugins(ILaunchConfiguration configuration, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
44 | | - Set<String> requiredPlugins = new LinkedHashSet<>(getRequiredJunitRuntimeEclipsePlugins(configuration)); |
45 | | - |
46 | | - if (allBundles.containsKey("junit-platform-runner")) { //$NON-NLS-1$ |
47 | | - // add launcher and jupiter.engine to support @RunWith(JUnitPlatform.class) |
48 | | - requiredPlugins.add("junit-platform-launcher"); //$NON-NLS-1$ |
49 | | - requiredPlugins.add("junit-jupiter-engine"); //$NON-NLS-1$ |
50 | | - } |
51 | | - |
52 | | - Set<BundleDescription> addedRequirements = new HashSet<>(); |
53 | | - addAbsentRequirements(requiredPlugins, addedRequirements, allBundles, allModels); |
54 | | - |
55 | | - Set<BundleDescription> requirementsOfRequirements = DependencyManager.findRequirementsClosure(addedRequirements); |
56 | | - Set<String> rorIds = requirementsOfRequirements.stream().map(BundleDescription::getSymbolicName).collect(Collectors.toSet()); |
57 | | - addAbsentRequirements(rorIds, null, allBundles, allModels); |
| 46 | + Collection<String> runtimePlugins = getRequiredJunitRuntimeEclipsePlugins(configuration); |
| 47 | + Set<BundleDescription> addedRuntimeBundles = addAbsentRequirements(runtimePlugins, allBundles, allModels); |
| 48 | + Set<BundleDescription> runtimeRequirements = DependencyManager.findRequirementsClosure(addedRuntimeBundles); |
| 49 | + addAbsentRequirements(runtimeRequirements, allBundles, allModels); |
58 | 50 | } |
59 | 51 |
|
60 | 52 | @SuppressWarnings("restriction") |
61 | 53 | public static Collection<String> getRequiredJunitRuntimeEclipsePlugins(ILaunchConfiguration configuration) { |
62 | 54 | org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration); |
63 | 55 | if (testKind.isNull()) { |
64 | | - return Collections.emptyList(); |
| 56 | + return List.of(); |
65 | 57 | } |
66 | 58 | List<String> plugins = new ArrayList<>(); |
67 | 59 | plugins.add("org.eclipse.pde.junit.runtime"); //$NON-NLS-1$ |
68 | | - |
69 | | - if (org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT4_TEST_KIND_ID.equals(testKind.getId())) { |
70 | | - plugins.add("org.eclipse.jdt.junit4.runtime"); //$NON-NLS-1$ |
71 | | - } else if (org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID.equals(testKind.getId())) { |
72 | | - plugins.add("org.eclipse.jdt.junit5.runtime"); //$NON-NLS-1$ |
| 60 | + switch (testKind.getId()) { |
| 61 | + case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT3_TEST_KIND_ID -> { |
| 62 | + } // Nothing to add for JUnit-3 |
| 63 | + case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT4_TEST_KIND_ID -> plugins.add(JUNIT4_JDT_RUNTIME_PLUGIN); |
| 64 | + case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID -> plugins.add(JUNIT5_JDT_RUNTIME_PLUGIN); |
| 65 | + default -> throw new IllegalArgumentException("Unsupported junit test kind: " + testKind.getId()); //$NON-NLS-1$ |
73 | 66 | } |
74 | 67 | return plugins; |
75 | 68 | } |
76 | 69 |
|
77 | | - private static void addAbsentRequirements(Collection<String> requirements, Set<BundleDescription> addedRequirements, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 70 | + private static Set<BundleDescription> addAbsentRequirements(Collection<String> requirements, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 71 | + Set<BundleDescription> addedRequirements = new LinkedHashSet<>(); |
78 | 72 | for (String id : requirements) { |
79 | 73 | List<IPluginModelBase> models = allBundles.computeIfAbsent(id, k -> new ArrayList<>()); |
80 | | - if (models.stream().noneMatch(m -> m.getBundleDescription().isResolved())) { |
81 | | - IPluginModelBase model = findRequiredPluginInTargetOrHost(id); |
| 74 | + if (models.stream().noneMatch(p -> p.getBundleDescription().isResolved())) { |
| 75 | + IPluginModelBase model = findRequiredPluginInTargetOrHost(PluginRegistry.findModel(id), plugins -> plugins.max(VERSION), id); |
| 76 | + models.add(model); |
| 77 | + BundleLauncherHelper.addDefaultStartingBundle(allModels, model); |
| 78 | + addedRequirements.add(model.getBundleDescription()); |
| 79 | + } |
| 80 | + } |
| 81 | + return addedRequirements; |
| 82 | + } |
| 83 | + |
| 84 | + private static void addAbsentRequirements(Set<BundleDescription> requirements, Map<String, List<IPluginModelBase>> allBundles, Map<IPluginModelBase, String> allModels) throws CoreException { |
| 85 | + for (BundleRevision bundle : requirements) { |
| 86 | + String id = bundle.getSymbolicName(); |
| 87 | + List<IPluginModelBase> models = allBundles.computeIfAbsent(id, k -> new ArrayList<>()); |
| 88 | + if (models.stream().map(IPluginModelBase::getBundleDescription).noneMatch(b -> b.isResolved() && b.getVersion().equals(bundle.getVersion()))) { |
| 89 | + IPluginModelBase model = findRequiredPluginInTargetOrHost(PluginRegistry.findModel(bundle), plgs -> plgs.filter(p -> p.getBundleDescription() == bundle).findFirst(), id); |
82 | 90 | models.add(model); |
83 | 91 | BundleLauncherHelper.addDefaultStartingBundle(allModels, model); |
84 | | - if (addedRequirements != null) { |
85 | | - addedRequirements.add(model.getBundleDescription()); |
86 | | - } |
87 | 92 | } |
88 | 93 | } |
89 | 94 | } |
90 | 95 |
|
91 | | - private static IPluginModelBase findRequiredPluginInTargetOrHost(String id) throws CoreException { |
92 | | - IPluginModelBase model = PluginRegistry.findModel(id); |
| 96 | + private static IPluginModelBase findRequiredPluginInTargetOrHost(IPluginModelBase model, Function<Stream<IPluginModelBase>, Optional<IPluginModelBase>> pluginSelector, String id) throws CoreException { |
93 | 97 | if (model == null || !model.getBundleDescription().isResolved()) { |
94 | 98 | // prefer bundle from host over unresolved bundle from target |
95 | | - model = PDECore.getDefault().findPluginInHost(id).max(Comparator.comparing(p -> p.getBundleDescription().getVersion())).orElse(null); |
96 | | - } |
97 | | - if (model == null) { |
98 | | - throw new CoreException(Status.error(NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_missingPlugin, id))); |
| 99 | + model = pluginSelector.apply(PDECore.getDefault().findPluginInHost(id)) // |
| 100 | + .orElseThrow(() -> new CoreException(Status.error(NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_missingPlugin, id)))); |
99 | 101 | } |
100 | 102 | return model; |
101 | 103 | } |
| 104 | + |
102 | 105 | } |
0 commit comments