Skip to content

Commit 459d1b4

Browse files
committed
update arch tests to add enum checks
1 parent 7588267 commit 459d1b4

1 file changed

Lines changed: 36 additions & 34 deletions

File tree

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
package org.kohsuke.github;
22

3-
import com.tngtech.archunit.core.domain.JavaClass;
43
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;
74
import com.tngtech.archunit.core.importer.ClassFileImporter;
85
import com.tngtech.archunit.core.importer.ImportOption;
9-
import com.tngtech.archunit.lang.ArchCondition;
106
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;
138
import org.junit.Test;
149

1510
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;
1614

1715
public class ArchTests {
1816

19-
private final JavaClasses classFiles = new ClassFileImporter()
17+
private static final JavaClasses classFiles = new ClassFileImporter()
2018
.withImportOption(new ImportOption.DoNotIncludeTests())
2119
.withImportOption(new ImportOption.DoNotIncludeJars())
2220
.importPackages("org.kohsuke.github");
2321

22+
@BeforeClass
23+
public static void beforeClass() {
24+
assertTrue(classFiles.size() > 0);
25+
}
26+
2427
@Test
2528
public void testPreviewsAreFlaggedAsDeprecated() {
2629

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);
5456

5557
}
5658
}

0 commit comments

Comments
 (0)