Skip to content
Closed
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
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");
}
4 changes: 2 additions & 2 deletions archunit-example/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</Console>
</Appenders>
<Loggers>
<Root level="info">
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -250,6 +251,9 @@ public void visitEnd() {
return;
}

if (LOG.isDebugEnabled()) {
LOG.debug("Found annotations {} on class '{}'", annotations.stream().map(a -> a.getJavaType().getName()).collect(Collectors.joining(", ", "[", "]")), this.className);
}
declarationHandler.onDeclaredAnnotations(annotations);
LOG.debug("Done analysing {}", className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_TYPE;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.have;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.testutil.Assertions.assertThat;
import static com.tngtech.archunit.testutil.Assertions.assertThatClasses;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -205,6 +206,14 @@ public void areAnnotatedWith_type() {
assertThat(getOnlyElement(classes)).matches(AnnotatedClass.class);
}

@Test
public void areAnnotatedWith_type_inverse() {
List<JavaClass> classes = filterResultOf(noClasses().that().areNotAnnotatedWith(SomeAnnotation.class))
.on(AnnotatedClass.class, SimpleClass.class);

assertThat(getOnlyElement(classes)).matches(AnnotatedClass.class);
}

@Test
public void areNotAnnotatedWith_type() {
List<JavaClass> classes = filterResultOf(classes().that().areNotAnnotatedWith(SomeAnnotation.class))
Expand All @@ -213,6 +222,14 @@ public void areNotAnnotatedWith_type() {
assertThat(getOnlyElement(classes)).matches(SimpleClass.class);
}

@Test
public void areNotAnnotatedWith_type_usingNoClasses() {
List<JavaClass> classes = filterResultOf(noClasses().that().areAnnotatedWith(SomeAnnotation.class))
.on(AnnotatedClass.class, SimpleClass.class);

assertThat(getOnlyElement(classes)).matches(SimpleClass.class);
}

@Test
public void areAnnotatedWith_typeName() {
List<JavaClass> classes = filterResultOf(classes().that().areAnnotatedWith(SomeAnnotation.class.getName()))
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ subprojects {
def addModuleDescription = { "${it} - Module '${project.name}'" }
description addModuleDescription(rootProject.description)

sourceCompatibility = '1.7'
targetCompatibility = '1.7'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

tasks.withType(Jar) {
manifest {
Expand Down Expand Up @@ -213,4 +213,4 @@ productionProjects*.with {
}
}

apply from: 'build-utils/build-utils.gradle'
apply from: 'build-utils/build-utils.gradle'