Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> resolvePatterns(Set<String> patterns) {
Set<String> resolved = new HashSet<>();
for (String pattern : patterns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading