1717import java .util .LinkedHashMap ;
1818import java .util .Map ;
1919import java .util .Set ;
20- import java .util .stream .Collectors ;
2120
2221import org .eclipse .core .runtime .CoreException ;
2322import org .eclipse .core .runtime .IProgressMonitor ;
2423import org .eclipse .core .runtime .Status ;
2524import org .eclipse .debug .core .ILaunchConfiguration ;
2625import org .eclipse .jdt .internal .junit .launcher .TestKindRegistry ;
2726import org .eclipse .osgi .service .resolver .BundleDescription ;
27+ import org .eclipse .osgi .service .resolver .ExportPackageDescription ;
2828import org .eclipse .osgi .util .NLS ;
2929import org .eclipse .pde .core .plugin .IPluginModelBase ;
3030import org .eclipse .pde .internal .launching .launcher .LaunchValidationOperation ;
31- import org .osgi .framework .Version ;
3231
3332public class JUnitLaunchValidationOperation extends LaunchValidationOperation {
3433
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$
34+ private static final String JUNIT_ORBIT_BUNDLE_PREFIX = "org.junit" ; //$NON-NLS-1$
35+ private static final String JUNIT_MAVEN_BUNDLE_PREFIX = "junit" ; //$NON-NLS-1$
36+
37+ private static final String ORG_JUNIT_JUPITER_ENGINE_PACKAGE = "org.junit.jupiter.engine" ; //$NON-NLS-1$
38+
39+ private static final Set <String > JUNIT_JUPITER_ENGINE_BUNDLES = Set .of (new String [] { //
40+ "junit-jupiter-engine" , //$NON-NLS-1$
41+ "org.junit.jupiter.engine" , //$NON-NLS-1$
3842 });
3943
4044 private final Map <Object , Object []> fErrors = new HashMap <>(2 );
@@ -58,17 +62,34 @@ private void checkJunitVersion(ILaunchConfiguration configuration, Set<IPluginMo
5862 if (testKind .isNull ()) {
5963 return ;
6064 }
61- Set <Version > junitPlatformBundlesVersions = junitPlatformBundleVersions (models );
6265 String testKindId = testKind .getId ();
6366 switch (testKindId ) {
6467 case TestKindRegistry .JUNIT3_TEST_KIND_ID , TestKindRegistry .JUNIT4_TEST_KIND_ID -> {
6568 } // nothing to check
6669 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- });
70+ for (IPluginModelBase model : models ) {
71+ BundleDescription description = model .getBundleDescription ();
72+ String symbolicName = description .getSymbolicName ();
73+ if (symbolicName .startsWith (JUNIT_MAVEN_BUNDLE_PREFIX ) || symbolicName .startsWith (JUNIT_ORBIT_BUNDLE_PREFIX )) {
74+ continue ;
75+ }
76+ BundleDescription [] requires = description .getResolvedRequires ();
77+ for (BundleDescription required : requires ) {
78+ if (JUNIT_JUPITER_ENGINE_BUNDLES .contains (required .getSymbolicName ()) && required .getVersion ().getMajor () == 6 ) {
79+ String message = NLS .bind (PDEMessages .JUnitLaunchConfiguration_error_JUnitLaunchAndRuntimeMissmatch , 5 , required .getVersion ().getMajor (), symbolicName );
80+ addError (message );
81+ break ;
82+ }
83+ }
84+ ExportPackageDescription [] resolvedImports = description .getResolvedImports ();
85+ for (ExportPackageDescription resolvedImport : resolvedImports ) {
86+ if (resolvedImport .getName ().startsWith (ORG_JUNIT_JUPITER_ENGINE_PACKAGE ) && resolvedImport .getVersion ().getMajor () == 6 ) {
87+ String message = NLS .bind (PDEMessages .JUnitLaunchConfiguration_error_JUnitLaunchAndRuntimeMissmatch , 5 , resolvedImport .getVersion ().getMajor (), symbolicName );
88+ addError (message );
89+ break ;
90+ }
91+ }
92+ }
7293 }
7394 default -> throw new CoreException (Status .error ("Unsupported test kind: " + testKindId )); //$NON-NLS-1$
7495 }
@@ -89,10 +110,4 @@ public Map<Object, Object[]> getInput() {
89110 map .putAll (fErrors );
90111 return map ;
91112 }
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- }
98113}
0 commit comments