Skip to content

Commit

Permalink
add tests for classes().should().{be/notBe}MetaAnnotatedWith(..)
Browse files Browse the repository at this point in the history
It seems like these tests have been forgotten. Luckily almost all the logic is already tested in `MembersShouldTest`, except for delegating one-liners, nevertheless I've added the correct tests now in `ClassesShouldTest`.

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric committed Feb 21, 2021
1 parent 5d68c45 commit 71835ef
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,68 @@ public void notAnnotatedWith(ArchRule rule, Class<?> correctClass, Class<?> wron
.doesNotMatch(String.format(".*<%s>.*annotated.*", quote(correctClass.getName())));
}

@DataProvider
public static Object[][] metaAnnotated_rules() {
return $$(
$(classes().should().beMetaAnnotatedWith(SomeMetaAnnotation.class),
SomeAnnotatedClass.class, String.class),
$(classes().should(ArchConditions.beMetaAnnotatedWith(SomeMetaAnnotation.class)),
SomeAnnotatedClass.class, String.class),
$(classes().should().beMetaAnnotatedWith(SomeMetaAnnotation.class.getName()),
SomeAnnotatedClass.class, String.class),
$(classes().should(ArchConditions.beMetaAnnotatedWith(SomeMetaAnnotation.class.getName())),
SomeAnnotatedClass.class, String.class),
$(classes().should().beMetaAnnotatedWith(annotation(SomeMetaAnnotation.class)),
SomeAnnotatedClass.class, String.class),
$(classes().should(ArchConditions.beMetaAnnotatedWith(annotation(SomeMetaAnnotation.class))),
SomeAnnotatedClass.class, String.class));
}

@Test
@UseDataProvider("metaAnnotated_rules")
public void metaAnnotatedWith(ArchRule rule, Class<?> correctClass, Class<?> wrongClass) {
EvaluationResult result = rule.evaluate(importClasses(correctClass, wrongClass));

assertThat(singleLineFailureReportOf(result))
.contains(String.format("classes should be meta-annotated with @%s", SomeMetaAnnotation.class.getSimpleName()))
.containsPattern(String.format("Class <%s> is not meta-annotated with @%s in %s",
quote(wrongClass.getName()),
SomeMetaAnnotation.class.getSimpleName(),
locationPattern(String.class)))
.doesNotMatch(String.format(".*<%s>.*meta-annotated.*", quote(correctClass.getName())));
}

@DataProvider
public static Object[][] notMetaAnnotated_rules() {
return $$(
$(classes().should().notBeMetaAnnotatedWith(SomeMetaAnnotation.class),
String.class, SomeAnnotatedClass.class),
$(classes().should(ArchConditions.notBeMetaAnnotatedWith(SomeMetaAnnotation.class)),
String.class, SomeAnnotatedClass.class),
$(classes().should().notBeMetaAnnotatedWith(SomeMetaAnnotation.class.getName()),
String.class, SomeAnnotatedClass.class),
$(classes().should(ArchConditions.notBeMetaAnnotatedWith(SomeMetaAnnotation.class.getName())),
String.class, SomeAnnotatedClass.class),
$(classes().should().notBeMetaAnnotatedWith(annotation(SomeMetaAnnotation.class)),
String.class, SomeAnnotatedClass.class),
$(classes().should(ArchConditions.notBeMetaAnnotatedWith(annotation(SomeMetaAnnotation.class))),
String.class, SomeAnnotatedClass.class));
}

@Test
@UseDataProvider("notMetaAnnotated_rules")
public void notMetaAnnotatedWith(ArchRule rule, Class<?> correctClass, Class<?> wrongClass) {
EvaluationResult result = rule.evaluate(importClasses(correctClass, wrongClass));

assertThat(singleLineFailureReportOf(result))
.contains("classes should not be meta-annotated with @" + SomeMetaAnnotation.class.getSimpleName())
.containsPattern(String.format("Class <%s> is meta-annotated with @%s in %s",
quote(wrongClass.getName()),
SomeMetaAnnotation.class.getSimpleName(),
locationPattern(getClass())))
.doesNotMatch(String.format(".*<%s>.*meta-annotated.*", quote(correctClass.getName())));
}

/**
* Compare {@link CanBeAnnotatedTest#annotatedWith_Retention_Source_is_rejected}
*/
Expand Down Expand Up @@ -1971,10 +2033,18 @@ static class PackagePrivateClass {
private static class PrivateClass {
}

@SomeAnnotation
@RuntimeRetentionAnnotation
private static class SomeAnnotatedClass {
}

@interface SomeMetaAnnotation {
}

@SomeMetaAnnotation
@interface SomeAnnotation {
}

private static class NestedClassWithSomeMoreClasses {

static class StaticNestedClass {
Expand Down

0 comments on commit 71835ef

Please sign in to comment.