Polishing

This commit is contained in:
Juergen Hoeller
2016-06-07 15:42:16 +02:00
parent 6807bcb863
commit 8c4bc3656b
48 changed files with 283 additions and 287 deletions

View File

@@ -136,19 +136,16 @@ abstract class BootstrapUtils {
testContextBootstrapper.setBootstrapContext(bootstrapContext);
return testContextBootstrapper;
}
catch (IllegalStateException ex) {
throw ex;
}
catch (Throwable ex) {
if (ex instanceof IllegalStateException) {
throw (IllegalStateException) ex;
}
throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz +
"]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.",
ex);
}
}
/**
* @since 4.3
*/
private static Class<?> resolveExplicitTestContextBootstrapper(Class<?> testClass) {
Set<BootstrapWith> annotations = AnnotatedElementUtils.findAllMergedAnnotations(testClass, BootstrapWith.class);
if (annotations.size() < 1) {
@@ -162,9 +159,6 @@ abstract class BootstrapUtils {
return annotations.iterator().next().value();
}
/**
* @since 4.3
*/
private static Class<?> resolveDefaultTestContextBootstrapper(Class<?> testClass) throws Exception {
ClassLoader classLoader = BootstrapUtils.class.getClassLoader();
AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(testClass,

View File

@@ -42,12 +42,13 @@ import org.springframework.util.StringUtils;
*/
public class ContextConfigurationAttributes {
private static final Log logger = LogFactory.getLog(ContextConfigurationAttributes.class);
private static final String[] EMPTY_LOCATIONS = new String[0];
private static final Class<?>[] EMPTY_CLASSES = new Class<?>[0];
private static final Log logger = LogFactory.getLog(ContextConfigurationAttributes.class);
private final Class<?> declaringClass;
private Class<?>[] classes;
@@ -66,8 +67,7 @@ public class ContextConfigurationAttributes {
/**
* Construct a new {@link ContextConfigurationAttributes} instance with default
* values.
* Construct a new {@link ContextConfigurationAttributes} instance with default values.
* @param declaringClass the test class that declared {@code @ContextConfiguration},
* either explicitly or implicitly
* @since 4.3
@@ -101,8 +101,8 @@ public class ContextConfigurationAttributes {
@SuppressWarnings("unchecked")
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
this(declaringClass, annAttrs.getStringArray("locations"), 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"));
(Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[]) annAttrs.getClassArray("initializers"),
annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"), (Class<? extends ContextLoader>) annAttrs.getClass("loader"));
}
/**
@@ -156,8 +156,8 @@ public class ContextConfigurationAttributes {
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.",
"and 'classes' %s attributes. Most SmartContextLoader implementations support " +
"only one declaration of resources per @ContextConfiguration annotation.",
declaringClass.getName(), ObjectUtils.nullSafeToString(locations),
ObjectUtils.nullSafeToString(classes)));
}

View File

@@ -27,11 +27,6 @@ import org.springframework.util.StringUtils;
*/
public abstract class ContextCacheUtils {
private ContextCacheUtils() {
/* no-op */
}
/**
* Retrieve the maximum size of the {@link ContextCache}.
* <p>Uses {@link SpringProperties} to retrieve a system property or Spring
@@ -49,7 +44,7 @@ public abstract class ContextCacheUtils {
}
}
catch (Exception ex) {
/* ignore */
// ignore
}
// Fallback

View File

@@ -99,7 +99,7 @@ public class DefaultContextCache implements ContextCache {
* @see #DefaultContextCache()
*/
public DefaultContextCache(int maxSize) {
Assert.isTrue(maxSize > 0, "maxSize must be positive");
Assert.isTrue(maxSize > 0, "'maxSize' must be positive");
this.maxSize = maxSize;
}
@@ -314,16 +314,14 @@ public class DefaultContextCache implements ContextCache {
* Simple cache implementation based on {@link LinkedHashMap} with a maximum
* size and a <em>least recently used</em> (LRU) eviction policy that
* properly closes application contexts.
*
* @author Sam Brannen
* @since 4.3
*/
@SuppressWarnings("serial")
private class LruCache extends LinkedHashMap<MergedContextConfiguration, ApplicationContext> {
/**
* Create a new {@code LruCache} with the supplied initial capacity and
* load factor.
* Create a new {@code LruCache} with the supplied initial capacity
* and load factor.
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
*/

View File

@@ -308,23 +308,20 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
}
}
/**
* @since 4.3
*/
private MergedContextConfiguration buildDefaultMergedContextConfiguration(Class<?> testClass,
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
List<ContextConfigurationAttributes> defaultConfigAttributesList
= Collections.singletonList(new ContextConfigurationAttributes(testClass));
List<ContextConfigurationAttributes> defaultConfigAttributesList =
Collections.singletonList(new ContextConfigurationAttributes(testClass));
ContextLoader contextLoader = resolveContextLoader(testClass, defaultConfigAttributesList);
if (logger.isInfoEnabled()) {
logger.info(String.format(
"Neither @ContextConfiguration nor @ContextHierarchy found for test class [%s], using %s",
testClass.getName(), contextLoader.getClass().getSimpleName()));
"Neither @ContextConfiguration nor @ContextHierarchy found for test class [%s], using %s",
testClass.getName(), contextLoader.getClass().getSimpleName()));
}
return buildMergedContextConfiguration(testClass, defaultConfigAttributesList, null,
cacheAwareContextLoaderDelegate, false);
cacheAwareContextLoaderDelegate, false);
}
/**
@@ -367,7 +364,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
for (ContextConfigurationAttributes configAttributes : configAttributesList) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("Processing locations and classes for context configuration attributes %s",
configAttributes));
configAttributes));
}
if (contextLoader instanceof SmartContextLoader) {
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
@@ -412,9 +409,6 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
return processMergedContextConfiguration(mergedConfig);
}
/**
* @since 4.3
*/
private Set<ContextCustomizer> getContextCustomizers(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
@@ -441,18 +435,6 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
return SpringFactoriesLoader.loadFactories(ContextCustomizerFactory.class, getClass().getClassLoader());
}
/**
* @since 4.3
*/
private boolean areAllEmpty(Collection<?>... collections) {
for (Collection<?> collection : collections) {
if (!collection.isEmpty()) {
return false;
}
}
return true;
}
/**
* Resolve the {@link ContextLoader} {@linkplain Class class} to use for the
* supplied list of {@link ContextConfigurationAttributes} and then instantiate
@@ -575,4 +557,14 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
return mergedConfig;
}
private static boolean areAllEmpty(Collection<?>... collections) {
for (Collection<?> collection : collections) {
if (!collection.isEmpty()) {
return false;
}
}
return true;
}
}

View File

@@ -39,7 +39,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.util.TestContextResourceUtils;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.*;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -58,8 +58,6 @@ import static org.springframework.test.util.MetaAnnotationUtils.*;
*/
public abstract class TestPropertySourceUtils {
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
/**
* The name of the {@link MapPropertySource} created from <em>inlined properties</em>.
* @since 4.1.5
@@ -67,10 +65,8 @@ public abstract class TestPropertySourceUtils {
*/
public static final String INLINED_PROPERTIES_PROPERTY_SOURCE_NAME = "Inlined Test Properties";
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
private TestPropertySourceUtils() {
/* no-op */
}
static MergedTestPropertySources buildMergedTestPropertySources(Class<?> testClass) {
Class<TestPropertySource> annotationType = TestPropertySource.class;

View File

@@ -106,6 +106,7 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
public static final String ACTIVATE_LISTENER = Conventions.getQualifiedAttributeName(
ServletTestExecutionListener.class, "activateListener");
private static final Log logger = LogFactory.getLog(ServletTestExecutionListener.class);
@@ -122,7 +123,6 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
* callback phase via Spring Web's {@link RequestContextHolder}, but only if
* the {@linkplain TestContext#getTestClass() test class} is annotated with
* {@link WebAppConfiguration @WebAppConfiguration}.
*
* @see TestExecutionListener#prepareTestInstance(TestContext)
* @see #setUpRequestContextIfNecessary(TestContext)
*/
@@ -136,7 +136,6 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
* {@link RequestContextHolder}, but only if the
* {@linkplain TestContext#getTestClass() test class} is annotated with
* {@link WebAppConfiguration @WebAppConfiguration}.
*
* @see TestExecutionListener#beforeTestMethod(TestContext)
* @see #setUpRequestContextIfNecessary(TestContext)
*/
@@ -154,11 +153,9 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
* into the test instance for subsequent tests by setting the
* {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
* in the test context to {@code true}.
*
* <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
* {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
* removed from the test context, regardless of their values.
*
* @see TestExecutionListener#afterTestMethod(TestContext)
*/
@Override

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;
import java.io.IOException;
@@ -33,7 +34,7 @@ import org.springframework.util.Assert;
* for storing expectations and actual requests, and checking for unsatisfied
* expectations at the end.
*
* <p>Sub-classes are responsible for validating each request by matching it to
* <p>Subclasses are responsible for validating each request by matching it to
* to expectations following the order of declaration or not.
*
* @author Rossen Stoyanchev
@@ -57,7 +58,7 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
@Override
public ResponseActions expectRequest(ExpectedCount count, RequestMatcher matcher) {
Assert.state(getRequests().isEmpty(), "Cannot add more expectations after actual requests are made.");
Assert.state(getRequests().isEmpty(), "Cannot add more expectations after actual requests are made");
RequestExpectation expectation = new DefaultRequestExpectation(count, matcher);
getExpectations().add(expectation);
return expectation;
@@ -81,11 +82,10 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
}
/**
* Sub-classes must implement the actual validation of the request
* Subclasses must implement the actual validation of the request
* matching it to a declared expectation.
*/
protected abstract ClientHttpResponse validateRequestInternal(ClientHttpRequest request)
throws IOException;
protected abstract ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException;
@Override
public void verify() {
@@ -149,7 +149,6 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
private final Set<RequestExpectation> expectations = new LinkedHashSet<RequestExpectation>();
public Set<RequestExpectation> getExpectations() {
return this.expectations;
}

View File

@@ -87,11 +87,12 @@ public class DefaultRequestExpectation implements RequestExpectation {
@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
if (getResponseCreator() == null) {
throw new IllegalStateException("createResponse called before ResponseCreator was set.");
ResponseCreator responseCreator = getResponseCreator();
if (responseCreator == null) {
throw new IllegalStateException("createResponse called before ResponseCreator was set");
}
getRequestCount().incrementAndValidate();
return getResponseCreator().createResponse(request);
return responseCreator.createResponse(request);
}
@Override
@@ -114,12 +115,10 @@ public class DefaultRequestExpectation implements RequestExpectation {
private int matchedRequestCount;
public RequestCount(ExpectedCount expectedCount) {
this.expectedCount = expectedCount;
}
public ExpectedCount getExpectedCount() {
return this.expectedCount;
}

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;
import org.springframework.util.Assert;

View File

@@ -83,11 +83,9 @@ public class MockRestServiceServer {
* Set up an expectation for a single HTTP request. The returned
* {@link ResponseActions} can be used to set up further expectations as
* well as to define the response.
*
* <p>This method may be invoked any number times before starting to make
* request through the underlying {@code RestTemplate} in order to set up
* all expected requests.
*
* @param matcher request matcher
* @return a representation of the expectation
*/
@@ -98,11 +96,9 @@ public class MockRestServiceServer {
/**
* An alternative to {@link #expect(RequestMatcher)} with an indication how
* many times the request is expected to be executed.
*
* <p>When request expectations have an expected count greater than one, only
* the first execution is expected to match the order of declaration. Subsequent
* request executions may be inserted anywhere thereafter.
*
* @param count the expected count
* @param matcher request matcher
* @return a representation of the expectation
@@ -194,10 +190,8 @@ public class MockRestServiceServer {
/**
* Whether to allow expected requests to be executed in any order not
* necessarily matching the order of declaration.
*
* <p>When set to "true" this is effectively a shortcut for:<br>
* {@code builder.build(new UnorderedRequestExpectationManager)}.
*
* @param ignoreExpectOrder whether to ignore the order of expectations
*/
MockRestServiceServerBuilder ignoreExpectOrder(boolean ignoreExpectOrder);
@@ -214,9 +208,9 @@ public class MockRestServiceServer {
* {@link RequestExpectationManager}.
*/
MockRestServiceServer build(RequestExpectationManager manager);
}
private static class DefaultBuilder implements MockRestServiceServerBuilder {
private final RestTemplate restTemplate;
@@ -225,20 +219,18 @@ public class MockRestServiceServer {
private boolean ignoreExpectOrder;
public DefaultBuilder(RestTemplate restTemplate) {
Assert.notNull(restTemplate, "'restTemplate' must not be null");
Assert.notNull(restTemplate, "RestTemplate must not be null");
this.restTemplate = restTemplate;
this.asyncRestTemplate = null;
}
public DefaultBuilder(AsyncRestTemplate asyncRestTemplate) {
Assert.notNull(asyncRestTemplate, "'asyncRestTemplate' must not be null");
Assert.notNull(asyncRestTemplate, "AsyncRestTemplate must not be null");
this.restTemplate = null;
this.asyncRestTemplate = asyncRestTemplate;
}
@Override
public MockRestServiceServerBuilder ignoreExpectOrder(boolean ignoreExpectOrder) {
this.ignoreExpectOrder = ignoreExpectOrder;

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;
/**

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;
import java.io.IOException;

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;
import java.io.IOException;
@@ -26,9 +27,9 @@ import org.springframework.util.Assert;
* Simple {@code RequestExpectationManager} that matches requests to expectations
* sequentially, i.e. in the order of declaration of expectations.
*
* <p>When request expectations have an expected count greater than one, only
* the first execution is expected to match the order of declaration. Subsequent
* request executions may be inserted anywhere thereafter.
* <p>When request expectations have an expected count greater than one,
* only the first execution is expected to match the order of declaration.
* Subsequent request executions may be inserted anywhere thereafter.
*
* @author Rossen Stoyanchev
* @since 4.3
@@ -77,4 +78,5 @@ public class SimpleRequestExpectationManager extends AbstractRequestExpectationM
this.expectationIterator = null;
this.repeatExpectations.reset();
}
}

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;
import java.io.IOException;
@@ -22,7 +23,7 @@ import org.springframework.http.client.ClientHttpResponse;
/**
* {@code RequestExpectationManager} that matches requests to expectations
* regardless of the order of declaration of expectated requests.
* regardless of the order of declaration of expected requests.
*
* @author Rossen Stoyanchev
* @since 4.3

View File

@@ -159,8 +159,8 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
* Create a new {@link WebConnection} that will use a {@link MockMvc}
* instance if one of the specified {@link WebRequestMatcher} instances
* matches.
* @param webClient the WebClient to use if none of
* the specified {@code WebRequestMatcher} instances matches; never {@code null}
* @param webClient the WebClient to use if none of the specified
* {@code WebRequestMatcher} instances matches (never {@code null})
* @return a new {@code WebConnection} that will use a {@code MockMvc}
* instance if one of the specified {@code WebRequestMatcher} matches
* @see #alwaysUseMockMvc()

View File

@@ -149,8 +149,8 @@ public class MockHttpServletRequestBuilder
* @since 4.3
*/
MockHttpServletRequestBuilder(String 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;
}