Skip to content

Commit 20dd585

Browse files
committed
Polish MergedAnnotation tests
(cherry picked from commit 952223d)
1 parent 707eb70 commit 20dd585

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void typeHierarchyStrategyOnClassWhenHasSuperclassScansSuperclass() {
197197
}
198198

199199
@Test
200-
void typeHierarchyStrategyOnClassWhenHasInterfaceDoesNotIncludeInterfaces() {
200+
void typeHierarchyStrategyOnClassWhenHasSingleInterfaceScansInterfaces() {
201201
Class<?> source = WithSingleInterface.class;
202202
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly(
203203
"0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2");
@@ -353,7 +353,7 @@ void typeHierarchyStrategyOnMethodWhenHasSuperclassScansSuperclass() {
353353
}
354354

355355
@Test
356-
void typeHierarchyStrategyOnMethodWhenHasInterfaceDoesNotIncludeInterfaces() {
356+
void typeHierarchyStrategyOnMethodWhenHasInterfaceScansInterfaces() {
357357
Method source = methodFrom(WithSingleInterface.class);
358358
assertThat(scan(source, SearchStrategy.TYPE_HIERARCHY)).containsExactly(
359359
"0:TestAnnotation1", "1:TestAnnotation2", "1:TestInheritedAnnotation2");

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,9 @@ void getWithTypeHierarchyFromSubSubNonInheritedAnnotationInterface() {
679679
}
680680

681681
@Test
682-
void getWithTypeHierarchyInheritedFromInterfaceMethod()
683-
throws NoSuchMethodException {
684-
Method method = ConcreteClassWithInheritedAnnotation.class.getMethod(
685-
"handleFromInterface");
686-
MergedAnnotation<?> annotation = MergedAnnotations.from(method,
687-
SearchStrategy.TYPE_HIERARCHY).get(Order.class);
682+
void getWithTypeHierarchyInheritedFromInterfaceMethod() throws Exception {
683+
Method method = ConcreteClassWithInheritedAnnotation.class.getMethod("handleFromInterface");
684+
MergedAnnotation<?> annotation = MergedAnnotations.from(method,SearchStrategy.TYPE_HIERARCHY).get(Order.class);
688685
assertThat(annotation.isPresent()).isTrue();
689686
assertThat(annotation.getAggregateIndex()).isEqualTo(1);
690687
}
@@ -1384,7 +1381,7 @@ void getDefaultValueFromAnnotationType() {
13841381
}
13851382

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

13961393
@Test
13971394
@SuppressWarnings("deprecation")
1398-
void getRepeatableDeclaredOnClassWithAttributeAliases() {
1395+
void streamRepeatableDeclaredOnClassWithAttributeAliases() {
13991396
assertThat(MergedAnnotations.from(HierarchyClass.class).stream(
14001397
TestConfiguration.class)).isEmpty();
14011398
RepeatableContainers containers = RepeatableContainers.of(TestConfiguration.class,
@@ -1409,47 +1406,47 @@ void getRepeatableDeclaredOnClassWithAttributeAliases() {
14091406
}
14101407

14111408
@Test
1412-
void getRepeatableDeclaredOnClass() {
1409+
void streamRepeatableDeclaredOnClass() {
14131410
Class<?> element = MyRepeatableClass.class;
14141411
String[] expectedValuesJava = { "A", "B", "C" };
14151412
String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
14161413
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
14171414
}
14181415

14191416
@Test
1420-
void getRepeatableDeclaredOnSuperclass() {
1417+
void streamRepeatableDeclaredOnSuperclass() {
14211418
Class<?> element = SubMyRepeatableClass.class;
14221419
String[] expectedValuesJava = { "A", "B", "C" };
14231420
String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
14241421
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
14251422
}
14261423

14271424
@Test
1428-
void getRepeatableDeclaredOnClassAndSuperclass() {
1425+
void streamRepeatableDeclaredOnClassAndSuperclass() {
14291426
Class<?> element = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
14301427
String[] expectedValuesJava = { "X", "Y", "Z" };
14311428
String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" };
14321429
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
14331430
}
14341431

14351432
@Test
1436-
void getRepeatableDeclaredOnMultipleSuperclasses() {
1433+
void streamRepeatableDeclaredOnMultipleSuperclasses() {
14371434
Class<?> element = SubSubMyRepeatableWithAdditionalLocalDeclarationsClass.class;
14381435
String[] expectedValuesJava = { "X", "Y", "Z" };
14391436
String[] expectedValuesSpring = { "X", "Y", "Z", "meta2" };
14401437
testRepeatables(SearchStrategy.SUPERCLASS, element, expectedValuesJava, expectedValuesSpring);
14411438
}
14421439

14431440
@Test
1444-
void getDirectRepeatablesDeclaredOnClass() {
1441+
void streamDirectRepeatablesDeclaredOnClass() {
14451442
Class<?> element = MyRepeatableClass.class;
14461443
String[] expectedValuesJava = { "A", "B", "C" };
14471444
String[] expectedValuesSpring = { "A", "B", "C", "meta1" };
14481445
testRepeatables(SearchStrategy.DIRECT, element, expectedValuesJava, expectedValuesSpring);
14491446
}
14501447

14511448
@Test
1452-
void getDirectRepeatablesDeclaredOnSuperclass() {
1449+
void streamDirectRepeatablesDeclaredOnSuperclass() {
14531450
Class<?> element = SubMyRepeatableClass.class;
14541451
String[] expectedValuesJava = {};
14551452
String[] expectedValuesSpring = {};
@@ -1476,20 +1473,17 @@ private void testExplicitRepeatables(SearchStrategy searchStrategy, Class<?> ele
14761473
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy,
14771474
RepeatableContainers.of(MyRepeatable.class, MyRepeatableContainer.class),
14781475
AnnotationFilter.PLAIN);
1479-
assertThat(annotations.stream(MyRepeatable.class).filter(
1480-
MergedAnnotationPredicates.firstRunOf(
1481-
MergedAnnotation::getAggregateIndex)).map(
1482-
annotation -> annotation.getString(
1483-
"value"))).containsExactly(expected);
1476+
Stream<String> values = annotations.stream(MyRepeatable.class)
1477+
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
1478+
.map(annotation -> annotation.getString("value"));
1479+
assertThat(values).containsExactly(expected);
14841480
}
14851481

14861482
private void testStandardRepeatables(SearchStrategy searchStrategy, Class<?> element, String[] expected) {
1487-
MergedAnnotations annotations = MergedAnnotations.from(element, searchStrategy);
1488-
assertThat(annotations.stream(MyRepeatable.class).filter(
1489-
MergedAnnotationPredicates.firstRunOf(
1490-
MergedAnnotation::getAggregateIndex)).map(
1491-
annotation -> annotation.getString(
1492-
"value"))).containsExactly(expected);
1483+
Stream<String> values = MergedAnnotations.from(element, searchStrategy).stream(MyRepeatable.class)
1484+
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
1485+
.map(annotation -> annotation.getString("value"));
1486+
assertThat(values).containsExactly(expected);
14931487
}
14941488

14951489
@Test

0 commit comments

Comments
 (0)