11package com .netflix .nebula .archrules .core ;
22
3+ import com .netflix .nebula .archrules .testpackage .TestDeprecated ;
4+ import com .tngtech .archunit .core .domain .JavaClass ;
5+ import com .tngtech .archunit .lang .ArchCondition ;
36import com .tngtech .archunit .lang .ArchRule ;
7+ import com .tngtech .archunit .lang .ConditionEvents ;
48import com .tngtech .archunit .lang .EvaluationResult ;
59import com .tngtech .archunit .lang .Priority ;
10+ import com .tngtech .archunit .lang .SimpleConditionEvent ;
611import com .tngtech .archunit .lang .syntax .ArchRuleDefinition ;
712import org .junit .jupiter .api .Test ;
813
914import static com .tngtech .archunit .core .domain .JavaClass .Predicates .simpleName ;
1015import static org .assertj .core .api .Assertions .assertThat ;
1116
1217public class RunnerTest {
18+ private final ArchRule noDeprecatedRule = ArchRuleDefinition .classes ().should ()
19+ .notBeAnnotatedWith (Deprecated .class );
20+
1321 @ Test
1422 public void test_pass () {
1523 final EvaluationResult result = Runner .check (noDeprecatedRule , PassingClass .class );
@@ -36,8 +44,6 @@ static class PassingClass {
3644 static class FailingClass {
3745 }
3846
39- private final ArchRule noDeprecatedRule = ArchRuleDefinition .classes ().should ()
40- .notBeAnnotatedWith (Deprecated .class );
4147 private final ArchRule smokeTestRule = ArchRuleDefinition .priority (Priority .MEDIUM )
4248 .classes ().that (simpleName ("SmokeTest" ))
4349 .should ().beAnnotatedWith (Deprecated .class )
@@ -63,4 +69,25 @@ public void test_smoke_fail() {
6369 assertThat (result .getFailureReport ().getDetails ())
6470 .contains (NoClassesMatchedEvent .NO_MATCH_MESSAGE );
6571 }
72+
73+ private final ArchCondition <JavaClass > notBeInDeprecatedPackage =
74+ new ArchCondition <JavaClass >("not be in a package marked with @Deprecated" ) {
75+ @ Override
76+ public void check (JavaClass javaClass , ConditionEvents events ) {
77+ boolean isInDeprecatedPackage = javaClass .getPackage ().getPackageInfo ().isAnnotatedWith (Deprecated .class );
78+ if (isInDeprecatedPackage ) {
79+ String message = String .format ("Class %s is in a package marked with @Deprecated" , javaClass .getName ());
80+ events .add (SimpleConditionEvent .violated (javaClass , message ));
81+ }
82+ }
83+ };
84+ private final ArchRule noDeprecatedPackageRule = ArchRuleDefinition
85+ .classes ().should (notBeInDeprecatedPackage );
86+
87+ @ Test
88+ public void test_package_deprecated_rule () {
89+ final EvaluationResult result = Runner .check (noDeprecatedPackageRule , TestDeprecated .class );
90+ assertThat (result .hasViolation ()).isTrue ();
91+ }
92+
6693}
0 commit comments