From 8acceba641725398e02cb59de6f8ca72f499f2c0 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 9 Aug 2016 12:29:15 +0200 Subject: [PATCH] Polishing (cherry picked from commit aade2d1) --- .../web/client/DefaultRequestExpectation.java | 4 +-- .../MockMvcClientHttpRequestFactory.java | 6 ++-- .../test/web/client/RequestMatcher.java | 2 +- .../client/match/ContentRequestMatchers.java | 1 + .../client/match/JsonPathRequestMatchers.java | 9 +++-- .../client/match/XpathRequestMatchers.java | 6 ++-- .../response/DefaultResponseCreator.java | 33 ++++++++++--------- .../response/MockRestResponseCreators.java | 6 +--- 8 files changed, 30 insertions(+), 37 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java b/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java index d1983be5d5..1d5f90edbe 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java @@ -46,8 +46,8 @@ public class DefaultRequestExpectation implements RequestExpectation { * @param expectedCount the expected request expectedCount */ public DefaultRequestExpectation(ExpectedCount expectedCount, RequestMatcher requestMatcher) { - Assert.notNull(expectedCount, "'expectedCount' is required"); - Assert.notNull(requestMatcher, "'requestMatcher' is required"); + Assert.notNull(expectedCount, "ExpectedCount is required"); + Assert.notNull(requestMatcher, "RequestMatcher is required"); this.requestCount = new RequestCount(expectedCount); this.requestMatchers.add(requestMatcher); } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java b/spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java index 5bfff0ec88..10082f690b 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java @@ -33,6 +33,7 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.util.Assert; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -48,6 +49,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory public MockMvcClientHttpRequestFactory(MockMvc mockMvc) { + Assert.notNull(mockMvc, "MockMvc must not be null"); this.mockMvc = mockMvc; } @@ -61,17 +63,13 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri.toString()); requestBuilder.content(getBodyAsBytes()); requestBuilder.headers(getHeaders()); - MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn(); - MockHttpServletResponse servletResponse = mvcResult.getResponse(); HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus()); byte[] body = servletResponse.getContentAsByteArray(); HttpHeaders headers = getResponseHeaders(servletResponse); - MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); clientResponse.getHeaders().putAll(headers); - return clientResponse; } catch (Exception ex) { diff --git a/spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.java b/spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.java index 84d53039c0..e68ad02a07 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/RequestMatcher.java @@ -33,7 +33,7 @@ import org.springframework.http.client.ClientHttpRequest; public interface RequestMatcher { /** - * Match the given request against some expectations. + * Match the given request against specific expectations. * @param request the request to make assertions on * @throws IOException in case of I/O errors * @throws AssertionError if expectations are not met diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java index e32a68aacc..25d66f8846 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java @@ -58,6 +58,7 @@ public class ContentRequestMatchers { this.xmlHelper = new XmlExpectationsHelper(); } + /** * Assert the request content type as a String. */ diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java index cccab60d93..2c3b7a99f9 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.test.web.client.match; import java.io.IOException; import java.text.ParseException; +import com.jayway.jsonpath.JsonPath; import org.hamcrest.Matcher; import org.springframework.http.client.ClientHttpRequest; @@ -26,8 +27,6 @@ import org.springframework.mock.http.client.MockClientHttpRequest; import org.springframework.test.util.JsonPathExpectationsHelper; import org.springframework.test.web.client.RequestMatcher; -import com.jayway.jsonpath.JsonPath; - /** * Factory for assertions on the request content using * JsonPath expressions. @@ -235,8 +234,8 @@ public class JsonPathRequestMatchers { MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; matchInternal(mockRequest); } - catch (ParseException e) { - throw new AssertionError("Failed to parse JSON request content: " + e.getMessage()); + catch (ParseException ex) { + throw new AssertionError("Failed to parse JSON request content: " + ex.getMessage()); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java index 4f32e158b7..3baf52d0a9 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,12 +46,10 @@ public class XpathRequestMatchers { * Class constructor, not for direct instantiation. Use * {@link MockRestRequestMatchers#xpath(String, Object...)} or * {@link MockRestRequestMatchers#xpath(String, Map, Object...)}. - * * @param expression the XPath expression * @param namespaces XML namespaces referenced in the XPath expression, or {@code null} * @param args arguments to parameterize the XPath expression with using the * formatting specifiers defined in {@link String#format(String, Object...)} - * * @throws XPathExpressionException */ protected XpathRequestMatchers(String expression, Map namespaces, Object ... args) @@ -60,6 +58,7 @@ public class XpathRequestMatchers { this.xpathHelper = new XpathExpectationsHelper(expression, namespaces, args); } + /** * Apply the XPath and assert it with the given {@code Matcher}. */ @@ -199,7 +198,6 @@ public class XpathRequestMatchers { } protected abstract void matchInternal(MockClientHttpRequest request) throws Exception; - } } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java b/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java index 4a7a9f7e11..d03de36722 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java @@ -39,14 +39,14 @@ import org.springframework.util.Assert; */ public class DefaultResponseCreator implements ResponseCreator { + private HttpStatus statusCode; + private byte[] content; private Resource contentResource; private final HttpHeaders headers = new HttpHeaders(); - private HttpStatus statusCode; - /** * Protected constructor. @@ -58,20 +58,6 @@ public class DefaultResponseCreator implements ResponseCreator { } - @Override - public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException { - MockClientHttpResponse response; - if (this.contentResource != null) { - InputStream stream = this.contentResource.getInputStream(); - response = new MockClientHttpResponse(stream, this.statusCode); - } - else { - response = new MockClientHttpResponse(this.content, this.statusCode); - } - response.getHeaders().putAll(this.headers); - return response; - } - /** * Set the body as a UTF-8 String. */ @@ -126,4 +112,19 @@ public class DefaultResponseCreator implements ResponseCreator { return this; } + + @Override + public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException { + MockClientHttpResponse response; + if (this.contentResource != null) { + InputStream stream = this.contentResource.getInputStream(); + response = new MockClientHttpResponse(stream, this.statusCode); + } + else { + response = new MockClientHttpResponse(this.content, this.statusCode); + } + response.getHeaders().putAll(this.headers); + return response; + } + } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java b/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java index 243309b3c9..0ff14df9a1 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,10 +33,6 @@ import org.springframework.test.web.client.ResponseCreator; */ public abstract class MockRestResponseCreators { - - private MockRestResponseCreators() { - } - /** * {@code ResponseCreator} for a 200 response (OK). */