|
| 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.HashMap; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import org.eclipse.core.runtime.CoreException; |
| 23 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 24 | +import org.eclipse.core.runtime.Status; |
| 25 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 26 | +import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry; |
| 27 | +import org.eclipse.osgi.service.resolver.BundleDescription; |
| 28 | +import org.eclipse.osgi.util.NLS; |
| 29 | +import org.eclipse.pde.core.plugin.IPluginModelBase; |
| 30 | +import org.eclipse.pde.internal.launching.launcher.LaunchValidationOperation; |
| 31 | +import org.osgi.framework.Version; |
| 32 | + |
| 33 | +public class JUnitLaunchValidationOperation extends LaunchValidationOperation { |
| 34 | + |
| 35 | + private static final Set<String> JUNIT_PLATFORM_ENGINE_BUNLDES = Set.of(new String[] { // |
| 36 | + "junit-platform-engine", //$NON-NLS-1$ |
| 37 | + "org.junit.platform.engine", //$NON-NLS-1$ |
| 38 | + }); |
| 39 | + |
| 40 | + private final Map<Object, Object[]> fErrors = new HashMap<>(2); |
| 41 | + |
| 42 | + public JUnitLaunchValidationOperation(ILaunchConfiguration configuration, Set<IPluginModelBase> models) { |
| 43 | + super(configuration, models, null); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void run(IProgressMonitor monitor) throws CoreException { |
| 48 | + try { |
| 49 | + checkJunitVersion(fLaunchConfiguration, fModels); |
| 50 | + } catch (CoreException e) { |
| 51 | + PDELaunchingPlugin.log(e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("restriction") |
| 56 | + private void checkJunitVersion(ILaunchConfiguration configuration, Set<IPluginModelBase> models) throws CoreException { |
| 57 | + org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration); |
| 58 | + if (testKind.isNull()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + Set<Version> junitPlatformBundlesVersions = junitPlatformBundleVersions(models); |
| 62 | + String testKindId = testKind.getId(); |
| 63 | + switch (testKindId) { |
| 64 | + case TestKindRegistry.JUNIT3_TEST_KIND_ID, TestKindRegistry.JUNIT4_TEST_KIND_ID -> { |
| 65 | + } // nothing to check |
| 66 | + case TestKindRegistry.JUNIT5_TEST_KIND_ID -> { |
| 67 | + // JUnit 5 platform bundles have version range [1.0,2.0) |
| 68 | + junitPlatformBundlesVersions.stream().map(Version::getMajor).filter(i -> i.intValue() != 1).findFirst().ifPresent(otherVersion -> { |
| 69 | + String message = NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_JUnitLaunchAndRuntimeMissmatch, 5, otherVersion); |
| 70 | + addError(message); |
| 71 | + }); |
| 72 | + } |
| 73 | + default -> throw new CoreException(Status.error("Unsupported test kind: " + testKindId)); //$NON-NLS-1$ |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void addError(String message) { |
| 78 | + fErrors.put(message.replaceAll("\\R", " "), null); //$NON-NLS-1$//$NON-NLS-2$ |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean hasErrors() { |
| 83 | + return !fErrors.isEmpty(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Map<Object, Object[]> getInput() { |
| 88 | + Map<Object, Object[]> map = new LinkedHashMap<>(); |
| 89 | + map.putAll(fErrors); |
| 90 | + return map; |
| 91 | + } |
| 92 | + |
| 93 | + private static Set<Version> junitPlatformBundleVersions(Set<IPluginModelBase> models) { |
| 94 | + return models.stream().map(IPluginModelBase::getBundleDescription) // |
| 95 | + .filter(d -> JUNIT_PLATFORM_ENGINE_BUNLDES.contains(d.getSymbolicName())) // |
| 96 | + .map(BundleDescription::getVersion).collect(Collectors.toSet()); |
| 97 | + } |
| 98 | +} |
0 commit comments