Browse Source

Polish AnnotatedElementUtils[Tests]

Issue: SPR-11514
pull/788/merge
Sam Brannen 11 years ago
parent
commit
6db8f24aaf
  1. 5
      spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java
  2. 55
      spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java

5
spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java

@ -226,6 +226,8 @@ public class AnnotatedElementUtils {
* @return the merged {@code AnnotationAttributes}, or {@code null} if * @return the merged {@code AnnotationAttributes}, or {@code null} if
* not found * not found
* @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean) * @see #getAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
* @see #findAnnotationAttributes(AnnotatedElement, Class)
* @see #findAnnotationAttributes(AnnotatedElement, String)
*/ */
public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element, String annotationType) { public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element, String annotationType) {
return getAnnotationAttributes(element, annotationType, false, false); return getAnnotationAttributes(element, annotationType, false, false);
@ -249,6 +251,9 @@ public class AnnotatedElementUtils {
* as Annotation instances * as Annotation instances
* @return the merged {@code AnnotationAttributes}, or {@code null} if * @return the merged {@code AnnotationAttributes}, or {@code null} if
* not found * not found
* @see #findAnnotationAttributes(AnnotatedElement, Class)
* @see #findAnnotationAttributes(AnnotatedElement, String)
* @see #findAnnotationAttributes(AnnotatedElement, String, boolean, boolean)
*/ */
public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element, String annotationType, public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement element, String annotationType,
boolean classValuesAsString, boolean nestedAnnotationsAsMap) { boolean classValuesAsString, boolean nestedAnnotationsAsMap) {

55
spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java

@ -170,16 +170,24 @@ public class AnnotatedElementUtilsTests {
@Test @Test
public void getAnnotationAttributesOnClassWithLocalAnnotation() { public void getAnnotationAttributesOnClassWithLocalAnnotation() {
AnnotationAttributes attributes = getAnnotationAttributes(TxConfig.class, Transactional.class.getName()); Class<?> element = TxConfig.class;
String name = Transactional.class.getName();
AnnotationAttributes attributes = getAnnotationAttributes(element, name);
assertNotNull("Annotation attributes for @Transactional on TxConfig", attributes); assertNotNull("Annotation attributes for @Transactional on TxConfig", attributes);
assertEquals("value for TxConfig.", "TxConfig", attributes.getString("value")); assertEquals("value for TxConfig.", "TxConfig", attributes.getString("value"));
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
} }
@Test @Test
public void getAnnotationAttributesOnClassWithLocalAnnotationThatShadowsAnnotationFromSuperclass() { public void getAnnotationAttributesOnClassWithLocalAnnotationThatShadowsAnnotationFromSuperclass() {
AnnotationAttributes attributes = getAnnotationAttributes(DerivedTxConfig.class, Transactional.class.getName()); Class<?> element = DerivedTxConfig.class;
String name = Transactional.class.getName();
AnnotationAttributes attributes = getAnnotationAttributes(element, name);
assertNotNull("Annotation attributes for @Transactional on DerivedTxConfig", attributes); assertNotNull("Annotation attributes for @Transactional on DerivedTxConfig", attributes);
assertEquals("value for DerivedTxConfig.", "DerivedTxConfig", attributes.getString("value")); assertEquals("value for DerivedTxConfig.", "DerivedTxConfig", attributes.getString("value"));
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
} }
@Test @Test
@ -191,9 +199,12 @@ public class AnnotatedElementUtilsTests {
@Test @Test
public void getAnnotationAttributesFavorsInheritedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() { public void getAnnotationAttributesFavorsInheritedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() {
AnnotationAttributes attributes = getAnnotationAttributes(SubSubClassWithInheritedAnnotation.class, Class<?> element = SubSubClassWithInheritedAnnotation.class;
Transactional.class.getName()); String name = Transactional.class.getName();
AnnotationAttributes attributes = getAnnotationAttributes(element, name);
assertNotNull("AnnotationAttributes for @Transactional on SubSubClassWithInheritedAnnotation", attributes); assertNotNull("AnnotationAttributes for @Transactional on SubSubClassWithInheritedAnnotation", attributes);
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
// TODO [SPR-11598] Set expected to true. // TODO [SPR-11598] Set expected to true.
boolean expected = false; boolean expected = false;
@ -202,10 +213,12 @@ public class AnnotatedElementUtilsTests {
@Test @Test
public void getAnnotationAttributesFavorsInheritedComposedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() { public void getAnnotationAttributesFavorsInheritedComposedAnnotationsOverMoreLocallyDeclaredComposedAnnotations() {
AnnotationAttributes attributes = getAnnotationAttributes(SubSubClassWithInheritedComposedAnnotation.class, Class<?> element = SubSubClassWithInheritedComposedAnnotation.class;
Transactional.class.getName()); String name = Transactional.class.getName();
assertNotNull("AnnotationAttributtes for @Transactional on SubSubClassWithInheritedComposedAnnotation.", AnnotationAttributes attributes = getAnnotationAttributes(element, name);
attributes); assertNotNull("AnnotationAttributtes for @Transactional on SubSubClassWithInheritedComposedAnnotation.", attributes);
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
// TODO [SPR-11598] Set expected to true. // TODO [SPR-11598] Set expected to true.
boolean expected = false; boolean expected = false;
@ -217,16 +230,32 @@ public class AnnotatedElementUtilsTests {
@Ignore("Disabled until SPR-11598 is resolved") @Ignore("Disabled until SPR-11598 is resolved")
@Test @Test
public void getAnnotationAttributesFromInterfaceImplementedBySuperclass() { public void getAnnotationAttributesFromInterfaceImplementedBySuperclass() {
Class<?> element = ConcreteClassWithInheritedAnnotation.class;
String name = Transactional.class.getName(); String name = Transactional.class.getName();
AnnotationAttributes attributes = getAnnotationAttributes(ConcreteClassWithInheritedAnnotation.class, name); AnnotationAttributes attributes = getAnnotationAttributes(element, name);
assertNotNull("Should find @Transactional on ConcreteClassWithInheritedAnnotation", attributes); assertNotNull("Should find @Transactional on ConcreteClassWithInheritedAnnotation", attributes);
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
} }
@Test @Test
public void getAnnotationAttributesOnInheritedAnnotationInterface() { public void getAnnotationAttributesOnInheritedAnnotationInterface() {
Class<?> element = InheritedAnnotationInterface.class;
String name = Transactional.class.getName(); String name = Transactional.class.getName();
AnnotationAttributes attributes = getAnnotationAttributes(InheritedAnnotationInterface.class, name); AnnotationAttributes attributes = getAnnotationAttributes(element, name);
assertNotNull("Should get @Transactional on InheritedAnnotationInterface", attributes); assertNotNull("Should get @Transactional on InheritedAnnotationInterface", attributes);
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
}
@Test
public void getAnnotationAttributesOnNonInheritedAnnotationInterface() {
Class<?> element = NonInheritedAnnotationInterface.class;
String name = Order.class.getName();
AnnotationAttributes attributes = getAnnotationAttributes(element, name);
assertNotNull("Should get @Order on NonInheritedAnnotationInterface", attributes);
// Verify contracts between utility methods:
assertTrue(isAnnotated(element, name));
} }
@Test @Test
@ -253,12 +282,6 @@ public class AnnotatedElementUtilsTests {
assertNotNull("Should find @Order on NonInheritedAnnotationInterface", attributes); assertNotNull("Should find @Order on NonInheritedAnnotationInterface", attributes);
} }
@Test
public void getAnnotationAttributesOnNonInheritedAnnotationInterface() {
AnnotationAttributes attributes = getAnnotationAttributes(NonInheritedAnnotationInterface.class, Order.class.getName());
assertNotNull("Should get @Order on NonInheritedAnnotationInterface", attributes);
}
@Test @Test
public void findAnnotationAttributesOnSubNonInheritedAnnotationInterface() { public void findAnnotationAttributesOnSubNonInheritedAnnotationInterface() {
AnnotationAttributes attributes = findAnnotationAttributes(SubNonInheritedAnnotationInterface.class, Order.class); AnnotationAttributes attributes = findAnnotationAttributes(SubNonInheritedAnnotationInterface.class, Order.class);

Loading…
Cancel
Save