Polishing

(cherry picked from commit 67ba187)
This commit is contained in:
Juergen Hoeller
2016-08-10 14:20:42 +02:00
parent 349f2ad578
commit 7b33cd6476
26 changed files with 224 additions and 219 deletions

View File

@@ -112,8 +112,8 @@ public class MockAsyncContext implements AsyncContext {
try {
listener.onComplete(new AsyncEvent(this, this.request, this.response));
}
catch (IOException e) {
throw new IllegalStateException("AsyncListener failure", e);
catch (IOException ex) {
throw new IllegalStateException("AsyncListener failure", ex);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -26,8 +26,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import static org.springframework.core.annotation.AnnotationUtils.*;
/**
* General utility methods for working with <em>profile values</em>.
*
@@ -49,12 +47,10 @@ public abstract class ProfileValueUtils {
* {@link ProfileValueSourceConfiguration
* &#064;ProfileValueSourceConfiguration} annotation and instantiates a new
* instance of that type.
* <p>
* If {@link ProfileValueSourceConfiguration
* <p>If {@link ProfileValueSourceConfiguration
* &#064;ProfileValueSourceConfiguration} is not present on the specified
* class or if a custom {@link ProfileValueSource} is not declared, the
* default {@link SystemProfileValueSource} will be returned instead.
*
* @param testClass The test class for which the ProfileValueSource should
* be retrieved
* @return the configured (or default) ProfileValueSource for the specified
@@ -66,10 +62,10 @@ public abstract class ProfileValueUtils {
Assert.notNull(testClass, "testClass must not be null");
Class<ProfileValueSourceConfiguration> annotationType = ProfileValueSourceConfiguration.class;
ProfileValueSourceConfiguration config = findAnnotation(testClass, annotationType);
ProfileValueSourceConfiguration config = AnnotationUtils.findAnnotation(testClass, annotationType);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class ["
+ testClass.getName() + "]");
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class [" +
testClass.getName() + "]");
}
Class<? extends ProfileValueSource> profileValueSourceType;
@@ -80,8 +76,8 @@ public abstract class ProfileValueUtils {
profileValueSourceType = (Class<? extends ProfileValueSource>) AnnotationUtils.getDefaultValue(annotationType);
}
if (logger.isDebugEnabled()) {
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class ["
+ testClass.getName() + "]");
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class [" +
testClass.getName() + "]");
}
ProfileValueSource profileValueSource;
@@ -92,10 +88,10 @@ public abstract class ProfileValueUtils {
try {
profileValueSource = profileValueSourceType.newInstance();
}
catch (Exception e) {
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType
+ "] for class [" + testClass.getName() + "]: using default.", e);
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType +
"] for class [" + testClass.getName() + "]: using default.", ex);
}
profileValueSource = SystemProfileValueSource.getInstance();
}
@@ -108,16 +104,14 @@ public abstract class ProfileValueUtils {
* Determine if the supplied {@code testClass} is <em>enabled</em> in
* the current environment, as specified by the {@link IfProfileValue
* &#064;IfProfileValue} annotation at the class level.
* <p>
* Defaults to {@code true} if no {@link IfProfileValue
* <p>Defaults to {@code true} if no {@link IfProfileValue
* &#064;IfProfileValue} annotation is declared.
*
* @param testClass the test class
* @return {@code true} if the test is <em>enabled</em> in the current
* environment
*/
public static boolean isTestEnabledInThisEnvironment(Class<?> testClass) {
IfProfileValue ifProfileValue = findAnnotation(testClass, IfProfileValue.class);
IfProfileValue ifProfileValue = AnnotationUtils.findAnnotation(testClass, IfProfileValue.class);
return isTestEnabledInThisEnvironment(retrieveProfileValueSource(testClass), ifProfileValue);
}
@@ -127,10 +121,8 @@ public abstract class ProfileValueUtils {
* &#064;IfProfileValue} annotation, which may be declared on the test
* method itself or at the class level. Class-level usage overrides
* method-level usage.
* <p>
* Defaults to {@code true} if no {@link IfProfileValue
* <p>Defaults to {@code true} if no {@link IfProfileValue
* &#064;IfProfileValue} annotation is declared.
*
* @param testMethod the test method
* @param testClass the test class
* @return {@code true} if the test is <em>enabled</em> in the current
@@ -146,10 +138,8 @@ public abstract class ProfileValueUtils {
* &#064;IfProfileValue} annotation, which may be declared on the test
* method itself or at the class level. Class-level usage overrides
* method-level usage.
* <p>
* Defaults to {@code true} if no {@link IfProfileValue
* <p>Defaults to {@code true} if no {@link IfProfileValue
* &#064;IfProfileValue} annotation is declared.
*
* @param profileValueSource the ProfileValueSource to use to determine if
* the test is enabled
* @param testMethod the test method
@@ -160,11 +150,11 @@ public abstract class ProfileValueUtils {
public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod,
Class<?> testClass) {
IfProfileValue ifProfileValue = findAnnotation(testClass, IfProfileValue.class);
IfProfileValue ifProfileValue = AnnotationUtils.findAnnotation(testClass, IfProfileValue.class);
boolean classLevelEnabled = isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
if (classLevelEnabled) {
ifProfileValue = findAnnotation(testMethod, IfProfileValue.class);
ifProfileValue = AnnotationUtils.findAnnotation(testMethod, IfProfileValue.class);
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
}
@@ -175,7 +165,6 @@ public abstract class ProfileValueUtils {
* Determine if the {@code value} (or one of the {@code values})
* in the supplied {@link IfProfileValue &#064;IfProfileValue} annotation is
* <em>enabled</em> in the current environment.
*
* @param profileValueSource the ProfileValueSource to use to determine if
* the test is enabled
* @param ifProfileValue the annotation to introspect; may be
@@ -195,8 +184,8 @@ public abstract class ProfileValueUtils {
String[] annotatedValues = ifProfileValue.values();
if (StringUtils.hasLength(ifProfileValue.value())) {
if (annotatedValues.length > 0) {
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes "
+ "of @IfProfileValue is not allowed: choose one or the other.");
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes " +
"of @IfProfileValue is not allowed: choose one or the other.");
}
annotatedValues = new String[] { ifProfileValue.value() };
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -19,7 +19,6 @@ package org.springframework.test.context.jdbc;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
@@ -147,17 +146,13 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
/**
* Execute the SQL scripts configured via the supplied {@link Sql @Sql}
* annotation for the given {@link ExecutionPhase} and {@link TestContext}.
*
* <p>Special care must be taken in order to properly support the configured
* {@link SqlConfig#transactionMode}.
*
* @param sql the {@code @Sql} annotation to parse
* @param executionPhase the current execution phase
* @param testContext the current {@code TestContext}
* @param classLevel {@code true} if {@link Sql @Sql} was declared at the
* class level
* @param classLevel {@code true} if {@link Sql @Sql} was declared at the class level
*/
@SuppressWarnings("serial")
private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel)
throws Exception {
if (executionPhase != sql.executionPhase()) {
@@ -222,10 +217,10 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager(transactionManager);
// Ensure user configured an appropriate DataSource/TransactionManager pair.
if ((dataSource != null) && (dataSourceFromTxMgr != null) && !dataSource.equals(dataSourceFromTxMgr)) {
throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: "
+ "the configured DataSource [%s] (named '%s') is not the one associated "
+ "with transaction manager [%s] (named '%s').", testContext, dataSource.getClass().getName(),
if (dataSource != null && dataSourceFromTxMgr != null && !dataSource.equals(dataSourceFromTxMgr)) {
throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: " +
"the configured DataSource [%s] (named '%s') is not the one associated with " +
"transaction manager [%s] (named '%s').", testContext, dataSource.getClass().getName(),
dsName, transactionManager.getClass().getName(), tmName));
}
@@ -239,8 +234,8 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
}
final DataSource finalDataSource = dataSource;
int propagation = newTxRequired ? TransactionDefinition.PROPAGATION_REQUIRES_NEW
: TransactionDefinition.PROPAGATION_REQUIRED;
int propagation = (newTxRequired ? TransactionDefinition.PROPAGATION_REQUIRES_NEW :
TransactionDefinition.PROPAGATION_REQUIRED);
TransactionAttribute transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(
testContext, new DefaultTransactionAttribute(propagation));
@@ -263,8 +258,8 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
return (DataSource) obj;
}
}
catch (Exception e) {
/* ignore */
catch (Exception ex) {
// ignore
}
return null;
}
@@ -304,9 +299,9 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
return prefixedResourcePath;
}
else {
String msg = String.format("Could not detect default SQL script for test %s [%s]: "
+ "%s does not exist. Either declare statements or scripts via @Sql or make the "
+ "default SQL script available.", elementType, elementName, classPathResource);
String msg = String.format("Could not detect default SQL script for test %s [%s]: " +
"%s does not exist. Either declare statements or scripts via @Sql or make the " +
"default SQL script available.", elementType, elementName, classPathResource);
logger.error(msg);
throw new IllegalStateException(msg);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -52,18 +52,12 @@ abstract class ActiveProfilesUtils {
private static final Log logger = LogFactory.getLog(ActiveProfilesUtils.class);
private ActiveProfilesUtils() {
/* no-op */
}
/**
* Resolve <em>active bean definition profiles</em> for the supplied {@link Class}.
*
* <p>Note that the {@link ActiveProfiles#inheritProfiles inheritProfiles} flag of
* {@link ActiveProfiles @ActiveProfiles} will be taken into consideration.
* Specifically, if the {@code inheritProfiles} flag is set to {@code true}, profiles
* defined in the test class will be merged with those defined in superclasses.
*
* @param testClass the class for which to resolve the active profiles (must not be
* {@code null})
* @return the set of active profiles for the specified class, including active
@@ -78,12 +72,12 @@ abstract class ActiveProfilesUtils {
final List<String[]> profileArrays = new ArrayList<String[]>();
Class<ActiveProfiles> annotationType = ActiveProfiles.class;
AnnotationDescriptor<ActiveProfiles> descriptor = MetaAnnotationUtils.findAnnotationDescriptor(testClass,
annotationType);
AnnotationDescriptor<ActiveProfiles> descriptor =
MetaAnnotationUtils.findAnnotationDescriptor(testClass, annotationType);
if (descriptor == null && logger.isDebugEnabled()) {
logger.debug(String.format(
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
annotationType.getName(), testClass.getName()));
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
annotationType.getName(), testClass.getName()));
}
while (descriptor != null) {
@@ -92,8 +86,8 @@ abstract class ActiveProfilesUtils {
ActiveProfiles annotation = descriptor.synthesizeAnnotation();
if (logger.isTraceEnabled()) {
logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
declaringClass.getName()));
logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s]",
annotation, declaringClass.getName()));
}
Class<? extends ActiveProfilesResolver> resolverClass = annotation.resolver();
@@ -101,22 +95,22 @@ abstract class ActiveProfilesUtils {
resolverClass = DefaultActiveProfilesResolver.class;
}
ActiveProfilesResolver resolver = null;
ActiveProfilesResolver resolver;
try {
resolver = BeanUtils.instantiateClass(resolverClass, ActiveProfilesResolver.class);
}
catch (Exception e) {
String msg = String.format("Could not instantiate ActiveProfilesResolver of "
+ "type [%s] for test class [%s].", resolverClass.getName(), rootDeclaringClass.getName());
catch (Exception ex) {
String msg = String.format("Could not instantiate ActiveProfilesResolver of type [%s] " +
"for test class [%s]", resolverClass.getName(), rootDeclaringClass.getName());
logger.error(msg);
throw new IllegalStateException(msg, e);
throw new IllegalStateException(msg, ex);
}
String[] profiles = resolver.resolve(rootDeclaringClass);
if (profiles == null) {
String msg = String.format(
"ActiveProfilesResolver [%s] returned a null array of bean definition profiles.",
resolverClass.getName());
"ActiveProfilesResolver [%s] returned a null array of bean definition profiles",
resolverClass.getName());
logger.error(msg);
throw new IllegalStateException(msg);
}
@@ -124,7 +118,7 @@ abstract class ActiveProfilesUtils {
profileArrays.add(profiles);
descriptor = (annotation.inheritProfiles() ? MetaAnnotationUtils.findAnnotationDescriptor(
rootDeclaringClass.getSuperclass(), annotationType) : null);
rootDeclaringClass.getSuperclass(), annotationType) : null);
}
// Reverse the list so that we can traverse "down" the hierarchy.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* @since 4.2
* @see org.springframework.aop.support.AopUtils
* @see org.springframework.aop.framework.AopProxyUtils
* @see ReflectionTestUtils
*/
public class AopTestUtils {
@@ -41,7 +42,6 @@ public class AopTestUtils {
* {@linkplain AopUtils#isAopProxy proxy}, the target of the proxy will
* be returned; otherwise, the {@code candidate} will be returned
* <em>as is</em>.
*
* @param candidate the instance to check (potentially a Spring AOP proxy);
* never {@code null}
* @return the target object or the {@code candidate}; never {@code null}
@@ -57,8 +57,8 @@ public class AopTestUtils {
return (T) ((Advised) candidate).getTargetSource().getTarget();
}
}
catch (Exception e) {
throw new IllegalStateException("Failed to unwrap proxied object.", e);
catch (Exception ex) {
throw new IllegalStateException("Failed to unwrap proxied object", ex);
}
return (T) candidate;
}
@@ -71,7 +71,6 @@ public class AopTestUtils {
* {@linkplain AopUtils#isAopProxy proxy}, the ultimate target of all
* nested proxies will be returned; otherwise, the {@code candidate}
* will be returned <em>as is</em>.
*
* @param candidate the instance to check (potentially a Spring AOP proxy);
* never {@code null}
* @return the ultimate target object or the {@code candidate}; never
@@ -88,8 +87,8 @@ public class AopTestUtils {
return (T) getUltimateTargetObject(((Advised) candidate).getTargetSource().getTarget());
}
}
catch (Exception e) {
throw new IllegalStateException("Failed to unwrap proxied object.", e);
catch (Exception ex) {
throw new IllegalStateException("Failed to unwrap proxied object", ex);
}
return (T) candidate;
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.client.match;
import java.io.IOException;
@@ -192,8 +193,8 @@ public class XpathRequestMatchers {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
matchInternal(mockRequest);
}
catch (Exception e) {
throw new AssertionError("Failed to parse XML request content: " + e.getMessage());
catch (Exception ex) {
throw new AssertionError("Failed to parse XML request content: " + ex.getMessage());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -133,8 +133,8 @@ public class MockHttpServletRequestBuilder
* @since 4.0.3
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
Assert.notNull(httpMethod, "httpMethod is required");
Assert.notNull(url, "url is required");
Assert.notNull(httpMethod, "'httpMethod' is required");
Assert.notNull(url, "'url' is required");
this.method = httpMethod;
this.url = url;
}
@@ -224,7 +224,7 @@ public class MockHttpServletRequestBuilder
* @param mediaTypes one or more media types
*/
public MockHttpServletRequestBuilder accept(MediaType... mediaTypes) {
Assert.notEmpty(mediaTypes, "No 'Accept' media types");
Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
this.headers.set("Accept", MediaType.toString(Arrays.asList(mediaTypes)));
return this;
}
@@ -234,7 +234,7 @@ public class MockHttpServletRequestBuilder
* @param mediaTypes one or more media types
*/
public MockHttpServletRequestBuilder accept(String... mediaTypes) {
Assert.notEmpty(mediaTypes, "No 'Accept' media types");
Assert.notEmpty(mediaTypes, "'mediaTypes' must not be empty");
List<MediaType> result = new ArrayList<MediaType>(mediaTypes.length);
for (String mediaType : mediaTypes) {
result.add(MediaType.parseMediaType(mediaType));
@@ -260,7 +260,7 @@ public class MockHttpServletRequestBuilder
try {
this.content = content.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
catch (UnsupportedEncodingException ex) {
// should never happen
}
return this;
@@ -271,7 +271,6 @@ public class MockHttpServletRequestBuilder
* @param cookies the cookies to add
*/
public MockHttpServletRequestBuilder cookie(Cookie... cookies) {
Assert.notNull(cookies, "'cookies' must not be null");
Assert.notEmpty(cookies, "'cookies' must not be empty");
this.cookies.addAll(Arrays.asList(cookies));
return this;
@@ -320,7 +319,7 @@ public class MockHttpServletRequestBuilder
* @param sessionAttributes the session attributes
*/
public MockHttpServletRequestBuilder sessionAttrs(Map<String, Object> sessionAttributes) {
Assert.notEmpty(sessionAttributes, "'sessionAttrs' must not be empty");
Assert.notEmpty(sessionAttributes, "'sessionAttributes' must not be empty");
for (String name : sessionAttributes.keySet()) {
sessionAttr(name, sessionAttributes.get(name));
}
@@ -342,7 +341,7 @@ public class MockHttpServletRequestBuilder
* @param flashAttributes the flash attributes
*/
public MockHttpServletRequestBuilder flashAttrs(Map<String, Object> flashAttributes) {
Assert.notEmpty(flashAttributes, "'flashAttrs' must not be empty");
Assert.notEmpty(flashAttributes, "'flashAttributes' must not be empty");
for (String name : flashAttributes.keySet()) {
flashAttr(name, flashAttributes.get(name));
}