Skip to content

Commit c8c97f6

Browse files
committed
Check for mixed JUnit 5 and JUnit 6 launch type and required bundles
This changes ensures an error dialog is shown, if a user runs a JUnit 5 launch with required JUnit 6 bundles. Fixes: #2045
1 parent dd6dca3 commit c8c97f6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/JUnitLaunchRequirements.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
package org.eclipse.pde.internal.launching;
1515

1616
import java.util.ArrayList;
17+
import java.util.Arrays;
1718
import java.util.Collection;
19+
import java.util.HashSet;
1820
import java.util.LinkedHashSet;
1921
import java.util.List;
2022
import java.util.Map;
@@ -41,7 +43,16 @@ public class JUnitLaunchRequirements {
4143
private static final String JUNIT4_JDT_RUNTIME_PLUGIN = "org.eclipse.jdt.junit4.runtime"; //$NON-NLS-1$
4244
private static final String JUNIT5_JDT_RUNTIME_PLUGIN = "org.eclipse.jdt.junit5.runtime"; //$NON-NLS-1$
4345

46+
private static final Set<String> JUNIT_JUPITER_BUNLDES = new HashSet<>(Arrays.asList(new String[] {
47+
"junit-jupiter-api", //$NON-NLS-1$
48+
"org.junit.jupiter.api", //$NON-NLS-1$
49+
"junit-jupiter-engine", //$NON-NLS-1$
50+
"org.junit.jupiter.engine", //$NON-NLS-1$
51+
}));
52+
53+
4454
public static void addRequiredJunitRuntimePlugins(ILaunchConfiguration configuration, Map<String, List<IPluginModelBase>> collectedModels, Map<IPluginModelBase, String> startLevelMap) throws CoreException {
55+
checkJunitVersion(configuration, startLevelMap.keySet());
4556
Collection<String> runtimePlugins = getRequiredJunitRuntimeEclipsePlugins(configuration);
4657
Set<BundleDescription> addedRuntimeBundles = addAbsentRequirements(runtimePlugins, collectedModels, startLevelMap);
4758
Set<BundleDescription> runtimeRequirements = DependencyManager.findRequirementsClosure(addedRuntimeBundles);
@@ -103,4 +114,31 @@ private static IPluginModelBase findRequiredPluginInTargetOrHost(IPluginModelBas
103114
return model;
104115
}
105116

117+
@SuppressWarnings("restriction")
118+
private static void checkJunitVersion(ILaunchConfiguration configuration, Set<IPluginModelBase> models) throws CoreException {
119+
org.eclipse.jdt.internal.junit.launcher.ITestKind testKind = org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration);
120+
if (testKind.isNull()) {
121+
return;
122+
}
123+
switch (testKind.getId()) {
124+
case org.eclipse.jdt.internal.junit.launcher.TestKindRegistry.JUNIT5_TEST_KIND_ID -> checkJunit5Version(models);
125+
}
126+
}
127+
128+
private static void checkJunit5Version(Set<IPluginModelBase> models) throws CoreException {
129+
List<String> junit6Bundles = junitBundles(models, 6);
130+
if (!junit6Bundles.isEmpty()) {
131+
String bundleIds = String.join(", ", junit6Bundles); //$NON-NLS-1$
132+
throw new CoreException(Status.error(NLS.bind(PDEMessages.JUnitLaunchConfiguration_error_JUnit5LaunchJUnit6Runtime, bundleIds)));
133+
}
134+
}
135+
136+
private static List<String> junitBundles(Set<IPluginModelBase> models, int majorVersion) {
137+
List<String> junit6Bundles = models.stream()
138+
.map(IPluginModelBase::getBundleDescription)
139+
.filter(d -> JUNIT_JUPITER_BUNLDES.contains(d.getSymbolicName()) && d.getVersion().getMajor() == 6)
140+
.map(BundleDescription::getSymbolicName)
141+
.toList();
142+
return junit6Bundles;
143+
}
106144
}

ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/PDEMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class PDEMessages extends NLS {
3636
public static String WorkbenchLauncherConfigurationDelegate_noStartup;
3737
public static String JUnitLaunchConfiguration_error_notaplugin;
3838
public static String JUnitLaunchConfiguration_error_missingPlugin;
39+
public static String JUnitLaunchConfiguration_error_JUnit5LaunchJUnit6Runtime;
3940

4041
public static String OSGiLaunchConfiguration_cannotFindLaunchConfiguration;
4142
public static String OSGiLaunchConfiguration_selected;

ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/pderesources.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ WorkbenchLauncherConfigurationDelegate_jrePathNotFound = The installation path t
2525
WorkbenchLauncherConfigurationDelegate_noStartup = Launching failed. Bootstrap code cannot be found.
2626
JUnitLaunchConfiguration_error_notaplugin = Could not launch the JUnit plug-in tests because project ''{0}'' is not a plug-in project.
2727
JUnitLaunchConfiguration_error_missingPlugin = Required plug-in ''{0}'' could not be found.
28+
JUnitLaunchConfiguration_error_JUnit5LaunchJUnit6Runtime = JUnit 5 type launch cannot run with JUnit 6 bundles. Update the launch to use JUnit 6 or restrict version requirements of ''{0}'' to JUnit 5.
2829

2930
OSGiLaunchConfiguration_cannotFindLaunchConfiguration=Cannot find the {0} OSGi framework.
3031
OSGiLaunchConfiguration_selected=selected

0 commit comments

Comments
 (0)