|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Simeon Andreev 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 | + * Simeon Andreev - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.pde.internal.launching; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +import org.eclipse.core.runtime.CoreException; |
| 26 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 27 | +import org.eclipse.core.runtime.IStatus; |
| 28 | +import org.eclipse.core.runtime.Status; |
| 29 | +import org.eclipse.debug.core.DebugPlugin; |
| 30 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 31 | +import org.eclipse.debug.core.IStatusHandler; |
| 32 | +import org.eclipse.osgi.service.resolver.BundleDescription; |
| 33 | +import org.eclipse.osgi.util.NLS; |
| 34 | +import org.eclipse.pde.core.plugin.IPluginModelBase; |
| 35 | +import org.eclipse.pde.internal.launching.launcher.LaunchValidationOperation; |
| 36 | + |
| 37 | +public class JUnitLaunchValidationOperation extends LaunchValidationOperation { |
| 38 | + |
| 39 | + private static final Set<String> JUNIT_JUPITER_BUNLDES = new HashSet<>(Arrays.asList(new String[] { |
| 40 | + "junit-jupiter-api", //$NON-NLS-1$ |
| 41 | + "org.junit.jupiter.api", //$NON-NLS-1$ |
| 42 | + "junit-jupiter-engine", //$NON-NLS-1$ |
| 43 | + "org.junit.jupiter.engine", //$NON-NLS-1$ |
| 44 | + })); |
| 45 | + |
| 46 | + private static final Object[] EMPTY = new Object[0]; |
| 47 | + private final Map<Object, Object[]> fErrors = new HashMap<>(2); |
| 48 | + |
| 49 | + public JUnitLaunchValidationOperation(ILaunchConfiguration configuration, Set<IPluginModelBase> models) { |
| 50 | + super(configuration, models, null); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void run(IProgressMonitor monitor) throws CoreException { |
| 55 | + try { |
| 56 | + checkJunitVersion(fLaunchConfiguration, fModels); |
| 57 | + } catch (CoreException e) { |
| 58 | + PDELaunchingPlugin.log(e); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("restriction") |
| 63 | + private void checkJunitVersion(ILaunchConfiguration configuration, Set<IPluginModelBase> models) throws CoreException { |
| 64 | + org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration); |
| 65 | + if (testKind.isNull()) { |
| 66 | + return; |
| 67 | + } |
| 68 | + String testKindId = testKind.getId(); |
| 69 | + boolean junit5Launch = org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID.equals(testKindId); |
| 70 | + if (junit5Launch) { |
| 71 | + List<BundleDescription> junitBundles = junitBundles(models); |
| 72 | + List<BundleDescription> junit5Bundles = junitBundles.stream().filter(d -> d.getVersion().getMajor() <= 5).toList(); |
| 73 | + List<BundleDescription> junit6Bundles = junitBundles.stream().filter(d -> d.getVersion().getMajor() == 6).toList(); |
| 74 | + |
| 75 | + if (!junit5Bundles.isEmpty() && !junit6Bundles.isEmpty()) { |
| 76 | + String junit5BundleIds = toString(junit5Bundles); |
| 77 | + String junit6BundleIds = toString(junit6Bundles); |
| 78 | + String message = NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_MixedJUnit5And6Runtime, junit5BundleIds, junit6BundleIds); |
| 79 | + addError(message); |
| 80 | + } |
| 81 | + if (!junit6Bundles.isEmpty() && junit5Launch) { |
| 82 | + String junit6BundleIds = toString(junit6Bundles); |
| 83 | + String message = NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_JUnit5LaunchJUnit6Runtime, junit6BundleIds); |
| 84 | + addError(message); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void addError(String message) throws CoreException { |
| 90 | + IStatus status = Status.error(message); |
| 91 | + IStatusHandler statusHandler = DebugPlugin.getDefault().getStatusHandler(status); |
| 92 | + Object extensionError = null; |
| 93 | + if (statusHandler == null) { |
| 94 | + extensionError = status.getMessage(); |
| 95 | + } else { |
| 96 | + extensionError = statusHandler.handleStatus(status, message); |
| 97 | + } |
| 98 | + fErrors.put(extensionError, EMPTY); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean hasErrors() { |
| 103 | + return !fErrors.isEmpty(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Map<Object, Object[]> getInput() { |
| 108 | + Map<Object, Object[]> map = new LinkedHashMap<>(); |
| 109 | + map.putAll(fErrors); |
| 110 | + return map; |
| 111 | + } |
| 112 | + |
| 113 | + private static List<BundleDescription> junitBundles(Set<IPluginModelBase> models) { |
| 114 | + return models.stream().map(IPluginModelBase::getBundleDescription).filter(d -> JUNIT_JUPITER_BUNLDES.contains(d.getSymbolicName())).toList(); |
| 115 | + } |
| 116 | + |
| 117 | + private static String toString(List<BundleDescription> bundleDescriptions) { |
| 118 | + return bundleDescriptions.stream().map(BundleDescription::getSymbolicName).collect(Collectors.joining(System.lineSeparator())); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments