Polish MockRestServiceServer internals regarding "effectively final"

This commit is contained in:
Sam Brannen 2019-07-18 15:37:56 +02:00
parent 42f033e439
commit 8db73c80e2
5 changed files with 38 additions and 38 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,7 +62,7 @@ public class MockMvcClientHttpRequestFactory
@Override @Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) { public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
return new MockClientHttpRequest(httpMethod, uri) { return new MockClientHttpRequest(httpMethod, uri) {
@Override @Override
public ClientHttpResponse executeInternal() throws IOException { public ClientHttpResponse executeInternal() throws IOException {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -74,7 +74,7 @@ public class ContentRequestMatchers {
/** /**
* Assert the request content type as a {@link MediaType}. * Assert the request content type as a {@link MediaType}.
*/ */
public RequestMatcher contentType(final MediaType expectedContentType) { public RequestMatcher contentType(MediaType expectedContentType) {
return request -> { return request -> {
MediaType actualContentType = request.getHeaders().getContentType(); MediaType actualContentType = request.getHeaders().getContentType();
assertTrue("Content type not set", actualContentType != null); assertTrue("Content type not set", actualContentType != null);
@ -94,7 +94,7 @@ public class ContentRequestMatchers {
* Assert the request content type is compatible with the given * Assert the request content type is compatible with the given
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}. * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
*/ */
public RequestMatcher contentTypeCompatibleWith(final MediaType contentType) { public RequestMatcher contentTypeCompatibleWith(MediaType contentType) {
return request -> { return request -> {
MediaType actualContentType = request.getHeaders().getContentType(); MediaType actualContentType = request.getHeaders().getContentType();
assertTrue("Content type not set", actualContentType != null); assertTrue("Content type not set", actualContentType != null);
@ -108,7 +108,7 @@ public class ContentRequestMatchers {
/** /**
* Get the body of the request as a UTF-8 string and apply the given {@link Matcher}. * Get the body of the request as a UTF-8 string and apply the given {@link Matcher}.
*/ */
public RequestMatcher string(final Matcher<? super String> matcher) { public RequestMatcher string(Matcher<? super String> matcher) {
return request -> { return request -> {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
assertThat("Request content", mockRequest.getBodyAsString(), matcher); assertThat("Request content", mockRequest.getBodyAsString(), matcher);
@ -118,7 +118,7 @@ public class ContentRequestMatchers {
/** /**
* Get the body of the request as a UTF-8 string and compare it to the given String. * Get the body of the request as a UTF-8 string and compare it to the given String.
*/ */
public RequestMatcher string(final String expectedContent) { public RequestMatcher string(String expectedContent) {
return request -> { return request -> {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
assertEquals("Request content", expectedContent, mockRequest.getBodyAsString()); assertEquals("Request content", expectedContent, mockRequest.getBodyAsString());
@ -128,7 +128,7 @@ public class ContentRequestMatchers {
/** /**
* Compare the body of the request to the given byte array. * Compare the body of the request to the given byte array.
*/ */
public RequestMatcher bytes(final byte[] expectedContent) { public RequestMatcher bytes(byte[] expectedContent) {
return request -> { return request -> {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
assertEquals("Request content", expectedContent, mockRequest.getBodyAsBytes()); assertEquals("Request content", expectedContent, mockRequest.getBodyAsBytes());
@ -139,7 +139,7 @@ public class ContentRequestMatchers {
* Parse the body as form data and compare to the given {@code MultiValueMap}. * Parse the body as form data and compare to the given {@code MultiValueMap}.
* @since 4.3 * @since 4.3
*/ */
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) { public RequestMatcher formData(MultiValueMap<String, String> expectedContent) {
return request -> { return request -> {
HttpInputMessage inputMessage = new HttpInputMessage() { HttpInputMessage inputMessage = new HttpInputMessage() {
@Override @Override
@ -165,7 +165,7 @@ public class ContentRequestMatchers {
* <a href="http://xmlunit.sourceforge.net/">XMLUnit</a> library is available. * <a href="http://xmlunit.sourceforge.net/">XMLUnit</a> library is available.
* @param expectedXmlContent the expected XML content * @param expectedXmlContent the expected XML content
*/ */
public RequestMatcher xml(final String expectedXmlContent) { public RequestMatcher xml(String expectedXmlContent) {
return new AbstractXmlRequestMatcher() { return new AbstractXmlRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -177,7 +177,7 @@ public class ContentRequestMatchers {
/** /**
* Parse the request content as {@link Node} and apply the given {@link Matcher}. * Parse the request content as {@link Node} and apply the given {@link Matcher}.
*/ */
public RequestMatcher node(final Matcher<? super Node> matcher) { public RequestMatcher node(Matcher<? super Node> matcher) {
return new AbstractXmlRequestMatcher() { return new AbstractXmlRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -190,7 +190,7 @@ public class ContentRequestMatchers {
* Parse the request content as {@link DOMSource} and apply the given {@link Matcher}. * Parse the request content as {@link DOMSource} and apply the given {@link Matcher}.
* @see <a href="https://code.google.com/p/xml-matchers/">https://code.google.com/p/xml-matchers/</a> * @see <a href="https://code.google.com/p/xml-matchers/">https://code.google.com/p/xml-matchers/</a>
*/ */
public RequestMatcher source(final Matcher<? super Source> matcher) { public RequestMatcher source(Matcher<? super Source> matcher) {
return new AbstractXmlRequestMatcher() { return new AbstractXmlRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -209,7 +209,7 @@ public class ContentRequestMatchers {
* @param expectedJsonContent the expected JSON content * @param expectedJsonContent the expected JSON content
* @since 5.0.5 * @since 5.0.5
*/ */
public RequestMatcher json(final String expectedJsonContent) { public RequestMatcher json(String expectedJsonContent) {
return json(expectedJsonContent, false); return json(expectedJsonContent, false);
} }
@ -228,7 +228,7 @@ public class ContentRequestMatchers {
* @param strict enables strict checking * @param strict enables strict checking
* @since 5.0.5 * @since 5.0.5
*/ */
public RequestMatcher json(final String expectedJsonContent, final boolean strict) { public RequestMatcher json(String expectedJsonContent, boolean strict) {
return request -> { return request -> {
try { try {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -61,7 +61,7 @@ public class JsonPathRequestMatchers {
* Evaluate the JSON path expression against the request content and * Evaluate the JSON path expression against the request content and
* assert the resulting value with the given Hamcrest {@link Matcher}. * assert the resulting value with the given Hamcrest {@link Matcher}.
*/ */
public <T> RequestMatcher value(final Matcher<T> matcher) { public <T> RequestMatcher value(Matcher<T> matcher) {
return new AbstractJsonPathRequestMatcher() { return new AbstractJsonPathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException { protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
@ -78,7 +78,7 @@ public class JsonPathRequestMatchers {
* to coerce an integer into a double. * to coerce an integer into a double.
* @since 4.3.3 * @since 4.3.3
*/ */
public <T> RequestMatcher value(final Matcher<T> matcher, final Class<T> targetType) { public <T> RequestMatcher value(Matcher<T> matcher, Class<T> targetType) {
return new AbstractJsonPathRequestMatcher() { return new AbstractJsonPathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException { protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
@ -92,7 +92,7 @@ public class JsonPathRequestMatchers {
* Evaluate the JSON path expression against the request content and * Evaluate the JSON path expression against the request content and
* assert that the result is equal to the supplied value. * assert that the result is equal to the supplied value.
*/ */
public RequestMatcher value(final Object expectedValue) { public RequestMatcher value(Object expectedValue) {
return new AbstractJsonPathRequestMatcher() { return new AbstractJsonPathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException { protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,7 +62,7 @@ public abstract class MockRestRequestMatchers {
* @param method the HTTP method * @param method the HTTP method
* @return the request matcher * @return the request matcher
*/ */
public static RequestMatcher method(final HttpMethod method) { public static RequestMatcher method(HttpMethod method) {
Assert.notNull(method, "'method' must not be null"); Assert.notNull(method, "'method' must not be null");
return request -> assertEquals("Unexpected HttpMethod", method, request.getMethod()); return request -> assertEquals("Unexpected HttpMethod", method, request.getMethod());
} }
@ -72,7 +72,7 @@ public abstract class MockRestRequestMatchers {
* @param matcher the String matcher for the expected URI * @param matcher the String matcher for the expected URI
* @return the request matcher * @return the request matcher
*/ */
public static RequestMatcher requestTo(final Matcher<String> matcher) { public static RequestMatcher requestTo(Matcher<String> matcher) {
Assert.notNull(matcher, "'matcher' must not be null"); Assert.notNull(matcher, "'matcher' must not be null");
return request -> assertThat("Request URI", request.getURI().toString(), matcher); return request -> assertThat("Request URI", request.getURI().toString(), matcher);
} }
@ -82,7 +82,7 @@ public abstract class MockRestRequestMatchers {
* @param expectedUri the expected URI * @param expectedUri the expected URI
* @return the request matcher * @return the request matcher
*/ */
public static RequestMatcher requestTo(final String expectedUri) { public static RequestMatcher requestTo(String expectedUri) {
Assert.notNull(expectedUri, "'uri' must not be null"); Assert.notNull(expectedUri, "'uri' must not be null");
return request -> assertEquals("Request URI", expectedUri, request.getURI().toString()); return request -> assertEquals("Request URI", expectedUri, request.getURI().toString());
} }
@ -95,7 +95,7 @@ public abstract class MockRestRequestMatchers {
* @param uriVars zero or more URI variables to populate the expected URI * @param uriVars zero or more URI variables to populate the expected URI
* @return the request matcher * @return the request matcher
*/ */
public static RequestMatcher requestToUriTemplate(final String expectedUri, final Object... uriVars) { public static RequestMatcher requestToUriTemplate(String expectedUri, Object... uriVars) {
Assert.notNull(expectedUri, "'uri' must not be null"); Assert.notNull(expectedUri, "'uri' must not be null");
URI uri = UriComponentsBuilder.fromUriString(expectedUri).buildAndExpand(uriVars).encode().toUri(); URI uri = UriComponentsBuilder.fromUriString(expectedUri).buildAndExpand(uriVars).encode().toUri();
return requestTo(uri); return requestTo(uri);
@ -106,7 +106,7 @@ public abstract class MockRestRequestMatchers {
* @param uri the expected URI * @param uri the expected URI
* @return the request matcher * @return the request matcher
*/ */
public static RequestMatcher requestTo(final URI uri) { public static RequestMatcher requestTo(URI uri) {
Assert.notNull(uri, "'uri' must not be null"); Assert.notNull(uri, "'uri' must not be null");
return request -> assertEquals("Unexpected request", uri, request.getURI()); return request -> assertEquals("Unexpected request", uri, request.getURI());
} }
@ -115,7 +115,7 @@ public abstract class MockRestRequestMatchers {
* Assert request query parameter values with the given Hamcrest matcher(s). * Assert request query parameter values with the given Hamcrest matcher(s).
*/ */
@SafeVarargs @SafeVarargs
public static RequestMatcher queryParam(final String name, final Matcher<? super String>... matchers) { public static RequestMatcher queryParam(String name, Matcher<? super String>... matchers) {
return request -> { return request -> {
MultiValueMap<String, String> params = getQueryParams(request); MultiValueMap<String, String> params = getQueryParams(request);
assertValueCount("query param", name, params, matchers.length); assertValueCount("query param", name, params, matchers.length);
@ -128,7 +128,7 @@ public abstract class MockRestRequestMatchers {
/** /**
* Assert request query parameter values. * Assert request query parameter values.
*/ */
public static RequestMatcher queryParam(final String name, final String... expectedValues) { public static RequestMatcher queryParam(String name, String... expectedValues) {
return request -> { return request -> {
MultiValueMap<String, String> params = getQueryParams(request); MultiValueMap<String, String> params = getQueryParams(request);
assertValueCount("query param", name, params, expectedValues.length); assertValueCount("query param", name, params, expectedValues.length);
@ -143,7 +143,7 @@ public abstract class MockRestRequestMatchers {
} }
private static void assertValueCount( private static void assertValueCount(
String valueType, final String name, MultiValueMap<String, String> map, int count) { String valueType, String name, MultiValueMap<String, String> map, int count) {
List<String> values = map.get(name); List<String> values = map.get(name);
String message = "Expected " + valueType + " <" + name + ">"; String message = "Expected " + valueType + " <" + name + ">";
@ -159,7 +159,7 @@ public abstract class MockRestRequestMatchers {
* Assert request header values with the given Hamcrest matcher(s). * Assert request header values with the given Hamcrest matcher(s).
*/ */
@SafeVarargs @SafeVarargs
public static RequestMatcher header(final String name, final Matcher<? super String>... matchers) { public static RequestMatcher header(String name, Matcher<? super String>... matchers) {
return request -> { return request -> {
assertValueCount("header", name, request.getHeaders(), matchers.length); assertValueCount("header", name, request.getHeaders(), matchers.length);
List<String> headerValues = request.getHeaders().get(name); List<String> headerValues = request.getHeaders().get(name);
@ -173,7 +173,7 @@ public abstract class MockRestRequestMatchers {
/** /**
* Assert request header values. * Assert request header values.
*/ */
public static RequestMatcher header(final String name, final String... expectedValues) { public static RequestMatcher header(String name, String... expectedValues) {
return request -> { return request -> {
assertValueCount("header", name, request.getHeaders(), expectedValues.length); assertValueCount("header", name, request.getHeaders(), expectedValues.length);
List<String> headerValues = request.getHeaders().get(name); List<String> headerValues = request.getHeaders().get(name);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -64,7 +64,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert it with the given {@code Matcher<Node>}. * Apply the XPath and assert it with the given {@code Matcher<Node>}.
*/ */
public <T> RequestMatcher node(final Matcher<? super Node> matcher) { public <T> RequestMatcher node(Matcher<? super Node> matcher) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -101,7 +101,7 @@ public class XpathRequestMatchers {
* Apply the XPath and assert the number of nodes found with the given * Apply the XPath and assert the number of nodes found with the given
* {@code Matcher<Integer>}. * {@code Matcher<Integer>}.
*/ */
public <T> RequestMatcher nodeCount(final Matcher<Integer> matcher) { public <T> RequestMatcher nodeCount(Matcher<Integer> matcher) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -113,7 +113,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert the number of nodes found. * Apply the XPath and assert the number of nodes found.
*/ */
public <T> RequestMatcher nodeCount(final int expectedCount) { public <T> RequestMatcher nodeCount(int expectedCount) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -125,7 +125,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert the String content found with the given matcher. * Apply the XPath and assert the String content found with the given matcher.
*/ */
public <T> RequestMatcher string(final Matcher<? super String> matcher) { public <T> RequestMatcher string(Matcher<? super String> matcher) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -137,7 +137,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert the String content found. * Apply the XPath and assert the String content found.
*/ */
public RequestMatcher string(final String value) { public RequestMatcher string(String value) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -149,7 +149,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert the number found with the given matcher. * Apply the XPath and assert the number found with the given matcher.
*/ */
public <T> RequestMatcher number(final Matcher<? super Double> matcher) { public <T> RequestMatcher number(Matcher<? super Double> matcher) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -161,7 +161,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert the number of nodes found. * Apply the XPath and assert the number of nodes found.
*/ */
public RequestMatcher number(final Double value) { public RequestMatcher number(Double value) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {
@ -173,7 +173,7 @@ public class XpathRequestMatchers {
/** /**
* Apply the XPath and assert the boolean value found. * Apply the XPath and assert the boolean value found.
*/ */
public <T> RequestMatcher booleanValue(final Boolean value) { public <T> RequestMatcher booleanValue(Boolean value) {
return new AbstractXpathRequestMatcher() { return new AbstractXpathRequestMatcher() {
@Override @Override
protected void matchInternal(MockClientHttpRequest request) throws Exception { protected void matchInternal(MockClientHttpRequest request) throws Exception {