forked from TNG/ArchUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable
@AnalyzeClasses
annotation to be used as meta annotation
so far users are forced to repeat `@AnalyzeClasses` annotation an every test class. This cause additional maintenance overhead when common properties (e.g. package structure) changes. To support the DRY approach, `@AnalzyeClasses` annotation can now be used as meta annotation. Resolves: TNG#182 Signed-off-by: Mathze <[email protected]>
- Loading branch information
Showing
8 changed files
with
348 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ch/archunit/junit/internal/testexamples/TestClassWithMetaAnnotationForAnalyzeClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.tngtech.archunit.junit.internal.testexamples; | ||
|
||
import com.tngtech.archunit.junit.AnalyzeClasses; | ||
import com.tngtech.archunit.junit.ArchTest; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@TestClassWithMetaAnnotationForAnalyzeClasses.MetaAnalyzeCls | ||
public class TestClassWithMetaAnnotationForAnalyzeClasses { | ||
|
||
@ArchTest | ||
public static final ArchRule rule_in_class_with_meta_analyze_class_annotation = RuleThatFails.on(UnwantedClass.class); | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
@AnalyzeClasses(wholeClasspath = true) | ||
public @interface MetaAnalyzeCls { | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...unit/internal/testexamples/wrong/WrongTestClassWithMultipleAnalyzeClassesAnnotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.tngtech.archunit.junit.internal.testexamples.wrong; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import com.tngtech.archunit.junit.AnalyzeClasses; | ||
import com.tngtech.archunit.junit.ArchTest; | ||
import com.tngtech.archunit.junit.internal.testexamples.RuleThatFails; | ||
import com.tngtech.archunit.junit.internal.testexamples.UnwantedClass; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@AnalyzeClasses(packages = "dummy") | ||
@WrongTestClassWithMultipleAnalyzeClassesAnnotations.MetaAnalyzeCls | ||
public class WrongTestClassWithMultipleAnalyzeClassesAnnotations { | ||
|
||
@ArchTest | ||
public static final ArchRule dummy_rule = RuleThatFails.on(UnwantedClass.class); | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
@AnalyzeClasses(wholeClasspath = true) | ||
public @interface MetaAnalyzeCls { | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
archunit-junit/src/main/java/com/tngtech/archunit/junit/internal/AnnotationFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.tngtech.archunit.junit.internal; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
class AnnotationFinder<T extends Annotation> { | ||
|
||
private final Class<T> annotationClass; | ||
|
||
public AnnotationFinder(final Class<T> annotationClass) { | ||
this.annotationClass = annotationClass; | ||
} | ||
|
||
/** | ||
* Recursively retrieve all {@link T} annotations from a given element. | ||
* | ||
* @param clazz The clazz from which to retrieve the annotation. | ||
* @return List of all found annotation instance or empty list. | ||
*/ | ||
public List<T> findAnnotationsOn(final Class<?> clazz) { | ||
return findAnnotations(clazz.getAnnotations(), new HashSet<>()); | ||
} | ||
|
||
private List<T> findAnnotations(final Annotation[] annotations, final HashSet<Annotation> visited) { | ||
final List<T> result = new LinkedList<>(); | ||
for (Annotation annotation : annotations) { | ||
if (visited.contains(annotation)) { | ||
continue; | ||
} else { | ||
visited.add(annotation); | ||
} | ||
if (annotationClass.isInstance(annotation)) { | ||
result.add(annotationClass.cast(annotation)); | ||
} else { | ||
result.addAll(findAnnotations(annotation.annotationType().getAnnotations(), visited)); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.