Skip to content

Commit

Permalink
[575] junit-native does not support org.junit.rules.ExpectedException.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzezula committed Feb 19, 2024
1 parent ffd9d85 commit 154b00c
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.UniqueIdSelector;
Expand All @@ -59,13 +60,15 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
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.ServiceLoader;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -93,6 +96,46 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
Launcher launcher = LauncherFactory.create();
TestPlan testplan = discoverTestsAndRegisterTestClassesForReflection(launcher, selectors);
ImageSingletons.add(NativeImageJUnitLauncher.class, new NativeImageJUnitLauncher(launcher, testplan));

Method registerAllDeclaredMethods = findMethodOrNull(RuntimeReflection.class, "registerAllDeclaredMethods", Class.class);
if (registerAllDeclaredMethods != null) {
// graalvm-23.0+ with `RuntimeReflection#registerAllDeclaredMethods` method.
ClassLoader applicationLoader = access.getApplicationClassLoader();
Class<?> typeSafeMatcher = findClassOrNull(applicationLoader, "org.hamcrest.TypeSafeMatcher");
Class<?> typeSafeDiagnosingMatcher = findClassOrNull(applicationLoader, "org.hamcrest.TypeSafeDiagnosingMatcher");
if (typeSafeMatcher != null || typeSafeDiagnosingMatcher != null) {
BiConsumer<DuringAnalysisAccess, Class<?>> registerMatcherForReflection = (a, c) -> {
try {
registerAllDeclaredMethods.invoke(null, c);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
};
if (typeSafeMatcher != null) {
access.registerSubtypeReachabilityHandler(registerMatcherForReflection, typeSafeMatcher);
}
if (typeSafeDiagnosingMatcher != null) {
access.registerSubtypeReachabilityHandler(registerMatcherForReflection, typeSafeDiagnosingMatcher);
}
}

}
}

private static Class<?> findClassOrNull(ClassLoader loader, String className) {
try {
return loader.loadClass(className);
} catch (ClassNotFoundException e) {
return null;
}
}

private static Method findMethodOrNull(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
try {
return clazz.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
return null;
}
}

private List<? extends DiscoverySelector> getSelectors(List<Path> classpathRoots) {
Expand Down

0 comments on commit 154b00c

Please sign in to comment.