Skip to content

Commit

Permalink
Polish MergedAnnotation tests
Browse files Browse the repository at this point in the history
(cherry picked from commit 952223d)
  • Loading branch information
sbrannen committed Dec 12, 2023
1 parent 707eb70 commit 20dd585
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void typeHierarchyStrategyOnClassWhenHasSuperclassScansSuperclass() {
}

@Test
void typeHierarchyStrategyOnClassWhenHasInterfaceDoesNotIncludeInterfaces() {
void typeHierarchyStrategyOnClassWhenHasSingleInterfaceScansInterfaces() {
Class<?> source = WithSingleInterface.class;
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly(
"0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2");
Expand Down Expand Up @@ -353,7 +353,7 @@ void typeHierarchyStrategyOnMethodWhenHasSuperclassScansSuperclass() {
}

@Test
void typeHierarchyStrategyOnMethodWhenHasInterfaceDoesNotIncludeInterfaces() {
void typeHierarchyStrategyOnMethodWhenHasInterfaceScansInterfaces() {
Method source = methodFrom(WithSingleInterface.class);
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly(
"0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,9 @@ void getWithTypeHierarchyFromSubSubNonInheritedAnnotationInterface() {
}

@Test
void getWithTypeHierarchyInheritedFromInterfaceMethod()
throws NoSuchMethodException {
Method method = ConcreteClassWithInheritedAnnotation.class.getMethod(
"handleFromInterface");
MergedAnnotation<?> annotation = MergedAnnotations.from(method,
SearchStrategy.TYPE_HIERARCHY).get(Order.class);
void getWithTypeHierarchyInheritedFromInterfaceMethod() throws Exception {
Method method = ConcreteClassWithInheritedAnnotation.class.getMethod("handleFromInterface");
MergedAnnotation<?> annotation = MergedAnnotations.from(method,SearchStrategy.TYPE_HIERARCHY).get(Order.class);
assertThat(annotation.isPresent()).isTrue();
assertThat(annotation.getAggregateIndex()).isEqualTo(1);
}
Expand Down Expand Up @@ -1384,7 +1381,7 @@ void getDefaultValueFromAnnotationType() {
}

@Test
void getRepeatableDeclaredOnMethod() throws Exception {
void streamRepeatableDeclaredOnMethod() throws Exception {
Method method = InterfaceWithRepeated.class.getMethod("foo");
Stream<MergedAnnotation<MyRepeatable>> annotations = MergedAnnotations.from(
method, SearchStrategy.TYPE_HIERARCHY).stream(MyRepeatable.class);
Expand All @@ -1395,7 +1392,7 @@ void getRepeatableDeclaredOnMethod() throws Exception {

@Test
@SuppressWarnings("deprecation")
void getRepeatableDeclaredOnClassWithAttributeAliases() {
void streamRepeatableDeclaredOnClassWithAttributeAliases() {
assertThat(MergedAnnotations.from(HierarchyClass.class).stream(
TestConfiguration.class)).isEmpty();
RepeatableContainers containers = RepeatableContainers.of(TestConfiguration.class,
Expand All @@ -1409,47 +1406,47 @@ void getRepeatableDeclaredOnClassWithAttributeAliases() {
}

@Test
void getRepeatableDeclaredOnClass() {
void streamRepeatableDeclaredOnClass() {
Class<?> element = MyRepeatableClass.class;
String[] expectedValuesJava = { "A", "B", "C" };
String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
}

@Test
void getRepeatableDeclaredOnSuperclass() {
void streamRepeatableDeclaredOnSuperclass() {
Class<?> element = SubMyRepeatableClass.class;
String[] expectedValuesJava = { "A", "B", "C" };
String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
}

@Test
void getRepeatableDeclaredOnClassAndSuperclass() {
void streamRepeatableDeclaredOnClassAndSuperclass() {
Class<?> element = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
String[] expectedValuesJava = { "X", "Y", "Z" };
String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" };
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
}

@Test
void getRepeatableDeclaredOnMultipleSuperclasses() {
void streamRepeatableDeclaredOnMultipleSuperclasses() {
Class<?> element = SubSubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
String[] expectedValuesJava = { "X", "Y", "Z" };
String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" };
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
}

@Test
void getDirectRepeatablesDeclaredOnClass() {
void streamDirectRepeatablesDeclaredOnClass() {
Class<?> element = MyRepeatableClass.class;
String[] expectedValuesJava = { "A", "B", "C" };
String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
testRepeatables(SearchStrategy.DIRECT, element, expectedValuesJava, expectedValuesSpring);
}

@Test
void getDirectRepeatablesDeclaredOnSuperclass() {
void streamDirectRepeatablesDeclaredOnSuperclass() {
Class<?> element = SubMyRepeatableClass.class;
String[] expectedValuesJava = {};
String[] expectedValuesSpring = {};
Expand All @@ -1476,20 +1473,17 @@ private void testExplicitRepeatables(SearchStrategy searchStrategy, Class<?> ele
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy,
RepeatableContainers.of(MyRepeatable.class, MyRepeatableContainer.class),
AnnotationFilter.PLAIN);
assertThat(annotations.stream(MyRepeatable.class).filter(
MergedAnnotationPredicates.firstRunOf(
MergedAnnotation::getAggregateIndex)).map(
annotation -> annotation.getString(
"value"))).containsExactly(expected);
Stream<String> values = annotations.stream(MyRepeatable.class)
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
.map(annotation -> annotation.getString("value"));
assertThat(values).containsExactly(expected);
}

private void testStandardRepeatables(SearchStrategy searchStrategy, Class<?> element, String[] expected) {
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy);
assertThat(annotations.stream(MyRepeatable.class).filter(
MergedAnnotationPredicates.firstRunOf(
MergedAnnotation::getAggregateIndex)).map(
annotation -> annotation.getString(
"value"))).containsExactly(expected);
Stream<String> values = MergedAnnotations.from(element, searchStrategy).stream(MyRepeatable.class)
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
.map(annotation -> annotation.getString("value"));
assertThat(values).containsExactly(expected);
}

@Test
Expand Down

0 comments on commit 20dd585

Please sign in to comment.