Skip to content

Commit d80a948

Browse files
committed
Provide support for earlier JUnit versions without JUnit duplicates on claspath
1 parent 829494f commit d80a948

File tree

4 files changed

+19
-58
lines changed

4 files changed

+19
-58
lines changed

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/JUnitPlatformFeature.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ private static void initializeClassesForJDK21OrEarlier() {
264264
try (InputStream is = JUnitPlatformFeature.class.getResourceAsStream("/initialize-at-buildtime")) {
265265
if (is != null) {
266266
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
267-
br.lines().forEach(RuntimeClassInitialization::initializeAtBuildTime);
267+
br.lines().forEach(cls -> {
268+
try {
269+
RuntimeClassInitialization.initializeAtBuildTime(cls);
270+
} catch (NoClassDefFoundError e) {
271+
// if users use older JUnit versions some classes might not be available
272+
}
273+
});
268274
}
269275
}
270276
} catch (IOException e) {

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/jupiter/JupiterConfigProvider.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
import org.junit.jupiter.params.converter.ConvertWith;
5555
import org.junit.jupiter.params.provider.ArgumentsSource;
5656
import org.junit.jupiter.params.provider.EnumSource;
57-
import org.junit.jupiter.params.provider.MethodSource;
5857
import org.junit.jupiter.params.provider.FieldSource;
58+
import org.junit.jupiter.params.provider.MethodSource;
5959

6060
import java.lang.reflect.Method;
6161
import java.util.ArrayList;
@@ -86,9 +86,15 @@ public void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration r
8686
AnnotationUtils.forEachAnnotatedMethodParameter(testClass, AggregateWith.class, annotation -> registry.registerAllClassMembersForReflection(annotation.value()));
8787
AnnotationUtils.forEachAnnotatedMethod(testClass, EnumSource.class, (m, annotation) -> handleEnumSource(m, annotation, registry));
8888
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, MethodSource.class, JupiterConfigProvider::handleMethodSource);
89-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, FieldSource.class, JupiterConfigProvider::handleFieldSource);
9089
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, EnabledIf.class, JupiterConfigProvider::handleEnabledIf);
9190
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, DisabledIf.class, JupiterConfigProvider::handleDisabledIf);
91+
92+
try {
93+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, FieldSource.class, JupiterConfigProvider::handleFieldSource);
94+
} catch (NoClassDefFoundError e) {
95+
// if users use JUnit version older than 5.11, FieldSource class won't be available
96+
}
97+
9298
}
9399

