Polishing (includes varargs for selected setters)

This commit is contained in:
Juergen Hoeller
2014-08-11 22:12:26 +02:00
parent 6639320e8e
commit 36918d6bb7
44 changed files with 482 additions and 490 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,15 +23,15 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* {@code ContextConfigurationAttributes} encapsulates the context
* configuration attributes declared on a test class via
* {@link ContextConfiguration @ContextConfiguration}.
* {@code ContextConfigurationAttributes} encapsulates the context configuration
* attributes declared via {@link ContextConfiguration @ContextConfiguration}.
*
* @author Sam Brannen
* @since 3.1
@@ -45,13 +45,11 @@ public class ContextConfigurationAttributes {
private final Class<?> declaringClass;
private String[] locations;
private Class<?>[] classes;
private final boolean inheritLocations;
private String[] locations;
private final Class<? extends ContextLoader> contextLoaderClass;
private final boolean inheritLocations;
private final Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers;
@@ -59,34 +57,8 @@ public class ContextConfigurationAttributes {
private final String name;
private final Class<? extends ContextLoader> contextLoaderClass;
/**
* Resolve resource locations from the {@link ContextConfiguration#locations() locations}
* and {@link ContextConfiguration#value() value} attributes of the supplied
* {@link ContextConfiguration} annotation.
*
* @throws IllegalStateException if both the locations and value attributes have been declared
*/
private static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
Assert.notNull(declaringClass, "declaringClass must not be null");
String[] locations = contextConfiguration.locations();
String[] valueLocations = contextConfiguration.value();
if (!ObjectUtils.isEmpty(valueLocations) && !ObjectUtils.isEmpty(locations)) {
String msg = String.format("Test class [%s] has been configured with @ContextConfiguration's 'value' %s "
+ "and 'locations' %s attributes. Only one declaration of resource "
+ "locations is permitted per @ContextConfiguration annotation.", declaringClass.getName(),
ObjectUtils.nullSafeToString(valueLocations), ObjectUtils.nullSafeToString(locations));
logger.error(msg);
throw new IllegalStateException(msg);
}
else if (!ObjectUtils.isEmpty(valueLocations)) {
locations = valueLocations;
}
return locations;
}
/**
* Construct a new {@link ContextConfigurationAttributes} instance for the
@@ -97,8 +69,26 @@ public class ContextConfigurationAttributes {
*/
public ContextConfigurationAttributes(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
this(declaringClass, resolveLocations(declaringClass, contextConfiguration), contextConfiguration.classes(),
contextConfiguration.inheritLocations(), contextConfiguration.initializers(),
contextConfiguration.inheritInitializers(), contextConfiguration.name(), contextConfiguration.loader());
contextConfiguration.inheritLocations(), contextConfiguration.initializers(),
contextConfiguration.inheritInitializers(), contextConfiguration.name(), contextConfiguration.loader());
}
/**
* Construct a new {@link ContextConfigurationAttributes} instance for the
* supplied {@link AnnotationAttributes} (parsed from a
* {@link ContextConfiguration @ContextConfiguration} annotation) and
* the {@linkplain Class test class} that declared them.
* @param declaringClass the test class that declared {@code @ContextConfiguration}
* @param annAttrs the annotation attributes from which to retrieve the attributes
*/
@SuppressWarnings("unchecked")
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
this(declaringClass,
resolveLocations(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getStringArray("value")),
annAttrs.getClassArray("classes"), annAttrs.getBoolean("inheritLocations"),
(Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[]) annAttrs.getClassArray("initializers"),
annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"),
(Class<? extends ContextLoader>) annAttrs.getClass("loader"));
}
/**
@@ -106,14 +96,13 @@ public class ContextConfigurationAttributes {
* {@linkplain Class test class} that declared the
* {@link ContextConfiguration @ContextConfiguration} annotation and its
* corresponding attributes.
*
* @param declaringClass the test class that declared {@code @ContextConfiguration}
* @param locations the resource locations declared via {@code @ContextConfiguration}
* @param classes the annotated classes declared via {@code @ContextConfiguration}
* @param inheritLocations the {@code inheritLocations} flag declared via {@code @ContextConfiguration}
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
* {@code null}, or if the {@code locations} and {@code classes} are both non-empty
* {@code null}
* @deprecated as of Spring 3.2, use
* {@link #ContextConfigurationAttributes(Class, String[], Class[], boolean, Class[], boolean, String, Class)}
* instead
@@ -121,6 +110,7 @@ public class ContextConfigurationAttributes {
@Deprecated
public ContextConfigurationAttributes(Class<?> declaringClass, String[] locations, Class<?>[] classes,
boolean inheritLocations, Class<? extends ContextLoader> contextLoaderClass) {
this(declaringClass, locations, classes, inheritLocations, null, true, null, contextLoaderClass);
}
@@ -129,7 +119,6 @@ public class ContextConfigurationAttributes {
* {@linkplain Class test class} that declared the
* {@link ContextConfiguration @ContextConfiguration} annotation and its
* corresponding attributes.
*
* @param declaringClass the test class that declared {@code @ContextConfiguration}
* @param locations the resource locations declared via {@code @ContextConfiguration}
* @param classes the annotated classes declared via {@code @ContextConfiguration}
@@ -138,14 +127,15 @@ public class ContextConfigurationAttributes {
* @param inheritInitializers the {@code inheritInitializers} flag declared via {@code @ContextConfiguration}
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
* {@code null}, or if the {@code locations} and {@code classes} are both non-empty
* {@code null}
*/
public ContextConfigurationAttributes(Class<?> declaringClass, String[] locations, Class<?>[] classes,
boolean inheritLocations,
public ContextConfigurationAttributes(
Class<?> declaringClass, String[] locations, Class<?>[] classes, boolean inheritLocations,
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers,
boolean inheritInitializers, Class<? extends ContextLoader> contextLoaderClass) {
this(declaringClass, locations, classes, inheritLocations, initializers, inheritInitializers, null,
contextLoaderClass);
contextLoaderClass);
}
/**
@@ -153,7 +143,6 @@ public class ContextConfigurationAttributes {
* {@linkplain Class test class} that declared the
* {@link ContextConfiguration @ContextConfiguration} annotation and its
* corresponding attributes.
*
* @param declaringClass the test class that declared {@code @ContextConfiguration}
* @param locations the resource locations declared via {@code @ContextConfiguration}
* @param classes the annotated classes declared via {@code @ContextConfiguration}
@@ -163,24 +152,23 @@ public class ContextConfigurationAttributes {
* @param name the name of level in the context hierarchy, or {@code null} if not applicable
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
* {@code null}, or if the {@code locations} and {@code classes} are both non-empty
* {@code null}
*/
public ContextConfigurationAttributes(Class<?> declaringClass, String[] locations, Class<?>[] classes,
boolean inheritLocations,
public ContextConfigurationAttributes(
Class<?> declaringClass, String[] locations, Class<?>[] classes, boolean inheritLocations,
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers,
boolean inheritInitializers, String name, Class<? extends ContextLoader> contextLoaderClass) {
Assert.notNull(declaringClass, "declaringClass must not be null");
Assert.notNull(contextLoaderClass, "contextLoaderClass must not be null");
if (!ObjectUtils.isEmpty(locations) && !ObjectUtils.isEmpty(classes)) {
String msg = String.format(
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') %s "
+ "and 'classes' %s attributes. Only one declaration of resources "
+ "is permitted per @ContextConfiguration annotation.", declaringClass.getName(),
ObjectUtils.nullSafeToString(locations), ObjectUtils.nullSafeToString(classes));
logger.error(msg);
throw new IllegalArgumentException(msg);
if (!ObjectUtils.isEmpty(locations) && !ObjectUtils.isEmpty(classes) && logger.isDebugEnabled()) {
logger.debug(String.format(
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') %s " +
"and 'classes' %s attributes. Most SmartContextLoader implementations support " +
"only one declaration of resources per @ContextConfiguration annotation.",
declaringClass.getName(), ObjectUtils.nullSafeToString(locations),
ObjectUtils.nullSafeToString(classes)));
}
this.declaringClass = declaringClass;
@@ -189,77 +177,112 @@ public class ContextConfigurationAttributes {
this.inheritLocations = inheritLocations;
this.initializers = initializers;
this.inheritInitializers = inheritInitializers;
this.name = StringUtils.hasText(name) ? name : null;
this.name = (StringUtils.hasText(name) ? name : null);
this.contextLoaderClass = contextLoaderClass;
}
/**
* Resolve resource locations from the {@link ContextConfiguration#locations() locations}
* and {@link ContextConfiguration#value() value} attributes of the supplied
* {@link ContextConfiguration} annotation.
* @throws IllegalStateException if both the locations and value attributes have been declared
*/
private static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
return resolveLocations(declaringClass, contextConfiguration.locations(), contextConfiguration.value());
}
/**
* Resolve resource locations from the supplied {@code locations} and
* {@code value} arrays, which correspond to attributes of the same names in
* the {@link ContextConfiguration} annotation.
* @throws IllegalStateException if both the locations and value attributes have been declared
*/
private static String[] resolveLocations(Class<?> declaringClass, String[] locations, String[] value) {
Assert.notNull(declaringClass, "declaringClass must not be null");
if (!ObjectUtils.isEmpty(value) && !ObjectUtils.isEmpty(locations)) {
throw new IllegalStateException(String.format("Test class [%s] has been configured with " +
"@ContextConfiguration's 'value' %s and 'locations' %s attributes. Only one declaration " +
"of resource locations is permitted per @ContextConfiguration annotation.",
declaringClass.getName(), ObjectUtils.nullSafeToString(value), ObjectUtils.nullSafeToString(locations)));
}
else if (!ObjectUtils.isEmpty(value)) {
locations = value;
}
return locations;
}
/**
* Get the {@linkplain Class class} that declared the
* {@link ContextConfiguration @ContextConfiguration} annotation.
*
* @return the declaring class; never {@code null}
* @return the declaring class (never {@code null})
*/
public Class<?> getDeclaringClass() {
return declaringClass;
return this.declaringClass;
}
/**
* Set the <em>processed</em> annotated classes, effectively overriding the
* original value declared via {@link ContextConfiguration @ContextConfiguration}.
* @see #getClasses()
*/
public void setClasses(Class<?>... classes) {
this.classes = classes;
}
/**
* Get the annotated classes that were declared via
* {@link ContextConfiguration @ContextConfiguration}.
* <p>Note: this is a mutable property. The returned value may therefore
* represent a <em>processed</em> value that does not match the original value
* declared via {@link ContextConfiguration @ContextConfiguration}.
* @return the annotated classes; potentially {@code null} or <em>empty</em>
* @see ContextConfiguration#classes
* @see #setClasses(Class[])
*/
public Class<?>[] getClasses() {
return this.classes;
}
/**
* Determine if this {@code ContextConfigurationAttributes} instance has
* class-based resources.
* @return {@code true} if the {@link #getClasses() classes} array is not empty
* @see #hasResources()
* @see #hasLocations()
*/
public boolean hasClasses() {
return !ObjectUtils.isEmpty(getClasses());
}
/**
* Set the <em>processed</em> resource locations, effectively overriding the
* original value declared via {@link ContextConfiguration @ContextConfiguration}.
* @see #getLocations()
*/
public void setLocations(String... locations) {
this.locations = locations;
}
/**
* Get the resource locations that were declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* <p>Note: this is a mutable property. The returned value may therefore
* represent a <em>processed</em> value that does not match the original value
* declared via {@link ContextConfiguration @ContextConfiguration}.
*
* @return the resource locations; potentially {@code null} or <em>empty</em>
* @see ContextConfiguration#value
* @see ContextConfiguration#locations
* @see #setLocations(String[])
*/
public String[] getLocations() {
return locations;
}
/**
* Set the <em>processed</em> resource locations, effectively overriding the
* original value declared via {@link ContextConfiguration @ContextConfiguration}.
*
* @see #getLocations()
*/
public void setLocations(String[] locations) {
this.locations = locations;
}
/**
* Get the annotated classes that were declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* <p>Note: this is a mutable property. The returned value may therefore
* represent a <em>processed</em> value that does not match the original value
* declared via {@link ContextConfiguration @ContextConfiguration}.
*
* @return the annotated classes; potentially {@code null} or <em>empty</em>
* @see ContextConfiguration#classes
* @see #setClasses(Class[])
*/
public Class<?>[] getClasses() {
return classes;
}
/**
* Set the <em>processed</em> annotated classes, effectively overriding the
* original value declared via {@link ContextConfiguration @ContextConfiguration}.
*
* @see #getClasses()
*/
public void setClasses(Class<?>[] classes) {
this.classes = classes;
return this.locations;
}
/**
* Determine if this {@code ContextConfigurationAttributes} instance has
* path-based resource locations.
*
* @return {@code true} if the {@link #getLocations() locations} array is not empty
* @see #hasResources()
* @see #hasClasses()
@@ -268,79 +291,51 @@ public class ContextConfigurationAttributes {
return !ObjectUtils.isEmpty(getLocations());
}
/**
* Determine if this {@code ContextConfigurationAttributes} instance has
* class-based resources.
*
* @return {@code true} if the {@link #getClasses() classes} array is not empty
* @see #hasResources()
* @see #hasLocations()
*/
public boolean hasClasses() {
return !ObjectUtils.isEmpty(getClasses());
}
/**
* Determine if this {@code ContextConfigurationAttributes} instance has
* either path-based resource locations or class-based resources.
*
* @return {@code true} if either the {@link #getLocations() locations}
* or the {@link #getClasses() classes} array is not empty
* @see #hasLocations()
* @see #hasClasses()
*/
public boolean hasResources() {
return hasLocations() || hasClasses();
return (hasLocations() || hasClasses());
}
/**
* Get the {@code inheritLocations} flag that was declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* @return the {@code inheritLocations} flag
* @see ContextConfiguration#inheritLocations
*/
public boolean isInheritLocations() {
return inheritLocations;
return this.inheritLocations;
}
/**
* Get the {@code ApplicationContextInitializer} classes that were declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* @return the {@code ApplicationContextInitializer} classes
* @since 3.2
*/
public Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] getInitializers() {
return initializers;
return this.initializers;
}
/**
* Get the {@code inheritInitializers} flag that was declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* @return the {@code inheritInitializers} flag
* @since 3.2
*/
public boolean isInheritInitializers() {
return inheritInitializers;
}
/**
* Get the {@code ContextLoader} class that was declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* @return the {@code ContextLoader} class
* @see ContextConfiguration#loader
*/
public Class<? extends ContextLoader> getContextLoaderClass() {
return contextLoaderClass;
return this.inheritInitializers;
}
/**
* Get the name of the context hierarchy level that was declared via
* {@link ContextConfiguration @ContextConfiguration}.
*
* @return the name of the context hierarchy level or {@code null} if not applicable
* @see ContextConfiguration#name()
* @since 3.2.2
@@ -350,24 +345,16 @@ public class ContextConfigurationAttributes {
}
/**
* Generate a unique hash code for all properties of this
* {@code ContextConfigurationAttributes} instance excluding the
* {@linkplain #getName() name}.
* Get the {@code ContextLoader} class that was declared via
* {@link ContextConfiguration @ContextConfiguration}.
* @return the {@code ContextLoader} class
* @see ContextConfiguration#loader
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + declaringClass.hashCode();
result = prime * result + Arrays.hashCode(locations);
result = prime * result + Arrays.hashCode(classes);
result = prime * result + (inheritLocations ? 1231 : 1237);
result = prime * result + Arrays.hashCode(initializers);
result = prime * result + (inheritInitializers ? 1231 : 1237);
result = prime * result + contextLoaderClass.hashCode();
return result;
public Class<? extends ContextLoader> getContextLoaderClass() {
return this.contextLoaderClass;
}
/**
* Determine if the supplied object is equal to this
* {@code ContextConfigurationAttributes} instance by comparing both object's
@@ -380,56 +367,36 @@ public class ContextConfigurationAttributes {
* {@link #getContextLoaderClass() ContextLoader class}.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(obj instanceof ContextConfigurationAttributes)) {
if (!(other instanceof ContextConfigurationAttributes)) {
return false;
}
ContextConfigurationAttributes otherAttr = (ContextConfigurationAttributes) other;
return (ObjectUtils.nullSafeEquals(this.declaringClass, otherAttr.declaringClass) &&
Arrays.equals(this.classes, otherAttr.classes)) &&
Arrays.equals(this.locations, otherAttr.locations) &&
this.inheritLocations == otherAttr.inheritLocations &&
Arrays.equals(this.initializers, otherAttr.initializers) &&
this.inheritInitializers == otherAttr.inheritInitializers &&
ObjectUtils.nullSafeEquals(this.name, otherAttr.name) &&
ObjectUtils.nullSafeEquals(this.contextLoaderClass, otherAttr.contextLoaderClass);
}
final ContextConfigurationAttributes that = (ContextConfigurationAttributes) obj;
if (this.declaringClass == null) {
if (that.declaringClass != null) {
return false;
}
}
else if (!this.declaringClass.equals(that.declaringClass)) {
return false;
}
if (!Arrays.equals(this.locations, that.locations)) {
return false;
}
if (!Arrays.equals(this.classes, that.classes)) {
return false;
}
if (this.inheritLocations != that.inheritLocations) {
return false;
}
if (!Arrays.equals(this.initializers, that.initializers)) {
return false;
}
if (this.inheritInitializers != that.inheritInitializers) {
return false;
}
if (this.contextLoaderClass == null) {
if (that.contextLoaderClass != null) {
return false;
}
}
else if (!this.contextLoaderClass.equals(that.contextLoaderClass)) {
return false;
}
return true;
/**
* Generate a unique hash code for all properties of this
* {@code ContextConfigurationAttributes} instance excluding the
* {@linkplain #getName() name}.
*/
@Override
public int hashCode() {
int result = this.declaringClass.hashCode();
result = 31 * result + Arrays.hashCode(this.classes);
result = 31 * result + Arrays.hashCode(this.locations);
result = 31 * result + Arrays.hashCode(this.initializers);
return result;
}
/**
@@ -438,16 +405,16 @@ public class ContextConfigurationAttributes {
*/
@Override
public String toString() {
return new ToStringCreator(this)//
.append("declaringClass", declaringClass.getName())//
.append("locations", ObjectUtils.nullSafeToString(locations))//
.append("classes", ObjectUtils.nullSafeToString(classes))//
.append("inheritLocations", inheritLocations)//
.append("initializers", ObjectUtils.nullSafeToString(initializers))//
.append("inheritInitializers", inheritInitializers)//
.append("name", name)//
.append("contextLoaderClass", contextLoaderClass.getName())//
.toString();
return new ToStringCreator(this)
.append("declaringClass", this.declaringClass.getName())
.append("classes", ObjectUtils.nullSafeToString(this.classes))
.append("locations", ObjectUtils.nullSafeToString(this.locations))
.append("inheritLocations", this.inheritLocations)
.append("initializers", ObjectUtils.nullSafeToString(this.initializers))
.append("inheritInitializers", this.inheritInitializers)
.append("name", this.name)
.append("contextLoaderClass", this.contextLoaderClass.getName())
.toString();
}
}