Skip to content

Commit 573e733

Browse files
committed
Limit Test Engines by using only compatible ones
Currently the RemotePluginTestRunner uses Class.forName to check for compatible engines but with JUnit 5/6 this now depends on the order of the (flat) classpath. This now changes the code to use a predicate and handle the case better by loading the class trough the provider or limiting the version when we know to run on JUnit 5.
1 parent e5ecc3a commit 573e733

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/RemotePluginTestRunner.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* Runs JUnit tests contained inside a plugin.
3737
*/
3838
public class RemotePluginTestRunner extends RemoteTestRunner {
39+
40+
private static final String TEST_ENGINE_CLASS = "org.junit.platform.engine.TestEngine"; //$NON-NLS-1$
3941

4042
private String fTestPluginName;
4143
private ClassLoader fLoaderClassLoader;
@@ -138,17 +140,30 @@ private static ClassLoader createJUnit5PluginClassLoader(String testPluginName)
138140
throw new IllegalArgumentException("Bundle \"" + testPluginName + "\" not found. Possible causes include missing dependencies, too restrictive version ranges, or a non-matching required execution environment."); //$NON-NLS-1$ //$NON-NLS-2$
139141
}
140142
Bundle junit5RuntimeBundle = Platform.getBundle("org.eclipse.jdt.junit5.runtime"); //$NON-NLS-1$
141-
List<Bundle> platformEngineBundles = findTestEngineBundles();
143+
List<Bundle> platformEngineBundles = findTestEngineBundles(engine -> {
144+
try {
145+
if (junit5RuntimeBundle == null) {
146+
//Fallback ...
147+
Class<?> thisTestEngine = Class.forName(TEST_ENGINE_CLASS);
148+
Class<?> bundleTestEngine = engine.loadClass(TEST_ENGINE_CLASS);
149+
return thisTestEngine == bundleTestEngine;
150+
}
151+
return engine.loadClass(TEST_ENGINE_CLASS).equals(junit5RuntimeBundle.loadClass(TEST_ENGINE_CLASS));
152+
} catch (ClassNotFoundException e) {
153+
throw new RuntimeException("Not found?", e); //$NON-NLS-1$
154+
}
155+
});
142156
platformEngineBundles.add(testBundle);
143157
if (junit5RuntimeBundle != null) {
144158
platformEngineBundles.add(junit5RuntimeBundle);
145159
}
146160
return new MultiBundleClassLoader(platformEngineBundles);
147161
}
148162

149-
private static List<Bundle> findTestEngineBundles() {
150-
BundleContext bundleContext = FrameworkUtil.getBundle(RemotePluginTestRunner.class).getBundleContext();
151-
return Arrays.stream(bundleContext.getBundles()).filter(RemotePluginTestRunner::providesCompatibleTestEngine).collect(toCollection(ArrayList::new));
163+
private static List<Bundle> findTestEngineBundles(Predicate<Bundle> engineFilter) {
164+
Bundle bundle = FrameworkUtil.getBundle(RemotePluginTestRunner.class);
165+
BundleContext bundleContext = bundle.getBundleContext();
166+
return Arrays.stream(bundleContext.getBundles()).filter(b -> providesCompatibleTestEngine(b, engineFilter)).collect(toCollection(ArrayList::new));
152167
}
153168

154169
/**
@@ -161,16 +176,14 @@ private static List<Bundle> findTestEngineBundles() {
161176
* the org.eclipse.tycho.surefire.osgibooter bundle is found
162177
* that may provide a different JUnit platform version than the
163178
* one available via the Eclipse target platform.
179+
* @param engineFilter
164180
*/
165-
private static boolean providesCompatibleTestEngine(Bundle bundle) {
181+
private static boolean providesCompatibleTestEngine(Bundle bundle, Predicate<Bundle> engineFilter) {
166182
try {
167183
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
168-
String testEngineClass = "org.junit.platform.engine.TestEngine"; //$NON-NLS-1$
169-
Collection<String> engineProviders = bundleWiring.listResources("META-INF/services", testEngineClass, BundleWiring.LISTRESOURCES_LOCAL); //$NON-NLS-1$
184+
Collection<String> engineProviders = bundleWiring.listResources("META-INF/services", TEST_ENGINE_CLASS, BundleWiring.LISTRESOURCES_LOCAL); //$NON-NLS-1$
170185
if (!engineProviders.isEmpty()) {
171-
Class<?> thisTestEngine = Class.forName(testEngineClass);
172-
Class<?> bundleTestEngine = bundle.loadClass(testEngineClass);
173-
return thisTestEngine == bundleTestEngine;
186+
return engineFilter.test(bundle);
174187
}
175188
} catch (Exception e) {
176189
// skip this bundle
@@ -204,7 +217,7 @@ public void init(String[] args) {
204217
// during initialization - see bug 520811
205218
ClassLoader currentTCCL = Thread.currentThread().getContextClassLoader();
206219
try {
207-
Thread.currentThread().setContextClassLoader(new MultiBundleClassLoader(findTestEngineBundles()));
220+
Thread.currentThread().setContextClassLoader(new MultiBundleClassLoader(findTestEngineBundles(bundle -> bundle.getVersion().getMajor() < 6)));
208221
defaultInit(args);
209222
} finally {
210223
Thread.currentThread().setContextClassLoader(currentTCCL);

0 commit comments

Comments
 (0)