diff --git a/.zenodo.json b/.zenodo.json new file mode 100644 index 0000000..5514f13 --- /dev/null +++ b/.zenodo.json @@ -0,0 +1,40 @@ +{ + "upload_type": "software", + "title": "Modulith Rules: ArchUnit rules for module boundaries in modular monoliths", + "description": "Modulith Rules is an open-source Java library of pre-built ArchUnit rules for enforcing module boundaries in modular monoliths. A single module declaration, written with a fluent API, YAML or package conventions, drives rules for allowed dependencies, internal-package protection, API-only access, module cycles and directional communication contracts, with optional rules for Spring applications. Every violation message names the offending class and ends with a concrete fix. The same declaration renders the module graph as a Mermaid diagram. The library runs at test time only and its core has no framework dependency.", + "creators": [ + { + "name": "Matheolane, Omphile" + } + ], + "license": "Apache-2.0", + "keywords": [ + "software architecture", + "modular monolith", + "architecture testing", + "ArchUnit", + "Java", + "module boundaries", + "Spring Boot" + ], + "related_identifiers": [ + { + "identifier": "https://github.com/modular-monolith/modular-monolith-rules", + "relation": "isSupplementTo", + "resource_type": "software", + "scheme": "url" + }, + { + "identifier": "https://central.sonatype.com/artifact/io.modularmonolith/modulith-rules-core", + "relation": "isIdenticalTo", + "resource_type": "software", + "scheme": "url" + }, + { + "identifier": "https://modularmonolith.io", + "relation": "isDocumentedBy", + "resource_type": "other", + "scheme": "url" + } + ] +} diff --git a/modulith-rules-core/src/main/java/io/modulith/rules/api/ModuleDefinition.java b/modulith-rules-core/src/main/java/io/modulith/rules/api/ModuleDefinition.java index 059624a..6217801 100644 --- a/modulith-rules-core/src/main/java/io/modulith/rules/api/ModuleDefinition.java +++ b/modulith-rules-core/src/main/java/io/modulith/rules/api/ModuleDefinition.java @@ -173,14 +173,37 @@ public boolean isPublicApi(String fullClassName) { return false; } for (String pattern : archUnitApiPackageIdentifiers()) { - String prefix = pattern.endsWith("..") ? pattern.substring(0, pattern.length() - 2) : pattern; - if (fullClassName.startsWith(prefix)) { + if (classMatchesPackagePattern(fullClassName, pattern)) { return true; } } return false; } + /** + * Checks whether a class sits in the package named by a resolved pattern, or in one + * of its sub-packages. The match respects package boundaries, so the pattern + * {@code com.example.b.c..} matches {@code com.example.b.c.Foo} and + * {@code com.example.b.c.sub.Bar} but not {@code com.example.b.cache.Foo}. + * A trailing {@code ..} is optional and makes no difference. + * + * @param fullClassName fully-qualified class name + * @param pattern resolved package pattern, for example {@code "com.example.b.api.."} + * @return {@code true} if the class lives in that package tree + */ + public static boolean classMatchesPackagePattern(String fullClassName, String pattern) { + if (fullClassName == null || pattern == null || pattern.isEmpty()) { + return false; + } + String packageName = pattern.endsWith("..") ? pattern.substring(0, pattern.length() - 2) : pattern; + while (packageName.endsWith(".")) { + packageName = packageName.substring(0, packageName.length() - 1); + } + int lastDot = fullClassName.lastIndexOf('.'); + String classPackage = lastDot < 0 ? "" : fullClassName.substring(0, lastDot); + return classPackage.equals(packageName) || classPackage.startsWith(packageName + "."); + } + private Set resolvePatterns(Set patterns) { Set resolved = new HashSet<>(); for (String pattern : patterns) { diff --git a/modulith-rules-core/src/main/java/io/modulith/rules/condition/ModulithPredicates.java b/modulith-rules-core/src/main/java/io/modulith/rules/condition/ModulithPredicates.java index f7cd966..cdcec27 100644 --- a/modulith-rules-core/src/main/java/io/modulith/rules/condition/ModulithPredicates.java +++ b/modulith-rules-core/src/main/java/io/modulith/rules/condition/ModulithPredicates.java @@ -98,10 +98,7 @@ public boolean test(JavaClass javaClass) { } boolean inInternalPackage = false; for (String pattern : internalPatterns) { - String prefix = pattern.endsWith("..") - ? pattern.substring(0, pattern.length() - 2) - : pattern; - if (javaClass.getName().startsWith(prefix)) { + if (ModuleDefinition.classMatchesPackagePattern(javaClass.getName(), pattern)) { inInternalPackage = true; break; } diff --git a/modulith-rules-core/src/test/java/io/modulith/rules/api/ModuleDefinitionTest.java b/modulith-rules-core/src/test/java/io/modulith/rules/api/ModuleDefinitionTest.java new file mode 100644 index 0000000..dcdd5f8 --- /dev/null +++ b/modulith-rules-core/src/test/java/io/modulith/rules/api/ModuleDefinitionTest.java @@ -0,0 +1,59 @@ +package io.modulith.rules.api; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Package matching for API and internal package declarations. The match has to stop at + * a package boundary, otherwise an API package of {@code .c.} also claims + * {@code com.example.b.cache}. + */ +class ModuleDefinitionTest { + + private static final ModuleDefinition B = ModuleDefinition.builder("b") + .basePackage("com.example.b") + .apiPackages(".c.") + .internalPackages(".internal.") + .build(); + + @Test + @DisplayName("a class in the declared API package is public API") + void classInApiPackage_isPublicApi() { + assertThat(B.isPublicApi("com.example.b.c.Foo")).isTrue(); + } + + @Test + @DisplayName("a class in a sub-package of the API package is public API") + void classInApiSubPackage_isPublicApi() { + assertThat(B.isPublicApi("com.example.b.c.events.OrderPlaced")).isTrue(); + } + + @Test + @DisplayName("a sibling package sharing the API name as a prefix is not public API") + void siblingPackageWithSamePrefix_isNotPublicApi() { + assertThat(B.isPublicApi("com.example.b.cache.Foo")).isFalse(); + } + + @Test + @DisplayName("an internal class is not public API") + void internalClass_isNotPublicApi() { + assertThat(B.isPublicApi("com.example.b.internal.Foo")).isFalse(); + } + + @Test + @DisplayName("absolute patterns match with or without the trailing dots") + void absolutePatterns_matchWithOrWithoutTrailingDots() { + assertThat(ModuleDefinition.classMatchesPackagePattern("com.example.b.api.Foo", "com.example.b.api..")).isTrue(); + assertThat(ModuleDefinition.classMatchesPackagePattern("com.example.b.api.Foo", "com.example.b.api")).isTrue(); + assertThat(ModuleDefinition.classMatchesPackagePattern("com.example.b.apiinternal.Foo", "com.example.b.api..")).isFalse(); + } + + @Test + @DisplayName("nested classes follow their enclosing class's package") + void nestedClass_followsEnclosingPackage() { + assertThat(B.isPublicApi("com.example.b.c.Foo$Bar")).isTrue(); + assertThat(B.isPublicApi("com.example.b.cache.Foo$Bar")).isFalse(); + } +}