From 1a227da19e6166126babf9d9f80385ac2e288041 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 9 Oct 2015 22:51:27 +0200 Subject: [PATCH] Polish AnnotatedElementUtilsTests --- .../AnnotatedElementUtilsTests.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) 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 a3552b62de1..34db68bb1cc 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 @@ -288,8 +288,8 @@ public class AnnotatedElementUtilsTests { AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); - assertArrayEquals("locations", new String[] { "explicitDeclaration" }, attributes.getStringArray("locations")); - assertArrayEquals("value", new String[] { "explicitDeclaration" }, attributes.getStringArray("value")); + assertArrayEquals("locations", asArray("explicitDeclaration"), attributes.getStringArray("locations")); + assertArrayEquals("value", asArray("explicitDeclaration"), attributes.getStringArray("value")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); @@ -305,7 +305,7 @@ public class AnnotatedElementUtilsTests { } private void getMergedAnnotationAttributesWithHalfConventionBasedAndHalfAliasedComposedAnnotation(Class clazz) { - String[] expected = new String[] { "explicitDeclaration" }; + String[] expected = asArray("explicitDeclaration"); String name = ContextConfig.class.getName(); String simpleName = clazz.getSimpleName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(clazz, name); @@ -325,8 +325,8 @@ public class AnnotatedElementUtilsTests { AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); - assertArrayEquals("value", new String[] { "test.xml" }, attributes.getStringArray("value")); - assertArrayEquals("locations", new String[] { "test.xml" }, attributes.getStringArray("locations")); + assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value")); + assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); @@ -339,8 +339,8 @@ public class AnnotatedElementUtilsTests { AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); - assertArrayEquals("locations", new String[] { "test.xml" }, attributes.getStringArray("locations")); - assertArrayEquals("value", new String[] { "test.xml" }, attributes.getStringArray("value")); + assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations")); + assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); @@ -351,7 +351,7 @@ public class AnnotatedElementUtilsTests { Class element = ComposedImplicitAliasesContextConfigClass.class; String name = ImplicitAliasesContextConfig.class.getName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); - String[] expected = new String[] { "A.xml", "B.xml" }; + String[] expected = asArray("A.xml", "B.xml"); assertNotNull("Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), attributes); assertArrayEquals("groovyScripts", expected, attributes.getStringArray("groovyScripts")); @@ -403,7 +403,7 @@ public class AnnotatedElementUtilsTests { Class element = ComposedImplicitAliasesContextConfigClass.class; String name = ImplicitAliasesContextConfig.class.getName(); ImplicitAliasesContextConfig config = getMergedAnnotation(element, ImplicitAliasesContextConfig.class); - String[] expected = new String[] { "A.xml", "B.xml" }; + String[] expected = asArray("A.xml", "B.xml"); assertNotNull("Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), config); assertArrayEquals("groovyScripts", expected, config.groovyScripts()); @@ -573,8 +573,8 @@ public class AnnotatedElementUtilsTests { @Test public void findMergedAnnotationForMultipleMetaAnnotationsWithClashingAttributeNames() { - final String[] xmlLocations = new String[] { "test.xml" }; - final String[] propFiles = new String[] { "test.properties" }; + final String[] xmlLocations = asArray("test.xml"); + final String[] propFiles = asArray("test.properties"); Class element = AliasedComposedContextConfigAndTestPropSourceClass.class; @@ -597,7 +597,7 @@ public class AnnotatedElementUtilsTests { @Test public void findMergedAnnotationAttributesOnClassWithAttributeAliasInComposedAnnotationAndNestedAnnotationsInTargetAnnotation() { - String[] expected = new String[] { "com.example.app.test" }; + String[] expected = asArray("com.example.app.test"); Class element = TestComponentScanClass.class; AnnotationAttributes attributes = findMergedAnnotationAttributes(element, ComponentScan.class); assertNotNull("Should find @ComponentScan on " + element, attributes); @@ -617,7 +617,7 @@ public class AnnotatedElementUtilsTests { */ @Test public void findMergedAnnotationAttributesOnClassWithBothAttributesOfAnAliasPairDeclared() { - String[] expected = new String[] { "com.example.app.test" }; + String[] expected = asArray("com.example.app.test"); Class element = ComponentScanWithBasePackagesAndValueAliasClass.class; AnnotationAttributes attributes = findMergedAnnotationAttributes(element, ComponentScan.class); @@ -638,12 +638,17 @@ public class AnnotatedElementUtilsTests { assertArrayEquals("classes for " + element, new Class[] { Number.class }, contextConfig.classes()); } + private Set names(Class... classes) { return stream(classes).map(Class::getName).collect(toSet()); } + @SafeVarargs + private static T[] asArray(T... arr) { + return arr; + } - static AnnotationAttributes findMergedAnnotationAttributes(AnnotatedElement element, Class annotationType) { + private static AnnotationAttributes findMergedAnnotationAttributes(AnnotatedElement element, Class annotationType) { Assert.notNull(annotationType, "annotationType must not be null"); return AnnotatedElementUtils.findMergedAnnotationAttributes(element, annotationType.getName(), false, false); }