94100
private static Class<?>[] handleMethodSource(MethodSource annotation) {

common/junit-platform-native/src/main/resources/initialize-at-buildtime

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ org.junit.jupiter.engine.discovery.MethodSelectorResolver$MethodType$3
4646
org.junit.jupiter.engine.discovery.predicates.IsTestClassWithTests
4747
org.junit.jupiter.engine.discovery.predicates.IsTestFactoryMethod
4848
org.junit.jupiter.engine.execution.ConditionEvaluator
49+
org.junit.jupiter.engine.execution.ExecutableInvoker
4950
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker
5051
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall
5152
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall$VoidMethodInterceptorCall
@@ -78,6 +79,7 @@ org.junit.platform.launcher.core.DiscoveryIssueNotifier
7879
org.junit.platform.launcher.core.EngineDiscoveryOrchestrator
7980
org.junit.platform.launcher.core.EngineExecutionOrchestrator
8081
org.junit.platform.launcher.core.EngineFilterer
82+
org.junit.platform.launcher.core.EngineIdValidator
8183
org.junit.platform.launcher.core.HierarchicalOutputDirectoryProvider
8284
org.junit.platform.launcher.core.InternalTestPlan
8385
org.junit.platform.launcher.core.LauncherConfig
@@ -90,6 +92,8 @@ org.junit.platform.launcher.core.LauncherDiscoveryResult
9092
org.junit.platform.launcher.core.LauncherDiscoveryResult$EngineResultInfo
9193
org.junit.platform.launcher.core.LauncherListenerRegistry
9294
org.junit.platform.launcher.core.ListenerRegistry
95+
org.junit.platform.launcher.core.ServiceLoaderRegistry
96+
org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry
9397
org.junit.platform.launcher.core.SessionPerRequestLauncher
9498
org.junit.platform.launcher.EngineDiscoveryResult
9599
org.junit.platform.launcher.LauncherSessionListener$1

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,9 @@
6060
import org.eclipse.aether.resolution.DependencyRequest;
6161
import org.eclipse.aether.resolution.DependencyResolutionException;
6262
import org.eclipse.aether.resolution.DependencyResult;
63-
import org.graalvm.buildtools.utils.FileUtils;
6463
import org.graalvm.buildtools.utils.JUnitUtils;
6564
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
66-
import org.w3c.dom.Document;
67-
import org.xml.sax.SAXException;
6865

69-
import javax.xml.parsers.DocumentBuilder;
70-
import javax.xml.parsers.DocumentBuilderFactory;
71-
import javax.xml.parsers.ParserConfigurationException;
72-
import java.io.File;
7366
import java.io.IOException;
7467
import java.io.UncheckedIOException;
7568
import java.nio.file.Files;
@@ -82,7 +75,6 @@
8275
import java.util.HashSet;
8376
import java.util.List;
8477
import java.util.Set;
85-
import java.util.regex.Pattern;
8678
import java.util.stream.Collectors;
8779
import java.util.stream.Stream;
8880

@@ -141,8 +133,6 @@ protected void addDependenciesToClasspath() throws MojoExecutionException {
141133
.filter(it -> it.getGroupId().startsWith(NativeImageConfigurationUtils.MAVEN_GROUP_ID) || it.getGroupId().startsWith("org.junit"))
142134
.map(it -> it.getFile().toPath())
143135
.forEach(imageClasspath::add);
144-
145-
modules.addAll(collectJUnitModulesAlreadyOnClasspath());
146136
var jars = findJunitPlatformNativeJars(modules);
147137
imageClasspath.addAll(jars);
148138
}
@@ -318,51 +308,6 @@ private List<Path> findJunitPlatformNativeJars(Set<Module> modulesAlreadyOnClass
318308
.collect(Collectors.toList());
319309
}
320310

321-
private Set<Module> collectJUnitModulesAlreadyOnClasspath() {
322-
Set<Module> artifacts = new HashSet<>();
323-
for (Path entry : imageClasspath) {
324-
if (isJUnitArtifact(entry)) {
325-
File pom = getArtifactPOM(entry);
326-
if (pom != null) {
327-
artifacts.add(getModuleFromPOM(pom));
328-
}
329-
}
330-
}
331-
332-
return artifacts;
333-
}
334-
335-
private boolean isJUnitArtifact(Path entry) {
336-
return entry.toString().contains("junit");
337-
}
338-
339-
private File getArtifactPOM(Path classpathEntry) {
340-
List<File> artifactContent = getArtifactContent(classpathEntry.getParent());
341-
List<File> candidates = artifactContent.stream().filter(f -> f.getName().endsWith(".pom")).collect(Collectors.toList());
342-
return candidates.size() != 1 ? null : candidates.get(0);
343-
}
344-
345-
private List<File> getArtifactContent(Path path) {
346-
File[] content = path.toFile().listFiles();
347-
return content == null ? List.of() : List.of(content);
348-
}
349-
350-
private Module getModuleFromPOM(File pom) {
351-
try {
352-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
353-
factory.setIgnoringElementContentWhitespace(true);
354-
DocumentBuilder builder = factory.newDocumentBuilder();
355-
Document doc = builder.parse(pom);
356-
357-
String groupId = doc.getElementsByTagName("groupId").item(0).getFirstChild().getTextContent();
358-
String artifactId = doc.getElementsByTagName("artifactId").item(0).getFirstChild().getTextContent();
359-
360-
return new Module(groupId, artifactId);
361-
} catch (ParserConfigurationException | IOException | SAXException e) {
362-
throw new RuntimeException("Cannot get maven coordinates from " + pom.getPath() + ". Reason: " + e.getMessage());
363-
}
364-
}
365-
366311
private static final class Module {
367312
private final String groupId;
368313
private final String artifactId;

0 commit comments

Comments
 (0)