Skip to content

Commit 6924f00

Browse files
committed
Polishing
1 parent e8562bb commit 6924f00

File tree

12 files changed

+77
-95
lines changed

12 files changed

+77
-95
lines changed

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

Lines changed: 5 additions & 6 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;
1718

1819
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request;
@@ -33,6 +34,7 @@
3334
import org.springframework.test.web.servlet.MockMvc;
3435
import org.springframework.test.web.servlet.MvcResult;
3536
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
37+
import org.springframework.util.Assert;
3638

3739
/**
3840
* A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}.
@@ -46,29 +48,26 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
4648

4749

4850
public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
51+
Assert.notNull(mockMvc, "MockMvc must not be null");
4952
this.mockMvc = mockMvc;
5053
}
5154

55+
5256
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
5357
return new MockClientHttpRequest(httpMethod, uri) {
54-
5558
@Override
5659
public ClientHttpResponse executeInternal() throws IOException {
5760
try {
5861
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString());
5962
requestBuilder.content(getBodyAsBytes());
6063
requestBuilder.headers(getHeaders());
61-
6264
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
63-
6465
MockHttpServletResponse servletResponse = mvcResult.getResponse();
6566
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
6667
byte[] body = servletResponse.getContentAsByteArray();
6768
HttpHeaders headers = getResponseHeaders(servletResponse);
68-
6969
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
7070
clientResponse.getHeaders().putAll(headers);
71-
7271
return clientResponse;
7372
}
7473
catch (Exception ex) {

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

Lines changed: 2 additions & 3 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.
@@ -28,8 +28,7 @@
2828
public interface RequestMatcher {
2929

3030
/**
31-
* Match the given request against some expectations.
32-
*
31+
* Match the given request against specific expectations.
3332
* @param request the request to make assertions on
3433
* @throws IOException in case of I/O errors
3534
* @throws AssertionError if expectations are not met

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

Lines changed: 4 additions & 8 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.
@@ -45,6 +45,7 @@ public RequestMatcherClientHttpRequest(RequestMatcher requestMatcher) {
4545
this.requestMatchers.add(requestMatcher);
4646
}
4747

48+
4849
public ResponseActions andExpect(RequestMatcher requestMatcher) {
4950
Assert.notNull(requestMatcher, "RequestMatcher is required");
5051
this.requestMatchers.add(requestMatcher);
@@ -57,22 +58,17 @@ public void andRespond(ResponseCreator responseCreator) {
5758
}
5859

5960
public ClientHttpResponse executeInternal() throws IOException {
60-
6161
if (this.requestMatchers.isEmpty()) {
6262
throw new AssertionError("No request expectations to execute");
6363
}
64-
6564
if (this.responseCreator == null) {
66-
throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, "
67-
+ "e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())");
65+
throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, " +
66+
"e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())");
6867
}
69-
7068
for (RequestMatcher requestMatcher : this.requestMatchers) {
7169
requestMatcher.match(this);
7270
}
73-
7471
setResponse(this.responseCreator.createResponse(this));
75-
7672
return super.executeInternal();
7773
}
7874

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 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
*/
@@ -135,10 +136,8 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
135136
* Parse the request body and the given String as XML and assert that the
136137
* two are "similar" - i.e. they contain the same elements and attributes
137138
* regardless of order.
138-
*
139139
* <p>Use of this matcher assumes the
140140
* <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
141-
*
142141
* @param expectedXmlContent the expected XML content
143142
*/
144143
public RequestMatcher xml(final String expectedXmlContent) {
@@ -175,6 +174,7 @@ protected void matchInternal(MockClientHttpRequest request) throws Exception {
175174
};
176175
}
177176

177+
178178
/**
179179
* Abstract base class for XML {@link RequestMatcher}'s.
180180
*/
@@ -191,6 +191,6 @@ public final void match(ClientHttpRequest request) throws IOException, Assertion
191191
}
192192

193193
protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
194-
195194
}
195+
196196
}

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

Lines changed: 6 additions & 7 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.
@@ -25,8 +25,8 @@
2525
import org.springframework.test.web.client.RequestMatcher;
2626

