Files
github-api/src/test/java/org/kohsuke/github/ArchTests.java
Liam Newman d13e490be2 Enforce use of assertThat
Assert.assert* methods other than assertThat() produce less clear tests.
This change enforces using assertThat() for all verifications.
While more verbose they are generally more consistent and easier to understand.
2021-04-20 00:46:43 -07:00

127 lines
4.6 KiB
Java

package org.kohsuke.github;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaAnnotation;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.tngtech.archunit.lang.conditions.ArchConditions.*;
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 static final JavaClasses classFiles = new ClassFileImporter()
.withImportOption(new ImportOption.DoNotIncludeTests())
.withImportOption(new ImportOption.DoNotIncludeJars())
.importPackages("org.kohsuke.github");
private static final JavaClasses tesetClassFiles = new ClassFileImporter()
.withImportOption(new ImportOption.OnlyIncludeTests())
.withImportOption(new ImportOption.DoNotIncludeJars())
.importPackages("org.kohsuke.github");
private static final DescribedPredicate<JavaAnnotation<?>> previewAnnotationWithNoMediaType = new DescribedPredicate<JavaAnnotation<?>>(
"preview has no required media types defined") {
@Override
public boolean apply(JavaAnnotation<?> javaAnnotation) {
boolean isPreview = javaAnnotation.getRawType().isEquivalentTo(Preview.class);
Object[] values = (Object[]) javaAnnotation.getProperties().get("value");
return isPreview && values != null && values.length < 1;
}
};
@BeforeClass
public static void beforeClass() {
assertTrue(classFiles.size() > 0);
}
@Test
public void testPreviewsAreFlaggedAsDeprecated() {
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)
.andShould(not(beAnnotatedWith(previewAnnotationWithNoMediaType)))
.because(reason);
ArchRule methodRule = methods().that()
.areAnnotatedWith(Preview.class)
.should()
.beAnnotatedWith(Deprecated.class)
.andShould(not(beAnnotatedWith(previewAnnotationWithNoMediaType)))
.because(reason);
ArchRule enumFieldsRule = fields().that()
.areDeclaredInClassesThat()
.areEnums()
.and()
.areAnnotatedWith(Preview.class)
.should()
.beAnnotatedWith(Deprecated.class)
.andShould(not(beAnnotatedWith(previewAnnotationWithNoMediaType)))
.because(reason);
classRule.check(classFiles);
enumFieldsRule.check(classFiles);
methodRule.check(classFiles);
}
@Test
public void testBetaApisAreFlaggedAsDeprecated() {
String reason = "all beta APIs must be annotated as @Deprecated until they are promoted to stable";
ArchRule classRule = classes().that()
.areAnnotatedWith(BetaApi.class)
.should()
.beAnnotatedWith(Deprecated.class)
.because(reason);
ArchRule methodRule = methods().that()
.areAnnotatedWith(BetaApi.class)
.should()
.beAnnotatedWith(Deprecated.class)
.because(reason);
ArchRule enumFieldsRule = fields().that()
.areDeclaredInClassesThat()
.areEnums()
.and()
.areAnnotatedWith(BetaApi.class)
.should()
.beAnnotatedWith(Deprecated.class)
.because(reason);
classRule.check(classFiles);
enumFieldsRule.check(classFiles);
methodRule.check(classFiles);
}
@Test
public void testRequireUseOfAssertThat() {
String reason = "This project uses `assertThat(...)` instead of other assert*() methods.";
ArchRule onlyAssertThatRule = methods().that()
.haveNameContaining("assert")
.should()
.haveName("assertThat")
.because(reason);
onlyAssertThatRule.check(tesetClassFiles);
}
}