Skip to content

Commit 5b90185

Browse files
committed
Upgrade to HtmlUnit 2.19
This upgrade includes AutoCloseable support for HtmlUnit WebConnections as introduced in 2.19, while remaining compatible with 2.18. Issue: SPR-13686
1 parent ce58877 commit 5b90185

10 files changed

+76
-63
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ configure(allprojects) { project ->
4545
ext.hibval4Version = "4.3.2.Final"
4646
ext.hibval5Version = "5.2.2.Final"
4747
ext.hsqldbVersion = "2.3.3"
48-
ext.htmlunitVersion = "2.18"
48+
ext.htmlunitVersion = "2.19"
4949
ext.httpasyncVersion = "4.1.1"
5050
ext.httpclientVersion = "4.5.1"
5151
ext.jackson2Version = "2.6.3"
@@ -66,7 +66,7 @@ configure(allprojects) { project ->
6666
ext.reactorVersion = "2.0.7.RELEASE"
6767
ext.romeVersion = "1.5.1"
6868
ext.seleniumVersion = "2.48.2"
69-
ext.slf4jVersion = "1.7.12"
69+
ext.slf4jVersion = "1.7.13"
7070
ext.snakeyamlVersion = "1.16"
7171
ext.snifferVersion = "1.14"
7272
ext.testngVersion = "6.9.9"

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnection.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23-
import org.springframework.util.Assert;
24-
2523
import com.gargoylesoftware.htmlunit.WebConnection;
2624
import com.gargoylesoftware.htmlunit.WebRequest;
2725
import com.gargoylesoftware.htmlunit.WebResponse;
2826

27+
import org.springframework.util.Assert;
28+
2929
/**
3030
* Implementation of {@link WebConnection} that allows delegating to various
3131
* {@code WebConnection} implementations.
@@ -62,8 +62,8 @@ public final class DelegatingWebConnection implements WebConnection {
6262

6363

6464
public DelegatingWebConnection(WebConnection defaultConnection, List<DelegateWebConnection> connections) {
65-
Assert.notNull(defaultConnection, "defaultConnection must not be null");
66-
Assert.notEmpty(connections, "connections must not be empty");
65+
Assert.notNull(defaultConnection, "Default WebConnection must not be null");
66+
Assert.notEmpty(connections, "Connections List must not be empty");
6767
this.connections = connections;
6868
this.defaultConnection = defaultConnection;
6969
}
@@ -72,6 +72,7 @@ public DelegatingWebConnection(WebConnection defaultConnection, DelegateWebConne
7272
this(defaultConnection, Arrays.asList(connections));
7373
}
7474

75+
7576
@Override
7677
public WebResponse getResponse(WebRequest request) throws IOException {
7778
for (DelegateWebConnection connection : this.connections) {
@@ -82,26 +83,29 @@ public WebResponse getResponse(WebRequest request) throws IOException {
8283
return this.defaultConnection.getResponse(request);
8384
}
8485

86+
@Override
87+
public void close() {
88+
}
89+
8590

8691
public static final class DelegateWebConnection {
8792

8893
private final WebRequestMatcher matcher;
8994

9095
private final WebConnection delegate;
9196

92-
9397
public DelegateWebConnection(WebRequestMatcher matcher, WebConnection delegate) {
9498
this.matcher = matcher;
9599
this.delegate = delegate;
96100
}
97101

98102
private WebRequestMatcher getMatcher() {
99-
return matcher;
103+
return this.matcher;
100104
}
101105

102106
private WebConnection getDelegate() {
103-
return delegate;
107+
return this.delegate;
104108
}
105109
}
106110

107-
}
111+
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/ForwardRequestPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class ForwardRequestPostProcessor implements RequestPostProcessor {
3131

3232

3333
public ForwardRequestPostProcessor(String forwardUrl) {
34-
Assert.hasText(forwardUrl, "forwardUrl must not be null or empty");
34+
Assert.hasText(forwardUrl, "Forward URL must not be null or empty");
3535
this.forwardUrl = forwardUrl;
3636
}
3737

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
import javax.servlet.http.HttpServletRequest;
3535
import javax.servlet.http.HttpSession;
3636

37+
import com.gargoylesoftware.htmlunit.CookieManager;
38+
import com.gargoylesoftware.htmlunit.WebClient;
39+
import com.gargoylesoftware.htmlunit.WebRequest;
40+
import com.gargoylesoftware.htmlunit.util.NameValuePair;
41+
3742
import org.springframework.beans.Mergeable;
3843
import org.springframework.http.MediaType;
3944
import org.springframework.mock.web.MockHttpServletRequest;
@@ -46,11 +51,6 @@
4651
import org.springframework.web.util.UriComponents;
4752
import org.springframework.web.util.UriComponentsBuilder;
4853

49-
import com.gargoylesoftware.htmlunit.CookieManager;
50-
import com.gargoylesoftware.htmlunit.WebClient;
51-
import com.gargoylesoftware.htmlunit.WebRequest;
52-
import com.gargoylesoftware.htmlunit.util.NameValuePair;
53-
5454
/**
5555
* Internal class used to transform a {@link WebRequest} into a
5656
* {@link MockHttpServletRequest} using Spring MVC Test's {@link RequestBuilder}.
@@ -91,9 +91,9 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
9191
* {@link MockHttpServletRequest}; never {@code null}
9292
*/
9393
public HtmlUnitRequestBuilder(Map<String, MockHttpSession> sessions, WebClient webClient, WebRequest webRequest) {
94-
Assert.notNull(sessions, "sessions map must not be null");
95-
Assert.notNull(webClient, "webClient must not be null");
96-
Assert.notNull(webRequest, "webRequest must not be null");
94+
Assert.notNull(sessions, "Sessions Map must not be null");
95+
Assert.notNull(webClient, "WebClient must not be null");
96+
Assert.notNull(webRequest, "WebRequest must not be null");
9797

9898
this.sessions = sessions;
9999
this.webClient = webClient;

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package org.springframework.test.web.servlet.htmlunit;
1818

19+
import com.gargoylesoftware.htmlunit.WebClient;
20+
1921
import org.springframework.test.web.servlet.MockMvc;
2022
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
2123
import org.springframework.util.Assert;
2224
import org.springframework.web.context.WebApplicationContext;
2325

24-
import com.gargoylesoftware.htmlunit.WebClient;
25-
2626
/**
2727
* {@code MockMvcWebClientBuilder} simplifies the creation of an HtmlUnit
2828
* {@link WebClient} that delegates to a {@link MockMvc} instance.
@@ -57,6 +57,7 @@ protected MockMvcWebClientBuilder(WebApplicationContext context, MockMvcConfigur
5757
super(context, configurer);
5858
}
5959

60+
6061
/**
6162
* Create a new {@code MockMvcWebClientBuilder} based on the supplied
6263
* {@link MockMvc} instance.
@@ -104,7 +105,7 @@ public static MockMvcWebClientBuilder webAppContextSetup(WebApplicationContext c
104105
* @see #build()
105106
*/
106107
public MockMvcWebClientBuilder withDelegate(WebClient webClient) {
107-
Assert.notNull(webClient, "webClient must not be null");
108+
Assert.notNull(webClient, "WebClient must not be null");
108109
webClient.setWebConnection(createConnection(webClient.getWebConnection()));
109110
this.webClient = webClient;
110111
return this;
@@ -125,4 +126,4 @@ public WebClient build() {
125126
return (this.webClient != null ? this.webClient : withDelegate(new WebClient()).build());
126127
}
127128

128-
}
129+
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnection.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23+
import com.gargoylesoftware.htmlunit.WebClient;
24+
import com.gargoylesoftware.htmlunit.WebConnection;
25+
import com.gargoylesoftware.htmlunit.WebRequest;
26+
import com.gargoylesoftware.htmlunit.WebResponse;
27+
2328
import org.springframework.mock.web.MockHttpServletResponse;
2429
import org.springframework.mock.web.MockHttpSession;
2530
import org.springframework.test.web.servlet.MockMvc;
2631
import org.springframework.test.web.servlet.RequestBuilder;
2732
import org.springframework.test.web.servlet.ResultActions;
2833
import org.springframework.util.Assert;
2934

30-
import com.gargoylesoftware.htmlunit.WebClient;
31-
import com.gargoylesoftware.htmlunit.WebConnection;
32-
import com.gargoylesoftware.htmlunit.WebRequest;
33-
import com.gargoylesoftware.htmlunit.WebResponse;
34-
3535
/**
3636
* {@code MockMvcWebConnection} enables {@link MockMvc} to transform a
3737
* {@link WebRequest} into a {@link WebResponse}.
@@ -86,21 +86,27 @@ public MockMvcWebConnection(MockMvc mockMvc) {
8686
* @param contextPath the contextPath to use
8787
*/
8888
public MockMvcWebConnection(MockMvc mockMvc, String contextPath) {
89-
Assert.notNull(mockMvc, "mockMvc must not be null");
89+
Assert.notNull(mockMvc, "MockMvc must not be null");
9090
validateContextPath(contextPath);
9191

9292
this.webClient = new WebClient();
9393
this.mockMvc = mockMvc;
9494
this.contextPath = contextPath;
9595
}
9696

97+
98+
public void setWebClient(WebClient webClient) {
99+
Assert.notNull(webClient, "WebClient must not be null");
100+
this.webClient = webClient;
101+
}
102+
103+
97104
public WebResponse getResponse(WebRequest webRequest) throws IOException {
98105
long startTime = System.currentTimeMillis();
99106
HtmlUnitRequestBuilder requestBuilder = new HtmlUnitRequestBuilder(this.sessions, this.webClient, webRequest);
100107
requestBuilder.setContextPath(this.contextPath);
101108

102109
MockHttpServletResponse httpServletResponse = getResponse(requestBuilder);
103-
104110
String forwardedUrl = httpServletResponse.getForwardedUrl();
105111
while (forwardedUrl != null) {
106112
requestBuilder.setForwardPostProcessor(new ForwardRequestPostProcessor(forwardedUrl));
@@ -111,23 +117,23 @@ public WebResponse getResponse(WebRequest webRequest) throws IOException {
111117
return new MockWebResponseBuilder(startTime, webRequest, httpServletResponse).build();
112118
}
113119

114-
public void setWebClient(WebClient webClient) {
115-
Assert.notNull(webClient, "webClient must not be null");
116-
this.webClient = webClient;
117-
}
118-
119120
private MockHttpServletResponse getResponse(RequestBuilder requestBuilder) throws IOException {
120121
ResultActions resultActions;
121122
try {
122123
resultActions = this.mockMvc.perform(requestBuilder);
123124
}
124-
catch (Exception e) {
125-
throw (IOException) new IOException(e.getMessage()).initCause(e);
125+
catch (Exception ex) {
126+
throw new IOException(ex);
126127
}
127128

128129
return resultActions.andReturn().getResponse();
129130
}
130131

132+
@Override
133+
public void close() {
134+
}
135+
136+
131137
/**
132138
* Validate the supplied {@code contextPath}.
133139
* <p>If the value is not {@code null}, it must conform to
@@ -148,4 +154,4 @@ static void validateContextPath(String contextPath) {
148154
}
149155
}
150156

151-
}
157+
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionBuilderSupport.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import com.gargoylesoftware.htmlunit.WebConnection;
23+
2224
import org.springframework.test.web.servlet.MockMvc;
2325
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2426
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
2527
import org.springframework.util.Assert;
2628
import org.springframework.web.context.WebApplicationContext;
2729

28-
import com.gargoylesoftware.htmlunit.WebConnection;
29-
3030
/**
3131
* Support class that simplifies the creation of a {@link WebConnection} that
3232
* uses {@link MockMvc} and optionally delegates to a real {@link WebConnection}
@@ -55,7 +55,7 @@ public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebCon
5555
* @param mockMvc the {@code MockMvc} instance to use; never {@code null}
5656
*/
5757
protected MockMvcWebConnectionBuilderSupport(MockMvc mockMvc) {
58-
Assert.notNull(mockMvc, "mockMvc must not be null");
58+
Assert.notNull(mockMvc, "MockMvc must not be null");
5959
this.mockMvc = mockMvc;
6060
this.mockMvcRequestMatchers.add(new HostRequestMatcher("localhost"));
6161
}
@@ -80,6 +80,7 @@ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context, Mock
8080
this(MockMvcBuilders.webAppContextSetup(context).apply(configurer).build());
8181
}
8282

83+
8384
/**
8485
* Set the context path to use.
8586
* <p>If the supplied value is {@code null} or empty, the first path
@@ -146,7 +147,7 @@ public T useMockMvcForHosts(String... hosts) {
146147
* @see #useMockMvcForHosts(String...)
147148
*/
148149
protected final WebConnection createConnection(WebConnection defaultConnection) {
149-
Assert.notNull(defaultConnection, "defaultConnection must not be null");
150+
Assert.notNull(defaultConnection, "Default WebConnection must not be null");
150151
MockMvcWebConnection mockMvcWebConnection = new MockMvcWebConnection(this.mockMvc, this.contextPath);
151152

152153
if (this.alwaysUseMockMvc) {
@@ -162,4 +163,4 @@ protected final WebConnection createConnection(WebConnection defaultConnection)
162163
return new DelegatingWebConnection(defaultConnection, delegates);
163164
}
164165

165-
}
166+
}

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilder.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
import java.util.Collection;
2222
import java.util.List;
2323

24-
import org.springframework.http.HttpStatus;
25-
import org.springframework.mock.web.MockHttpServletResponse;
26-
import org.springframework.util.Assert;
27-
import org.springframework.util.StringUtils;
28-
2924
import com.gargoylesoftware.htmlunit.WebRequest;
3025
import com.gargoylesoftware.htmlunit.WebResponse;
3126
import com.gargoylesoftware.htmlunit.WebResponseData;
3227
import com.gargoylesoftware.htmlunit.util.NameValuePair;
3328

29+
import org.springframework.http.HttpStatus;
30+
import org.springframework.mock.web.MockHttpServletResponse;
31+
import org.springframework.util.Assert;
32+
import org.springframework.util.StringUtils;
33+
3434
/**
3535
* @author Rob Winch
3636
* @author Sam Brannen
@@ -48,8 +48,8 @@ final class MockWebResponseBuilder {
4848

4949

5050
public MockWebResponseBuilder(long startTime, WebRequest webRequest, MockHttpServletResponse response) {
51-
Assert.notNull(webRequest, "webRequest must not be null");
52-
Assert.notNull(response, "response must not be null");
51+
Assert.notNull(webRequest, "WebRequest must not be null");
52+
Assert.notNull(response, "HttpServletResponse must not be null");
5353
this.startTime = startTime;
5454
this.webRequest = webRequest;
5555
this.response = response;
@@ -63,8 +63,8 @@ public WebResponse build() throws IOException {
6363

6464
private WebResponseData webResponseData() throws IOException {
6565
List<NameValuePair> responseHeaders = responseHeaders();
66-
int statusCode = (this.response.getRedirectedUrl() != null ? HttpStatus.MOVED_PERMANENTLY.value()
67-
: this.response.getStatus());
66+
int statusCode = (this.response.getRedirectedUrl() != null ?
67+
HttpStatus.MOVED_PERMANENTLY.value() : this.response.getStatus());
6868
String statusMessage = statusMessage(statusCode);
6969
return new WebResponseData(this.response.getContentAsByteArray(), statusCode, statusMessage, responseHeaders);
7070
}

0 commit comments

Comments
 (0)