Skip to content

Commit

Permalink
Simple demo, how accesses by classes that are annotated with @Allow c…
Browse files Browse the repository at this point in the history
…an be ignored. Remove @Allow from Okay.java and the violation will be reported (likewise, add it to Bad.java and the test will pass). Note that annotations may not be @retention(SOURCE) to be picked up.
  • Loading branch information
codecholeric committed Jul 16, 2017
1 parent 3fd3247 commit 7a7e3d5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions archunit-example/src/main/java/com/bar/Allow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.bar;

public @interface Allow {
}
4 changes: 4 additions & 0 deletions archunit-example/src/main/java/com/bar/evil/Evil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.bar.evil;

public class Evil {
}
9 changes: 9 additions & 0 deletions archunit-example/src/main/java/com/bar/some/Bad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bar.some;

import com.bar.evil.Evil;

public class Bad {
public Bad() {
new Evil();
}
}
11 changes: 11 additions & 0 deletions archunit-example/src/main/java/com/bar/some/Okay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bar.some;

import com.bar.Allow;
import com.bar.evil.Evil;

@Allow
public class Okay {
public Okay() {
new Evil();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bar;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.junit.ArchUnitRunner;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.runner.RunWith;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;

@RunWith(ArchUnitRunner.class)
@AnalyzeClasses(packages = "com.bar")
public class IgnoreByAnnotationWorksTest {
@ArchTest
public static final ArchRule ignoring_classes_annotated_with_Allow =
noClasses().that().areNotAnnotatedWith(Allow.class)
.should().accessClassesThat().resideInAPackage("com.bar.evil");
}

0 comments on commit 7a7e3d5

Please sign in to comment.