|
1 | 1 | package org.kohsuke.github; |
2 | 2 |
|
3 | | -import com.tngtech.archunit.core.domain.JavaClass; |
4 | 3 | import com.tngtech.archunit.core.domain.JavaClasses; |
5 | | -import com.tngtech.archunit.core.domain.properties.HasAnnotations; |
6 | | -import com.tngtech.archunit.core.domain.properties.HasName.AndFullName; |
7 | 4 | import com.tngtech.archunit.core.importer.ClassFileImporter; |
8 | 5 | import com.tngtech.archunit.core.importer.ImportOption; |
9 | | -import com.tngtech.archunit.lang.ArchCondition; |
10 | 6 | import com.tngtech.archunit.lang.ArchRule; |
11 | | -import com.tngtech.archunit.lang.ConditionEvents; |
12 | | -import com.tngtech.archunit.lang.SimpleConditionEvent; |
| 7 | +import org.junit.BeforeClass; |
13 | 8 | import org.junit.Test; |
14 | 9 |
|
15 | 10 | import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; |
| 11 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields; |
| 12 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; |
| 13 | +import static org.junit.Assert.assertTrue; |
16 | 14 |
|
17 | 15 | public class ArchTests { |
18 | 16 |
|
19 | | - private final JavaClasses classFiles = new ClassFileImporter() |
| 17 | + private static final JavaClasses classFiles = new ClassFileImporter() |
20 | 18 | .withImportOption(new ImportOption.DoNotIncludeTests()) |
21 | 19 | .withImportOption(new ImportOption.DoNotIncludeJars()) |
22 | 20 | .importPackages("org.kohsuke.github"); |
23 | 21 |
|
| 22 | + @BeforeClass |
| 23 | + public static void beforeClass() { |
| 24 | + assertTrue(classFiles.size() > 0); |
| 25 | + } |
| 26 | + |
24 | 27 | @Test |
25 | 28 | public void testPreviewsAreFlaggedAsDeprecated() { |
26 | 29 |
|
27 | | - String description = "annotate all preview APIs as @Deprecated until they are promoted to stable"; |
28 | | - |
29 | | - ArchRule rule = classes().should(new ArchCondition<JavaClass>(description) { |
30 | | - |
31 | | - @Override |
32 | | - public void check(final JavaClass targetClazz, final ConditionEvents events) { |
33 | | - checkForPreviewAnnotation(targetClazz, events); |
34 | | - targetClazz.getAllMethods().forEach(method -> { |
35 | | - checkForPreviewAnnotation(method, events); |
36 | | - }); |
37 | | - } |
38 | | - |
39 | | - <T extends HasAnnotations<T> & AndFullName> void checkForPreviewAnnotation(T codeTarget, |
40 | | - ConditionEvents events) { |
41 | | - |
42 | | - if (codeTarget.tryGetAnnotationOfType(Preview.class).isPresent() |
43 | | - && !codeTarget.tryGetAnnotationOfType(Deprecated.class).isPresent()) { |
44 | | - |
45 | | - String message = codeTarget.getFullName() |
46 | | - + " uses a preview API and is missing the '@Deprecated' annotation."; |
47 | | - |
48 | | - events.add(new SimpleConditionEvent(codeTarget, false, message)); |
49 | | - } |
50 | | - } |
51 | | - }); |
52 | | - |
53 | | - rule.check(classFiles); |
| 30 | + String reason = "all preview APIs must be annotated as @Deprecated until they are promoted to stable"; |
| 31 | + |
| 32 | + ArchRule classRule = classes().that() |
| 33 | + .areAnnotatedWith(Preview.class) |
| 34 | + .should() |
| 35 | + .beAnnotatedWith(Deprecated.class) |
| 36 | + .because(reason); |
| 37 | + |
| 38 | + ArchRule methodRule = methods().that() |
| 39 | + .areAnnotatedWith(Preview.class) |
| 40 | + .should() |
| 41 | + .beAnnotatedWith(Deprecated.class) |
| 42 | + .because(reason); |
| 43 | + |
| 44 | + ArchRule enumFieldsRule = fields().that() |
| 45 | + .areDeclaredInClassesThat() |
| 46 | + .areEnums() |
| 47 | + .and() |
| 48 | + .areAnnotatedWith(Preview.class) |
| 49 | + .should() |
| 50 | + .beAnnotatedWith(Deprecated.class) |
| 51 | + .because(reason); |
| 52 | + |
| 53 | + classRule.check(classFiles); |
| 54 | + enumFieldsRule.check(classFiles); |
| 55 | + methodRule.check(classFiles); |
54 | 56 |
|
55 | 57 | } |
56 | 58 | } |
0 commit comments