Skip to content

Commit

Permalink
Reduce number of Helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 28, 2024
1 parent 0839b2b commit 4ca5bfd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,22 @@

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;

/**
* @since 1.3
*/
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");
Expand Down Expand Up @@ -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<String> 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<JarFile> loadJarFiles() {
return loadModuleDirectoryNames().stream().map(Helper::createJarFile).collect(Collectors.toList());
}

public static Optional<Path> getJavaHome(String version) {
// First, try various system sources...
var sources = Stream.of( //
Expand All @@ -127,32 +82,4 @@ public static Optional<Path> 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<String> treeWalk(Path root) {
var lines = new ArrayList<String>();
treeWalk(root, lines::add);
return lines;
}

/** Walk directory tree structure. */
public static void treeWalk(Path root, Consumer<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static Path pom(String artifactId) {

private static Path artifact(String artifactId, Predicate<String> 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)) {
Expand All @@ -57,4 +57,16 @@ private static Path artifact(String artifactId, Predicate<String> 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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
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;
import java.lang.annotation.Target;
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;
Expand All @@ -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 {

Expand Down Expand Up @@ -133,9 +139,22 @@ static class AllJars implements LocationProvider {

@Override
public Set<Location> get(Class<?> testClass) {
return loadJarFiles().stream().map(Location::of).collect(Collectors.toSet());
return loadJarFiles().map(Location::of).collect(toSet());
}

private static Stream<JarFile> 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<T extends Annotation> extends DescribedPredicate<JavaClass> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,14 +23,16 @@
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;
import org.junit.jupiter.api.extension.DisabledOnOpenJ9;
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;

Expand Down Expand Up @@ -89,7 +92,7 @@ private static List<String> 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());

Expand Down Expand Up @@ -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 >>", //
Expand All @@ -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<String> treeWalk(Path root) {
var lines = new ArrayList<String>();
treeWalk(root, lines::add);
return lines;
}

private static void treeWalk(Path root, Consumer<String> 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);
}
}

}

0 comments on commit 4ca5bfd

Please sign in to comment.