Skip to content

Commit

Permalink
Simplify HexagonalArchTest: compute rootPackagePath on the fly from r…
Browse files Browse the repository at this point in the history
…oot package name

That way it's easier to change root package on generated code, and it also simplifies the generator that don't need anymore to compute walk paths
  • Loading branch information
murdos committed Apr 22, 2024
1 parent 3f44213 commit df664c6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.LogLevel;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
Expand All @@ -26,9 +24,6 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {

//@formatter:off
return moduleBuilder(properties)
.context()
.put("packageWalkPath", packageWalkPath(packagePath))
.and()
.files()
.add(SOURCE.template("archunit.properties"), to("src/test/resources/archunit.properties"))
.add(SOURCE.template("AnnotationArchTest.java"), testDestination.append("AnnotationArchTest.java"))
Expand All @@ -42,10 +37,6 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {
//@formatter:on
}

private String packageWalkPath(String packagePath) {
return Stream.of(packagePath.split("/")).map(folder -> QUOTE + folder + QUOTE).collect(Collectors.joining(", "));
}

private JavaDependency archUnitDependency() {
return javaDependency()
.groupId("com.tngtech.archunit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class HexagonalArchTest {

private static Collection<String> packagesWithAnnotation(Class<? extends Annotation> annotationClass) throws AssertionError {
try {
return Files
.walk(Paths.get("src", "main", "java", {{packageWalkPath}}))
return Files.walk(rootPackagePath())
.filter(path -> path.toString().endsWith("package-info.java"))
.map(toPackageName())
.map(path -> path.replaceAll("[\\/]", "."))
Expand All @@ -64,6 +63,10 @@ class HexagonalArchTest {
}
}

private static Path rootPackagePath() {
return Stream.of(ROOT_PACKAGE.split("\\.")).map(Paths::get).reduce(Paths.get("src", "main", "java"), Path::resolve);
}

private static Function<Path, String> toPackageName() {
return path -> {
String stringPath = path.toString();
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/tech/jhipster/lite/HexagonalArchTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.jhipster.lite;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
Expand Down Expand Up @@ -49,7 +48,7 @@ private static Collection<String> buildPackagesPatterns(Collection<String> packa

private static Collection<String> packagesWithAnnotation(Class<? extends Annotation> annotationClass) throws AssertionError {
try {
return Files.walk(Paths.get("src", "main", "java", "tech", "jhipster", "lite"))
return Files.walk(rootPackagePath())
.filter(path -> path.toString().endsWith("package-info.java"))
.map(toPackageName())
.map(path -> path.replaceAll("[\\/]", "."))
Expand All @@ -64,6 +63,10 @@ private static Collection<String> packagesWithAnnotation(Class<? extends Annotat
}
}

private static Path rootPackagePath() {
return Stream.of(ROOT_PACKAGE.split("\\.")).map(Paths::get).reduce(Paths.get("src", "main", "java"), Path::resolve);
}

private static Function<Path, String> toPackageName() {
return path -> {
String stringPath = path.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ void shouldBuildModule() {

assertThatModuleWithFiles(module, pomFile(), testLogbackFile())
.hasFiles("src/test/resources/archunit.properties", "src/test/java/com/jhipster/test/AnnotationArchTest.java")
.hasFile("src/test/java/com/jhipster/test/HexagonalArchTest.java")
.containing("\"src\", \"main\", \"java\", \"com\", \"jhipster\", \"test\"")
.and()
.hasFile("pom.xml")
.containing("<artifactId>archunit-junit5-api</artifactId>")
.and()
Expand Down

0 comments on commit df664c6

Please sign in to comment.