//Execution environment
* AdoptOpenJDK 11.0.9.1+1
* JUnit 5.7.0
* ArchUnit 0.14.1
package com.example;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.Dependency;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import org.junit.jupiter.api.Test;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
class ArchitectureTest {
//Class to be inspected
private static final JavaClasses CLASSES =
new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.example");
@Test
void Make classes that depend only from the same package package private() {
classes()
.that()
.arePublic()
.and(new DescribedPredicate<>("only have dependent classes that reside in same package") {
@Override
public boolean apply(final JavaClass clazz) {
return clazz.getDirectDependenciesToSelf()
.stream()
.map(Dependency::getOriginClass)
.allMatch(dependentClass
-> dependentClass.getPackageName().equals(clazz.getPackageName()));
}
})
.should()
.notBePublic()
.check(CLASSES);
}
}
Recommended Posts