diff --git a/platform-tooling-support-tests/src/main/java/platform/tooling/support/Helper.java b/platform-tooling-support-tests/src/main/java/platform/tooling/support/Helper.java index 4bb2c6c73ad6..9f5d3f08d1ee 100644 --- a/platform-tooling-support-tests/src/main/java/platform/tooling/support/Helper.java +++ b/platform-tooling-support-tests/src/main/java/platform/tooling/support/Helper.java @@ -10,23 +10,15 @@ package platform.tooling.support; -import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.time.Duration; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.Properties; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -34,8 +26,6 @@ */ public class Helper { - public static final Duration TOOL_TIMEOUT = Duration.ofMinutes(3); - private static final Path ROOT = Paths.get(".."); private static final Path GRADLE_PROPERTIES = ROOT.resolve("gradle.properties"); private static final Path SETTINGS_GRADLE = ROOT.resolve("settings.gradle.kts"); @@ -64,56 +54,21 @@ public static String version(String module) { throw new AssertionError("Unknown module: " + module); } - static String groupId(String artifactId) { - if (artifactId.startsWith("junit-jupiter")) { - return "org.junit.jupiter"; - } - if (artifactId.startsWith("junit-platform")) { - return "org.junit.platform"; - } - if (artifactId.startsWith("junit-vintage")) { - return "org.junit.vintage"; - } - return "org.junit"; - } - - public static String replaceVersionPlaceholders(String line) { - line = line.replace("${jupiterVersion}", version("junit-jupiter")); - line = line.replace("${vintageVersion}", version("junit-vintage")); - line = line.replace("${platformVersion}", version("junit-platform")); - return line; - } - public static List loadModuleDirectoryNames() { var moduleLinePattern = Pattern.compile("include\\(\"(.+)\"\\)"); - try (var stream = Files.lines(SETTINGS_GRADLE) // - .map(moduleLinePattern::matcher) // - .filter(Matcher::matches) // - .map(matcher -> matcher.group(1)) // - .filter(name -> name.startsWith("junit-")) // - .filter(name -> !name.equals("junit-bom")) // - .filter(name -> !name.equals("junit-platform-console-standalone"))) { - return stream.collect(Collectors.toList()); + try (var stream = Files.lines(SETTINGS_GRADLE)) { + return stream.map(moduleLinePattern::matcher) // + .filter(Matcher::matches) // + .map(matcher -> matcher.group(1)) // + .filter(name -> name.startsWith("junit-")) // + .filter(name -> !name.equals("junit-bom")) // + .filter(name -> !name.equals("junit-platform-console-standalone")).toList(); } catch (Exception e) { throw new AssertionError("loading module directory names failed: " + SETTINGS_GRADLE); } } - static JarFile createJarFile(String module) { - var path = MavenRepo.jar(module); - try { - return new JarFile(path.toFile()); - } - catch (IOException e) { - throw new UncheckedIOException("Creating JarFile for '" + path + "' failed.", e); - } - } - - public static List loadJarFiles() { - return loadModuleDirectoryNames().stream().map(Helper::createJarFile).collect(Collectors.toList()); - } - public static Optional getJavaHome(String version) { // First, try various system sources... var sources = Stream.of( // @@ -127,32 +82,4 @@ public static Optional getJavaHome(String version) { ); return sources.filter(Objects::nonNull).findFirst().map(Path::of); } - - /** Load, here copy, modular jar files to the given target directory. */ - public static void loadAllJUnitModules(Path target) throws Exception { - for (var module : loadModuleDirectoryNames()) { - var jar = MavenRepo.jar(module); - Files.copy(jar, target.resolve(jar.getFileName())); - } - } - - /** Walk directory tree structure. */ - public static List treeWalk(Path root) { - var lines = new ArrayList(); - treeWalk(root, lines::add); - return lines; - } - - /** Walk directory tree structure. */ - public static void treeWalk(Path root, Consumer out) { - try (var stream = Files.walk(root)) { - stream.map(root::relativize) // - .map(path -> path.toString().replace('\\', '/')) // - .sorted().filter(Predicate.not(String::isEmpty)) // - .forEach(out); - } - catch (Exception e) { - throw new Error("Walking tree failed: " + root, e); - } - } } diff --git a/platform-tooling-support-tests/src/main/java/platform/tooling/support/MavenRepo.java b/platform-tooling-support-tests/src/main/java/platform/tooling/support/MavenRepo.java index 2ba22ebeb8cc..84e47bfd98b8 100644 --- a/platform-tooling-support-tests/src/main/java/platform/tooling/support/MavenRepo.java +++ b/platform-tooling-support-tests/src/main/java/platform/tooling/support/MavenRepo.java @@ -45,7 +45,7 @@ public static Path pom(String artifactId) { private static Path artifact(String artifactId, Predicate fileNamePredicate) { var parentDir = dir() // - .resolve(Helper.groupId(artifactId).replace('.', File.separatorChar)) // + .resolve(groupId(artifactId).replace('.', File.separatorChar)) // .resolve(artifactId) // .resolve(Helper.version(artifactId)); try (var files = Files.list(parentDir)) { @@ -57,4 +57,16 @@ private static Path artifact(String artifactId, Predicate fileNamePredic } } + private static String groupId(String artifactId) { + if (artifactId.startsWith("junit-jupiter")) { + return "org.junit.jupiter"; + } + if (artifactId.startsWith("junit-platform")) { + return "org.junit.platform"; + } + if (artifactId.startsWith("junit-vintage")) { + return "org.junit.vintage"; + } + return "org.junit"; + } } diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ArchUnitTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ArchUnitTests.java index 21c7e2076f30..54c297c863c8 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ArchUnitTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ArchUnitTests.java @@ -26,9 +26,11 @@ import static com.tngtech.archunit.lang.conditions.ArchPredicates.have; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices; +import static java.util.stream.Collectors.toSet; import static org.junit.jupiter.api.Assertions.assertTrue; -import static platform.tooling.support.Helper.loadJarFiles; +import java.io.IOException; +import java.io.UncheckedIOException; import java.lang.annotation.Annotation; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; @@ -36,7 +38,8 @@ import java.util.Arrays; import java.util.Set; import java.util.function.BiPredicate; -import java.util.stream.Collectors; +import java.util.jar.JarFile; +import java.util.stream.Stream; import com.tngtech.archunit.base.DescribedPredicate; import com.tngtech.archunit.core.domain.JavaClass; @@ -51,6 +54,9 @@ import org.apiguardian.api.API; +import platform.tooling.support.Helper; +import platform.tooling.support.MavenRepo; + @AnalyzeClasses(locations = ArchUnitTests.AllJars.class) class ArchUnitTests { @@ -133,9 +139,22 @@ static class AllJars implements LocationProvider { @Override public Set get(Class testClass) { - return loadJarFiles().stream().map(Location::of).collect(Collectors.toSet()); + return loadJarFiles().map(Location::of).collect(toSet()); + } + + private static Stream loadJarFiles() { + return Helper.loadModuleDirectoryNames().stream().map(AllJars::createJarFile); } + private static JarFile createJarFile(String module) { + var path = MavenRepo.jar(module); + try { + return new JarFile(path.toFile()); + } + catch (IOException e) { + throw new UncheckedIOException("Creating JarFile for '" + path + "' failed.", e); + } + } } private static class RepeatableAnnotationPredicate extends DescribedPredicate { diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JarDescribeModuleTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JarDescribeModuleTests.java index 2b113e359790..1f4df71f454b 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JarDescribeModuleTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/JarDescribeModuleTests.java @@ -46,7 +46,7 @@ void describeModule(String module) throws Exception { assertEquals(0, result.exitCode()); assertEquals("", result.stdErr(), "error log isn't empty"); - var expectedLines = Helper.replaceVersionPlaceholders( + var expectedLines = replaceVersionPlaceholders( Files.readString(sourceDirectory.resolve(module + ".expected.txt")).trim()); assertLinesMatch(expectedLines.lines().toList(), result.stdOut().trim().lines().toList()); } @@ -62,4 +62,11 @@ void packageNamesStartWithNameOfTheModule(String module) { } } + private static String replaceVersionPlaceholders(String line) { + line = line.replace("${jupiterVersion}", Helper.version("junit-jupiter")); + line = line.replace("${vintageVersion}", Helper.version("junit-vintage")); + line = line.replace("${platformVersion}", Helper.version("junit-platform")); + return line; + } + } diff --git a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ModularUserGuideTests.java b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ModularUserGuideTests.java index 1ef24d82e44d..a386d06244a6 100644 --- a/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ModularUserGuideTests.java +++ b/platform-tooling-support-tests/src/test/java/platform/tooling/support/tests/ModularUserGuideTests.java @@ -13,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertLinesMatch; import static org.junit.jupiter.api.Assertions.assertTrue; +import static platform.tooling.support.Helper.loadModuleDirectoryNames; import java.io.File; import java.io.PrintWriter; @@ -22,6 +23,8 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.spi.ToolProvider; import org.junit.jupiter.api.Test; @@ -29,7 +32,7 @@ import org.junit.jupiter.api.io.TempDir; import org.junit.platform.launcher.LauncherConstants; -import platform.tooling.support.Helper; +import platform.tooling.support.MavenRepo; import platform.tooling.support.ThirdPartyJars; import platform.tooling.support.process.ProcessStarters; @@ -89,7 +92,7 @@ private static List compile(Path temp, Writer out, Writer err) throws Ex ThirdPartyJars.copy(lib, "org.opentest4j.reporting", "open-test-reporting-tooling-spi"); ThirdPartyJars.copy(lib, "com.google.jimfs", "jimfs"); ThirdPartyJars.copy(lib, "com.google.guava", "guava"); - Helper.loadAllJUnitModules(lib); + loadAllJUnitModules(lib); args.add("--module-path"); args.add(lib.toString()); @@ -156,7 +159,7 @@ void runTestsFromUserGuideWithinModularBoundaries(@TempDir Path temp) throws Exc // args.forEach(System.out::println); assertTrue(err.toString().isBlank(), () -> err + "\n\n" + String.join("\n", args)); - var listing = Helper.treeWalk(temp); + var listing = treeWalk(temp); assertLinesMatch(List.of( // "destination", // ">> CLASSES >>", // @@ -180,4 +183,29 @@ void runTestsFromUserGuideWithinModularBoundaries(@TempDir Path temp) throws Exc junit(temp); } + private static void loadAllJUnitModules(Path target) throws Exception { + for (var module : loadModuleDirectoryNames()) { + var jar = MavenRepo.jar(module); + Files.copy(jar, target.resolve(jar.getFileName())); + } + } + + private static List treeWalk(Path root) { + var lines = new ArrayList(); + treeWalk(root, lines::add); + return lines; + } + + private static void treeWalk(Path root, Consumer out) { + try (var stream = Files.walk(root)) { + stream.map(root::relativize) // + .map(path -> path.toString().replace('\\', '/')) // + .sorted().filter(Predicate.not(String::isEmpty)) // + .forEach(out); + } + catch (Exception e) { + throw new Error("Walking tree failed: " + root, e); + } + } + }