Skip to content

Commit 1a227da

Browse files
committed
Polish AnnotatedElementUtilsTests
1 parent 5e1d6e4 commit 1a227da

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ public void getMergedAnnotationAttributesWithConventionBasedComposedAnnotation()
288288
AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
289289

290290
assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes);
291-
assertArrayEquals("locations", new String[] { "explicitDeclaration" }, attributes.getStringArray("locations"));
292-
assertArrayEquals("value", new String[] { "explicitDeclaration" }, attributes.getStringArray("value"));
291+
assertArrayEquals("locations", asArray("explicitDeclaration"), attributes.getStringArray("locations"));
292+
assertArrayEquals("value", asArray("explicitDeclaration"), attributes.getStringArray("value"));
293293

294294
// Verify contracts between utility methods:
295295
assertTrue(isAnnotated(element, name));
@@ -305,7 +305,7 @@ public void getMergedAnnotationAttributesWithHalfConventionBasedAndHalfAliasedCo
305305
}
306306

307307
private void getMergedAnnotationAttributesWithHalfConventionBasedAndHalfAliasedComposedAnnotation(Class<?> clazz) {
308-
String[] expected = new String[] { "explicitDeclaration" };
308+
String[] expected = asArray("explicitDeclaration");
309309
String name = ContextConfig.class.getName();
310310
String simpleName = clazz.getSimpleName();
311311
AnnotationAttributes attributes = getMergedAnnotationAttributes(clazz, name);
@@ -325,8 +325,8 @@ public void getMergedAnnotationAttributesWithAliasedComposedAnnotation() {
325325
AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
326326

327327
assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes);
328-
assertArrayEquals("value", new String[] { "test.xml" }, attributes.getStringArray("value"));
329-
assertArrayEquals("locations", new String[] { "test.xml" }, attributes.getStringArray("locations"));
328+
assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value"));
329+
assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations"));
330330

331331
// Verify contracts between utility methods:
332332
assertTrue(isAnnotated(element, name));
@@ -339,8 +339,8 @@ public void getMergedAnnotationAttributesWithAliasedValueComposedAnnotation() {
339339
AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
340340

341341
assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes);
342-
assertArrayEquals("locations", new String[] { "test.xml" }, attributes.getStringArray("locations"));
343-
assertArrayEquals("value", new String[] { "test.xml" }, attributes.getStringArray("value"));
342+
assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations"));
343+
assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value"));
344344

345345
// Verify contracts between utility methods:
346346
assertTrue(isAnnotated(element, name));
@@ -351,7 +351,7 @@ public void getMergedAnnotationAttributesWithImplicitAliasesInMetaAnnotationOnCo
351351
Class<?> element = ComposedImplicitAliasesContextConfigClass.class;
352352
String name = ImplicitAliasesContextConfig.class.getName();
353353
AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name);
354-
String[] expected = new String[] { "A.xml", "B.xml" };
354+
String[] expected = asArray("A.xml", "B.xml");
355355

356356
assertNotNull("Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), attributes);
357357
assertArrayEquals("groovyScripts", expected, attributes.getStringArray("groovyScripts"));
@@ -403,7 +403,7 @@ public void getMergedAnnotationWithImplicitAliasesInMetaAnnotationOnComposedAnno
403403
Class<?> element = ComposedImplicitAliasesContextConfigClass.class;
404404
String name = ImplicitAliasesContextConfig.class.getName();
405405
ImplicitAliasesContextConfig config = getMergedAnnotation(element, ImplicitAliasesContextConfig.class);
406-
String[] expected = new String[] { "A.xml", "B.xml" };
406+
String[] expected = asArray("A.xml", "B.xml");
407407

408408
assertNotNull("Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), config);
409409
assertArrayEquals("groovyScripts", expected, config.groovyScripts());
@@ -573,8 +573,8 @@ public void findMergedAnnotationWithAttributeAliasesInTargetAnnotation() {
573573

574574
@Test
575575
public void findMergedAnnotationForMultipleMetaAnnotationsWithClashingAttributeNames() {
576-
final String[] xmlLocations = new String[] { "test.xml" };
577-
final String[] propFiles = new String[] { "test.properties" };
576+
final String[] xmlLocations = asArray("test.xml");
577+
final String[] propFiles = asArray("test.properties");
578578

579579
Class<?> element = AliasedComposedContextConfigAndTestPropSourceClass.class;
580580

@@ -597,7 +597,7 @@ public void findMergedAnnotationForMultipleMetaAnnotationsWithClashingAttributeN
597597

598598
@Test
599599
public void findMergedAnnotationAttributesOnClassWithAttributeAliasInComposedAnnotationAndNestedAnnotationsInTargetAnnotation() {
600-
String[] expected = new String[] { "com.example.app.test" };
600+
String[] expected = asArray("com.example.app.test");
601601
Class<?> element = TestComponentScanClass.class;
602602
AnnotationAttributes attributes = findMergedAnnotationAttributes(element, ComponentScan.class);
603603
assertNotNull("Should find @ComponentScan on " + element, attributes);
@@ -617,7 +617,7 @@ public void findMergedAnnotationAttributesOnClassWithAttributeAliasInComposedAnn
617617
*/
618618
@Test
619619
public void findMergedAnnotationAttributesOnClassWithBothAttributesOfAnAliasPairDeclared() {
620-
String[] expected = new String[] { "com.example.app.test" };
620+
String[] expected = asArray("com.example.app.test");
621621
Class<?> element = ComponentScanWithBasePackagesAndValueAliasClass.class;
622622
AnnotationAttributes attributes = findMergedAnnotationAttributes(element, ComponentScan.class);
623623

@@ -638,12 +638,17 @@ public void findMergedAnnotationWithLocalAliasesThatConflictWithAttributesInMeta
638638
assertArrayEquals("classes for " + element, new Class<?>[] { Number.class }, contextConfig.classes());
639639
}
640640

641+
641642
private Set<String> names(Class<?>... classes) {
642643
return stream(classes).map(Class::getName).collect(toSet());
643644
}
644645

646+
@SafeVarargs
647+
private static <T> T[] asArray(T... arr) {
648+
return arr;
649+
}
645650

646-
static AnnotationAttributes findMergedAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType) {
651+
private static AnnotationAttributes findMergedAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType) {
647652
Assert.notNull(annotationType, "annotationType must not be null");
648653
return AnnotatedElementUtils.findMergedAnnotationAttributes(element, annotationType.getName(), false, false);
649654
}

0 commit comments

Comments
 (0)