Move convention-based attribute override tests to @Nested class

This commit is contained in:
Sam Brannen
2022-07-06 11:42:39 +02:00
parent 2af27d899f
commit 07cfcbc3a9

View File

@@ -38,6 +38,7 @@ import java.util.stream.Stream;
import javax.annotation.Resource;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.core.Ordered;
@@ -75,6 +76,70 @@ import static org.assertj.core.api.Assertions.entry;
*/
class MergedAnnotationsTests {
@Nested
class ConventionBasedAnnotationAttributeOverrideTests {
@Test
void getWithInheritedAnnotationsAttributesWithConventionBasedComposedAnnotation() {
MergedAnnotation<?> annotation =
MergedAnnotations.from(ConventionBasedComposedContextConfigurationClass.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
assertThat(annotation.isPresent()).isTrue();
assertThat(annotation.getStringArray("locations")).containsExactly("explicitDeclaration");
assertThat(annotation.getStringArray("value")).containsExactly("explicitDeclaration");
}
@Test
void getWithInheritedAnnotationsFromHalfConventionBasedAndHalfAliasedComposedAnnotation1() {
// SPR-13554: convention mapping mixed with AliasFor annotations
// xmlConfigFiles can be used because it has an AliasFor annotation
MergedAnnotation<?> annotation =
MergedAnnotations.from(HalfConventionBasedAndHalfAliasedComposedContextConfigurationClass1.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
assertThat(annotation.getStringArray("locations")).containsExactly("explicitDeclaration");
assertThat(annotation.getStringArray("value")).containsExactly("explicitDeclaration");
}
@Test
void withInheritedAnnotationsFromHalfConventionBasedAndHalfAliasedComposedAnnotation2() {
// SPR-13554: convention mapping mixed with AliasFor annotations
// locations doesn't apply because it has no AliasFor annotation
MergedAnnotation<?> annotation =
MergedAnnotations.from(HalfConventionBasedAndHalfAliasedComposedContextConfigurationClass2.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
assertThat(annotation.getStringArray("locations")).isEmpty();
assertThat(annotation.getStringArray("value")).isEmpty();
}
@Test
void getWithInheritedAnnotationsFromInvalidConventionBasedComposedAnnotation() {
assertThatExceptionOfType(AnnotationConfigurationException.class)
.isThrownBy(() -> MergedAnnotations.from(InvalidConventionBasedComposedContextConfigurationClass.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class));
}
@Test
void getWithTypeHierarchyWithSingleElementOverridingAnArrayViaConvention() {
testGetWithTypeHierarchy(ConventionBasedSinglePackageComponentScanClass.class, "com.example.app.test");
}
@Test
void getWithTypeHierarchyWithLocalAliasesThatConflictWithAttributesInMetaAnnotationByConvention() {
MergedAnnotation<?> annotation =
MergedAnnotations.from(SpringApplicationConfigurationClass.class, SearchStrategy.TYPE_HIERARCHY)
.get(ContextConfiguration.class);
assertThat(annotation.getStringArray("locations")).isEmpty();
assertThat(annotation.getStringArray("value")).isEmpty();
assertThat(annotation.getClassArray("classes")).containsExactly(Number.class);
}
@Test
void getWithTypeHierarchyOnMethodWithSingleElementOverridingAnArrayViaConvention() throws Exception {
testGetWithTypeHierarchyWebMapping(WebController.class.getMethod("postMappedWithPathAttribute"));
}
}
@Test
void fromPreconditions() {
SearchStrategy strategy = SearchStrategy.DIRECT;
@@ -347,41 +412,7 @@ class MergedAnnotationsTests {
assertThat(annotation.isPresent()).isTrue();
}
@Test
void getWithInheritedAnnotationsAttributesWithConventionBasedComposedAnnotation() {
MergedAnnotation<?> annotation = MergedAnnotations.from(
ConventionBasedComposedContextConfigurationClass.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
assertThat(annotation.isPresent()).isTrue();
assertThat(annotation.getStringArray("locations")).containsExactly(
"explicitDeclaration");
assertThat(annotation.getStringArray("value")).containsExactly(
"explicitDeclaration");
}
@Test
void getWithInheritedAnnotationsFromHalfConventionBasedAndHalfAliasedComposedAnnotation1() {
// SPR-13554: convention mapping mixed with AliasFor annotations
// xmlConfigFiles can be used because it has an AliasFor annotation
MergedAnnotation<?> annotation = MergedAnnotations.from(
HalfConventionBasedAndHalfAliasedComposedContextConfigurationClass1.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
assertThat(annotation.getStringArray("locations")).containsExactly(
"explicitDeclaration");
assertThat(annotation.getStringArray("value")).containsExactly(
"explicitDeclaration");
}
@Test
void withInheritedAnnotationsFromHalfConventionBasedAndHalfAliasedComposedAnnotation2() {
// SPR-13554: convention mapping mixed with AliasFor annotations
// locations doesn't apply because it has no AliasFor annotation
MergedAnnotation<?> annotation = MergedAnnotations.from(
HalfConventionBasedAndHalfAliasedComposedContextConfigurationClass2.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class);
assertThat(annotation.getStringArray("locations")).isEmpty();
assertThat(annotation.getStringArray("value")).isEmpty();
}
@Test
void withInheritedAnnotationsFromAliasedComposedAnnotation() {
@@ -459,13 +490,6 @@ class MergedAnnotationsTests {
assertThat(annotation.getClassArray("classes")).isEmpty();
}
@Test
void getWithInheritedAnnotationsFromInvalidConventionBasedComposedAnnotation() {
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
MergedAnnotations.from(InvalidConventionBasedComposedContextConfigurationClass.class,
SearchStrategy.INHERITED_ANNOTATIONS).get(ContextConfiguration.class));
}
@Test
void getWithInheritedAnnotationsFromShadowedAliasComposedAnnotation() {
MergedAnnotation<?> annotation = MergedAnnotations.from(
@@ -628,11 +652,6 @@ class MergedAnnotationsTests {
testGetWithTypeHierarchy(ComponentScanWithBasePackagesAndValueAliasClass.class, "com.example.app.test");
}
@Test
void getWithTypeHierarchyWithSingleElementOverridingAnArrayViaConvention() {
testGetWithTypeHierarchy(ConventionBasedSinglePackageComponentScanClass.class, "com.example.app.test");
}
@Test
void getWithTypeHierarchyWithSingleElementOverridingAnArrayViaAliasFor() {
testGetWithTypeHierarchy(AliasForBasedSinglePackageComponentScanClass.class, "com.example.app.test");
@@ -661,22 +680,6 @@ class MergedAnnotationsTests {
"test.properties");
}
@Test
void getWithTypeHierarchyWithLocalAliasesThatConflictWithAttributesInMetaAnnotationByConvention() {
MergedAnnotation<?> annotation = MergedAnnotations.from(
SpringApplicationConfigurationClass.class, SearchStrategy.TYPE_HIERARCHY).get(
ContextConfiguration.class);
assertThat(annotation.getStringArray("locations")).isEmpty();
assertThat(annotation.getStringArray("value")).isEmpty();
assertThat(annotation.getClassArray("classes")).containsExactly(Number.class);
}
@Test
void getWithTypeHierarchyOnMethodWithSingleElementOverridingAnArrayViaConvention() throws Exception {
testGetWithTypeHierarchyWebMapping(
WebController.class.getMethod("postMappedWithPathAttribute"));
}
@Test
void getWithTypeHierarchyOnMethodWithSingleElementOverridingAnArrayViaAliasFor() throws Exception {
testGetWithTypeHierarchyWebMapping(