Skip to content

Commit 7588267

Browse files
committed
Add arch test for preview API usage
1 parent ed4f9c8 commit 7588267

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@
410410
<artifactId>commons-lang3</artifactId>
411411
<version>3.9</version>
412412
</dependency>
413+
<dependency>
414+
<groupId>com.tngtech.archunit</groupId>
415+
<artifactId>archunit</artifactId>
416+
<version>0.14.1</version>
417+
<scope>test</scope>
418+
</dependency>
413419
<dependency>
414420
<groupId>org.hamcrest</groupId>
415421
<artifactId>hamcrest</artifactId>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.kohsuke.github;
2+
3+
import com.tngtech.archunit.core.domain.JavaClass;
4+
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+
import com.tngtech.archunit.core.importer.ClassFileImporter;
8+
import com.tngtech.archunit.core.importer.ImportOption;
9+
import com.tngtech.archunit.lang.ArchCondition;
10+
import com.tngtech.archunit.lang.ArchRule;
11+
import com.tngtech.archunit.lang.ConditionEvents;
12+
import com.tngtech.archunit.lang.SimpleConditionEvent;
13+
import org.junit.Test;
14+
15+
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
16+
17+
public class ArchTests {
18+
19+
private final JavaClasses classFiles = new ClassFileImporter()
20+
.withImportOption(new ImportOption.DoNotIncludeTests())
21+
.withImportOption(new ImportOption.DoNotIncludeJars())
22+
.importPackages("org.kohsuke.github");
23+
24+
@Test
25+
public void testPreviewsAreFlaggedAsDeprecated() {
26+
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);
54+
55+
}
56+
}

0 commit comments

Comments
 (0)