diff options
Diffstat (limited to 'spring-test/src/main/java/org/springframework/test')
4 files changed, 50 insertions, 44 deletions
diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/ContextCache.java b/spring-test/src/main/java/org/springframework/test/context/cache/ContextCache.java index 895109fa..27902a6d 100644 --- a/spring-test/src/main/java/org/springframework/test/context/cache/ContextCache.java +++ b/spring-test/src/main/java/org/springframework/test/context/cache/ContextCache.java @@ -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. @@ -22,8 +22,8 @@ import org.springframework.test.context.MergedContextConfiguration; /** * {@code ContextCache} defines the SPI for caching Spring - * {@link ApplicationContext ApplicationContexts} within the <em>Spring - * TestContext Framework</em>. + * {@link ApplicationContext ApplicationContexts} within the + * <em>Spring TestContext Framework</em>. * * <p>A {@code ContextCache} maintains a cache of {@code ApplicationContexts} * keyed by {@link MergedContextConfiguration} instances. @@ -47,7 +47,7 @@ public interface ContextCache { * The name of the logging category used for reporting {@code ContextCache} * statistics. */ - public static final String CONTEXT_CACHE_LOGGING_CATEGORY = "org.springframework.test.context.cache"; + String CONTEXT_CACHE_LOGGING_CATEGORY = "org.springframework.test.context.cache"; /** @@ -59,8 +59,8 @@ public interface ContextCache { /** * Obtain a cached {@code ApplicationContext} for the given key. - * <p>The {@link #getHitCount() hit} and {@link #getMissCount() miss} counts - * must be updated accordingly. + * <p>The {@linkplain #getHitCount() hit} and {@linkplain #getMissCount() miss} + * counts must be updated accordingly. * @param key the context key (never {@code null}) * @return the corresponding {@code ApplicationContext} instance, or {@code null} * if not found in the cache @@ -70,7 +70,7 @@ public interface ContextCache { /** * Explicitly add an {@code ApplicationContext} instance to the cache - * under the given key. + * under the given key, potentially honoring a custom eviction policy. * @param key the context key (never {@code null}) * @param context the {@code ApplicationContext} instance (never {@code null}) */ @@ -80,9 +80,10 @@ public interface ContextCache { * Remove the context with the given key from the cache and explicitly * {@linkplain org.springframework.context.ConfigurableApplicationContext#close() close} * it if it is an instance of {@code ConfigurableApplicationContext}. - * <p>Generally speaking, this method should be called if the state of - * a singleton bean has been modified, potentially affecting future - * interaction with the context. + * <p>Generally speaking, this method should be called to properly evict + * a context from the cache (e.g., due to a custom eviction policy) or if + * the state of a singleton bean has been modified, potentially affecting + * future interaction with the context. * <p>In addition, the semantics of the supplied {@code HierarchyMode} must * be honored. See the Javadoc for {@link HierarchyMode} for details. * @param key the context key; never {@code null} diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/package-info.java b/spring-test/src/main/java/org/springframework/test/context/cache/package-info.java new file mode 100644 index 00000000..85fa1784 --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/context/cache/package-info.java @@ -0,0 +1,4 @@ +/** + * Support for context caching within the <em>Spring TestContext Framework</em>. + */ +package org.springframework.test.context.cache; diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 9637bfc6..318e9d88 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -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. @@ -155,8 +155,8 @@ public abstract class MockRestRequestMatchers { private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) { List<String> actualValues = headers.get(name); AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null); - AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount - + "> values but found " + actualValues, expectedCount <= actualValues.size()); + AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount + + "> values but found " + actualValues, expectedCount <= actualValues.size()); } /** diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java index 771e8c54..4fdaa22a 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java @@ -105,8 +105,8 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { String httpMethod = this.webRequest.getHttpMethod().name(); UriComponents uriComponents = uriComponents(); - MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod, - uriComponents.getPath()); + MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest( + servletContext, httpMethod, uriComponents.getPath()); parent(request, this.parentBuilder); request.setServerName(uriComponents.getHost()); // needs to be first for additional headers authType(request); @@ -123,7 +123,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { request.setProtocol("HTTP/1.1"); request.setQueryString(uriComponents.getQuery()); request.setScheme(uriComponents.getScheme()); - pathInfo(uriComponents,request); + request.setPathInfo(null); return postProcess(request); } @@ -223,14 +223,14 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { try { request.setContent(requestBody.getBytes(charset)); } - catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + catch (UnsupportedEncodingException ex) { + throw new IllegalStateException(ex); } } private void contentType(MockHttpServletRequest request) { String contentType = header("Content-Type"); - request.setContentType(contentType == null ? MediaType.ALL_VALUE.toString() : contentType); + request.setContentType(contentType != null ? contentType : MediaType.ALL_VALUE); } private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) { @@ -245,8 +245,8 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } else { if (!uriComponents.getPath().startsWith(this.contextPath)) { - throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath " - + this.contextPath); + throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath " + + this.contextPath); } request.setContextPath(this.contextPath); } @@ -360,14 +360,10 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { private void params(MockHttpServletRequest request, UriComponents uriComponents) { for (Entry<String, List<String>> entry : uriComponents.getQueryParams().entrySet()) { String name = entry.getKey(); + String urlDecodedName = urlDecode(name); for (String value : entry.getValue()) { - try { - value = (value != null ? URLDecoder.decode(value, "UTF-8") : ""); - request.addParameter(name, value); - } - catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + value = (value != null ? urlDecode(value) : ""); + request.addParameter(urlDecodedName, value); } } for (NameValuePair param : this.webRequest.getRequestParameters()) { @@ -375,6 +371,15 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } } + private String urlDecode(String value) { + try { + return URLDecoder.decode(value, "UTF-8"); + } + catch (UnsupportedEncodingException ex) { + throw new IllegalStateException(ex); + } + } + private Locale parseLocale(String locale) { Matcher matcher = LOCALE_PATTERN.matcher(locale); if (!matcher.matches()) { @@ -392,10 +397,6 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { return new Locale(language, country, qualifier); } - private void pathInfo(UriComponents uriComponents, MockHttpServletRequest request) { - request.setPathInfo(null); - } - private void servletPath(MockHttpServletRequest request, String requestPath) { String servletPath = requestPath.substring(request.getContextPath().length()); if ("".equals(servletPath)) { @@ -426,8 +427,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { private UriComponents uriComponents() { URL url = this.webRequest.getUrl(); - UriComponentsBuilder uriBldr = UriComponentsBuilder.fromUriString(url.toExternalForm()); - return uriBldr.build(); + return UriComponentsBuilder.fromUriString(url.toExternalForm()).build(); } @Override @@ -450,14 +450,18 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { return this; } + private CookieManager getCookieManager() { + return this.webClient.getCookieManager(); + } + /** - * An extension to {@link MockHttpServletRequest} that ensures that - * when a new {@link HttpSession} is created, it is added to the managed sessions. + * An extension to {@link MockHttpServletRequest} that ensures that when a + * new {@link HttpSession} is created, it is added to the managed sessions. */ private final class HtmlUnitMockHttpServletRequest extends MockHttpServletRequest { - private HtmlUnitMockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { + public HtmlUnitMockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { super(servletContext, method, requestURI); } @@ -486,16 +490,17 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } } + /** * An extension to {@link MockHttpSession} that ensures when - * {@link #invalidate()} is called that the {@link HttpSession} is - * removed from the managed sessions. + * {@link #invalidate()} is called that the {@link HttpSession} + * is removed from the managed sessions. */ private final class HtmlUnitMockHttpSession extends MockHttpSession { private final MockHttpServletRequest request; - private HtmlUnitMockHttpSession(MockHttpServletRequest request) { + public HtmlUnitMockHttpSession(MockHttpServletRequest request) { super(request.getServletContext()); this.request = request; } @@ -514,8 +519,4 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } } - private CookieManager getCookieManager() { - return this.webClient.getCookieManager(); - } - } |