Skip to content

Commit 9475c06

Browse files
committed
Polishing
1 parent 8c6cb3e commit 9475c06

File tree

8 files changed

+41
-48
lines changed

8 files changed

+41
-48
lines changed

spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.test.web.servlet.MockMvc;
3333
import org.springframework.test.web.servlet.MvcResult;
3434
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
35+
import org.springframework.util.Assert;
3536

3637
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
3738

@@ -47,9 +48,11 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
4748

4849

4950
public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
51+
Assert.notNull(mockMvc, "MockMvc must not be null");
5052
this.mockMvc = mockMvc;
5153
}
5254

55+
5356
@Override
5457
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
5558
return new MockClientHttpRequest(httpMethod, uri) {
@@ -59,17 +62,13 @@ public ClientHttpResponse executeInternal() throws IOException {
5962
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString());
6063
requestBuilder.content(getBodyAsBytes());
6164
requestBuilder.headers(getHeaders());
62-
6365
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
64-
6566
MockHttpServletResponse servletResponse = mvcResult.getResponse();
6667
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
6768
byte[] body = servletResponse.getContentAsByteArray();
6869
HttpHeaders headers = getResponseHeaders(servletResponse);
69-
7070
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
7171
clientResponse.getHeaders().putAll(headers);
72-
7372
return clientResponse;
7473
}
7574
catch (Exception ex) {

spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
public interface RequestMatcher {
3030

3131
/**
32-
* Match the given request against some expectations.
32+
* Match the given request against specific expectations.
3333
* @param request the request to make assertions on
3434
* @throws IOException in case of I/O errors
3535
* @throws AssertionError if expectations are not met

spring-test/src/main/java/org/springframework/test/web/client/RequestMatcherClientHttpRequest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,12 +65,10 @@ public ClientHttpResponse executeInternal() throws IOException {
6565
if (this.requestMatchers.isEmpty()) {
6666
throw new AssertionError("No request expectations to execute");
6767
}
68-
6968
if (this.responseCreator == null) {
7069
throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, " +
7170
"e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())");
7271
}
73-
7472
for (RequestMatcher requestMatcher : this.requestMatchers) {
7573
requestMatcher.match(this);
7674
}

spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ protected ContentRequestMatchers() {
5252
this.xmlHelper = new XmlExpectationsHelper();
5353
}
5454

55+
5556
/**
5657
* Assert the request content type as a String.
5758
*/
@@ -140,10 +141,8 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
140141
* Parse the request body and the given String as XML and assert that the
141142
* two are "similar" - i.e. they contain the same elements and attributes
142143
* regardless of order.
143-
*
144144
* <p>Use of this matcher assumes the
145145
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
146-
*
147146
* @param expectedXmlContent the expected XML content
148147
*/
149148
public RequestMatcher xml(final String expectedXmlContent) {
@@ -180,6 +179,7 @@ protected void matchInternal(MockClientHttpRequest request) throws Exception {
180179
};
181180
}
182181

182+
183183
/**
184184
* Abstract base class for XML {@link RequestMatcher}'s.
185185
*/
@@ -191,12 +191,12 @@ public final void match(ClientHttpRequest request) throws IOException, Assertion
191191
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
192192
matchInternal(mockRequest);
193193
}
194-
catch (Exception e) {
195-
throw new AssertionError("Failed to parse expected or actual XML request content: " + e.getMessage());
194+
catch (Exception ex) {
195+
throw new AssertionError("Failed to parse expected or actual XML request content: " + ex.getMessage());
196196
}
197197
}
198198

199199
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
200-
201200
}
201+
202202
}

spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,14 @@
1919
import java.io.IOException;
2020
import java.text.ParseException;
2121

22+
import com.jayway.jsonpath.JsonPath;
2223
import org.hamcrest.Matcher;
2324

2425
import org.springframework.http.client.ClientHttpRequest;
2526
import org.springframework.mock.http.client.MockClientHttpRequest;
2627
import org.springframework.test.util.JsonPathExpectationsHelper;
2728
import org.springframework.test.web.client.RequestMatcher;
2829

