Skip to content

Commit

Permalink
Discover tests with another classloader
Browse files Browse the repository at this point in the history
JUnitPlatformFeature discovers tests before analysis. The discovery
procsee will initialize some of the test classes. As they are loaded by
the ImageClassloader, the initialization will cause eager class
initialization error during native image building time.

This commit uses a different classloader than the ImageClassLoader in
the JUnitPlatformFeature to make sure the test class initialization at
discovery time won't affect the native image class initialization
policy.
  • Loading branch information
ziyilin committed Jun 2, 2023
1 parent 71c741a commit d4a6011
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 99 deletions.
Expand Up @@ -45,30 +45,16 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.UniqueIdTrackingListener;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressWarnings("unused")
public final class JUnitPlatformFeature implements Feature {
Expand All @@ -86,65 +72,67 @@ public void duringSetup(DuringSetupAccess access) {
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
RuntimeClassInitialization.initializeAtBuildTime(NativeImageJUnitLauncher.class);

List<Path> classpathRoots = access.getApplicationClassPath();
List<? extends DiscoverySelector> selectors = getSelectors(classpathRoots);
/**
* In order to avoid initializing test classes loaded by {@link com.oracle.svm.hosted.ImageClassLoader} during
* test discovery process, a separated classloader should be used.
*/
ClassLoader imageClassLoader = Thread.currentThread().getContextClassLoader();

Launcher launcher = LauncherFactory.create();
TestPlan testplan = discoverTestsAndRegisterTestClassesForReflection(launcher, selectors);
ImageSingletons.add(NativeImageJUnitLauncher.class, new NativeImageJUnitLauncher(launcher, testplan));
}
URL[] classpaths = classpathRoots.stream().map(p -> {
try {
return p.toUri().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}).toArray(URL[]::new);
/**
* Get AppClassloader(JDK11) or ExtClassLoader(JDK8) as parent classloader.
* Can't use Classloader.getSystemClassLoader, because current systemClassLoader is NativeImageSystemClassLoader which
* delegates the actual classloader to {@link com.oracle.svm.hosted.ImageClassLoader}.
*/
ClassLoader parent = imageClassLoader.getParent().getParent();
URLClassLoader featureClassLoader = new URLClassLoader(classpaths, parent);
// Set the context classloader for tests discovery
Thread.currentThread().setContextClassLoader(featureClassLoader);

/**
* The following code makes inter-classloader call, i.e., current class is loaded by {@link com.oracle.svm.hosted.ImageClassLoader},
* but need to call methods in class loaded by featureCLassLoader.
* Have to use reflection to call the method of a class that is loaded by different classloader.
*/
Class<TestsDiscoveryHelper> testsDiscoveryHelperClass;
Object testsDiscoveryHelperObj;

private List<? extends DiscoverySelector> getSelectors(List<Path> classpathRoots) {
try {
Path outputDir = Paths.get(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME));
String prefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX);
List<UniqueIdSelector> selectors = readAllFiles(outputDir, prefix)
.map(DiscoverySelectors::selectUniqueId)
.collect(Collectors.toList());
if (!selectors.isEmpty()) {
System.out.printf(
"[junit-platform-native] Running in 'test listener' mode using files matching pattern [%s*] "
+ "found in folder [%s] and its subfolders.%n",
prefix, outputDir.toAbsolutePath());
return selectors;
testsDiscoveryHelperClass = (Class<TestsDiscoveryHelper>) featureClassLoader.loadClass(TestsDiscoveryHelper.class.getName());
Class<List> listClass = (Class<List>) featureClassLoader.loadClass(List.class.getName());
Constructor<TestsDiscoveryHelper> cons = testsDiscoveryHelperClass.getConstructor(boolean.class, listClass);
testsDiscoveryHelperObj = cons.newInstance(debug, classpathRoots);
Method discoverTestsMethod = testsDiscoveryHelperClass.getDeclaredMethod("discoverTests");
Class<List<Class<?>>> listClassWithClass = (Class<List<Class<?>>>) featureClassLoader.loadClass(List.class.getName());

/**
* Call {@link TestsDiscoveryHelper#discoverTests()} to get all the discovered test classes.
*/
Object classListObj = discoverTestsMethod.invoke(testsDiscoveryHelperObj);

/**
* Turn the featureClassloader loaded test classes to {@link com.oracle.svm.hosted.ImageClassLoader} loaded,
* and then registered them for native image building.
*/
for (Class<?> c : listClassWithClass.cast(classListObj)) {
Class<?> class4Register = Class.forName(c.getName(), false, imageClassLoader);
registerTestClassForReflection(class4Register);
}
} catch (Exception ex) {
debug("Failed to read UIDs from UniqueIdTrackingListener output files: " + ex.getMessage());
}

System.out.println("[junit-platform-native] Running in 'test discovery' mode. Note that this is a fallback mode.");
if (debug) {
classpathRoots.forEach(entry -> debug("Selecting classpath root: " + entry));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
return DiscoverySelectors.selectClasspathRoots(new HashSet<>(classpathRoots));
}
// Set the context classloader back
Thread.currentThread().setContextClassLoader(imageClassLoader);

/**
* Use the JUnit Platform Launcher to discover tests and register classes
* for reflection.
*/
private TestPlan discoverTestsAndRegisterTestClassesForReflection(Launcher launcher,
List<? extends DiscoverySelector> selectors) {

LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectors)
.build();

TestPlan testPlan = launcher.discover(request);

testPlan.getRoots().stream()
.flatMap(rootIdentifier -> testPlan.getDescendants(rootIdentifier).stream())
.map(TestIdentifier::getSource)
.filter(Optional::isPresent)
.map(Optional::get)
.filter(ClassSource.class::isInstance)
.map(ClassSource.class::cast)
.map(ClassSource::getJavaClass)
.forEach(this::registerTestClassForReflection);

return testPlan;
ImageSingletons.add(NativeImageJUnitLauncher.class,
new NativeImageJUnitLauncher(new TestsDiscoveryHelper(debug, classpathRoots)));
}

private void registerTestClassForReflection(Class<?> clazz) {
Expand Down Expand Up @@ -173,23 +161,4 @@ public static boolean debug() {
return ImageSingletons.lookup(JUnitPlatformFeature.class).debug;
}

private Stream<String> readAllFiles(Path dir, String prefix) throws IOException {
return findFiles(dir, prefix).map(outputFile -> {
try {
return Files.readAllLines(outputFile);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}).flatMap(List::stream);
}

private static Stream<Path> findFiles(Path dir, String prefix) throws IOException {
if (!Files.exists(dir)) {
return Stream.empty();
}
return Files.find(dir, Integer.MAX_VALUE,
(path, basicFileAttributes) -> (basicFileAttributes.isRegularFile()
&& path.getFileName().toString().startsWith(prefix)));
}

}
Expand Up @@ -43,6 +43,8 @@

import org.graalvm.nativeimage.ImageInfo;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;
Expand All @@ -59,11 +61,17 @@ public class NativeImageJUnitLauncher {
static final String DEFAULT_OUTPUT_FOLDER = Paths.get("test-results-native").resolve("test").toString();

final Launcher launcher;
final TestPlan testPlan;
TestPlan testPlan;
final TestsDiscoveryHelper testsDiscoveryHelper;

public NativeImageJUnitLauncher(Launcher launcher, TestPlan testPlan) {
this.launcher = launcher;
this.testPlan = testPlan;
@Platforms(Platform.HOSTED_ONLY.class)
public NativeImageJUnitLauncher(TestsDiscoveryHelper testsDiscoveryHelper) {
this.testsDiscoveryHelper = testsDiscoveryHelper;
launcher = testsDiscoveryHelper.getLauncher();
}

private void discoverTests() {
testPlan = testsDiscoveryHelper.discoverTestPlan();
}

public void registerTestExecutionListeners(TestExecutionListener testExecutionListener) {
Expand Down Expand Up @@ -115,7 +123,8 @@ public static void main(String... args) {

PrintWriter out = new PrintWriter(System.out);
NativeImageJUnitLauncher launcher = ImageSingletons.lookup(NativeImageJUnitLauncher.class);

//Discover the test plan at runtime.
launcher.discoverTests();
if (!silent) {
out.println("JUnit Platform on Native Image - report");
out.println("----------------------------------------\n");
Expand Down
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.junit.platform;

import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.support.descriptor.ClassSource;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.UniqueIdTrackingListener;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestsDiscoveryHelper {
private List<? extends DiscoverySelector> selectors;
private Launcher launcher = LauncherFactory.create();
private TestPlan testPlan;

public TestsDiscoveryHelper(boolean debug, List<Path> classpathRoots) {
selectors = getSelectors(debug, classpathRoots);
}

public Launcher getLauncher() {
return launcher;
}

public TestPlan discoverTestPlan() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectors)
.build();
testPlan = launcher.discover(request);
return testPlan;
}

/**
* Use the JUnit Platform Launcher to discover tests and register classes
* for reflection.
*
* @return a List of discovered junit test classes
*/
public List<Class<?>> discoverTests() {
discoverTestPlan();
return testPlan.getRoots().stream()
.flatMap(rootIdentifier -> testPlan.getDescendants(rootIdentifier).stream())
.map(TestIdentifier::getSource)
.filter(Optional::isPresent)
.map(Optional::get)
.filter(ClassSource.class::isInstance)
.map(ClassSource.class::cast)
.map(cs -> cs.getJavaClass()).collect(Collectors.toList());
}

private List<? extends DiscoverySelector> getSelectors(boolean debug, List<Path> classpathRoots) {
try {
Path outputDir = Paths.get(System.getProperty(UniqueIdTrackingListener.OUTPUT_DIR_PROPERTY_NAME));
String prefix = System.getProperty(UniqueIdTrackingListener.OUTPUT_FILE_PREFIX_PROPERTY_NAME,
UniqueIdTrackingListener.DEFAULT_OUTPUT_FILE_PREFIX);
List<UniqueIdSelector> selectors = readAllFiles(outputDir, prefix)
.map(DiscoverySelectors::selectUniqueId)
.collect(Collectors.toList());
if (!selectors.isEmpty()) {
System.out.printf(
"[junit-platform-native] Running in 'test listener' mode using files matching pattern [%s*] "
+ "found in folder [%s] and its subfolders.%n",
prefix, outputDir.toAbsolutePath());
return selectors;
}
} catch (Exception ex) {
JUnitPlatformFeature.debug("Failed to read UIDs from UniqueIdTrackingListener output files: " + ex.getMessage());
}

System.out.println("[junit-platform-native] Running in 'test discovery' mode. Note that this is a fallback mode.");
if (debug) {
classpathRoots.forEach(entry -> JUnitPlatformFeature.debug("Selecting classpath root: " + entry));
}
return DiscoverySelectors.selectClasspathRoots(new HashSet<>(classpathRoots));
}

private Stream<String> readAllFiles(Path dir, String prefix) throws IOException {
return findFiles(dir, prefix).map(outputFile -> {
try {
return Files.readAllLines(outputFile);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}).flatMap(List::stream);
}

private static Stream<Path> findFiles(Path dir, String prefix) throws IOException {
if (!Files.exists(dir)) {
return Stream.empty();
}
return Files.find(dir, Integer.MAX_VALUE,
(path, basicFileAttributes) -> (basicFileAttributes.isRegularFile()
&& path.getFileName().toString().startsWith(prefix)));
}
}
Expand Up @@ -58,7 +58,6 @@ public void onLoad(NativeImageConfiguration config) {
"org.junit.platform.launcher.core.LauncherConfigurationParameters",
"org.junit.platform.commons.logging.LoggerFactory",
"org.junit.platform.engine.UniqueIdFormat",
"org.junit.platform.commons.util.ReflectionUtils",
// https://github.com/graalvm/native-build-tools/issues/300
"org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener"
};
Expand Down
Expand Up @@ -53,8 +53,6 @@ public void onLoad(NativeImageConfiguration config) {
"org.junit.vintage.engine.support.UniqueIdReader",
"org.junit.vintage.engine.support.UniqueIdStringifier",
"org.junit.runner.Description",
"org.junit.runners.BlockJUnit4ClassRunner",
"org.junit.runners.JUnit4",
/* Workaround until we can register serializable classes from a native-image feature */
"org.junit.runner.Result"
};
Expand Down

0 comments on commit d4a6011

Please sign in to comment.