Skip to content

Commit 8acceba

Browse files
committed
Polishing
(cherry picked from commit aade2d1)
1 parent eae079a commit 8acceba

File tree

8 files changed

+30
-37
lines changed

8 files changed

+30
-37
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public class DefaultRequestExpectation implements RequestExpectation {
4646
* @param expectedCount the expected request expectedCount
4747
*/
4848
public DefaultRequestExpectation(ExpectedCount expectedCount, RequestMatcher requestMatcher) {
49-
Assert.notNull(expectedCount, "'expectedCount' is required");
50-
Assert.notNull(requestMatcher, "'requestMatcher' is required");
49+
Assert.notNull(expectedCount, "ExpectedCount is required");
50+
Assert.notNull(requestMatcher, "RequestMatcher is required");
5151
this.requestCount = new RequestCount(expectedCount);
5252
this.requestMatchers.add(requestMatcher);
5353
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.test.web.servlet.MockMvc;
3434
import org.springframework.test.web.servlet.MvcResult;
3535
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
36+
import org.springframework.util.Assert;
3637

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

@@ -48,6 +49,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
4849

4950

5051
public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
52+
Assert.notNull(mockMvc, "MockMvc must not be null");
5153
this.mockMvc = mockMvc;
5254
}
5355

@@ -61,17 +63,13 @@ public ClientHttpResponse executeInternal() throws IOException {
6163
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString());
6264
requestBuilder.content(getBodyAsBytes());
6365
requestBuilder.headers(getHeaders());
64-
6566
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
66-
6767
MockHttpServletResponse servletResponse = mvcResult.getResponse();
6868
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
6969
byte[] body = servletResponse.getContentAsByteArray();
7070
HttpHeaders headers = getResponseHeaders(servletResponse);
71-
7271
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
7372
clientResponse.getHeaders().putAll(headers);
74-
7573
return clientResponse;
7674
}
7775
catch (Exception ex) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
public interface RequestMatcher {
3434

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected ContentRequestMatchers() {
5858
this.xmlHelper = new XmlExpectationsHelper();
5959
}
6060

61+
6162
/**
6263
* Assert the request content type as a String.
6364
*/

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: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
*/
4040
public class DefaultResponseCreator implements ResponseCreator {
4141

42+
private HttpStatus statusCode;
43+
4244
private byte[] content;
4345

4446
private Resource contentResource;
4547

4648
private final HttpHeaders headers = new HttpHeaders();
4749

48-
private HttpStatus statusCode;
49-
5050

5151
/**
5252
* Protected constructor.
@@ -58,20 +58,6 @@ protected DefaultResponseCreator(HttpStatus statusCode) {
5858
}
5959

6060

61-
@Override
62-
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
63-
MockClientHttpResponse response;
64-
if (this.contentResource != null) {
65-
InputStream stream = this.contentResource.getInputStream();
66-
response = new MockClientHttpResponse(stream, this.statusCode);
67-
}
68-
else {
69-
response = new MockClientHttpResponse(this.content, this.statusCode);
70-
}
71-
response.getHeaders().putAll(this.headers);
72-
return response;
73-
}
74-
7561
/**
7662
* Set the body as a UTF-8 String.
7763
*/
@@ -126,4 +112,19 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
126112
return this;
127113
}
128114

115+
116+
@Override
117+
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
118+
MockClientHttpResponse response;
119+
if (this.contentResource != null) {
120+
InputStream stream = this.contentResource.getInputStream();
121+
response = new MockClientHttpResponse(stream, this.statusCode);
122+
}
123+
else {
124+
response = new MockClientHttpResponse(this.content, this.statusCode);
125+
}
126+
response.getHeaders().putAll(this.headers);
127+
return response;
128+
}
129+
129130
}

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)