Introduce getAliasedString() in AnnotationAttributes

Issue: SPR-11393
This commit is contained in:
Sam Brannen
2015-06-12 19:27:56 +02:00
parent 05de9a8c4a
commit 31c547456a
2 changed files with 153 additions and 48 deletions

View File

@@ -144,6 +144,63 @@ public class AnnotationAttributesTests {
attributes.getEnum("color");
}
@Test
public void getAliasedString() {
attributes.clear();
attributes.put("name", "metaverse");
assertEquals("metaverse", getAliasedString("name"));
assertEquals("metaverse", getAliasedString("value"));
attributes.clear();
attributes.put("value", "metaverse");
assertEquals("metaverse", getAliasedString("name"));
assertEquals("metaverse", getAliasedString("value"));
attributes.clear();
attributes.put("name", "metaverse");
attributes.put("value", "metaverse");
assertEquals("metaverse", getAliasedString("name"));
assertEquals("metaverse", getAliasedString("value"));
}
@Test
public void getAliasedStringFromSynthesizedAnnotationAttributes() {
Scope scope = ScopedComponent.class.getAnnotation(Scope.class);
AnnotationAttributes scopeAttributes = AnnotationUtils.getAnnotationAttributes(ScopedComponent.class, scope);
assertEquals("custom", getAliasedString(scopeAttributes, "name"));
assertEquals("custom", getAliasedString(scopeAttributes, "value"));
}
@Test
public void getAliasedStringWithMissingAliasedAttributes() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(equalTo("Neither attribute 'name' nor its alias 'value' was found in attributes for annotation [unknown]"));
getAliasedString("name");
}
@Test
public void getAliasedStringWithDifferentAliasedValues() {
attributes.put("name", "request");
attributes.put("value", "session");
exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(containsString("In annotation [" + Scope.class.getName() + "]"));
exception.expectMessage(containsString("attribute [name] and its alias [value]"));
exception.expectMessage(containsString("[request] and [session]"));
exception.expectMessage(containsString("but only one is permitted"));
getAliasedString("name");
}
private String getAliasedString(String attributeName) {
return getAliasedString(this.attributes, attributeName);
}
private String getAliasedString(AnnotationAttributes attrs, String attributeName) {
return attrs.getAliasedString(attributeName, Scope.class, null);
}
@Test
public void getAliasedStringArray() {
final String[] INPUT = new String[] { "test.xml" };
@@ -197,7 +254,7 @@ public class AnnotationAttributesTests {
attributes.put("value", new String[] { "2.xml" });
exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(containsString("In annotation [unknown]"));
exception.expectMessage(containsString("In annotation [" + ContextConfig.class.getName() + "]"));
exception.expectMessage(containsString("attribute [locations] and its alias [value]"));
exception.expectMessage(containsString("[{1.xml}] and [{2.xml}]"));
exception.expectMessage(containsString("but only one is permitted"));
@@ -262,7 +319,7 @@ public class AnnotationAttributesTests {
attributes.put("value", new Class[] { Number.class });
exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(containsString("In annotation [unknown]"));
exception.expectMessage(containsString("In annotation [" + Filter.class.getName() + "]"));
exception.expectMessage(containsString("attribute [classes] and its alias [value]"));
exception.expectMessage(containsString("[{class java.lang.String}] and [{class java.lang.Number}]"));
exception.expectMessage(containsString("but only one is permitted"));
@@ -308,4 +365,21 @@ public class AnnotationAttributesTests {
String locations() default "";
}
/**
* Mock of {@code org.springframework.context.annotation.Scope}.
*/
@Retention(RetentionPolicy.RUNTIME)
@interface Scope {
@AliasFor(attribute = "name")
String value() default "singleton";
@AliasFor(attribute = "value")
String name() default "singleton";
}
@Scope(name = "custom")
static class ScopedComponent {
}
}