From e20b47c3bc424ac4fd30dd101e730c280681c3f1 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 12 Aug 2015 14:38:01 +0200 Subject: [PATCH] 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 --- .../AnnotatedElementUtilsTests.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java index 958173b365..069633888b 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java @@ -530,6 +530,18 @@ public class AnnotatedElementUtilsTests { 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 names(Class... classes) { return stream(classes).map(Class::getName).collect(toSet()); } @@ -670,6 +682,8 @@ public class AnnotatedElementUtilsTests { @AliasFor(attribute = "value") String[] locations() default {}; + + Class[] classes() default {}; } @ContextConfig @@ -725,6 +739,23 @@ public class AnnotatedElementUtilsTests { 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} */ @@ -909,4 +940,8 @@ public class AnnotatedElementUtilsTests { static class TestComponentScanClass { } + @SpringAppConfig(Number.class) + static class SpringAppConfigClass { + } + }