-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce test for scanning classes in a modular JAR
Addresses #2500
- Loading branch information
Showing
2 changed files
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -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("/[email protected]"); | ||
|
||
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"); | ||
|
Binary file not shown.