parent
92c1751d35
commit
fcf75c90b1
|
@ -117,7 +117,7 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
|
||||||
else {
|
else {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
String.format("Attribute '%s' is of type [%s], but [%s] was expected.",
|
String.format("Attribute '%s' is of type [%s], but [%s] was expected.",
|
||||||
attributeName, value.getClass().getName(), expectedType.getName()));
|
attributeName, value.getClass().getSimpleName(), expectedType.getSimpleName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (T) value;
|
return (T) value;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2015 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.
|
||||||
|
@ -16,7 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.core.annotation;
|
package org.springframework.core.annotation;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
@ -25,84 +27,77 @@ import static org.junit.Assert.*;
|
||||||
* Unit tests for {@link AnnotationAttributes}.
|
* Unit tests for {@link AnnotationAttributes}.
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 3.1.1
|
* @since 3.1.1
|
||||||
*/
|
*/
|
||||||
public class AnnotationAttributesTests {
|
public class AnnotationAttributesTests {
|
||||||
|
|
||||||
enum Color { RED, WHITE, BLUE }
|
static enum Color {
|
||||||
|
RED, WHITE, BLUE
|
||||||
|
}
|
||||||
|
|
||||||
|
private final AnnotationAttributes attributes = new AnnotationAttributes();
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTypeSafeAttributeAccess() {
|
public void typeSafeAttributeAccess() {
|
||||||
AnnotationAttributes a = new AnnotationAttributes();
|
attributes.put("name", "dave");
|
||||||
a.put("name", "dave");
|
attributes.put("names", new String[] { "dave", "frank", "hal" });
|
||||||
a.put("names", new String[] { "dave", "frank", "hal" });
|
attributes.put("bool1", true);
|
||||||
a.put("bool1", true);
|
attributes.put("bool2", false);
|
||||||
a.put("bool2", false);
|
attributes.put("color", Color.RED);
|
||||||
a.put("color", Color.RED);
|
attributes.put("clazz", Integer.class);
|
||||||
a.put("clazz", Integer.class);
|
attributes.put("classes", new Class<?>[] { Number.class, Short.class, Integer.class });
|
||||||
a.put("classes", new Class<?>[] { Number.class, Short.class, Integer.class });
|
attributes.put("number", 42);
|
||||||
a.put("number", 42);
|
attributes.put("numbers", new int[] { 42, 43 });
|
||||||
a.put("numbers", new int[] { 42, 43 });
|
AnnotationAttributes nestedAttributes = new AnnotationAttributes();
|
||||||
AnnotationAttributes anno = new AnnotationAttributes();
|
nestedAttributes.put("value", 10);
|
||||||
anno.put("value", 10);
|
nestedAttributes.put("name", "algernon");
|
||||||
anno.put("name", "algernon");
|
attributes.put("anno", nestedAttributes);
|
||||||
a.put("anno", anno);
|
attributes.put("annoArray", new AnnotationAttributes[] { nestedAttributes });
|
||||||
a.put("annoArray", new AnnotationAttributes[] { anno });
|
|
||||||
|
|
||||||
assertThat(a.getString("name"), equalTo("dave"));
|
assertThat(attributes.getString("name"), equalTo("dave"));
|
||||||
assertThat(a.getStringArray("names"), equalTo(new String[] { "dave", "frank", "hal" }));
|
assertThat(attributes.getStringArray("names"), equalTo(new String[] { "dave", "frank", "hal" }));
|
||||||
assertThat(a.getBoolean("bool1"), equalTo(true));
|
assertThat(attributes.getBoolean("bool1"), equalTo(true));
|
||||||
assertThat(a.getBoolean("bool2"), equalTo(false));
|
assertThat(attributes.getBoolean("bool2"), equalTo(false));
|
||||||
assertThat(a.<Color>getEnum("color"), equalTo(Color.RED));
|
assertThat(attributes.<Color>getEnum("color"), equalTo(Color.RED));
|
||||||
assertTrue(a.getClass("clazz").equals(Integer.class));
|
assertTrue(attributes.getClass("clazz").equals(Integer.class));
|
||||||
assertThat(a.getClassArray("classes"), equalTo(new Class[] { Number.class, Short.class, Integer.class }));
|
assertThat(attributes.getClassArray("classes"), equalTo(new Class[] { Number.class, Short.class, Integer.class }));
|
||||||
assertThat(a.<Integer>getNumber("number"), equalTo(42));
|
assertThat(attributes.<Integer>getNumber("number"), equalTo(42));
|
||||||
assertThat(a.getAnnotation("anno").<Integer>getNumber("value"), equalTo(10));
|
assertThat(attributes.getAnnotation("anno").<Integer>getNumber("value"), equalTo(10));
|
||||||
assertThat(a.getAnnotationArray("annoArray")[0].getString("name"), equalTo("algernon"));
|
assertThat(attributes.getAnnotationArray("annoArray")[0].getString("name"), equalTo("algernon"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getEnum_emptyAttributeName() {
|
public void getEnumWithNullAttributeName() {
|
||||||
AnnotationAttributes a = new AnnotationAttributes();
|
exception.expect(IllegalArgumentException.class);
|
||||||
a.put("color", "RED");
|
exception.expectMessage(containsString("attributeName must not be null or empty"));
|
||||||
try {
|
attributes.getEnum(null);
|
||||||
a.getEnum("");
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
assertThat(ex.getMessage(), equalTo("attributeName must not be null or empty"));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
a.getEnum(null);
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
assertThat(ex.getMessage(), equalTo("attributeName must not be null or empty"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getEnum_notFound() {
|
public void getEnumWithEmptyAttributeName() {
|
||||||
AnnotationAttributes a = new AnnotationAttributes();
|
exception.expect(IllegalArgumentException.class);
|
||||||
a.put("color", "RED");
|
exception.expectMessage(containsString("attributeName must not be null or empty"));
|
||||||
try {
|
attributes.getEnum("");
|
||||||
a.getEnum("colour");
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
assertThat(ex.getMessage(), equalTo("Attribute 'colour' not found"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getEnum_typeMismatch() {
|
public void getEnumWithUnknownAttributeName() {
|
||||||
AnnotationAttributes a = new AnnotationAttributes();
|
exception.expect(IllegalArgumentException.class);
|
||||||
a.put("color", "RED");
|
exception.expectMessage(containsString("Attribute 'bogus' not found"));
|
||||||
try {
|
attributes.getEnum("bogus");
|
||||||
a.getEnum("color");
|
}
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
@Test
|
||||||
String expected =
|
public void getEnumWithTypeMismatch() {
|
||||||
"Attribute 'color' is of type [String], but [Enum] was expected";
|
attributes.put("color", "RED");
|
||||||
assertThat(ex.getMessage().substring(0, expected.length()), equalTo(expected));
|
exception.expect(IllegalArgumentException.class);
|
||||||
}
|
exception.expectMessage(containsString("Attribute 'color' is of type [String], but [Enum] was expected"));
|
||||||
|
attributes.getEnum("color");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue