diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/ClasspathScannerTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/ClasspathScannerTests.java index 38f9c0304da3..a752394aef77 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/ClasspathScannerTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/ClasspathScannerTests.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assumptions.assumeFalse; import java.io.IOException; +import java.lang.module.ModuleFinder; import java.net.URI; import java.net.URL; import java.net.URLClassLoader; @@ -27,6 +28,7 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.logging.Level; @@ -176,6 +178,26 @@ void scanForClassesInPackage() { assertTrue(classes.contains(MemberClassToBeFound.class)); } + @Test + // #2500 + void scanForClassesInPackageWithinModuleSharingNames() throws Exception { + var jarfile = getClass().getResource("/com.greetings@1-ea.jar"); + + var module = "com.greetings"; + var before = ModuleFinder.of(); + var finder = ModuleFinder.of(Path.of(jarfile.toURI())); + var boot = ModuleLayer.boot(); + var configuration = boot.configuration().resolveAndBind(before, finder, Set.of(module)); + var parent = ClassLoader.getPlatformClassLoader(); + var layer = ModuleLayer.defineModulesWithOneLoader(configuration, List.of(boot), parent).layer(); + + var classpathScanner = new ClasspathScanner(() -> layer.findLoader(module), ReflectionUtils::tryToLoadClass); + + var classes = classpathScanner.scanForClassesInPackage("com.greetings", allClasses); + var classNames = classes.stream().map(Class::getName).collect(Collectors.toList()); + assertThat(classNames).hasSize(1).contains("com.greetings.Main"); + } + @Test void findAllClassesInPackageWithinJarFile() throws Exception { var jarfile = getClass().getResource("/jartest.jar"); diff --git a/platform-tests/src/test/resources/com.greetings@1-ea.jar b/platform-tests/src/test/resources/com.greetings@1-ea.jar new file mode 100644 index 000000000000..38c7b0659395 Binary files /dev/null and b/platform-tests/src/test/resources/com.greetings@1-ea.jar differ