2929import org .osgi .framework .Bundle ;
3030import org .osgi .framework .BundleContext ;
3131import org .osgi .framework .FrameworkUtil ;
32+ import org .osgi .framework .VersionRange ;
3233import org .osgi .framework .wiring .BundleWiring ;
3334
3435/**
3536 * Runs JUnit tests contained inside a plugin.
3637 */
3738public class RemotePluginTestRunner extends RemoteTestRunner {
3839
40+ private static final String ORG_ECLIPSE_JDT_JUNIT5_RUNTIME = "org.eclipse.jdt.junit5.runtime" ; //$NON-NLS-1$
41+ private static final String ORG_ECLIPSE_JDT_JUNIT6_RUNTIME = "org.eclipse.jdt.junit6.runtime" ; //$NON-NLS-1$
42+ private static final VersionRange JUNIT5_VERSION_RANGE = new VersionRange ("[1.0.0,6.0.0)" ); //$NON-NLS-1$
43+ private static final VersionRange JUNIT6_VERSION_RANGE = new VersionRange ("[6.0.0,7.0.0)" ); //$NON-NLS-1$
3944 private String fTestPluginName ;
4045 private ClassLoader fLoaderClassLoader ;
4146
@@ -102,13 +107,24 @@ public static void main(String[] args) {
102107 RemotePluginTestRunner testRunner = new RemotePluginTestRunner ();
103108 testRunner .init (args );
104109 ClassLoader currentTCCL = Thread .currentThread ().getContextClassLoader ();
105- if (isJUnit5 (args )) {
106- //change the classloader so that the test classes in testplugin are discoverable
107- //by junit5 framework see bug 520811
108- Thread .currentThread ().setContextClassLoader (createJUnit5PluginClassLoader (testRunner .getTestPluginName ()));
110+ boolean isJUnit5 = isJUnit5 (args );
111+ boolean isJUnit6 = !isJUnit5 && isJUnit6 (args );
112+ String junitRuntimeBundle = ORG_ECLIPSE_JDT_JUNIT6_RUNTIME ;
113+ VersionRange junitVersionRange = JUNIT6_VERSION_RANGE ;
114+ int junitVersion = 6 ;
115+ if (isJUnit5 ) {
116+ junitRuntimeBundle = ORG_ECLIPSE_JDT_JUNIT5_RUNTIME ;
117+ junitVersionRange = JUNIT5_VERSION_RANGE ;
118+ junitVersion = 5 ;
119+ }
120+ boolean isJUnitJupiter = isJUnit5 || isJUnit6 ;
121+ //change the classloader so that the test classes in testplugin are discoverable
122+ //by junit5 framework see bug 520811
123+ if (isJUnitJupiter ) {
124+ Thread .currentThread ().setContextClassLoader (createJUnitJupiterPluginClassLoader (testRunner .getTestPluginName (), junitRuntimeBundle , junitVersionRange , junitVersion ));
109125 }
110126 testRunner .run ();
111- if (isJUnit5 ( args ) ) {
127+ if (isJUnitJupiter ) {
112128 Thread .currentThread ().setContextClassLoader (currentTCCL );
113129 }
114130 }
@@ -131,21 +147,21 @@ private static String getState(int state) {
131147 return Integer .toString (state );
132148 }
133149
134- private static ClassLoader createJUnit5PluginClassLoader (String testPluginName ) {
150+ private static ClassLoader createJUnitJupiterPluginClassLoader (String testPluginName , String junitRuntimeBundle , VersionRange versionRange , int junitVersion ) {
135151 Bundle testBundle = Platform .getBundle (testPluginName );
136152 if (testBundle == null ) {
137153 throw new IllegalArgumentException ("Bundle \" " + testPluginName + "\" not found. Possible causes include missing dependencies, too restrictive version ranges, mixed JUnit versions, or a non-matching required execution environment." ); //$NON-NLS-1$ //$NON-NLS-2$
138154 }
139- Bundle junit5RuntimeBundle = Platform .getBundle ("org.eclipse.jdt.junit5.runtime" ); //$NON-NLS-1$
140- List <Bundle > platformEngineBundles = findTestEngineBundles ();
155+ Bundle junit5RuntimeBundle = Platform .getBundle (junitRuntimeBundle );
156+ List <Bundle > platformEngineBundles = findTestEngineBundles (versionRange );
141157 platformEngineBundles .add (testBundle );
142158 if (junit5RuntimeBundle != null ) {
143159 platformEngineBundles .add (junit5RuntimeBundle );
144160 }
145- return new SPIBundleClassLoader (platformEngineBundles );
161+ return new SPIBundleClassLoader (platformEngineBundles , junitVersion );
146162 }
147163
148- private static List <Bundle > findTestEngineBundles () {
164+ private static List <Bundle > findTestEngineBundles (VersionRange versionRange ) {
149165 BundleContext bundleContext = FrameworkUtil .getBundle (RemotePluginTestRunner .class ).getBundleContext ();
150166 return Arrays .stream (bundleContext .getBundles ()).filter (RemotePluginTestRunner ::providesTestEngine ).collect (Collectors .toCollection (ArrayList ::new ));
151167 }
@@ -186,12 +202,20 @@ public ClassLoader getClassLoader(final String bundleId) {
186202 @ Override
187203 public void init (String [] args ) {
188204 readPluginArgs (args );
189- if (isJUnit5 (args )) {
190- // changing the classloader to get the testengines for junit5
191- // during initialization - see bug 520811
205+ // changing the classloader to get the testengines for junit5/junit6
206+ // during initialization - see bug 520811
207+ boolean isJUnit5 = isJUnit5 (args );
208+ boolean isJUnit6 = !isJUnit5 && isJUnit6 (args );
209+ VersionRange versionRange = JUNIT6_VERSION_RANGE ;
210+ int junitVersion = 6 ;
211+ if (isJUnit5 ) {
212+ versionRange = JUNIT5_VERSION_RANGE ;
213+ junitVersion = 5 ;
214+ }
215+ if (isJUnit5 || isJUnit6 ) {
192216 ClassLoader currentTCCL = Thread .currentThread ().getContextClassLoader ();
193217 try {
194- Thread .currentThread ().setContextClassLoader (new SPIBundleClassLoader (findTestEngineBundles () ));
218+ Thread .currentThread ().setContextClassLoader (new SPIBundleClassLoader (findTestEngineBundles (versionRange ), junitVersion ));
195219 defaultInit (args );
196220 } finally {
197221 Thread .currentThread ().setContextClassLoader (currentTCCL );
@@ -206,6 +230,11 @@ private static boolean isJUnit5(String[] args) {
206230 return indexOf (args , "-runasjunit5" ::equalsIgnoreCase ) > -1 || indexOf (args , "org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader" ::equals ) > -1 ;
207231 }
208232
233+ @ SuppressWarnings ("nls" )
234+ private static boolean isJUnit6 (String [] args ) {
235+ return indexOf (args , "-runasjunit6" ::equalsIgnoreCase ) > -1 || indexOf (args , "org.eclipse.jdt.internal.junit6.runner.JUnit6TestLoader" ::equals ) > -1 ;
236+ }
237+
209238 public void readPluginArgs (String [] args ) {
210239 fTestPluginName = getArgumentValue (args , "-testpluginname" ); //$NON-NLS-1$
211240 String loaderPlugin = getArgumentValue (args , "-loaderpluginname" ); //$NON-NLS-1$
0 commit comments