From 7588267743d544a7281ce677f4c09dee0f4deaa3 Mon Sep 17 00:00:00 2001 From: Marco Ferrer Date: Wed, 25 Nov 2020 13:16:04 -0500 Subject: [PATCH 1/2] Add arch test for preview API usage --- pom.xml | 6 ++ .../java/org/kohsuke/github/ArchTests.java | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/test/java/org/kohsuke/github/ArchTests.java diff --git a/pom.xml b/pom.xml index df5887a68b..d78e03a9f8 100644 --- a/pom.xml +++ b/pom.xml @@ -410,6 +410,12 @@ commons-lang3 3.9 + + com.tngtech.archunit + archunit + 0.14.1 + test + org.hamcrest hamcrest diff --git a/src/test/java/org/kohsuke/github/ArchTests.java b/src/test/java/org/kohsuke/github/ArchTests.java new file mode 100644 index 0000000000..420d558bd7 --- /dev/null +++ b/src/test/java/org/kohsuke/github/ArchTests.java @@ -0,0 +1,56 @@ +package org.kohsuke.github; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.properties.HasAnnotations; +import com.tngtech.archunit.core.domain.properties.HasName.AndFullName; +import com.tngtech.archunit.core.importer.ClassFileImporter; +import com.tngtech.archunit.core.importer.ImportOption; +import com.tngtech.archunit.lang.ArchCondition; +import com.tngtech.archunit.lang.ArchRule; +import com.tngtech.archunit.lang.ConditionEvents; +import com.tngtech.archunit.lang.SimpleConditionEvent; +import org.junit.Test; + +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; + +public class ArchTests { + + private final JavaClasses classFiles = new ClassFileImporter() + .withImportOption(new ImportOption.DoNotIncludeTests()) + .withImportOption(new ImportOption.DoNotIncludeJars()) + .importPackages("org.kohsuke.github"); + + @Test + public void testPreviewsAreFlaggedAsDeprecated() { + + String description = "annotate all preview APIs as @Deprecated until they are promoted to stable"; + + ArchRule rule = classes().should(new ArchCondition(description) { + + @Override + public void check(final JavaClass targetClazz, final ConditionEvents events) { + checkForPreviewAnnotation(targetClazz, events); + targetClazz.getAllMethods().forEach(method -> { + checkForPreviewAnnotation(method, events); + }); + } + + & AndFullName> void checkForPreviewAnnotation(T codeTarget, + ConditionEvents events) { + + if (codeTarget.tryGetAnnotationOfType(Preview.class).isPresent() + && !codeTarget.tryGetAnnotationOfType(Deprecated.class).isPresent()) { + + String message = codeTarget.getFullName() + + " uses a preview API and is missing the '@Deprecated' annotation."; + + events.add(new SimpleConditionEvent(codeTarget, false, message)); + } + } + }); + + rule.check(classFiles); + + } +} From 459d1b4f5629182d54454097f96a42e5c6fd746f Mon Sep 17 00:00:00 2001 From: Marco Ferrer Date: Tue, 15 Dec 2020 12:41:02 -0500 Subject: [PATCH 2/2] update arch tests to add enum checks --- .../java/org/kohsuke/github/ArchTests.java | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/test/java/org/kohsuke/github/ArchTests.java b/src/test/java/org/kohsuke/github/ArchTests.java index 420d558bd7..662ee76422 100644 --- a/src/test/java/org/kohsuke/github/ArchTests.java +++ b/src/test/java/org/kohsuke/github/ArchTests.java @@ -1,56 +1,58 @@ package org.kohsuke.github; -import com.tngtech.archunit.core.domain.JavaClass; import com.tngtech.archunit.core.domain.JavaClasses; -import com.tngtech.archunit.core.domain.properties.HasAnnotations; -import com.tngtech.archunit.core.domain.properties.HasName.AndFullName; import com.tngtech.archunit.core.importer.ClassFileImporter; import com.tngtech.archunit.core.importer.ImportOption; -import com.tngtech.archunit.lang.ArchCondition; import com.tngtech.archunit.lang.ArchRule; -import com.tngtech.archunit.lang.ConditionEvents; -import com.tngtech.archunit.lang.SimpleConditionEvent; +import org.junit.BeforeClass; import org.junit.Test; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; +import static org.junit.Assert.assertTrue; public class ArchTests { - private final JavaClasses classFiles = new ClassFileImporter() + private static final JavaClasses classFiles = new ClassFileImporter() .withImportOption(new ImportOption.DoNotIncludeTests()) .withImportOption(new ImportOption.DoNotIncludeJars()) .importPackages("org.kohsuke.github"); + @BeforeClass + public static void beforeClass() { + assertTrue(classFiles.size() > 0); + } + @Test public void testPreviewsAreFlaggedAsDeprecated() { - String description = "annotate all preview APIs as @Deprecated until they are promoted to stable"; - - ArchRule rule = classes().should(new ArchCondition(description) { - - @Override - public void check(final JavaClass targetClazz, final ConditionEvents events) { - checkForPreviewAnnotation(targetClazz, events); - targetClazz.getAllMethods().forEach(method -> { - checkForPreviewAnnotation(method, events); - }); - } - - & AndFullName> void checkForPreviewAnnotation(T codeTarget, - ConditionEvents events) { - - if (codeTarget.tryGetAnnotationOfType(Preview.class).isPresent() - && !codeTarget.tryGetAnnotationOfType(Deprecated.class).isPresent()) { - - String message = codeTarget.getFullName() - + " uses a preview API and is missing the '@Deprecated' annotation."; - - events.add(new SimpleConditionEvent(codeTarget, false, message)); - } - } - }); - - rule.check(classFiles); + String reason = "all preview APIs must be annotated as @Deprecated until they are promoted to stable"; + + ArchRule classRule = classes().that() + .areAnnotatedWith(Preview.class) + .should() + .beAnnotatedWith(Deprecated.class) + .because(reason); + + ArchRule methodRule = methods().that() + .areAnnotatedWith(Preview.class) + .should() + .beAnnotatedWith(Deprecated.class) + .because(reason); + + ArchRule enumFieldsRule = fields().that() + .areDeclaredInClassesThat() + .areEnums() + .and() + .areAnnotatedWith(Preview.class) + .should() + .beAnnotatedWith(Deprecated.class) + .because(reason); + + classRule.check(classFiles); + enumFieldsRule.check(classFiles); + methodRule.check(classFiles); } }