-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Check for classpath alignment on LinkageErrors #4244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...rm-launcher/src/main/java/org/junit/platform/launcher/core/ClasspathAlignmentChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Copyright 2015-2025 the original author or authors. | ||
| * | ||
| * All rights reserved. This program and the accompanying materials are | ||
| * made available under the terms of the Eclipse Public License v2.0 which | ||
| * accompanies this distribution and is available at | ||
| * | ||
| * https://www.eclipse.org/legal/epl-v20.html | ||
| */ | ||
|
|
||
| package org.junit.platform.launcher.core; | ||
|
|
||
| import static java.util.Collections.unmodifiableList; | ||
| import static java.util.Comparator.comparing; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
|
|
||
| import org.junit.platform.commons.JUnitException; | ||
| import org.junit.platform.commons.support.ReflectionSupport; | ||
| import org.junit.platform.commons.util.ClassLoaderUtils; | ||
|
|
||
| /** | ||
| * @since 1.12 | ||
| */ | ||
| class ClasspathAlignmentChecker { | ||
|
|
||
| // VisibleForTesting | ||
| static final List<String> WELL_KNOWN_PACKAGES = unmodifiableList(Arrays.asList( // | ||
| "org.junit.jupiter.api", // | ||
| "org.junit.jupiter.engine", // | ||
| "org.junit.jupiter.migrationsupport", // | ||
| "org.junit.jupiter.params", // | ||
| "org.junit.platform.commons", // | ||
| "org.junit.platform.console", // | ||
| "org.junit.platform.engine", // | ||
| "org.junit.platform.jfr", // | ||
| "org.junit.platform.launcher", // | ||
| "org.junit.platform.reporting", // | ||
| "org.junit.platform.runner", // | ||
| "org.junit.platform.suite.api", // | ||
| "org.junit.platform.suite.commons", // | ||
| "org.junit.platform.suite.engine", // | ||
| "org.junit.platform.testkit", // | ||
| "org.junit.vintage.engine" // | ||
| )); | ||
|
|
||
| static Optional<JUnitException> check(LinkageError error) { | ||
| ClassLoader classLoader = ClassLoaderUtils.getClassLoader(ClasspathAlignmentChecker.class); | ||
| Function<String, Package> packageLookup = name -> ReflectionSupport.findMethod(ClassLoader.class, | ||
| "getDefinedPackage", String.class) // | ||
| .map(m -> (Package) ReflectionSupport.invokeMethod(m, classLoader, name)) // | ||
| .orElseGet(() -> getPackage(name)); | ||
|
Comment on lines
+56
to
+59
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only works for packages for which classes have already been loaded by the class loader and thus is likely to contain the conflicting JARs. |
||
| return check(error, packageLookup); | ||
| } | ||
|
|
||
| // VisibleForTesting | ||
| static Optional<JUnitException> check(LinkageError error, Function<String, Package> packageLookup) { | ||
| Map<String, List<Package>> packagesByVersions = new HashMap<>(); | ||
| WELL_KNOWN_PACKAGES.stream() // | ||
| .map(packageLookup) // | ||
| .filter(Objects::nonNull) // | ||
| .forEach(pkg -> { | ||
| String version = pkg.getImplementationVersion(); | ||
| if (version != null) { | ||
| if (pkg.getName().startsWith("org.junit.platform") && version.contains(".")) { | ||
| version = platformToJupiterVersion(version); | ||
| } | ||
| packagesByVersions.computeIfAbsent(version, __ -> new ArrayList<>()).add(pkg); | ||
| } | ||
| }); | ||
| if (packagesByVersions.size() > 1) { | ||
| StringBuilder message = new StringBuilder(); | ||
| String lineBreak = System.lineSeparator(); | ||
| message.append("The wrapped ").append(error.getClass().getSimpleName()) // | ||
| .append(" is likely caused by the versions of JUnit jars on the classpath/module path ") // | ||
| .append("not being properly aligned. ") // | ||
| .append(lineBreak) // | ||
| .append("Please ensure consistent versions are used (see https://junit.org/junit5/docs/") // | ||
| .append(platformToJupiterVersion( | ||
| ClasspathAlignmentChecker.class.getPackage().getImplementationVersion())) // | ||
| .append("/user-guide/#dependency-metadata).") // | ||
marcphilipp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .append(lineBreak) // | ||
| .append("The following conflicting versions were detected:").append(lineBreak); | ||
| packagesByVersions.values().stream() // | ||
| .flatMap(List::stream) // | ||
| .sorted(comparing(Package::getName)) // | ||
| .map(pkg -> String.format("- %s: %s%n", pkg.getName(), pkg.getImplementationVersion())) // | ||
| .forEach(message::append); | ||
| return Optional.of(new JUnitException(message.toString(), error)); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private static String platformToJupiterVersion(String version) { | ||
| int majorVersion = Integer.parseInt(version.substring(0, version.indexOf("."))) + 4; | ||
| return majorVersion + version.substring(version.indexOf(".")); | ||
| } | ||
|
|
||
| @SuppressWarnings({ "deprecation", "RedundantSuppression" }) // only called when running on JDK 8 | ||
| private static Package getPackage(String name) { | ||
| return Package.getPackage(name); | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
.../java/org/junit/platform/launcher/core/ClasspathAlignmentCheckingLauncherInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2015-2025 the original author or authors. | ||
| * | ||
| * All rights reserved. This program and the accompanying materials are | ||
| * made available under the terms of the Eclipse Public License v2.0 which | ||
| * accompanies this distribution and is available at | ||
| * | ||
| * https://www.eclipse.org/legal/epl-v20.html | ||
| */ | ||
|
|
||
| package org.junit.platform.launcher.core; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.junit.platform.commons.JUnitException; | ||
| import org.junit.platform.launcher.LauncherInterceptor; | ||
|
|
||
| class ClasspathAlignmentCheckingLauncherInterceptor implements LauncherInterceptor { | ||
|
|
||
| static final LauncherInterceptor INSTANCE = new ClasspathAlignmentCheckingLauncherInterceptor(); | ||
|
|
||
| @Override | ||
| public <T> T intercept(Invocation<T> invocation) { | ||
| try { | ||
| return invocation.proceed(); | ||
| } | ||
| catch (LinkageError e) { | ||
| Optional<JUnitException> exception = ClasspathAlignmentChecker.check(e); | ||
| if (exception.isPresent()) { | ||
| throw exception.get(); | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| // do nothing | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...-tests/src/test/java/org/junit/platform/launcher/core/ClasspathAlignmentCheckerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Copyright 2015-2025 the original author or authors. | ||
| * | ||
| * All rights reserved. This program and the accompanying materials are | ||
| * made available under the terms of the Eclipse Public License v2.0 which | ||
| * accompanies this distribution and is available at | ||
| * | ||
| * https://www.eclipse.org/legal/epl-v20.html | ||
| */ | ||
|
|
||
| package org.junit.platform.launcher.core; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.platform.launcher.core.ClasspathAlignmentChecker.WELL_KNOWN_PACKAGES; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.Function; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import io.github.classgraph.ClassGraph; | ||
| import io.github.classgraph.PackageInfo; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ClasspathAlignmentCheckerTests { | ||
|
|
||
| @Test | ||
| void classpathIsAligned() { | ||
| assertThat(ClasspathAlignmentChecker.check(new LinkageError())).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void wrapsLinkageErrorForUnalignedClasspath() { | ||
| var cause = new LinkageError(); | ||
| AtomicInteger counter = new AtomicInteger(); | ||
| Function<String, Package> packageLookup = name -> { | ||
| var pkg = mock(Package.class); | ||
| when(pkg.getName()).thenReturn(name); | ||
| when(pkg.getImplementationVersion()).thenReturn(counter.incrementAndGet() + ".0.0"); | ||
| return pkg; | ||
| }; | ||
|
|
||
| var result = ClasspathAlignmentChecker.check(cause, packageLookup); | ||
|
|
||
| assertThat(result).isPresent(); | ||
| assertThat(result.get()) // | ||
| .hasMessageStartingWith("The wrapped LinkageError is likely caused by the versions of " | ||
| + "JUnit jars on the classpath/module path not being properly aligned.") // | ||
| .hasMessageContaining("Please ensure consistent versions are used") // | ||
| .hasMessageFindingMatch("https://junit\\.org/junit5/docs/.*/user-guide/#dependency-metadata") // | ||
| .hasMessageContaining("The following conflicting versions were detected:") // | ||
| .hasMessageContaining("- org.junit.jupiter.api: 1.0.0") // | ||
| .hasMessageContaining("- org.junit.jupiter.engine: 2.0.0") // | ||
| .cause().isSameAs(cause); | ||
| } | ||
|
|
||
| @Test | ||
| void allRootPackagesAreChecked() { | ||
| var allowedFileNames = Pattern.compile("junit-(?:platform|jupiter|vintage)-.+[\\d.]+(?:-SNAPSHOT)?\\.jar"); | ||
| var classGraph = new ClassGraph() // | ||
| .acceptPackages("org.junit.platform", "org.junit.jupiter", "org.junit.vintage") // | ||
| .rejectPackages("org.junit.platform.reporting.shadow", "org.junit.jupiter.params.shadow") // | ||
| .filterClasspathElements(e -> { | ||
| var path = Path.of(e); | ||
| var fileName = path.getFileName().toString(); | ||
| return allowedFileNames.matcher(fileName).matches(); | ||
| }); | ||
|
|
||
| try (var scanResult = classGraph.scan()) { | ||
| var foundPackages = scanResult.getPackageInfo().stream() // | ||
| .filter(it -> !it.getClassInfo().isEmpty()) // | ||
| .map(PackageInfo::getName) // | ||
| .sorted() // | ||
| .toList(); | ||
|
|
||
| assertThat(foundPackages) // | ||
| .allMatch(name -> WELL_KNOWN_PACKAGES.stream().anyMatch(name::startsWith)); | ||
| assertThat(WELL_KNOWN_PACKAGES) // | ||
| .allMatch(name -> foundPackages.stream().anyMatch(it -> it.startsWith(name))); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java 9+