|
| 1 | +/* |
| 2 | + * Copyright 2015-2025 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * https://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +final Path lib = Path.of("lib"); // local directory to be used in module path |
| 12 | +final Set<String> roots = Set.of("org.junit.start"); // single root module to lookup |
| 13 | +final String version = "6.1.0-M1"; // of JUnit Framework |
| 14 | +final String repository = "https://repo.maven.apache.org/maven2"; // of JUnit Framework |
| 15 | +final String lookup = |
| 16 | + //language=Properties |
| 17 | + """ |
| 18 | + org.apiguardian.api=https://repo.maven.apache.org/maven2/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar |
| 19 | + org.jspecify=https://repo.maven.apache.org/maven2/org/jspecify/jspecify/1.0.0/jspecify-1.0.0.jar |
| 20 | + org.junit.jupiter.api={{repository}}/org/junit/jupiter/junit-jupiter-api/{{version}}/junit-jupiter-api-{{version}}.jar |
| 21 | + org.junit.jupiter.engine={{repository}}/org/junit/jupiter/junit-jupiter-engine/{{version}}/junit-jupiter-engine-{{version}}.jar |
| 22 | + org.junit.jupiter.params={{repository}}/org/junit/jupiter/junit-jupiter-params/{{version}}/junit-jupiter-params-{{version}}.jar |
| 23 | + org.junit.jupiter={{repository}}/org/junit/jupiter/junit-jupiter/{{version}}/junit-jupiter-{{version}}.jar |
| 24 | + org.junit.platform.commons={{repository}}/org/junit/platform/junit-platform-commons/{{version}}/junit-platform-commons-{{version}}.jar |
| 25 | + org.junit.platform.console={{repository}}/org/junit/platform/junit-platform-console/{{version}}/junit-platform-console-{{version}}.jar |
| 26 | + org.junit.platform.engine={{repository}}/org/junit/platform/junit-platform-engine/{{version}}/junit-platform-engine-{{version}}.jar |
| 27 | + org.junit.platform.launcher={{repository}}/org/junit/platform/junit-platform-launcher/{{version}}/junit-platform-launcher-{{version}}.jar |
| 28 | + org.junit.platform.reporting={{repository}}/org/junit/platform/junit-platform-reporting/{{version}}/junit-platform-reporting-{{version}}.jar |
| 29 | + org.junit.platform.suite.api={{repository}}/org/junit/platform/junit-platform-suite-api/{{version}}/junit-platform-suite-api-{{version}}.jar |
| 30 | + org.junit.platform.suite.engine={{repository}}/org/junit/platform/junit-platform-suite-engine/{{version}}/junit-platform-suite-engine-{{version}}.jar |
| 31 | + org.junit.platform.suite={{repository}}/org/junit/platform/junit-platform-suite/{{version}}/junit-platform-suite-{{version}}.jar |
| 32 | + org.junit.start={{repository}}/org/junit/junit-start/{{version}}/junit-start-{{version}}.jar |
| 33 | + org.opentest4j.reporting.tooling.spi=https://repo.maven.apache.org/maven2/org/opentest4j/reporting/open-test-reporting-tooling-spi/0.2.5/open-test-reporting-tooling-spi-0.2.5.jar |
| 34 | + org.opentest4j=https://repo.maven.apache.org/maven2/org/opentest4j/opentest4j/1.3.0/opentest4j-1.3.0.jar |
| 35 | + """ |
| 36 | + .replace("{{repository}}", repository) |
| 37 | + .replace("{{version}}", version); |
| 38 | + |
| 39 | +void main() throws Exception { |
| 40 | + // Ensure being launched inside expected working directory |
| 41 | + var program = Path.of("src", "HelloTests.java"); |
| 42 | + if (!Files.exists(program)) { |
| 43 | + throw new AssertionError("Expected %s in current working directory".formatted(program)); |
| 44 | + } |
| 45 | + |
| 46 | + // Read mapping file to locate remote modules |
| 47 | + var properties = new Properties(); |
| 48 | + properties.load(new StringReader(lookup)); |
| 49 | + |
| 50 | + // Create and initialize lib directory with root module(s) |
| 51 | + Files.createDirectories(lib); |
| 52 | + downloadModules(roots, properties); |
| 53 | + |
| 54 | + // Compute missing modules and download them transitively |
| 55 | + var missing = computeMissingModuleNames(); |
| 56 | + while (!missing.isEmpty()) { |
| 57 | + downloadModules(missing, properties); |
| 58 | + missing = computeMissingModuleNames(); |
| 59 | + } |
| 60 | + |
| 61 | + IO.println("%nList modules of %s directory".formatted(lib)); |
| 62 | + listModules(); |
| 63 | +} |
| 64 | + |
| 65 | +void downloadModules(Set<String> names, Properties properties) { |
| 66 | + IO.println("Downloading %d module%s".formatted(names.size(), names.size() == 1 ? "" : "s")); |
| 67 | + names.stream().parallel().forEach(name -> { |
| 68 | + var target = lib.resolve(name + ".jar"); |
| 69 | + if (Files.exists(target)) return; // Don't overwrite existing JAR file |
| 70 | + var source = URI.create(properties.getProperty(name)); |
| 71 | + try (var stream = source.toURL().openStream()) { |
| 72 | + IO.println(name + " <- " + source + "..."); |
| 73 | + Files.copy(stream, target); |
| 74 | + } catch (IOException cause) { |
| 75 | + throw new UncheckedIOException(cause); |
| 76 | + } |
| 77 | + }); |
| 78 | + // Ensure that every name can be found to avoid eternal loops |
| 79 | + var finder = ModuleFinder.of(lib); |
| 80 | + var remainder = new TreeSet<>(names); |
| 81 | + remainder.removeIf(name -> finder.find(name).isPresent()); |
| 82 | + if (remainder.isEmpty()) return; |
| 83 | + throw new AssertionError("Modules not downloaded: " + remainder); |
| 84 | +} |
| 85 | + |
| 86 | +Set<String> computeMissingModuleNames() { |
| 87 | + var system = ModuleFinder.ofSystem(); |
| 88 | + var finder = ModuleFinder.of(lib); |
| 89 | + var names = |
| 90 | + finder.findAll().stream() |
| 91 | + .parallel() |
| 92 | + .map(ModuleReference::descriptor) |
| 93 | + .map(ModuleDescriptor::requires) |
| 94 | + .flatMap(Collection::stream) |
| 95 | + .filter(this::mustBePresentAtCompileTime) |
| 96 | + .map(ModuleDescriptor.Requires::name) |
| 97 | + .filter(name -> finder.find(name).isEmpty()) |
| 98 | + .filter(name -> system.find(name).isEmpty()) |
| 99 | + .toList(); |
| 100 | + return new TreeSet<>(names); |
| 101 | +} |
| 102 | + |
| 103 | +boolean mustBePresentAtCompileTime(ModuleDescriptor.Requires requires) { |
| 104 | + var isStatic = requires.modifiers().contains(ModuleDescriptor.Requires.Modifier.STATIC); |
| 105 | + var isTransitive = requires.modifiers().contains(ModuleDescriptor.Requires.Modifier.TRANSITIVE); |
| 106 | + return !isStatic || isTransitive; |
| 107 | +} |
| 108 | + |
| 109 | +void listModules() { |
| 110 | + var finder = ModuleFinder.of(lib); |
| 111 | + var modules = finder.findAll(); |
| 112 | + modules.stream() |
| 113 | + .map(ModuleReference::descriptor) |
| 114 | + .map(ModuleDescriptor::toNameAndVersion) |
| 115 | + .sorted() |
| 116 | + .forEach(IO::println); |
| 117 | + IO.println(" %d modules".formatted(modules.size())); |
| 118 | +} |
0 commit comments