29-
import com.jayway.jsonpath.JsonPath;
30-
3130
/**
3231
* Factory for assertions on the request content using
3332
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
@@ -235,8 +234,8 @@ public final void match(ClientHttpRequest request) throws IOException, Assertion
235234
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
236235
matchInternal(mockRequest);
237236
}
238-
catch (ParseException e) {
239-
throw new AssertionError("Failed to parse JSON request content: " + e.getMessage());
237+
catch (ParseException ex) {
238+
throw new AssertionError("Failed to parse JSON request content: " + ex.getMessage());
240239
}
241240
}
242241

spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,12 +46,10 @@ public class XpathRequestMatchers {
4646
* Class constructor, not for direct instantiation. Use
4747
* {@link MockRestRequestMatchers#xpath(String, Object...)} or
4848
* {@link MockRestRequestMatchers#xpath(String, Map, Object...)}.
49-
*
5049
* @param expression the XPath expression
5150
* @param namespaces XML namespaces referenced in the XPath expression, or {@code null}
5251
* @param args arguments to parameterize the XPath expression with using the
5352
* formatting specifiers defined in {@link String#format(String, Object...)}
54-
*
5553
* @throws XPathExpressionException
5654
*/
5755
protected XpathRequestMatchers(String expression, Map<String, String> namespaces, Object ... args)
@@ -60,6 +58,7 @@ protected XpathRequestMatchers(String expression, Map<String, String> namespaces
6058
this.xpathHelper = new XpathExpectationsHelper(expression, namespaces, args);
6159
}
6260

61+
6362
/**
6463
* Apply the XPath and assert it with the given {@code Matcher<Node>}.
6564
*/
@@ -199,7 +198,6 @@ public final void match(ClientHttpRequest request) throws IOException, Assertion
199198
}
200199

201200
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
202-
203201
}
204202

205203
}

spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.test.web.client.response;
1718

1819
import java.io.IOException;
@@ -38,14 +39,14 @@
3839
*/
3940
public class DefaultResponseCreator implements ResponseCreator {
4041

42+
private HttpStatus statusCode;
43+
4144
private byte[] content;
4245

4346
private Resource contentResource;
4447

4548
private final HttpHeaders headers = new HttpHeaders();
4649

47-
private HttpStatus statusCode;
48-
4950

5051
/**
5152
* Protected constructor.
@@ -56,19 +57,6 @@ protected DefaultResponseCreator(HttpStatus statusCode) {
5657
this.statusCode = statusCode;
5758
}
5859

59-
@Override
60-
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
61-
MockClientHttpResponse response;
62-
if (this.contentResource != null) {
63-
InputStream stream = this.contentResource.getInputStream();
64-
response = new MockClientHttpResponse(stream, this.statusCode);
65-
}
66-
else {
67-
response = new MockClientHttpResponse(this.content, this.statusCode);
68-
}
69-
response.getHeaders().putAll(this.headers);
70-
return response;
71-
}
7260

7361
/**
7462
* Set the body as a UTF-8 String.
@@ -77,9 +65,9 @@ public DefaultResponseCreator body(String content) {
7765
try {
7866
this.content = content.getBytes("UTF-8");
7967
}
80-
catch (UnsupportedEncodingException e) {
68+
catch (UnsupportedEncodingException ex) {
8169
// should not happen, UTF-8 is always supported
82-
throw new IllegalStateException(e);
70+
throw new IllegalStateException(ex);
8371
}
8472
return this;
8573
}
@@ -130,4 +118,19 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
130118
return this;
131119
}
132120

121+
122+
@Override
123+
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
124+
MockClientHttpResponse response;
125+
if (this.contentResource != null) {
126+
InputStream stream = this.contentResource.getInputStream();
127+
response = new MockClientHttpResponse(stream, this.statusCode);
128+
}
129+
else {
130+
response = new MockClientHttpResponse(this.content, this.statusCode);
131+
}
132+
response.getHeaders().putAll(this.headers);
133+
return response;
134+
}
135+
133136
}

spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,10 +33,6 @@
3333
*/
3434
public abstract class MockRestResponseCreators {
3535

36-
37-
private MockRestResponseCreators() {
38-
}
39-
4036
/**
4137
* {@code ResponseCreator} for a 200 response (OK).
4238
*/

0 commit comments

Comments
 (0)