Ensure local aliases don't override meta-annotation

This commit introduces an additional test case to ensure that explicit
local attribute aliases (configured via @AliasFor) do not accidentally
override attributes of the same names in meta-annotations (i.e., by
convention).

Issue: SPR-13325
master
Sam Brannen 9 years ago
parent 4317e76e79
commit e20b47c3bc
  1. 35
      spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java

@ -530,6 +530,18 @@ public class AnnotatedElementUtilsTests {
assertEquals(asList("*Test", "*Tests"), patterns); assertEquals(asList("*Test", "*Tests"), patterns);
} }
@Test
public void findMergedAnnotationWithLocalAliasesThatConflictWithAttributesInMetaAnnotationByConvention() {
final String[] EMPTY = new String[] {};
Class<?> element = SpringAppConfigClass.class;
ContextConfig contextConfig = findMergedAnnotation(element, ContextConfig.class);
assertNotNull("Should find @ContextConfig on " + element, contextConfig);
assertArrayEquals("locations for " + element, EMPTY, contextConfig.locations());
// 'value' in @SpringAppConfig should not override 'value' in @ContextConfig
assertArrayEquals("value for " + element, EMPTY, contextConfig.value());
assertArrayEquals("classes for " + element, new Class<?>[] { Number.class }, contextConfig.classes());
}
private Set<String> names(Class<?>... classes) { private Set<String> names(Class<?>... classes) {
return stream(classes).map(Class::getName).collect(toSet()); return stream(classes).map(Class::getName).collect(toSet());
} }
@ -670,6 +682,8 @@ public class AnnotatedElementUtilsTests {
@AliasFor(attribute = "value") @AliasFor(attribute = "value")
String[] locations() default {}; String[] locations() default {};
Class<?>[] classes() default {};
} }
@ContextConfig @ContextConfig
@ -725,6 +739,23 @@ public class AnnotatedElementUtilsTests {
String[] xmlConfigFiles() default "default.xml"; String[] xmlConfigFiles() default "default.xml";
} }
/**
* Mock of {@code org.springframework.boot.test.SpringApplicationConfiguration}.
*/
@ContextConfig
@Retention(RetentionPolicy.RUNTIME)
@interface SpringAppConfig {
@AliasFor(annotation = ContextConfig.class, attribute = "locations")
String[] locations() default {};
@AliasFor("value")
Class<?>[] classes() default {};
@AliasFor("classes")
Class<?>[] value() default {};
}
/** /**
* Mock of {@code org.springframework.context.annotation.ComponentScan} * Mock of {@code org.springframework.context.annotation.ComponentScan}
*/ */
@ -909,4 +940,8 @@ public class AnnotatedElementUtilsTests {
static class TestComponentScanClass { static class TestComponentScanClass {
} }
@SpringAppConfig(Number.class)
static class SpringAppConfigClass {
}
} }

Loading…
Cancel
Save