Skip to content

Commit aade2d1

Browse files
committed
Polishing
(cherry picked from commit 9475c06)
1 parent 4627545 commit aade2d1

File tree

8 files changed

+31
-38
lines changed

8 files changed

+31
-38
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

@@ -50,6 +51,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
5051

5152

5253
public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
54+
Assert.notNull(mockMvc, "MockMvc must not be null");
5355
this.mockMvc = mockMvc;
5456
}
5557

@@ -63,17 +65,13 @@ public ClientHttpResponse executeInternal() throws IOException {
6365
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString());
6466
requestBuilder.content(getBodyAsBytes());
6567
requestBuilder.headers(getHeaders());
66-
6768
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
68-
6969
MockHttpServletResponse servletResponse = mvcResult.getResponse();
7070
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
7171
byte[] body = servletResponse.getContentAsByteArray();
7272
HttpHeaders headers = getResponseHeaders(servletResponse);
73-
7473
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
7574
clientResponse.getHeaders().putAll(headers);
76-
7775
return clientResponse;
7876
}
7977
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.
@@ -32,7 +32,7 @@
3232
public interface RequestMatcher {
3333

3434
/**
35-
* Match the given request against some expectations.
35+
* Match the given request against specific expectations.
3636
* @param request the request to make assertions on
3737
* @throws IOException in case of I/O errors
3838
* @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
@@ -42,14 +42,14 @@ public class DefaultResponseCreator implements ResponseCreator {
4242
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
4343

4444

45+
private HttpStatus statusCode;
46+
4547
private byte[] content;
4648

4749
private Resource contentResource;
4850

4951
private final HttpHeaders headers = new HttpHeaders();
5052

51-
private HttpStatus statusCode;
52-
5353

5454
/**
5555
* Protected constructor.
@@ -61,20 +61,6 @@ protected DefaultResponseCreator(HttpStatus statusCode) {
6161
}
6262

6363

64-
@Override
65-
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
66-
MockClientHttpResponse response;
67-
if (this.contentResource != null) {
68-
InputStream stream = this.contentResource.getInputStream();
69-
response = new MockClientHttpResponse(stream, this.statusCode);
70-
}
71-
else {
72-
response = new MockClientHttpResponse(this.content, this.statusCode);
73-
}
74-
response.getHeaders().putAll(this.headers);
75-
return response;
76-
}
77-
7864
/**
7965
* Set the body as a UTF-8 String.
8066
*/
@@ -129,4 +115,19 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
129115
return this;
130116
}
131117

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

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)