Skip to content

Commit

Permalink
Introduce test for scanning classes in a modular JAR
Browse files Browse the repository at this point in the history
Addresses junit-team#2500
  • Loading branch information
sormuras authored and runningcode committed Feb 15, 2023
1 parent 6cc3b16 commit d6acad8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Binary file not shown.

0 comments on commit d6acad8

Please sign in to comment.