Consistent generics in spring-test Matcher declarations

Closes gh-25610
This commit is contained in:
Rossen Stoyanchev
2020-08-26 11:28:00 +01:00
parent 21d25b23d9
commit 568b44eb9d
19 changed files with 44 additions and 44 deletions

View File

@@ -69,7 +69,7 @@ public class JsonPathExpectationsHelper {
* @param matcher the matcher with which to assert the result
*/
@SuppressWarnings("unchecked")
public <T> void assertValue(String content, Matcher<T> matcher) {
public <T> void assertValue(String content, Matcher<? super T> matcher) {
T value = (T) evaluateJsonPath(content);
MatcherAssert.assertThat("JSON path \"" + this.expression + "\"", value, matcher);
}
@@ -84,7 +84,7 @@ public class JsonPathExpectationsHelper {
* @since 4.3.3
*/
@SuppressWarnings("unchecked")
public <T> void assertValue(String content, Matcher<T> matcher, Class<T> targetType) {
public <T> void assertValue(String content, Matcher<? super T> matcher, Class<T> targetType) {
T value = (T) evaluateJsonPath(content, targetType);
MatcherAssert.assertThat("JSON path \"" + this.expression + "\"", value, matcher);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -138,7 +138,7 @@ public class XpathExpectationsHelper {
* given Hamcrest matcher.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNodeCount(byte[] content, @Nullable String encoding, Matcher<Integer> matcher)
public void assertNodeCount(byte[] content, @Nullable String encoding, Matcher<? super Integer> matcher)
throws Exception {
NodeList nodeList = evaluateXpath(content, encoding, NodeList.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -61,7 +61,7 @@ public class JsonPathRequestMatchers {
* Evaluate the JSON path expression against the request content and
* assert the resulting value with the given Hamcrest {@link Matcher}.
*/
public <T> RequestMatcher value(Matcher<T> matcher) {
public <T> RequestMatcher value(Matcher<? super T> matcher) {
return new AbstractJsonPathRequestMatcher() {
@Override
protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
@@ -78,7 +78,7 @@ public class JsonPathRequestMatchers {
* to coerce an integer into a double.
* @since 4.3.3
*/
public <T> RequestMatcher value(Matcher<T> matcher, Class<T> targetType) {
public <T> RequestMatcher value(Matcher<? super T> matcher, Class<T> targetType) {
return new AbstractJsonPathRequestMatcher() {
@Override
protected void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -73,7 +73,7 @@ public abstract class MockRestRequestMatchers {
* @param matcher the String matcher for the expected URI
* @return the request matcher
*/
public static RequestMatcher requestTo(Matcher<String> matcher) {
public static RequestMatcher requestTo(Matcher<? super String> matcher) {
Assert.notNull(matcher, "'matcher' must not be null");
return request -> assertThat("Request URI", request.getURI().toString(), matcher);
}
@@ -227,7 +227,7 @@ public abstract class MockRestRequestMatchers {
* @param expression the JSON path expression
* @param matcher a matcher for the value expected at the JSON path
*/
public static <T> RequestMatcher jsonPath(String expression, Matcher<T> matcher) {
public static <T> RequestMatcher jsonPath(String expression, Matcher<? super T> matcher) {
return new JsonPathRequestMatchers(expression).value(matcher);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -93,7 +93,7 @@ public class XpathRequestMatchers {
* Apply the XPath and assert the number of nodes found with the given
* {@code Matcher<Integer>}.
*/
public RequestMatcher nodeCount(Matcher<Integer> matcher) {
public RequestMatcher nodeCount(Matcher<? super Integer> matcher) {
return (XpathRequestMatcher) request ->
this.xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
}

View File

@@ -422,13 +422,13 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public <T extends S> T value(Matcher<B> matcher) {
public <T extends S> T value(Matcher<? super B> matcher) {
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
return self();
}
@Override
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<R> matcher) {
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher) {
this.result.assertWithDiagnostics(() -> {
B body = this.result.getResponseBody();
MatcherAssert.assertThat(bodyMapper.apply(body), matcher);

View File

@@ -160,7 +160,7 @@ public class HeaderAssertions {
* @param matcher the matcher to use
* @since 5.3
*/
public WebTestClient.ResponseSpec values(String name, Matcher<Iterable<String>> matcher) {
public WebTestClient.ResponseSpec values(String name, Matcher<? super Iterable<String>> matcher) {
List<String> values = getHeaders().get(name);
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -141,7 +141,7 @@ public class JsonPathAssertions {
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher)}.
* @since 5.1
*/
public <T> WebTestClient.BodyContentSpec value(Matcher<T> matcher) {
public <T> WebTestClient.BodyContentSpec value(Matcher<? super T> matcher) {
this.pathHelper.assertValue(this.content, matcher);
return this.bodySpec;
}
@@ -150,7 +150,7 @@ public class JsonPathAssertions {
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, Class)}.
* @since 5.1
*/
public <T> WebTestClient.BodyContentSpec value(Matcher<T> matcher, Class<T> targetType) {
public <T> WebTestClient.BodyContentSpec value(Matcher<? super T> matcher, Class<T> targetType) {
this.pathHelper.assertValue(this.content, matcher, targetType);
return this.bodySpec;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -204,7 +204,7 @@ public class StatusAssertions {
* @param matcher the matcher to use
* @since 5.1
*/
public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) {
public WebTestClient.ResponseSpec value(Matcher<? super Integer> matcher) {
int value = this.exchangeResult.getStatus().value();
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", value, matcher));
return this.responseSpec;

View File

@@ -838,14 +838,14 @@ public interface WebTestClient {
* Assert the extracted body with a {@link Matcher}.
* @since 5.1
*/
<T extends S> T value(Matcher<B> matcher);
<T extends S> T value(Matcher<? super B> matcher);
/**
* Transform the extracted the body with a function, e.g. extracting a
* property, and assert the mapped value with a {@link Matcher}.
* @since 5.1
*/
<T extends S, R> T value(Function<B, R> bodyMapper, Matcher<R> matcher);
<T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher);
/**
* Assert the extracted body with a {@link Consumer}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -126,7 +126,7 @@ public class XpathAssertions {
* Delegates to {@link XpathExpectationsHelper#assertNodeCount(byte[], String, Matcher)}.
* @since 5.1
*/
public WebTestClient.BodyContentSpec nodeCount(Matcher<Integer> matcher){
public WebTestClient.BodyContentSpec nodeCount(Matcher<? super Integer> matcher){
return assertWith(() -> this.xpathHelper.assertNodeCount(getContent(), getCharset(), matcher));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -47,7 +47,7 @@ public class FlashAttributeResultMatchers {
* Assert a flash attribute's value with the given Hamcrest {@link Matcher}.
*/
@SuppressWarnings("unchecked")
public <T> ResultMatcher attribute(String name, Matcher<T> matcher) {
public <T> ResultMatcher attribute(String name, Matcher<? super T> matcher) {
return result -> assertThat("Flash attribute '" + name + "'", (T) result.getFlashMap().get(name), matcher);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -65,7 +65,7 @@ public class HeaderResultMatchers {
* Iterable {@link Matcher}.
* @since 4.3
*/
public ResultMatcher stringValues(String name, Matcher<Iterable<String>> matcher) {
public ResultMatcher stringValues(String name, Matcher<? super Iterable<String>> matcher) {
return result -> {
List<String> values = result.getResponse().getHeaders(name);
assertThat("Response header '" + name + "'", values, matcher);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -83,7 +83,7 @@ public class JsonPathResultMatchers {
* @see #value(Matcher, Class)
* @see #value(Object)
*/
public <T> ResultMatcher value(Matcher<T> matcher) {
public <T> ResultMatcher value(Matcher<? super T> matcher) {
return result -> this.jsonPathHelper.assertValue(getContent(result), matcher);
}
@@ -97,7 +97,7 @@ public class JsonPathResultMatchers {
* @see #value(Matcher)
* @see #value(Object)
*/
public <T> ResultMatcher value(Matcher<T> matcher, Class<T> targetType) {
public <T> ResultMatcher value(Matcher<? super T> matcher, Class<T> targetType) {
return result -> this.jsonPathHelper.assertValue(getContent(result), matcher, targetType);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -206,7 +206,7 @@ public abstract class MockMvcResultMatchers {
* @see #jsonPath(String, Object...)
* @see #jsonPath(String, Matcher, Class)
*/
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher) {
public static <T> ResultMatcher jsonPath(String expression, Matcher<? super T> matcher) {
return new JsonPathResultMatchers(expression).value(matcher);
}
@@ -224,7 +224,7 @@ public abstract class MockMvcResultMatchers {
* @see #jsonPath(String, Object...)
* @see #jsonPath(String, Matcher)
*/
public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher, Class<T> targetType) {
public static <T> ResultMatcher jsonPath(String expression, Matcher<? super T> matcher, Class<T> targetType) {
return new JsonPathResultMatchers(expression).value(matcher, targetType);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -57,7 +57,7 @@ public class ModelResultMatchers {
* Assert a model attribute value with the given Hamcrest {@link Matcher}.
*/
@SuppressWarnings("unchecked")
public <T> ResultMatcher attribute(String name, Matcher<T> matcher) {
public <T> ResultMatcher attribute(String name, Matcher<? super T> matcher) {
return result -> {
ModelAndView mav = getModelAndView(result);
assertThat("Model attribute '" + name + "'", (T) mav.getModel().get(name), matcher);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -84,7 +84,7 @@ public class RequestResultMatchers {
* or {@link WebAsyncTask}.
*/
@SuppressWarnings("unchecked")
public <T> ResultMatcher asyncResult(Matcher<T> matcher) {
public <T> ResultMatcher asyncResult(Matcher<? super T> matcher) {
return result -> {
HttpServletRequest request = result.getRequest();
assertAsyncStarted(request);
@@ -110,7 +110,7 @@ public class RequestResultMatchers {
* Assert a request attribute value with the given Hamcrest {@link Matcher}.
*/
@SuppressWarnings("unchecked")
public <T> ResultMatcher attribute(String name, Matcher<T> matcher) {
public <T> ResultMatcher attribute(String name, Matcher<? super T> matcher) {
return result -> {
T value = (T) result.getRequest().getAttribute(name);
assertThat("Request attribute '" + name + "'", value, matcher);
@@ -129,7 +129,7 @@ public class RequestResultMatchers {
* Assert a session attribute value with the given Hamcrest {@link Matcher}.
*/
@SuppressWarnings("unchecked")
public <T> ResultMatcher sessionAttribute(String name, Matcher<T> matcher) {
public <T> ResultMatcher sessionAttribute(String name, Matcher<? super T> matcher) {
return result -> {
HttpSession session = result.getRequest().getSession();
Assert.state(session != null, "No HttpSession");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -51,7 +51,7 @@ public class StatusResultMatchers {
* Assert the response status code with the given Hamcrest {@link Matcher}.
* Use the {@code StatusResultMatchers.isEqualTo} extension in Kotlin.
*/
public ResultMatcher is(Matcher<Integer> matcher) {
public ResultMatcher is(Matcher<? super Integer> matcher) {
return result -> assertThat("Response status", result.getResponse().getStatus(), matcher);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -114,7 +114,7 @@ public class XpathResultMatchers {
* Evaluate the XPath and assert the number of nodes found with the given
* Hamcrest {@link Matcher}.
*/
public ResultMatcher nodeCount(Matcher<Integer> matcher) {
public ResultMatcher nodeCount(Matcher<? super Integer> matcher) {
return result -> {
MockHttpServletResponse response = result.getResponse();
this.xpathHelper.assertNodeCount(response.getContentAsByteArray(), getDefinedEncoding(response), matcher);