Polishing

This commit is contained in:
Sam Brannen
2025-03-21 14:45:00 +01:00
parent 58246ec5ec
commit b7a9bee020
2 changed files with 35 additions and 36 deletions

View File

@@ -1300,8 +1300,8 @@ class MergedAnnotationsTests {
@Test @Test
void getDirectWithAttributeAliasesWithDifferentValues() throws Exception { void getDirectWithAttributeAliasesWithDifferentValues() throws Exception {
Method method = WebController.class.getMethod("handleMappedWithDifferentPathAndValueAttributes"); Method method = WebController.class.getMethod("handleMappedWithDifferentPathAndValueAttributes");
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() -> assertThatExceptionOfType(AnnotationConfigurationException.class)
MergedAnnotations.from(method).get(RequestMapping.class)) .isThrownBy(() -> MergedAnnotations.from(method).get(RequestMapping.class))
.withMessageContaining("attribute 'path' and its alias 'value'") .withMessageContaining("attribute 'path' and its alias 'value'")
.withMessageContaining("values of [{/test}] and [{/enigma}]"); .withMessageContaining("values of [{/test}] and [{/enigma}]");
} }
@@ -1363,16 +1363,16 @@ class MergedAnnotationsTests {
@Test @Test
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
void streamRepeatableDeclaredOnClassWithAttributeAliases() { 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, Hierarchy.class);
RepeatableContainers containers = RepeatableContainers.of(TestConfiguration.class,
Hierarchy.class);
MergedAnnotations annotations = MergedAnnotations.from(HierarchyClass.class, MergedAnnotations annotations = MergedAnnotations.from(HierarchyClass.class,
SearchStrategy.DIRECT, containers, AnnotationFilter.NONE); SearchStrategy.DIRECT, containers, AnnotationFilter.NONE);
assertThat(annotations.stream(TestConfiguration.class).map( assertThat(annotations.stream(TestConfiguration.class)
annotation -> annotation.getString("location"))).containsExactly("A", "B"); .map(annotation -> annotation.getString("location")))
assertThat(annotations.stream(TestConfiguration.class).map( .containsExactly("A", "B");
annotation -> annotation.getString("value"))).containsExactly("A", "B"); assertThat(annotations.stream(TestConfiguration.class)
.map(annotation -> annotation.getString("value")))
.containsExactly("A", "B");
} }
@Test @Test
@@ -1435,14 +1435,12 @@ class MergedAnnotationsTests {
MyRepeatable[] annotations = searchStrategy == SearchStrategy.DIRECT ? MyRepeatable[] annotations = searchStrategy == SearchStrategy.DIRECT ?
element.getDeclaredAnnotationsByType(MyRepeatable.class) : element.getDeclaredAnnotationsByType(MyRepeatable.class) :
element.getAnnotationsByType(MyRepeatable.class); element.getAnnotationsByType(MyRepeatable.class);
assertThat(Arrays.stream(annotations).map(MyRepeatable::value)).containsExactly( assertThat(annotations).extracting(MyRepeatable::value).containsExactly(expected);
expected);
} }
private void testExplicitRepeatables(SearchStrategy searchStrategy, Class<?> element, String[] expected) { private void testExplicitRepeatables(SearchStrategy searchStrategy, Class<?> element, String[] expected) {
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);
Stream<String> values = annotations.stream(MyRepeatable.class) Stream<String> values = annotations.stream(MyRepeatable.class)
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex)) .filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
.map(annotation -> annotation.getString("value")); .map(annotation -> annotation.getString("value"));
@@ -1594,8 +1592,8 @@ class MergedAnnotationsTests {
AliasForWithMissingAttributeDeclarationClass.class.getAnnotation( AliasForWithMissingAttributeDeclarationClass.class.getAnnotation(
AliasForWithMissingAttributeDeclaration.class); AliasForWithMissingAttributeDeclaration.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(annotation)) .isThrownBy(() -> MergedAnnotation.from(annotation))
.withMessageStartingWith("@AliasFor declaration on attribute 'foo' in annotation") .withMessageStartingWith("@AliasFor declaration on attribute 'foo' in annotation")
.withMessageContaining(AliasForWithMissingAttributeDeclaration.class.getName()) .withMessageContaining(AliasForWithMissingAttributeDeclaration.class.getName())
.withMessageContaining("points to itself"); .withMessageContaining("points to itself");
@@ -1607,8 +1605,8 @@ class MergedAnnotationsTests {
AliasForWithDuplicateAttributeDeclarationClass.class.getAnnotation( AliasForWithDuplicateAttributeDeclarationClass.class.getAnnotation(
AliasForWithDuplicateAttributeDeclaration.class); AliasForWithDuplicateAttributeDeclaration.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(annotation)) .isThrownBy(() -> MergedAnnotation.from(annotation))
.withMessageStartingWith("In @AliasFor declared on attribute 'foo' in annotation") .withMessageStartingWith("In @AliasFor declared on attribute 'foo' in annotation")
.withMessageContaining(AliasForWithDuplicateAttributeDeclaration.class.getName()) .withMessageContaining(AliasForWithDuplicateAttributeDeclaration.class.getName())
.withMessageContaining("attribute 'attribute' and its alias 'value' are present with values of 'baz' and 'bar'"); .withMessageContaining("attribute 'attribute' and its alias 'value' are present with values of 'baz' and 'bar'");
@@ -1619,8 +1617,8 @@ class MergedAnnotationsTests {
AliasForNonexistentAttribute annotation = AliasForNonexistentAttributeClass.class.getAnnotation( AliasForNonexistentAttribute annotation = AliasForNonexistentAttributeClass.class.getAnnotation(
AliasForNonexistentAttribute.class); AliasForNonexistentAttribute.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(annotation)) .isThrownBy(() -> MergedAnnotation.from(annotation))
.withMessageStartingWith("@AliasFor declaration on attribute 'foo' in annotation") .withMessageStartingWith("@AliasFor declaration on attribute 'foo' in annotation")
.withMessageContaining(AliasForNonexistentAttribute.class.getName()) .withMessageContaining(AliasForNonexistentAttribute.class.getName())
.withMessageContaining("declares an alias for 'bar' which is not present"); .withMessageContaining("declares an alias for 'bar' which is not present");
@@ -1632,8 +1630,8 @@ class MergedAnnotationsTests {
AliasForWithMirroredAliasForWrongAttributeClass.class.getAnnotation( AliasForWithMirroredAliasForWrongAttributeClass.class.getAnnotation(
AliasForWithMirroredAliasForWrongAttribute.class); AliasForWithMirroredAliasForWrongAttribute.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(annotation)) .isThrownBy(() -> MergedAnnotation.from(annotation))
.withMessage("@AliasFor declaration on attribute 'bar' in annotation [" + .withMessage("@AliasFor declaration on attribute 'bar' in annotation [" +
AliasForWithMirroredAliasForWrongAttribute.class.getName() + AliasForWithMirroredAliasForWrongAttribute.class.getName() +
"] declares an alias for 'quux' which is not present."); "] declares an alias for 'quux' which is not present.");
@@ -1688,8 +1686,8 @@ class MergedAnnotationsTests {
AliasedComposedTestConfigurationNotMetaPresentClass.class.getAnnotation( AliasedComposedTestConfigurationNotMetaPresentClass.class.getAnnotation(
AliasedComposedTestConfigurationNotMetaPresent.class); AliasedComposedTestConfigurationNotMetaPresent.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(annotation)) .isThrownBy(() -> MergedAnnotation.from(annotation))
.withMessageStartingWith("@AliasFor declaration on attribute 'xmlConfigFile' in annotation") .withMessageStartingWith("@AliasFor declaration on attribute 'xmlConfigFile' in annotation")
.withMessageContaining(AliasedComposedTestConfigurationNotMetaPresent.class.getName()) .withMessageContaining(AliasedComposedTestConfigurationNotMetaPresent.class.getName())
.withMessageContaining("declares an alias for attribute 'location' in annotation") .withMessageContaining("declares an alias for attribute 'location' in annotation")
@@ -1781,8 +1779,8 @@ class MergedAnnotationsTests {
ImplicitAliasesWithMissingDefaultValuesTestConfiguration.class; ImplicitAliasesWithMissingDefaultValuesTestConfiguration.class;
ImplicitAliasesWithMissingDefaultValuesTestConfiguration config = clazz.getAnnotation(annotationType); ImplicitAliasesWithMissingDefaultValuesTestConfiguration config = clazz.getAnnotation(annotationType);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(clazz, config)) .isThrownBy(() -> MergedAnnotation.from(clazz, config))
.withMessageStartingWith("Misconfigured aliases:") .withMessageStartingWith("Misconfigured aliases:")
.withMessageContaining("attribute 'location1' in annotation [" + annotationType.getName() + "]") .withMessageContaining("attribute 'location1' in annotation [" + annotationType.getName() + "]")
.withMessageContaining("attribute 'location2' in annotation [" + annotationType.getName() + "]") .withMessageContaining("attribute 'location2' in annotation [" + annotationType.getName() + "]")
@@ -1796,8 +1794,8 @@ class MergedAnnotationsTests {
ImplicitAliasesWithDifferentDefaultValuesTestConfiguration.class; ImplicitAliasesWithDifferentDefaultValuesTestConfiguration.class;
ImplicitAliasesWithDifferentDefaultValuesTestConfiguration config = clazz.getAnnotation(annotationType); ImplicitAliasesWithDifferentDefaultValuesTestConfiguration config = clazz.getAnnotation(annotationType);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy( assertThatExceptionOfType(AnnotationConfigurationException.class)
() -> MergedAnnotation.from(clazz, config)) .isThrownBy(() -> MergedAnnotation.from(clazz, config))
.withMessageStartingWith("Misconfigured aliases:") .withMessageStartingWith("Misconfigured aliases:")
.withMessageContaining("attribute 'location1' in annotation [" + annotationType.getName() + "]") .withMessageContaining("attribute 'location1' in annotation [" + annotationType.getName() + "]")
.withMessageContaining("attribute 'location2' in annotation [" + annotationType.getName() + "]") .withMessageContaining("attribute 'location2' in annotation [" + annotationType.getName() + "]")
@@ -1893,8 +1891,8 @@ class MergedAnnotationsTests {
@Test @Test
void synthesizeWhenAttributeAliasesWithDifferentValues() { void synthesizeWhenAttributeAliasesWithDifferentValues() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() -> assertThatExceptionOfType(AnnotationConfigurationException.class)
MergedAnnotation.from(TestConfigurationMismatch.class.getAnnotation(TestConfiguration.class)).synthesize()); .isThrownBy(() -> MergedAnnotation.from(TestConfigurationMismatch.class.getAnnotation(TestConfiguration.class)));
} }
@Test @Test
@@ -1958,8 +1956,8 @@ class MergedAnnotationsTests {
} }
private void testMissingTextAttribute(Map<String, Object> attributes) { private void testMissingTextAttribute(Map<String, Object> attributes) {
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> assertThatExceptionOfType(NoSuchElementException.class)
MergedAnnotation.of(AnnotationWithoutDefaults.class, attributes).synthesize().text()) .isThrownBy(() -> MergedAnnotation.of(AnnotationWithoutDefaults.class, attributes).synthesize().text())
.withMessage("No value found for attribute named 'text' in merged annotation " + .withMessage("No value found for attribute named 'text' in merged annotation " +
AnnotationWithoutDefaults.class.getCanonicalName()); AnnotationWithoutDefaults.class.getCanonicalName());
} }
@@ -1968,7 +1966,8 @@ class MergedAnnotationsTests {
void synthesizeFromMapWithAttributeOfIncorrectType() { void synthesizeFromMapWithAttributeOfIncorrectType() {
Map<String, Object> map = Collections.singletonMap("value", 42L); Map<String, Object> map = Collections.singletonMap("value", 42L);
MergedAnnotation<Component> annotation = MergedAnnotation.of(Component.class, map); MergedAnnotation<Component> annotation = MergedAnnotation.of(Component.class, map);
assertThatIllegalStateException().isThrownBy(() -> annotation.synthesize().value()) assertThatIllegalStateException()
.isThrownBy(() -> annotation.synthesize().value())
.withMessage("Attribute 'value' in annotation " + .withMessage("Attribute 'value' in annotation " +
"org.springframework.core.testfixture.stereotype.Component should be " + "org.springframework.core.testfixture.stereotype.Component should be " +
"compatible with java.lang.String but a java.lang.Long value was returned"); "compatible with java.lang.String but a java.lang.Long value was returned");

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -161,9 +161,9 @@ class RepeatableContainersTests {
RepeatableContainers repeatableContainers = RepeatableContainers.standardRepeatables() RepeatableContainers repeatableContainers = RepeatableContainers.standardRepeatables()
.and(ExplicitContainer.class, ExplicitRepeatable.class); .and(ExplicitContainer.class, ExplicitRepeatable.class);
assertThat(findRepeatedAnnotationValues(repeatableContainers, StandardRepeatablesTestCase.class, StandardContainer.class)) assertThat(findRepeatedAnnotationValues(repeatableContainers, StandardRepeatablesTestCase.class, StandardContainer.class))
.containsExactly("a", "b"); .containsExactly("a", "b");
assertThat(findRepeatedAnnotationValues(repeatableContainers, ExplicitRepeatablesTestCase.class, ExplicitContainer.class)) assertThat(findRepeatedAnnotationValues(repeatableContainers, ExplicitRepeatablesTestCase.class, ExplicitContainer.class))
.containsExactly("a", "b"); .containsExactly("a", "b");
} }
@Test @Test