|
| 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