Browse Source

Polish MergedAnnotation tests

pull/31837/head
Sam Brannen 2 years ago
parent
commit
952223dcf9
  1. 4
      spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java
  2. 44
      spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java

4
spring-core/src/test/java/org/springframework/core/annotation/AnnotationsScannerTests.java

@ -197,7 +197,7 @@ class AnnotationsScannerTests {
} }
@Test @Test
void typeHierarchyStrategyOnClassWhenHasInterfaceDoesNotIncludeInterfaces() { void typeHierarchyStrategyOnClassWhenHasSingleInterfaceScansInterfaces() {
Class<?> source = WithSingleInterface.class; Class<?> source = WithSingleInterface.class;
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly( assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly(
"0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2"); "0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2");
@ -353,7 +353,7 @@ class AnnotationsScannerTests {
} }
@Test @Test
void typeHierarchyStrategyOnMethodWhenHasInterfaceDoesNotIncludeInterfaces() { void typeHierarchyStrategyOnMethodWhenHasInterfaceScansInterfaces() {
Method source = methodFrom(WithSingleInterface.class); Method source = methodFrom(WithSingleInterface.class);
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly( assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly(
"0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2"); "0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2");

44
spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java

@ -679,12 +679,9 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getWithTypeHierarchyInheritedFromInterfaceMethod() void getWithTypeHierarchyInheritedFromInterfaceMethod() throws Exception {
throws NoSuchMethodException { Method method = ConcreteClassWithInheritedAnnotation.class.getMethod("handleFromInterface");
Method method = ConcreteClassWithInheritedAnnotation.class.getMethod( MergedAnnotation<?> annotation = MergedAnnotations.from(method,SearchStrategy.TYPE_HIERARCHY).get(Order.class);
"handleFromInterface");
MergedAnnotation<?> annotation = MergedAnnotations.from(method,
SearchStrategy.TYPE_HIERARCHY).get(Order.class);
assertThat(annotation.isPresent()).isTrue(); assertThat(annotation.isPresent()).isTrue();
assertThat(annotation.getAggregateIndex()).isEqualTo(1); assertThat(annotation.getAggregateIndex()).isEqualTo(1);
} }
@ -1384,7 +1381,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getRepeatableDeclaredOnMethod() throws Exception { void streamRepeatableDeclaredOnMethod() throws Exception {
Method method = InterfaceWithRepeated.class.getMethod("foo"); Method method = InterfaceWithRepeated.class.getMethod("foo");
Stream<MergedAnnotation<MyRepeatable>> annotations = MergedAnnotations.from( Stream<MergedAnnotation<MyRepeatable>> annotations = MergedAnnotations.from(
method, SearchStrategy.TYPE_HIERARCHY).stream(MyRepeatable.class); method, SearchStrategy.TYPE_HIERARCHY).stream(MyRepeatable.class);
@ -1395,7 +1392,7 @@ class MergedAnnotationsTests {
@Test @Test
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void getRepeatableDeclaredOnClassWithAttributeAliases() { void streamRepeatableDeclaredOnClassWithAttributeAliases() {
assertThat(MergedAnnotations.from(HierarchyClass.class).stream( assertThat(MergedAnnotations.from(HierarchyClass.class).stream(
TestConfiguration.class)).isEmpty(); TestConfiguration.class)).isEmpty();
RepeatableContainers containers = RepeatableContainers.of(TestConfiguration.class, RepeatableContainers containers = RepeatableContainers.of(TestConfiguration.class,
@ -1409,7 +1406,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getRepeatableDeclaredOnClass() { void streamRepeatableDeclaredOnClass() {
Class<?> element = MyRepeatableClass.class; Class<?> element = MyRepeatableClass.class;
String[] expectedValuesJava = { "A", "B", "C" }; String[] expectedValuesJava = { "A", "B", "C" };
String[] expectedValuesSpring = { "A", "B", "C", "meta1" }; String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
@ -1417,7 +1414,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getRepeatableDeclaredOnSuperclass() { void streamRepeatableDeclaredOnSuperclass() {
Class<?> element = SubMyRepeatableClass.class; Class<?> element = SubMyRepeatableClass.class;
String[] expectedValuesJava = { "A", "B", "C" }; String[] expectedValuesJava = { "A", "B", "C" };
String[] expectedValuesSpring = { "A", "B", "C", "meta1" }; String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
@ -1425,7 +1422,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getRepeatableDeclaredOnClassAndSuperclass() { void streamRepeatableDeclaredOnClassAndSuperclass() {
Class<?> element = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class; Class<?> element = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
String[] expectedValuesJava = { "X", "Y", "Z" }; String[] expectedValuesJava = { "X", "Y", "Z" };
String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" }; String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" };
@ -1433,7 +1430,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getRepeatableDeclaredOnMultipleSuperclasses() { void streamRepeatableDeclaredOnMultipleSuperclasses() {
Class<?> element = SubSubMyRepeatableWithAdditionalLocalDeclarationsClass.class; Class<?> element = SubSubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
String[] expectedValuesJava = { "X", "Y", "Z" }; String[] expectedValuesJava = { "X", "Y", "Z" };
String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" }; String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" };
@ -1441,7 +1438,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getDirectRepeatablesDeclaredOnClass() { void streamDirectRepeatablesDeclaredOnClass() {
Class<?> element = MyRepeatableClass.class; Class<?> element = MyRepeatableClass.class;
String[] expectedValuesJava = { "A", "B", "C" }; String[] expectedValuesJava = { "A", "B", "C" };
String[] expectedValuesSpring = { "A", "B", "C", "meta1" }; String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
@ -1449,7 +1446,7 @@ class MergedAnnotationsTests {
} }
@Test @Test
void getDirectRepeatablesDeclaredOnSuperclass() { void streamDirectRepeatablesDeclaredOnSuperclass() {
Class<?> element = SubMyRepeatableClass.class; Class<?> element = SubMyRepeatableClass.class;
String[] expectedValuesJava = {}; String[] expectedValuesJava = {};
String[] expectedValuesSpring = {}; String[] expectedValuesSpring = {};
@ -1476,20 +1473,17 @@ class MergedAnnotationsTests {
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy, MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy,
RepeatableContainers.of(MyRepeatable.class, MyRepeatableContainer.class), RepeatableContainers.of(MyRepeatable.class, MyRepeatableContainer.class),
AnnotationFilter.PLAIN); AnnotationFilter.PLAIN);
assertThat(annotations.stream(MyRepeatable.class).filter( Stream<String> values = annotations.stream(MyRepeatable.class)
MergedAnnotationPredicates.firstRunOf( .filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
MergedAnnotation::getAggregateIndex)).map( .map(annotation -> annotation.getString("value"));
annotation -> annotation.getString( assertThat(values).containsExactly(expected);
"value"))).containsExactly(expected);
} }
private void testStandardRepeatables(SearchStrategy searchStrategy, Class<?> element, String[] expected) { private void testStandardRepeatables(SearchStrategy searchStrategy, Class<?> element, String[] expected) {
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy); Stream<String> values = MergedAnnotations.from(element, searchStrategy).stream(MyRepeatable.class)
assertThat(annotations.stream(MyRepeatable.class).filter( .filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
MergedAnnotationPredicates.firstRunOf( .map(annotation -> annotation.getString("value"));
MergedAnnotation::getAggregateIndex)).map( assertThat(values).containsExactly(expected);
annotation -> annotation.getString(
"value"))).containsExactly(expected);
} }
@Test @Test

Loading…
Cancel
Save