2727
/**
28-
* Factory methods for request content {@code RequestMatcher}'s using a <a
29-
* href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression.
28+
* Factory methods for request content {@code RequestMatcher}'s using a
29+
* <a href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression.
3030
* An instance of this class is typically accessed via
3131
* {@code RequestMatchers.jsonPath(..)}.
3232
*
@@ -42,7 +42,6 @@ public class JsonPathRequestMatchers {
4242
* Class constructor, not for direct instantiation. Use
4343
* {@link MockRestRequestMatchers#jsonPath(String, Matcher)} or
4444
* {@link MockRestRequestMatchers#jsonPath(String, Object...)}.
45-
*
4645
* @param expression the JSONPath expression
4746
* @param args arguments to parameterize the JSONPath expression with using
4847
* the formatting specifiers defined in
@@ -123,12 +122,12 @@ public final void match(ClientHttpRequest request) throws IOException, Assertion
123122
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
124123
matchInternal(mockRequest);
125124
}
126-
catch (ParseException e) {
127-
throw new AssertionError("Failed to parse JSON request content: " + e.getMessage());
125+
catch (ParseException ex) {
126+
throw new AssertionError("Failed to parse JSON request content: " + ex.getMessage());
128127
}
129128
}
130129

131130
protected abstract void matchInternal(MockClientHttpRequest request) throws IOException, ParseException;
132-
133131
}
132+
134133
}

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

Lines changed: 7 additions & 22 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.
@@ -47,13 +47,6 @@
4747
*/
4848
public abstract class MockRestRequestMatchers {
4949

50-
51-
/**
52-
* Private class constructor.
53-
*/
54-
private MockRestRequestMatchers() {
55-
}
56-
5750
/**
5851
* Match to any request.
5952
*/
@@ -66,7 +59,6 @@ public void match(ClientHttpRequest request) throws AssertionError {
6659

6760
/**
6861
* Assert the request URI string with the given matcher.
69-
*
7062
* @param matcher String matcher for the expected URI
7163
* @return the request matcher
7264
*/
@@ -81,7 +73,6 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
8173

8274
/**
8375
* Assert the request URI string.
84-
*
8576
* @param expectedUri the expected URI
8677
* @return the request matcher
8778
*/
@@ -96,7 +87,6 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
9687

9788
/**
9889
* Assert the {@link HttpMethod} of the request.
99-
*
10090
* @param method the HTTP method
10191
* @return the request matcher
10292
*/
@@ -111,7 +101,6 @@ public void match(ClientHttpRequest request) throws AssertionError {
111101

112102
/**
113103
* Expect a request to the given URI.
114-
*
115104
* @param uri the expected URI
116105
* @return the request matcher
117106
*/
@@ -156,8 +145,8 @@ public void match(ClientHttpRequest request) {
156145
private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) {
157146
List<String> actualValues = headers.get(name);
158147
AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null);
159-
AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount
160-
+ "> values but found " + actualValues, expectedCount <= actualValues.size());
148+
AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount +
149+
"> values but found " + actualValues, expectedCount <= actualValues.size());
161150
}
162151

163152
/**
@@ -168,12 +157,11 @@ public static ContentRequestMatchers content() {
168157
}
169158

170159
/**
171-
* Access to request body matchers using a <a
172-
* href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression to
160+
* Access to request body matchers using a
161+
* <a href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression to
173162
* inspect a specific subset of the body. The JSON path expression can be a
174163
* parameterized string using formatting specifiers as defined in
175164
* {@link String#format(String, Object...)}.
176-
*
177165
* @param expression the JSON path optionally parameterized with arguments
178166
* @param args arguments to parameterize the JSON path expression with
179167
*/
@@ -182,11 +170,10 @@ public static JsonPathRequestMatchers jsonPath(String expression, Object ... arg
182170
}
183171

184172
/**
185-
* Access to request body matchers using a <a
186-
* href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression to
173+
* Access to request body matchers using a
174+
* <a href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression to
187175
* inspect a specific subset of the body and a Hamcrest match for asserting
188176
* the value found at the JSON path.
189-
*
190177
* @param expression the JSON path expression
191178
* @param matcher a matcher for the value expected at the JSON path
192179
*/
@@ -199,7 +186,6 @@ public static <T> RequestMatcher jsonPath(String expression, Matcher<T> matcher)
199186
* subset of the body. The XPath expression can be a parameterized string
200187
* using formatting specifiers as defined in
201188
* {@link String#format(String, Object...)}.
202-
*
203189
* @param expression the XPath optionally parameterized with arguments
204190
* @param args arguments to parameterize the XPath expression with
205191
*/
@@ -212,7 +198,6 @@ public static XpathRequestMatchers xpath(String expression, Object... args) thro
212198
* subset of the body. The XPath expression can be a parameterized string
213199
* using formatting specifiers as defined in
214200
* {@link String#format(String, Object...)}.
215-
*
216201
* @param expression the XPath optionally parameterized with arguments
217202
* @param namespaces namespaces referenced in the XPath expression
218203
* @param args arguments to parameterize the XPath expression with

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

Lines changed: 3 additions & 3 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.match;
1718

1819
import java.io.IOException;
@@ -44,12 +45,10 @@ public class XpathRequestMatchers {
4445
* Class constructor, not for direct instantiation. Use
4546
* {@link MockRestRequestMatchers#xpath(String, Object...)} or
4647
* {@link MockRestRequestMatchers#xpath(String, Map, Object...)}.
47-
*
4848
* @param expression the XPath expression
4949
* @param namespaces XML namespaces referenced in the XPath expression, or {@code null}
5050
* @param args arguments to parameterize the XPath expression with using the
5151
* formatting specifiers defined in {@link String#format(String, Object...)}
52-
*
5352
* @throws XPathExpressionException
5453
*/
5554
protected XpathRequestMatchers(String expression, Map<String, String> namespaces, Object ... args)
@@ -58,6 +57,7 @@ protected XpathRequestMatchers(String expression, Map<String, String> namespaces
5857
this.xpathHelper = new XpathExpectationsHelper(expression, namespaces, args);
5958
}
6059

60+
6161
/**
6262
* Apply the XPath and assert it with the given {@code Matcher<Node>}.
6363
*/

0 commit comments

Comments
 (0)