diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java index cd03c96786..1fc2e17670 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,23 +13,27 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; import java.util.Arrays; import java.util.List; +import org.springframework.util.Assert; + import com.gargoylesoftware.htmlunit.WebConnection; import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.WebResponse; -import org.springframework.util.Assert; - /** - *

- * Implementation of WebConnection that allows delegating to various WebConnection implementations. For example, if - * you host your JavaScript on the domain code.jquery.com, you might want to use the following:

- *
+ * Implementation of {@link WebConnection} that allows delegating to various
+ * {@code WebConnection} implementations.
+ *
+ * 

For example, if you host your JavaScript on the domain {@code code.jquery.com}, + * you might want to use the following. + * + *

  * WebClient webClient = new WebClient();
  *
  * MockMvc mockMvc = ...
@@ -45,38 +49,47 @@ import org.springframework.util.Assert;
  * WebClient webClient = new WebClient();
  * webClient.setWebConnection(webConnection);
  * 
+ * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ public final class DelegatingWebConnection implements WebConnection { + private final List connections; + private final WebConnection defaultConnection; + public DelegatingWebConnection(WebConnection defaultConnection, List connections) { - Assert.notNull(defaultConnection, "defaultConnection cannot be null"); - Assert.notEmpty(connections, "connections cannot be empty"); + Assert.notNull(defaultConnection, "defaultConnection must not be null"); + Assert.notEmpty(connections, "connections must not be empty"); this.connections = connections; this.defaultConnection = defaultConnection; } - public DelegatingWebConnection(WebConnection defaultConnection,DelegateWebConnection... connections) { + public DelegatingWebConnection(WebConnection defaultConnection, DelegateWebConnection... connections) { this(defaultConnection, Arrays.asList(connections)); } @Override public WebResponse getResponse(WebRequest request) throws IOException { - for(DelegateWebConnection connection : connections) { - if(connection.getMatcher().matches(request)) { + for (DelegateWebConnection connection : this.connections) { + if (connection.getMatcher().matches(request)) { return connection.getDelegate().getResponse(request); } } - return defaultConnection.getResponse(request); + return this.defaultConnection.getResponse(request); } - public final static class DelegateWebConnection { + + public static final class DelegateWebConnection { + private final WebRequestMatcher matcher; + private final WebConnection delegate; + public DelegateWebConnection(WebRequestMatcher matcher, WebConnection delegate) { this.matcher = matcher; this.delegate = delegate; @@ -90,4 +103,5 @@ public final class DelegatingWebConnection implements WebConnection { return delegate; } } + } \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/ForwardRequestPostProcessor.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/ForwardRequestPostProcessor.java index 93a5f5d75c..75284bf447 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/ForwardRequestPostProcessor.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/ForwardRequestPostProcessor.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import org.springframework.mock.web.MockHttpServletRequest; @@ -21,19 +22,23 @@ import org.springframework.util.Assert; /** * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ final class ForwardRequestPostProcessor implements RequestPostProcessor { + private final String forwardUrl; - public ForwardRequestPostProcessor(String url) { - Assert.hasText(url, "Forward url must have text"); - forwardUrl = url; + + public ForwardRequestPostProcessor(String forwardUrl) { + Assert.hasText(forwardUrl, "forwardUrl must not be null or empty"); + this.forwardUrl = forwardUrl; } @Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { - request.setServletPath(forwardUrl); + request.setServletPath(this.forwardUrl); return request; } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcher.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcher.java index 9899555038..e6cee1e665 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcher.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcher.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.net.URL; @@ -23,50 +24,45 @@ import java.util.Set; import com.gargoylesoftware.htmlunit.WebRequest; /** - *

- * An implementation of WebRequestMatcher that allows matching on the host and optionally - * the port of WebRequest#getUrl(). For example, the following would match any request to - * the host "code.jquery.com" without regard for the port: - *

+ * A {@link WebRequestMatcher} that allows matching on the host and optionally + * the port of {@code WebRequest#getUrl()}. * - *
- * WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com");
- * 
+ *

For example, the following would match any request to the host + * {@code "code.jquery.com"} without regard for the port. * - * Multiple hosts can also be passed in. For example, the following would match an request - * to the host "code.jquery.com" or the host "cdn.com" without regard for the port: + *

WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com");
* - *
- * WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com", "cdn.com");
- * 
+ *

Multiple hosts can also be passed in. For example, the following would + * match any request to the host {@code "code.jquery.com"} or the host + * {@code "cdn.com"} without regard for the port. * - *

- * Alternatively, one can also specify the port. For example, the following would match - * any request to the host "code.jquery.com" with the port of 80. - *

+ *
WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com", "cdn.com");
* - *
- * WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com:80");
- * 
+ *

Alternatively, one can also specify the port. For example, the following would match + * any request to the host {@code "code.jquery.com"} with the port of {@code 80}. * - *

- * The above cdnMatcher would match: "http://code.jquery.com/jquery.js" (default port of - * 80) and "http://code.jquery.com:80/jquery.js". However, it would not match - * "https://code.jquery.com/jquery.js" (default port of 443). - *

+ *
WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com:80");
+ * + *

The above {@code cdnMatcher} would match {@code "http://code.jquery.com/jquery.js"} + * which has a default port of {@code 80} and {@code "http://code.jquery.com:80/jquery.js"}. + * However, it would not match {@code "https://code.jquery.com/jquery.js"} + * which has a default port of {@code 443}. * * @author Rob Winch + * @author Sam Brannen * @since 4.2 * @see UrlRegexRequestMatcher * @see org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection */ public final class HostRequestMatcher implements WebRequestMatcher { + private final Set hosts = new HashSet(); + /** - * Creates a new instance - * - * @param hosts the hosts to match on (i.e. "localhost", "example.com:443") + * Create a new {@code HostRequestMatcher} for the given hosts — + * for example: {@code "localhost"}, {@code "example.com:443"}, etc. + * @param hosts the hosts to match on */ public HostRequestMatcher(String... hosts) { this.hosts.addAll(Arrays.asList(hosts)); @@ -77,16 +73,17 @@ public final class HostRequestMatcher implements WebRequestMatcher { URL url = request.getUrl(); String host = url.getHost(); - if(hosts.contains(host)) { + if (this.hosts.contains(host)) { return true; } int port = url.getPort(); - if(port == -1) { + if (port == -1) { port = url.getDefaultPort(); } String hostAndPort = host + ":" + port; - return hosts.contains(hostAndPort); + return this.hosts.contains(hostAndPort); } + } 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 e8e48af01f..d9ef067c6c 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 @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.io.UnsupportedEncodingException; @@ -33,11 +34,6 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import com.gargoylesoftware.htmlunit.CookieManager; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.WebRequest; -import com.gargoylesoftware.htmlunit.util.NameValuePair; - import org.springframework.beans.Mergeable; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; @@ -49,21 +45,27 @@ import org.springframework.util.Assert; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; +import com.gargoylesoftware.htmlunit.CookieManager; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.WebRequest; +import com.gargoylesoftware.htmlunit.util.NameValuePair; + /** - *

- * Internal class used to allow a {@link WebRequest} into a {@link MockHttpServletRequest} using Spring MVC Test's - * {@link RequestBuilder}. - *

- *

- * By default the first path segment of the URL is used as the contextPath. To override this default see - * {@link #setContextPath(String)}. - *

+ * Internal class used to transform a {@link WebRequest} into a + * {@link MockHttpServletRequest} using Spring MVC Test's {@link RequestBuilder}. + * + *

By default the first path segment of the URL is used as the contextPath. + * To override this default see {@link #setContextPath(String)}. * * @author Rob Winch + * @author Sam Brannen * @since 4.2 * @see MockMvcWebConnection */ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { + + private static final Pattern LOCALE_PATTERN = Pattern.compile("^\\s*(\\w{2})(?:-(\\w{2}))?(?:;q=(\\d+\\.\\d+))?$"); + private final Map sessions; private final WebClient webClient; @@ -78,18 +80,19 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { private RequestPostProcessor forwardPostProcessor; + /** - * - * @param sessions A {@link Map} of the {@link HttpSession#getId()} to currently managed {@link HttpSession} - * objects. Cannot be null. + * Construct a new {@code HtmlUnitRequestBuilder}. + * @param sessions a {@link Map} from session {@linkplain HttpSession#getId() IDs} + * to currently managed {@link HttpSession} objects; never {@code null} * @param webClient the WebClient for retrieving cookies - * @param webRequest The {@link WebRequest} to transform into a {@link MockHttpServletRequest}. Cannot be null. + * @param webRequest the {@link WebRequest} to transform into a + * {@link MockHttpServletRequest}; never {@code null} */ - public HtmlUnitRequestBuilder(Map sessions, WebClient webClient, - WebRequest webRequest) { - Assert.notNull(sessions, "sessions cannot be null"); - Assert.notNull(webClient, "webClient cannot be null"); - Assert.notNull(webRequest, "webRequest cannot be null"); + public HtmlUnitRequestBuilder(Map sessions, WebClient webClient, WebRequest webRequest) { + Assert.notNull(sessions, "sessions map must not be null"); + Assert.notNull(webClient, "webClient must not be null"); + Assert.notNull(webRequest, "webRequest must not be null"); this.sessions = sessions; this.webClient = webClient; @@ -98,12 +101,12 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { public MockHttpServletRequest buildRequest(ServletContext servletContext) { String charset = getCharset(); - String httpMethod = webRequest.getHttpMethod().name(); + String httpMethod = this.webRequest.getHttpMethod().name(); UriComponents uriComponents = uriComponents(); MockHttpServletRequest result = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod, uriComponents.getPath()); - parent(result, parentBuilder); + parent(result, this.parentBuilder); result.setServerName(uriComponents.getHost()); // needs to be first for additional headers authType(result); result.setCharacterEncoding(charset); @@ -125,27 +128,27 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private MockHttpServletRequest postProcess(MockHttpServletRequest request) { - if(parentPostProcessor != null) { - request = parentPostProcessor.postProcessRequest(request); + if (this.parentPostProcessor != null) { + request = this.parentPostProcessor.postProcessRequest(request); } - if(forwardPostProcessor != null) { - request = forwardPostProcessor.postProcessRequest(request); + if (this.forwardPostProcessor != null) { + request = this.forwardPostProcessor.postProcessRequest(request); } - return request; } private void parent(MockHttpServletRequest result, RequestBuilder parent) { - if(parent == null) { + if (parent == null) { return; } + MockHttpServletRequest parentRequest = parent.buildRequest(result.getServletContext()); // session HttpSession parentSession = parentRequest.getSession(false); - if(parentSession != null) { + if (parentSession != null) { Enumeration attrNames = parentSession.getAttributeNames(); - while(attrNames.hasMoreElements()) { + while (attrNames.hasMoreElements()) { String attrName = attrNames.nextElement(); Object attrValue = parentSession.getAttribute(attrName); result.getSession().setAttribute(attrName, attrValue); @@ -154,10 +157,10 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { // header Enumeration headerNames = parentRequest.getHeaderNames(); - while(headerNames.hasMoreElements()) { + while (headerNames.hasMoreElements()) { String attrName = headerNames.nextElement(); Enumeration attrValues = parentRequest.getHeaders(attrName); - while(attrValues.hasMoreElements()) { + while (attrValues.hasMoreElements()) { String attrValue = attrValues.nextElement(); result.addHeader(attrName, attrValue); } @@ -165,7 +168,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { // parameter Map parentParams = parentRequest.getParameterMap(); - for(Map.Entry parentParam : parentParams.entrySet()) { + for (Map.Entry parentParam : parentParams.entrySet()) { String paramName = parentParam.getKey(); String[] paramValues = parentParam.getValue(); result.addParameter(paramName, paramValues); @@ -173,42 +176,34 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { // cookie Cookie[] parentCookies = parentRequest.getCookies(); - if(parentCookies != null) { + if (parentCookies != null) { result.setCookies(parentCookies); } // request attribute Enumeration parentAttrNames = parentRequest.getAttributeNames(); - while(parentAttrNames.hasMoreElements()) { + while (parentAttrNames.hasMoreElements()) { String parentAttrName = parentAttrNames.nextElement(); result.setAttribute(parentAttrName, parentRequest.getAttribute(parentAttrName)); } } /** - * Sets the contextPath to be used. The value may be null in which case the first path segment of the URL is turned - * into the contextPath. Otherwise it must conform to {@link HttpServletRequest#getContextPath()} which states it - * can be empty string or it must start with a "/" and not end in a "/". - * - * @param contextPath A valid contextPath - * @throws IllegalArgumentException if contextPath is not a valid {@link HttpServletRequest#getContextPath()}. + * Set the contextPath to be used. + *

The value may be null in which case the first path segment of the + * URL is turned into the contextPath. Otherwise it must conform to + * {@link HttpServletRequest#getContextPath()} which states it can be + * an empty string, or it must start with a "/" and not end with a "/". + * @param contextPath a valid contextPath + * @throws IllegalArgumentException if the contextPath is not a valid {@link HttpServletRequest#getContextPath()} */ public void setContextPath(String contextPath) { - if (contextPath == null || "".equals(contextPath)) { - this.contextPath = contextPath; - return; - } - if (contextPath.endsWith("/")) { - throw new IllegalArgumentException("contextPath cannot end with /. Got '" + contextPath + "'"); - } - if (!contextPath.startsWith("/")) { - throw new IllegalArgumentException("contextPath must start with /. Got '" + contextPath + "'"); - } + MockMvcWebConnection.validateContextPath(contextPath); this.contextPath = contextPath; } - public void setForwardPostProcessor(RequestPostProcessor postProcessor) { - this.forwardPostProcessor = postProcessor; + public void setForwardPostProcessor(RequestPostProcessor forwardPostProcessor) { + this.forwardPostProcessor = forwardPostProcessor; } private void authType(MockHttpServletRequest request) { @@ -220,7 +215,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private void content(MockHttpServletRequest result, String charset) { - String requestBody = webRequest.getRequestBody(); + String requestBody = this.webRequest.getRequestBody(); if (requestBody == null) { return; } @@ -238,7 +233,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private void contextPath(MockHttpServletRequest result, UriComponents uriComponents) { - if (contextPath == null) { + if (this.contextPath == null) { List pathSegments = uriComponents.getPathSegments(); if (pathSegments.isEmpty()) { result.setContextPath(""); @@ -248,11 +243,11 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } } else { - if (!uriComponents.getPath().startsWith(contextPath)) { + if (!uriComponents.getPath().startsWith(this.contextPath)) { throw new IllegalArgumentException(uriComponents.getPath() + " should start with contextPath " - + contextPath); + + this.contextPath); } - result.setContextPath(contextPath); + result.setContextPath(this.contextPath); } } @@ -265,21 +260,20 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { while (tokens.hasMoreTokens()) { String cookieName = tokens.nextToken().trim(); if (!tokens.hasMoreTokens()) { - throw new IllegalArgumentException("Expected value for cookie name " + cookieName - + ". Full cookie was " + cookieHeaderValue); + throw new IllegalArgumentException("Expected value for cookie name '" + cookieName + + "'. Full cookie was " + cookieHeaderValue); } String cookieValue = tokens.nextToken().trim(); processCookie(result, cookies, new Cookie(cookieName, cookieValue)); } } - @SuppressWarnings("unchecked") - Set managedCookies = webClient.getCookies(webRequest.getUrl()); + Set managedCookies = this.webClient.getCookies(this.webRequest.getUrl()); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) { processCookie(result, cookies, new Cookie(cookie.getName(), cookie.getValue())); } - if(parentCookies != null) { - for(Cookie cookie : parentCookies) { + if (parentCookies != null) { + for (Cookie cookie : parentCookies) { cookies.add(cookie); } } @@ -297,7 +291,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private String getCharset() { - String charset = webRequest.getCharset(); + String charset = this.webRequest.getCharset(); if (charset == null) { return "ISO-8859-1"; } @@ -305,24 +299,24 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private String header(String headerName) { - return webRequest.getAdditionalHeaders().get(headerName); + return this.webRequest.getAdditionalHeaders().get(headerName); } private void headers(MockHttpServletRequest result) { - for (Entry header : webRequest.getAdditionalHeaders().entrySet()) { + for (Entry header : this.webRequest.getAdditionalHeaders().entrySet()) { result.addHeader(header.getKey(), header.getValue()); } } private MockHttpSession httpSession(MockHttpServletRequest request, final String sessionid) { MockHttpSession session; - synchronized (sessions) { - session = sessions.get(sessionid); + synchronized (this.sessions) { + session = this.sessions.get(sessionid); if (session == null) { session = new HtmlUnitMockHttpSession(request, sessionid); session.setNew(true); - synchronized (sessions) { - sessions.put(sessionid, session); + synchronized (this.sessions) { + this.sessions.put(sessionid, session); } addSessionCookie(request, sessionid); } @@ -371,7 +365,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } } } - for (NameValuePair param : webRequest.getRequestParameters()) { + for (NameValuePair param : this.webRequest.getRequestParameters()) { result.addParameter(param.getName(), param.getValue()); } } @@ -416,7 +410,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { int serverPort = uriComponents.getPort(); result.setServerPort(serverPort); if (serverPort == -1) { - int portConnection = webRequest.getUrl().getDefaultPort(); + int portConnection = this.webRequest.getUrl().getDefaultPort(); result.setLocalPort(serverPort); result.setRemotePort(portConnection); } @@ -426,7 +420,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } private UriComponents uriComponents() { - URL url = webRequest.getUrl(); + URL url = this.webRequest.getUrl(); UriComponentsBuilder uriBldr = UriComponentsBuilder.fromUriString(url.toExternalForm()); return uriBldr.build(); } @@ -441,7 +435,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { if (parent == null) { return this; } - if(parent instanceof RequestBuilder) { + if (parent instanceof RequestBuilder) { this.parentBuilder = (RequestBuilder) parent; } if (parent instanceof SmartRequestBuilder) { @@ -451,13 +445,13 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { return this; } + /** - * An extension to {@link MockHttpServletRequest} that ensures that when a new {@link HttpSession} is created, it is - * added to the managed sessions. - * - * @author Rob Winch + * 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) { super(servletContext, method, requestURI); } @@ -469,8 +463,8 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { setSession(newSession); newSession.setNew(true); String sessionid = newSession.getId(); - synchronized (sessions) { - sessions.put(sessionid, newSession); + synchronized (HtmlUnitRequestBuilder.this.sessions) { + HtmlUnitRequestBuilder.this.sessions.put(sessionid, newSession); } addSessionCookie(this, sessionid); result = newSession; @@ -488,12 +482,12 @@ 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. - * - * @author Rob Winch + * An extension to {@link MockHttpSession} that ensures when + * {@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) { @@ -508,16 +502,15 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { public void invalidate() { super.invalidate(); - synchronized (sessions) { - sessions.remove(getId()); + synchronized (HtmlUnitRequestBuilder.this.sessions) { + HtmlUnitRequestBuilder.this.sessions.remove(getId()); } removeSessionCookie(request, getId()); } } private CookieManager getCookieManager() { - return webClient.getCookieManager(); + return this.webClient.getCookieManager(); } - private static final Pattern LOCALE_PATTERN = Pattern.compile("^\\s*(\\w{2})(?:-(\\w{2}))?(?:;q=(\\d+\\.\\d+))?$"); } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java index e9236d31a8..9764cea5c6 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,18 +13,22 @@ * License for the specific language governing permissions and limitations under * the License. */ -package org.springframework.test.web.servlet.htmlunit; -import com.gargoylesoftware.htmlunit.WebClient; +package org.springframework.test.web.servlet.htmlunit; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcConfigurer; +import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; +import com.gargoylesoftware.htmlunit.WebClient; + /** - * Simplifies creating a WebClient that delegates to a MockMvc instance. + * {@code MockMvcWebClientBuilder} simplifies the creation of a {@link WebClient} + * that delegates to a {@link MockMvc} instance. * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ public class MockMvcWebClientBuilder extends MockMvcWebConnectionBuilderSupport { @@ -42,56 +46,63 @@ public class MockMvcWebClientBuilder extends MockMvcWebConnectionBuilderSupport< } /** - * Creates a new instance with a WebApplicationContext. - * - * @param context the WebApplicationContext to use. Cannot be null. + * Create a new instance with the supplied {@link WebApplicationContext}. + * @param context the {@code WebApplicationContext} to create a {@link MockMvc} + * instance from; never {@code null} * @return the MockMvcWebClientBuilder to customize */ public static MockMvcWebClientBuilder webAppContextSetup(WebApplicationContext context) { + Assert.notNull(context, "WebApplicationContext must not be null"); return new MockMvcWebClientBuilder(context); } /** - * Creates a new instance using a WebApplicationContext - * @param context the WebApplicationContext to create a MockMvc instance from. - * @param configurer the MockMvcConfigurer to apply - * Cannot be null. - * @return the MockMvcWebClientBuilder to use + * Create a new instance with the supplied {@link WebApplicationContext} + * and {@link MockMvcConfigurer}. + * @param context the {@code WebApplicationContext} to create a {@link MockMvc} + * instance from; never {@code null} + * @param configurer the MockMvcConfigurer to apply; never {@code null} + * @return the MockMvcWebClientBuilder to customize */ public static MockMvcWebClientBuilder webAppContextSetup(WebApplicationContext context, MockMvcConfigurer configurer) { + Assert.notNull(context, "WebApplicationContext must not be null"); + Assert.notNull(configurer, "MockMvcConfigurer must not be null"); return new MockMvcWebClientBuilder(context, configurer); } /** - * Creates a new instance with a MockMvc instance. - * - * @param mockMvc the MockMvc to use. Cannot be null. + * Create a new instance with the supplied {@link MockMvc} instance. + * @param mockMvc the {@code MockMvc} instance to use; never {@code null} * @return the MockMvcWebClientBuilder to customize */ public static MockMvcWebClientBuilder mockMvcSetup(MockMvc mockMvc) { + Assert.notNull(mockMvc, "MockMvc must not be null"); return new MockMvcWebClientBuilder(mockMvc); } /** - * Creates a WebClient that uses the provided MockMvc for any matching requests and a - * WebClient with all the default settings for any other request. - * - * @return the WebClient to use + * Create a {@link WebClient} that uses the configured {@link MockMvc} + * instance for any matching requests and a {@code WebClient} with all + * the default settings for any other requests. + * @return the {@code WebClient} to use + * @see #configureWebClient(WebClient) */ public WebClient createWebClient() { return configureWebClient(new WebClient()); } /** - * Creates a WebClient that uses the provided MockMvc for any matching requests and the - * provided WebClient for any other request. - * - * @param webClient The WebClient to delegate to for requests that do not match. Cannot be null. - * + * Configure the supplied {@link WebClient} to use the configured + * {@link MockMvc} instance for any matching requests and the supplied + * {@code WebClient} for any other requests. + * @param webClient the WebClient to delegate to for requests that do not + * match; never {@code null} * @return the WebClient to use */ public WebClient configureWebClient(WebClient webClient) { + Assert.notNull(webClient, "webClient must not be null"); webClient.setWebConnection(createConnection(webClient.getWebConnection())); return webClient; } + } \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java index 1ab9494dc0..d277f4cef8 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,18 +13,12 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import javax.servlet.http.HttpServletRequest; - -import com.gargoylesoftware.htmlunit.CookieManager; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.WebConnection; -import com.gargoylesoftware.htmlunit.WebRequest; -import com.gargoylesoftware.htmlunit.WebResponse; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpSession; @@ -34,31 +28,33 @@ import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.htmlunit.webdriver.WebConnectionHtmlUnitDriver; import org.springframework.util.Assert; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.WebConnection; +import com.gargoylesoftware.htmlunit.WebRequest; +import com.gargoylesoftware.htmlunit.WebResponse; + /** - *

- * Allows {@link MockMvc} to transform a {@link WebRequest} into a {@link WebResponse}. This is the core integration - * with HTML Unit. - *

- *

- * Example usage can be seen below: - *

+ * {@code MockMvcWebConnection} enables {@link MockMvc} to transform a + * {@link WebRequest} into a {@link WebResponse}. + *

This is the core integration with HtmlUnit. + *

Example usage can be seen below. * - *

+ * 
  * WebClient webClient = new WebClient();
  * MockMvc mockMvc = ...
  * MockMvcWebConnection webConnection = new MockMvcWebConnection(mockMvc);
  * mockConnection.setWebClient(webClient);
  * webClient.setWebConnection(webConnection);
  *
- * ... use webClient as normal ...
+ * // Use webClient as normal ...
  * 
* * @author Rob Winch + * @author Sam Brannen * @since 4.2 * @see WebConnectionHtmlUnitDriver */ public final class MockMvcWebConnection implements WebConnection { - private WebClient webClient; private final Map sessions = new HashMap(); @@ -66,26 +62,32 @@ public final class MockMvcWebConnection implements WebConnection { private final String contextPath; + private WebClient webClient; + + /** - * Creates a new instance that assumes the context root of the application is "". For example, - * the URL http://localhost/test/this would use "" as the context root. - * - * @param mockMvc the MockMvc instance to use + * Create a new instance that assumes the context path of the application + * is {@code ""} (i.e., the root context). + *

For example, the URL {@code http://localhost/test/this} would use + * {@code ""} as the context path. + * @param mockMvc the {@code MockMvc} instance to use; never {@code null} */ public MockMvcWebConnection(MockMvc mockMvc) { this(mockMvc, ""); } /** - * Creates a new instance with a specified context root. - * - * @param mockMvc the MockMvc instance to use - * @param contextPath the contextPath to use. The value may be null in which case the first path segment of the URL is turned - * into the contextPath. Otherwise it must conform to {@link HttpServletRequest#getContextPath()} which states it - * can be empty string or it must start with a "/" and not end in a "/". + * Create a new instance with the specified context path. + *

The path may be {@code null} in which case the first path segment + * of the URL is turned into the contextPath. Otherwise it must conform + * to {@link javax.servlet.http.HttpServletRequest#getContextPath()} + * which states that it can be an empty string and otherwise must start + * with a "/" character and not end with a "/" character. + * @param mockMvc the {@code MockMvc} instance to use; never {@code null} + * @param contextPath the contextPath to use */ public MockMvcWebConnection(MockMvc mockMvc, String contextPath) { - Assert.notNull(mockMvc, "mockMvc cannot be null"); + Assert.notNull(mockMvc, "mockMvc must not be null"); validateContextPath(contextPath); this.webClient = new WebClient(); @@ -95,13 +97,13 @@ public final class MockMvcWebConnection implements WebConnection { public WebResponse getResponse(WebRequest webRequest) throws IOException { long startTime = System.currentTimeMillis(); - HtmlUnitRequestBuilder requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest); - requestBuilder.setContextPath(contextPath); + HtmlUnitRequestBuilder requestBuilder = new HtmlUnitRequestBuilder(this.sessions, this.webClient, webRequest); + requestBuilder.setContextPath(this.contextPath); MockHttpServletResponse httpServletResponse = getResponse(requestBuilder); String forwardedUrl = httpServletResponse.getForwardedUrl(); - while(forwardedUrl != null) { + while (forwardedUrl != null) { requestBuilder.setForwardPostProcessor(new ForwardRequestPostProcessor(forwardedUrl)); httpServletResponse = getResponse(requestBuilder); forwardedUrl = httpServletResponse.getForwardedUrl(); @@ -111,18 +113,14 @@ public final class MockMvcWebConnection implements WebConnection { } public void setWebClient(WebClient webClient) { - Assert.notNull(webClient, "webClient cannot be null"); + Assert.notNull(webClient, "webClient must not be null"); this.webClient = webClient; } - private CookieManager getCookieManager() { - return webClient.getCookieManager(); - } - private MockHttpServletResponse getResponse(RequestBuilder requestBuilder) throws IOException { ResultActions resultActions; try { - resultActions = mockMvc.perform(requestBuilder); + resultActions = this.mockMvc.perform(requestBuilder); } catch (Exception e) { throw (IOException) new IOException(e.getMessage()).initCause(e); @@ -132,19 +130,23 @@ public final class MockMvcWebConnection implements WebConnection { } /** - * Performs validation on the contextPath - * - * @param contextPath the contextPath to validate + * Validate the supplied {@code contextPath}. + *

If the value is not {@code null}, it must conform to + * {@link javax.servlet.http.HttpServletRequest#getContextPath()} which + * states that it can be an empty string and otherwise must start with + * a "/" character and not end with a "/" character. + * @param contextPath the path to validate */ - private static void validateContextPath(String contextPath) { + static void validateContextPath(String contextPath) { if (contextPath == null || "".equals(contextPath)) { return; } - if (contextPath.endsWith("/")) { - throw new IllegalArgumentException("contextPath cannot end with /. Got '" + contextPath + "'"); - } if (!contextPath.startsWith("/")) { - throw new IllegalArgumentException("contextPath must start with /. Got '" + contextPath + "'"); + throw new IllegalArgumentException("contextPath '" + contextPath + "' must start with '/'."); + } + if (contextPath.endsWith("/")) { + throw new IllegalArgumentException("contextPath '" + contextPath + "' must not end with '/'."); } } + } \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java index df2055fd45..10bedebaf4 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,72 +13,80 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.util.ArrayList; import java.util.List; -import com.gargoylesoftware.htmlunit.WebConnection; - import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcConfigurer; import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; +import com.gargoylesoftware.htmlunit.WebConnection; + /** - * Makes it easy to create a WebConnection that uses MockMvc and optionally delegates to - * a real WebConnection for specific requests. The default is to use MockMvc for any host - * that is "localhost" and otherwise use a real WebConnection. + * Support class that simplifies the creation of a {@link WebConnection} that + * uses {@link MockMvc} and optionally delegates to a real {@link WebConnection} + * for specific requests. + * + *

The default is to use {@link MockMvc} for requests to {@code localhost} + * and otherwise use a real {@link WebConnection}. * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ public abstract class MockMvcWebConnectionBuilderSupport> { - private String contextPath = ""; private final MockMvc mockMvc; - private List mockMvcRequestMatchers = new ArrayList(); + private final List mockMvcRequestMatchers = new ArrayList(); + + private String contextPath = ""; private boolean alwaysUseMockMvc; + /** - * Creates a new instance using a MockMvc instance - * - * @param mockMvc the MockMvc instance to use. Cannot be null. + * Create a new instance using the supplied {@link MockMvc} instance. + * @param mockMvc the {@code MockMvc} instance to use; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(MockMvc mockMvc) { - Assert.notNull(mockMvc, "mockMvc cannot be null"); + Assert.notNull(mockMvc, "mockMvc must not be null"); this.mockMvc = mockMvc; this.mockMvcRequestMatchers.add(new HostRequestMatcher("localhost")); } /** - * Creates a new instance using a WebApplicationContext - * @param context the WebApplicationContext to create a MockMvc instance from. - * Cannot be null. + * Create a new instance using the supplied {@link WebApplicationContext}. + * @param context the {@code WebApplicationContext} to create a {@code MockMvc} + * instance from; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context) { this(MockMvcBuilders.webAppContextSetup(context).build()); } /** - * Creates a new instance using a WebApplicationContext - * @param context the WebApplicationContext to create a MockMvc instance from. - * @param configurer the MockMvcConfigurer to apply - * Cannot be null. + * Create a new instance using the supplied {@link WebApplicationContext} + * and {@link MockMvcConfigurer}. + * @param context the {@code WebApplicationContext} to create a {@code MockMvc} + * instance from; never {@code null} + * @param configurer the MockMvcConfigurer to apply; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context, MockMvcConfigurer configurer) { this(MockMvcBuilders.webAppContextSetup(context).apply(configurer).build()); } /** - * The context path to use. Default is "". If the value is null, then the first path + * Set the context path to use. + *

If the supplied value is {@code null} or empty, the first path * segment of the request URL is assumed to be the context path. - * - * @param contextPath the context path to use. - * @return the builder for further customization + *

Default is {@code ""}. + * @param contextPath the context path to use + * @return this builder for further customization */ @SuppressWarnings("unchecked") public T contextPath(String contextPath) { @@ -87,9 +95,9 @@ public abstract class MockMvcWebConnectionBuilderSupport delegates = new ArrayList(mockMvcRequestMatchers.size()); - for(WebRequestMatcher matcher : mockMvcRequestMatchers) { + List delegates = new ArrayList( + this.mockMvcRequestMatchers.size()); + for (WebRequestMatcher matcher : this.mockMvcRequestMatchers) { delegates.add(new DelegatingWebConnection.DelegateWebConnection(matcher, mockMvcWebConnection)); } return new DelegatingWebConnection(defaultConnection, delegates); } + } \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilder.java index f619641bb6..7cf99f1329 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilder.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; @@ -20,32 +21,38 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.springframework.http.HttpStatus; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.WebResponse; import com.gargoylesoftware.htmlunit.WebResponseData; import com.gargoylesoftware.htmlunit.util.NameValuePair; -import org.springframework.http.HttpStatus; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.util.Assert; - /** * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ final class MockWebResponseBuilder { + + private static final String DEFAULT_STATUS_MESSAGE = "N/A"; + private final long startTime; private final WebRequest webRequest; private final MockHttpServletResponse response; - public MockWebResponseBuilder(long startTime, WebRequest webRequest, MockHttpServletResponse httpServletResponse) { - Assert.notNull(webRequest, "webRequest"); - Assert.notNull(httpServletResponse, "httpServletResponse cannot be null"); + + public MockWebResponseBuilder(long startTime, WebRequest webRequest, MockHttpServletResponse response) { + Assert.notNull(webRequest, "webRequest must not be null"); + Assert.notNull(response, "response must not be null"); this.startTime = startTime; this.webRequest = webRequest; - this.response = httpServletResponse; + this.response = response; } public WebResponse build() throws IOException { @@ -56,38 +63,42 @@ final class MockWebResponseBuilder { private WebResponseData webResponseData() throws IOException { List responseHeaders = responseHeaders(); - int statusCode = response.getRedirectedUrl() == null ? response.getStatus() : 301; + int statusCode = (this.response.getRedirectedUrl() != null ? HttpStatus.MOVED_PERMANENTLY.value() + : this.response.getStatus()); String statusMessage = statusMessage(statusCode); - return new WebResponseData(response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders); + return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders); } private String statusMessage(int statusCode) { - String errorMessage = response.getErrorMessage(); - if (errorMessage != null) { + String errorMessage = this.response.getErrorMessage(); + if (StringUtils.hasText(errorMessage)) { return errorMessage; } + try { return HttpStatus.valueOf(statusCode).getReasonPhrase(); } - catch (IllegalArgumentException useDefault) { + catch (IllegalArgumentException ex) { + // ignore } - ; - return "N/A"; + + return DEFAULT_STATUS_MESSAGE; } private List responseHeaders() { - Collection headerNames = response.getHeaderNames(); + Collection headerNames = this.response.getHeaderNames(); List responseHeaders = new ArrayList(headerNames.size()); for (String headerName : headerNames) { - List headerValues = response.getHeaderValues(headerName); + List headerValues = this.response.getHeaderValues(headerName); for (Object value : headerValues) { responseHeaders.add(new NameValuePair(headerName, String.valueOf(value))); } } - String location = response.getRedirectedUrl(); + String location = this.response.getRedirectedUrl(); if (location != null) { responseHeaders.add(new NameValuePair("Location", location)); } return responseHeaders; } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcher.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcher.java index c4593bd4d8..13d6b6e546 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcher.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcher.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.util.regex.Pattern; @@ -20,22 +21,25 @@ import java.util.regex.Pattern; import com.gargoylesoftware.htmlunit.WebRequest; /** - *

- * An implementation of WebRequestMatcher that allows matching on WebRequest#getUrl().toExternalForm() using a regular expression. For example, if you would like to match on the domain code.jquery.com, you might want to use the following:

+ * A {@link WebRequestMatcher} that allows matching on + * {@code WebRequest#getUrl().toExternalForm()} using a regular expression. * - *
- * WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");
- * 
+ *

For example, if you would like to match on the domain {@code code.jquery.com}, + * you might want to use the following. + * + *

WebRequestMatcher cdnMatcher = new UrlRegexRequestMatcher(".*?//code.jquery.com/.*");
* * @author Rob Winch + * @author Sam Brannen * @since 4.2 * @see org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection */ public final class UrlRegexRequestMatcher implements WebRequestMatcher { - private Pattern pattern; + + private final Pattern pattern; public UrlRegexRequestMatcher(String regex) { - pattern = Pattern.compile(regex); + this.pattern = Pattern.compile(regex); } public UrlRegexRequestMatcher(Pattern pattern) { @@ -45,6 +49,7 @@ public final class UrlRegexRequestMatcher implements WebRequestMatcher { @Override public boolean matches(WebRequest request) { String url = request.getUrl().toExternalForm(); - return pattern.matcher(url).matches(); + return this.pattern.matcher(url).matches(); } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/WebRequestMatcher.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/WebRequestMatcher.java index 034295490a..f4f3a32888 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/WebRequestMatcher.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/WebRequestMatcher.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,22 +13,24 @@ * License for the specific language governing permissions and limitations under * the License. */ - package org.springframework.test.web.servlet.htmlunit; + +package org.springframework.test.web.servlet.htmlunit; import com.gargoylesoftware.htmlunit.WebRequest; /** - * Strategy to match on a WebRequest + * Strategy for matching on a {@link WebRequest}. * * @author Rob Winch * @since 4.2 */ public interface WebRequestMatcher { + /** - * Return true if matches on WebRequest, else false - * - * @param request the WebRequest to attempt to match on - * @return true if matches on WebRequest, else false + * Whether this matcher matches on the supplied web request. + * @param request the {@link WebRequest} to attempt to match on + * @return {@code true} if this matcher matches on the {@code WebRequest} */ boolean matches(WebRequest request); + } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/package-info.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/package-info.java index 40810e0127..e94ee98667 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/package-info.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/package-info.java @@ -1,7 +1,6 @@ /** - * Support for MockMvc and HtmlUnit integration - * - * @author Rob Winch - * @since 4.2 + * Server-side support for testing Spring MVC applications with {@code MockMvc} + * and HtmlUnit. + * @see org.springframework.test.web.servlet.MockMvc */ package org.springframework.test.web.servlet.htmlunit; \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java index a732863c11..711ec484ea 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilder.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,9 +13,9 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit.webdriver; -import com.gargoylesoftware.htmlunit.BrowserVersion; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.springframework.test.web.servlet.MockMvc; @@ -23,18 +23,27 @@ import org.springframework.test.web.servlet.htmlunit.MockMvcWebConnectionBuilder import org.springframework.test.web.servlet.setup.MockMvcConfigurer; import org.springframework.web.context.WebApplicationContext; +import com.gargoylesoftware.htmlunit.BrowserVersion; +import com.gargoylesoftware.htmlunit.WebClient; + /** - * Convenience class for building an HtmlUnitDriver that will delegate to MockMvc and - * optionally delegate to an actual connection for specific requests. + * Convenience class for building an {@link HtmlUnitDriver} that delegates + * to {@link MockMvc} and optionally delegates to an actual connection for + * specific requests. * - * By default localhost will delegate to MockMvc and any other URL will delegate + *

By default, the driver will delegate to {@code MockMvc} to handle + * requests to {@code localhost} and to a {@link WebClient} to handle any + * other URL (i.e. to perform an actual HTTP request). * * @author Rob Winch + * @author Sam Brannen * @since 4.2 */ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSupport { + private boolean javascriptEnabled = true; + protected MockMvcHtmlUnitDriverBuilder(MockMvc mockMvc) { super(mockMvc); } @@ -48,9 +57,8 @@ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSup } /** - * Creates a new instance with a WebApplicationContext. - * - * @param context the WebApplicationContext to use. Cannot be null. + * Create a new instance using the supplied {@link WebApplicationContext}. + * @param context the WebApplicationContext to use; never {@code null} * @return the MockMvcHtmlUnitDriverBuilder to customize */ public static MockMvcHtmlUnitDriverBuilder webAppContextSetup(WebApplicationContext context) { @@ -58,20 +66,21 @@ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSup } /** - * Creates a new instance using a WebApplicationContext - * @param context the WebApplicationContext to create a MockMvc instance from. - * @param configurer the MockMvcConfigurer to apply - * Cannot be null. + * Create a new instance using the supplied {@link WebApplicationContext} + * and {@link MockMvcConfigurer}. + * @param context the WebApplicationContext to create a MockMvc instance from; + * never {@code null} + * @param configurer the MockMvcConfigurer to apply; never {@code null} * @return the MockMvcHtmlUnitDriverBuilder to customize */ - public static MockMvcHtmlUnitDriverBuilder webAppContextSetup(WebApplicationContext context, MockMvcConfigurer configurer) { + public static MockMvcHtmlUnitDriverBuilder webAppContextSetup(WebApplicationContext context, + MockMvcConfigurer configurer) { return new MockMvcHtmlUnitDriverBuilder(context, configurer); } /** - * Creates a new instance with a MockMvc instance. - * - * @param mockMvc the MockMvc to use. Cannot be null. + * Create a new instance using the supplied {@link MockMvc} instance. + * @param mockMvc the MockMvc instance to use; never {@code null} * @return the MockMvcHtmlUnitDriverBuilder to customize */ public static MockMvcHtmlUnitDriverBuilder mockMvcSetup(MockMvc mockMvc) { @@ -79,8 +88,8 @@ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSup } /** - * Specifies if JavaScript should be enabled or not. Default is true. - * + * Specify whether JavaScript should be enabled. + *

Default is {@code true}. * @param javascriptEnabled if JavaScript should be enabled or not. * @return the builder for further customizations */ @@ -90,10 +99,10 @@ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSup } /** - * Creates a new HtmlUnitDriver with the BrowserVersion set to CHROME. For additional - * configuration options, use configureDriver. - * - * @return the HtmlUnitDriver to use + * Create a new {@link HtmlUnitDriver} with the {@link BrowserVersion} + * set to {@link BrowserVersion#CHROME CHROME}. + *

For additional configuration options, use {@link #configureDriver}. + * @return the {@code HtmlUnitDriver} to use * @see #configureDriver(WebConnectionHtmlUnitDriver) */ public HtmlUnitDriver createDriver() { @@ -101,14 +110,14 @@ public class MockMvcHtmlUnitDriverBuilder extends MockMvcWebConnectionBuilderSup } /** - * Configures an existing WebConnectionHtmlUnitDriver. - * + * Configure an existing {@link WebConnectionHtmlUnitDriver}. * @param driver the WebConnectionHtmlUnitDriver to configure - * @return the HtmlUnitDriver to use + * @return the {@code HtmlUnitDriver} to use */ public HtmlUnitDriver configureDriver(WebConnectionHtmlUnitDriver driver) { driver.setJavascriptEnabled(javascriptEnabled); driver.setWebConnection(createConnection(driver.getWebConnection())); return driver; } + } \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java index 8b48e20921..bdfc41dd9b 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriver.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,31 +13,38 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit.webdriver; +import org.openqa.selenium.Capabilities; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; + +import org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection; +import org.springframework.util.Assert; + import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebConnection; -import org.openqa.selenium.Capabilities; -import org.openqa.selenium.htmlunit.HtmlUnitDriver; - -import org.springframework.util.Assert; /** - *

- * Allows configuring the WebConnection for an HtmlUnitDriver instance. This is useful - * because it allows a MockMvcWebConnection to be injected. - *

+ * {@code WebConnectionHtmlUnitDriver} enables configuration of the + * {@link WebConnection} for an {@link HtmlUnitDriver} instance. + * + *

This is useful because it allows a {@link MockMvcWebConnection} to + * be injected. * * @author Rob Winch + * @author Sam Brannen * @since 4.2 * @see MockMvcHtmlUnitDriverBuilder */ public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver { + private WebClient webClient; - public WebConnectionHtmlUnitDriver(BrowserVersion version) { - super(version); + + public WebConnectionHtmlUnitDriver(BrowserVersion browserVersion) { + super(browserVersion); } public WebConnectionHtmlUnitDriver() { @@ -52,45 +59,48 @@ public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver { } /** - * Captures the WebClient that is used so that its WebConnection is accessible. - * - * @param client The client to modify - * @return The modified client + * Modify the supplied {@link WebClient}, {@linkplain #configureWebClient + * configure} it, and retain a reference to it so that its {@link WebConnection} + * is {@linkplain #getWebConnection accessible} for later use. + * @param client the client to modify + * @return the modified client + * @see org.openqa.selenium.htmlunit.HtmlUnitDriver#modifyWebClient(WebClient) */ @Override - protected final WebClient modifyWebClient(WebClient client) { - webClient = super.modifyWebClient(client); - webClient = configureWebClient(webClient); - return webClient; + protected final WebClient modifyWebClient(WebClient webClient) { + this.webClient = super.modifyWebClient(webClient); + this.webClient = configureWebClient(this.webClient); + return this.webClient; } /** - * Subclasses can override this method to customise the WebClient that the HtmlUnit - * driver uses. - * - * @param client The client to modify - * @return The modified client + * Configure the supplied {@link WebClient}. + *

The default implementation simply returns the supplied client + * unmodified. + *

Subclasses can override this method to customize the {@code WebClient} + * that the {@link HtmlUnitDriver} driver uses. + * @param client the client to configure + * @return the configured client */ protected WebClient configureWebClient(WebClient client) { return client; } /** - * Allows accessing the current WebConnection - * - * @return the current WebConnection + * Access the current {@link WebConnection} for the {@link WebClient}. + * @return the current {@code WebConnection} */ public WebConnection getWebConnection() { - return webClient.getWebConnection(); + return this.webClient.getWebConnection(); } /** - * Sets the WebConnection to be used. - * - * @param webConnection the WebConnection to use. Cannot be null. + * Set the {@link WebConnection} to be used with the {@link WebClient}. + * @param webConnection the {@code WebConnection} to use; never {@code null} */ public void setWebConnection(WebConnection webConnection) { - Assert.notNull(webConnection, "webConnection cannot be null"); + Assert.notNull(webConnection, "WebConnection must not be null"); this.webClient.setWebConnection(webConnection); } + } \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/package-info.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/package-info.java index 9b4861d7e2..afc0f6881d 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/package-info.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/webdriver/package-info.java @@ -1,7 +1,7 @@ /** - * Support for MockMvc and HtmlUnitDriver - * - * @author Rob Winch - * @since 4.2 + * Server-side support for testing Spring MVC applications with {@code MockMvc} + * and the Selenium {@code HtmlUnitDriver}. + * @see org.springframework.test.web.servlet.MockMvc + * @see org.openqa.selenium.htmlunit.HtmlUnitDriver */ package org.springframework.test.web.servlet.htmlunit.webdriver; \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java index 98679a0511..eed3271f97 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,11 +13,24 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.net.URL; import java.util.Collections; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import org.springframework.stereotype.Controller; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection.DelegateWebConnection; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + import com.gargoylesoftware.htmlunit.HttpWebConnection; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebClient; @@ -26,29 +39,20 @@ import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.WebResponse; import com.gargoylesoftware.htmlunit.WebResponseData; import com.gargoylesoftware.htmlunit.util.NameValuePair; + import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.Matchers.isEmptyString; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertThat; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; import static org.mockito.Mockito.*; -import org.mockito.runners.MockitoJUnitRunner; -import static org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection.DelegateWebConnection; - -import org.springframework.stereotype.Controller; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; /** * @author Rob Winch + * @since 4.2 */ @RunWith(MockitoJUnitRunner.class) public class DelegatingWebConnectionTests { - private DelegatingWebConnection webConnection; @Mock private WebRequestMatcher matcher1; @@ -61,6 +65,7 @@ public class DelegatingWebConnectionTests { @Mock private WebConnection connection2; + private DelegatingWebConnection webConnection; private WebRequest request; private WebResponse expectedResponse; @@ -134,4 +139,5 @@ public class DelegatingWebConnectionTests { @Controller static class TestController {} -} \ No newline at end of file + +} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/ForwardController.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/ForwardController.java index 56dba45761..367b717738 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/ForwardController.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/ForwardController.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import org.springframework.stereotype.Controller; @@ -20,12 +21,14 @@ import org.springframework.web.bind.annotation.RequestMapping; /** * @author Rob Winch + * @since 4.2 */ - @Controller +@Controller public class ForwardController { @RequestMapping("/forward") public String forward() { return "forward:/"; } + } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java index 95c0f80572..80b64c7513 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import javax.servlet.http.HttpServletRequest; @@ -23,12 +24,15 @@ import org.springframework.web.bind.annotation.ResponseBody; /** * @author Rob Winch + * @since 4.2 */ @Controller public class HelloController { + @RequestMapping @ResponseBody public String header(HttpServletRequest request) { return "hello"; } + } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java index 1269114690..5d69bd953c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,20 +13,21 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.net.URL; -import com.gargoylesoftware.htmlunit.WebRequest; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; import org.junit.Test; -import org.springframework.test.web.servlet.htmlunit.HostRequestMatcher; -import org.springframework.test.web.servlet.htmlunit.WebRequestMatcher; +import com.gargoylesoftware.htmlunit.WebRequest; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; /** * @author Rob Winch + * @since 4.2 */ public class HostRequestMatcherTests { @@ -76,4 +77,5 @@ public class HostRequestMatcherTests { matches = matcher.matches(new WebRequest(new URL("http://localhost:9090/jquery-1.11.0.min.js"))); assertThat(matches, equalTo(false));; } -} \ No newline at end of file + +} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java index 5572fa3f63..a7810b6561 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.net.MalformedURLException; @@ -28,19 +29,10 @@ import javax.servlet.ServletContext; import javax.servlet.http.Cookie; import javax.servlet.http.HttpSession; -import com.gargoylesoftware.htmlunit.CookieManager; -import com.gargoylesoftware.htmlunit.HttpMethod; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.WebRequest; -import com.gargoylesoftware.htmlunit.util.NameValuePair; import org.apache.commons.io.IOUtils; import org.apache.http.auth.UsernamePasswordCredentials; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; - import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockServletContext; @@ -48,12 +40,21 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import com.gargoylesoftware.htmlunit.HttpMethod; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.WebRequest; +import com.gargoylesoftware.htmlunit.util.NameValuePair; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + /** - * * @author Rob Winch - * + * @since 4.2 */ public class HtmlUnitRequestBuilderTests { + private WebRequest webRequest; private ServletContext servletContext; @@ -64,6 +65,7 @@ public class HtmlUnitRequestBuilderTests { private HtmlUnitRequestBuilder requestBuilder; + @Before public void setUp() throws Exception { sessions = new HashMap<>(); @@ -779,4 +781,5 @@ public class HtmlUnitRequestBuilderTests { private String getContextPath() { return (String) ReflectionTestUtils.getField(requestBuilder, "contextPath"); } + } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java index a6343cca24..442c6072d1 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java @@ -13,24 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.test.web.servlet.htmlunit; + +package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; import java.net.URL; + import javax.servlet.http.HttpServletRequest; -import com.gargoylesoftware.htmlunit.WebConnection; -import com.gargoylesoftware.htmlunit.WebRequest; -import com.gargoylesoftware.htmlunit.WebResponse; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import static org.mockito.Mockito.mock; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; @@ -43,13 +36,26 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import com.gargoylesoftware.htmlunit.WebConnection; +import com.gargoylesoftware.htmlunit.WebRequest; +import com.gargoylesoftware.htmlunit.WebResponse; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + /** * @author Rob Winch + * @since 4.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration +@SuppressWarnings("rawtypes") public class MockMvcConnectionBuilderSupportTests { + @Autowired WebApplicationContext context; @@ -152,4 +158,5 @@ public class MockMvcConnectionBuilderSupportTests { } } } -} \ No newline at end of file + +} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java index c569052ca2..71ebb43e3d 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,18 +13,13 @@ * License for the specific language governing permissions and limitations under * the License. */ - package org.springframework.test.web.servlet.htmlunit; + +package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; import java.net.URL; import javax.servlet.http.HttpServletRequest; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.WebRequest; -import com.gargoylesoftware.htmlunit.WebResponse; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,13 +36,23 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.WebRequest; +import com.gargoylesoftware.htmlunit.WebResponse; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + /** * @author Rob Winch + * @since 4.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration public class MockMvcWebClientBuilderTests { + @Autowired WebApplicationContext context; @@ -114,4 +119,5 @@ public class MockMvcWebClientBuilderTests { } } } -} \ No newline at end of file + +} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java index 04344cf0a1..c9b1386dae 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,24 +13,28 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.io.IOException; -import com.gargoylesoftware.htmlunit.Page; -import com.gargoylesoftware.htmlunit.WebClient; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; - import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.WebClient; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + /** * @author Rob Winch + * @since 4.2 */ public class MockMvcWebConnectionTests { + MockMvc mockMvc; WebClient webClient; @@ -89,4 +93,5 @@ public class MockMvcWebConnectionTests { public void contextPathEndsWithSlash() throws IOException { new MockMvcWebConnection(mockMvc, "/context/"); } -} \ No newline at end of file + +} diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java index b4134894a8..770f1e06ee 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java @@ -4,30 +4,32 @@ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.net.URL; import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletResponse; + import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.WebResponse; import com.gargoylesoftware.htmlunit.util.NameValuePair; + import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; -import org.junit.Before; -import org.junit.Test; - -import org.springframework.mock.web.MockHttpServletResponse; /** - * * @author Rob Winch + * @since 4.2 */ public class MockWebResponseBuilderTests { @@ -132,4 +134,5 @@ public class MockWebResponseBuilderTests { assertThat(webResponse.getWebRequest(), equalTo(webRequest));; } + } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java index 40ce005614..76c9a5e757 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,20 +13,21 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit; import java.net.URL; -import com.gargoylesoftware.htmlunit.WebRequest; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; import org.junit.Test; -import org.springframework.test.web.servlet.htmlunit.UrlRegexRequestMatcher; -import org.springframework.test.web.servlet.htmlunit.WebRequestMatcher; +import com.gargoylesoftware.htmlunit.WebRequest; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; /** * @author Rob Winch + * @since 4.2 */ public class UrlRegexRequestMatcherTests { @@ -40,4 +41,5 @@ public class UrlRegexRequestMatcherTests { matches = cdnMatcher.matches(new WebRequest(new URL("http://localhost/jquery-1.11.0.min.js"))); assertThat(matches, equalTo(false)); } + } \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java index 871273b134..42c3e26592 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java @@ -5,7 +5,7 @@ * use this file except in compliance with the License. You may obtain a copy of * the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT @@ -13,6 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ + package org.springframework.test.web.servlet.htmlunit.webdriver; import java.io.IOException; @@ -39,6 +40,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * @author Rob Winch + * @since 4.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java index afcdf185c7..627a533f11 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java @@ -1,25 +1,45 @@ +/* + * Copyright 2002-2015 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. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + package org.springframework.test.web.servlet.htmlunit.webdriver; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + import com.gargoylesoftware.htmlunit.WebConnection; import com.gargoylesoftware.htmlunit.WebRequest; + import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.fail; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; import static org.mockito.Matchers.any; -import org.mockito.Mock; import static org.mockito.Mockito.when; -import org.mockito.runners.MockitoJUnitRunner; /** * @author Rob Winch + * @since 4.2 */ -// tag::junit-spring-setup[] @RunWith(MockitoJUnitRunner.class) public class WebConnectionHtmlUnitDriverTests { + @Mock WebConnection connection; @@ -52,4 +72,5 @@ public class WebConnectionHtmlUnitDriverTests { public void setWebConnectionNull() { driver.setWebConnection(null); } + } \ No newline at end of file