2424
2525package org .int4 .common .collection ;
2626
27+ import com .tngtech .archunit .core .domain .JavaClass ;
2728import com .tngtech .archunit .junit .AnalyzeClasses ;
2829import com .tngtech .archunit .junit .ArchTest ;
30+ import com .tngtech .archunit .lang .ArchCondition ;
2931import com .tngtech .archunit .lang .ArchRule ;
32+ import com .tngtech .archunit .lang .ConditionEvents ;
33+ import com .tngtech .archunit .lang .SimpleConditionEvent ;
3034import com .tngtech .archunit .library .DependencyRules ;
3135
3236import java .lang .invoke .MethodHandles ;
37+ import java .lang .reflect .Modifier ;
3338
3439import org .junit .jupiter .api .Test ;
3540
41+ import static com .tngtech .archunit .lang .syntax .ArchRuleDefinition .noClasses ;
3642import static com .tngtech .archunit .library .dependencies .SlicesRuleDefinition .slices ;
3743import static org .assertj .core .api .Assertions .assertThat ;
3844
@@ -46,6 +52,24 @@ public class ArchitectureTest {
4652 @ ArchTest
4753 private final ArchRule noClassesShouldDependOnUpperPackages = DependencyRules .NO_CLASSES_SHOULD_DEPEND_UPPER_PACKAGES ;
4854
55+ @ ArchTest
56+ private final ArchRule noClassesShouldPubliclyImplementInterfaceInSamePackage = noClasses ().should (publiclyImplementInterfacesInSamePackage ());
57+
58+ private static ArchCondition <JavaClass > publiclyImplementInterfacesInSamePackage () {
59+ return new ArchCondition <>("publicly implement interfaces that reside in same package" ) {
60+ @ Override
61+ public void check (JavaClass cls , ConditionEvents events ) {
62+ for (JavaClass iface : cls .getAllRawInterfaces ()) {
63+ boolean isSamePackageAndPublic = iface .getPackage ().equals (cls .getPackage ())
64+ && Modifier .isPublic (cls .reflect ().getModifiers ())
65+ && !Modifier .isAbstract (cls .reflect ().getModifiers ());
66+
67+ events .add (new SimpleConditionEvent (iface , isSamePackageAndPublic , cls .getDescription () + " is public and not abstract and implements <" + iface .getFullName () + "> in same package" ));
68+ }
69+ }
70+ };
71+ }
72+
4973 @ Test
5074 void shouldMatchPackageName () {
5175 assertThat (BASE_PACKAGE_NAME ).isEqualTo (MethodHandles .lookup ().lookupClass ().getPackageName ());
0 commit comments