Polishing

This commit is contained in:
Sam Brannen
2024-03-12 17:34:02 +01:00
parent 3b87c87a33
commit 7211db9262
8 changed files with 173 additions and 178 deletions

View File

@@ -56,10 +56,10 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
Assert.isTrue(expectedMethodNames.length > 0, "At least one expectedMethodName is required");
Set<String> expectedNames = new LinkedHashSet<>(Arrays.asList(expectedMethodNames));
final List<Method> found = Arrays.stream(enclosingClass.getDeclaredMethods())
.filter(method -> Modifier.isStatic(method.getModifiers()))
.filter(method -> expectedNames.contains(method.getName())
&& expectedMethodReturnType.isAssignableFrom(method.getReturnType()))
List<Method> found = Arrays.stream(enclosingClass.getDeclaredMethods())
.filter(method -> Modifier.isStatic(method.getModifiers()) &&
expectedNames.contains(method.getName()) &&
expectedMethodReturnType.isAssignableFrom(method.getReturnType()))
.toList();
Assert.state(found.size() == 1, () -> "Found " + found.size() + " static methods " +
@@ -71,13 +71,13 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
@Override
public OverrideMetadata createMetadata(Field field, Annotation overrideAnnotation, ResolvableType typeToOverride) {
final Class<?> enclosingClass = field.getDeclaringClass();
// if we can get an explicit method name right away, fail fast if it doesn't match
Class<?> declaringClass = field.getDeclaringClass();
// If we can, get an explicit method name right away; fail fast if it doesn't match.
if (overrideAnnotation instanceof TestBean testBeanAnnotation) {
Method overrideMethod = null;
String beanName = null;
if (!testBeanAnnotation.methodName().isBlank()) {
overrideMethod = ensureMethod(enclosingClass, field.getType(), testBeanAnnotation.methodName());
overrideMethod = ensureMethod(declaringClass, field.getType(), testBeanAnnotation.methodName());
}
if (!testBeanAnnotation.name().isBlank()) {
beanName = testBeanAnnotation.name();
@@ -85,9 +85,8 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
return new MethodConventionOverrideMetadata(field, overrideMethod, beanName,
overrideAnnotation, typeToOverride);
}
// otherwise defer the resolution of the static method until OverrideMetadata#createOverride
return new MethodConventionOverrideMetadata(field, null, null, overrideAnnotation,
typeToOverride);
// Otherwise defer the resolution of the static method until OverrideMetadata#createOverride.
return new MethodConventionOverrideMetadata(field, null, null, overrideAnnotation, typeToOverride);
}
static final class MethodConventionOverrideMetadata extends OverrideMetadata {
@@ -100,6 +99,7 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
public MethodConventionOverrideMetadata(Field field, @Nullable Method overrideMethod, @Nullable String beanName,
Annotation overrideAnnotation, ResolvableType typeToOverride) {
super(field, overrideAnnotation, typeToOverride, BeanOverrideStrategy.REPLACE_DEFINITION);
this.overrideMethod = overrideMethod;
this.beanName = beanName;
@@ -121,6 +121,7 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
@Override
protected Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition,
@Nullable Object existingBeanInstance) {
Method methodToInvoke = this.overrideMethod;
if (methodToInvoke == null) {
methodToInvoke = ensureMethod(field().getDeclaringClass(), field().getType(),
@@ -135,7 +136,7 @@ public class TestBeanOverrideProcessor implements BeanOverrideProcessor {
}
catch (IllegalAccessException | InvocationTargetException ex) {
throw new IllegalArgumentException("Could not invoke bean overriding method " + methodToInvoke.getName() +
", a static method with no input parameters is expected", ex);
"; a static method with no formal parameters is expected", ex);
}
return override;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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.
@@ -24,9 +24,10 @@ import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.springframework.http.ResponseCookie;
import org.springframework.test.util.AssertionErrors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.fail;
/**
* Assertions on cookies of the response.
@@ -48,18 +49,20 @@ public class CookieAssertions {
/**
* Expect a header with the given name to match the specified values.
* Expect a response cookie with the given name to match the specified value.
*/
public WebTestClient.ResponseSpec valueEquals(String name, String value) {
String cookieValue = getCookie(name).getValue();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);
AssertionErrors.assertEquals(message, value, getCookie(name).getValue());
assertEquals(message, value, cookieValue);
});
return this.responseSpec;
}
/**
* Assert the first value of the response cookie with a Hamcrest {@link Matcher}.
* Assert the value of the response cookie with the given name with a Hamcrest
* {@link Matcher}.
*/
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
String value = getCookie(name).getValue();
@@ -71,7 +74,7 @@ public class CookieAssertions {
}
/**
* Consume the value of the response cookie.
* Consume the value of the response cookie with the given name.
*/
public WebTestClient.ResponseSpec value(String name, Consumer<String> consumer) {
String value = getCookie(name).getValue();
@@ -94,25 +97,25 @@ public class CookieAssertions {
ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name);
if (cookie != null) {
String message = getMessage(name) + " exists with value=[" + cookie.getValue() + "]";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
this.exchangeResult.assertWithDiagnostics(() -> fail(message));
}
return this.responseSpec;
}
/**
* Assert a cookie's maxAge attribute.
* Assert a cookie's "Max-Age" attribute.
*/
public WebTestClient.ResponseSpec maxAge(String name, Duration expected) {
Duration maxAge = getCookie(name).getMaxAge();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " maxAge";
AssertionErrors.assertEquals(message, expected, maxAge);
assertEquals(message, expected, maxAge);
});
return this.responseSpec;
}
/**
* Assert a cookie's maxAge attribute with a Hamcrest {@link Matcher}.
* Assert a cookie's "Max-Age" attribute with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matcher) {
long maxAge = getCookie(name).getMaxAge().getSeconds();
@@ -124,19 +127,19 @@ public class CookieAssertions {
}
/**
* Assert a cookie's path attribute.
* Assert a cookie's "Path" attribute.
*/
public WebTestClient.ResponseSpec path(String name, String expected) {
String path = getCookie(name).getPath();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " path";
AssertionErrors.assertEquals(message, expected, path);
assertEquals(message, expected, path);
});
return this.responseSpec;
}
/**
* Assert a cookie's path attribute with a Hamcrest {@link Matcher}.
* Assert a cookie's "Path" attribute with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matcher) {
String path = getCookie(name).getPath();
@@ -148,19 +151,19 @@ public class CookieAssertions {
}
/**
* Assert a cookie's domain attribute.
* Assert a cookie's "Domain" attribute.
*/
public WebTestClient.ResponseSpec domain(String name, String expected) {
String path = getCookie(name).getDomain();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " domain";
AssertionErrors.assertEquals(message, expected, path);
assertEquals(message, expected, path);
});
return this.responseSpec;
}
/**
* Assert a cookie's domain attribute with a Hamcrest {@link Matcher}.
* Assert a cookie's "Domain" attribute with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> matcher) {
String domain = getCookie(name).getDomain();
@@ -172,37 +175,37 @@ public class CookieAssertions {
}
/**
* Assert a cookie's secure attribute.
* Assert a cookie's "Secure" attribute.
*/
public WebTestClient.ResponseSpec secure(String name, boolean expected) {
boolean isSecure = getCookie(name).isSecure();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " secure";
AssertionErrors.assertEquals(message, expected, isSecure);
assertEquals(message, expected, isSecure);
});
return this.responseSpec;
}
/**
* Assert a cookie's httpOnly attribute.
* Assert a cookie's "HttpOnly" attribute.
*/
public WebTestClient.ResponseSpec httpOnly(String name, boolean expected) {
boolean isHttpOnly = getCookie(name).isHttpOnly();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " httpOnly";
AssertionErrors.assertEquals(message, expected, isHttpOnly);
assertEquals(message, expected, isHttpOnly);
});
return this.responseSpec;
}
/**
* Assert a cookie's sameSite attribute.
* Assert a cookie's "SameSite" attribute.
*/
public WebTestClient.ResponseSpec sameSite(String name, String expected) {
String sameSite = getCookie(name).getSameSite();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " sameSite";
AssertionErrors.assertEquals(message, expected, sameSite);
assertEquals(message, expected, sameSite);
});
return this.responseSpec;
}
@@ -211,13 +214,12 @@ public class CookieAssertions {
private ResponseCookie getCookie(String name) {
ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name);
if (cookie == null) {
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.fail("No cookie with name '" + name + "'"));
this.exchangeResult.assertWithDiagnostics(() -> fail("No cookie with name '" + name + "'"));
}
return Objects.requireNonNull(cookie);
}
private String getMessage(String cookie) {
private static String getMessage(String cookie) {
return "Response cookie '" + cookie + "'";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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.
@@ -23,19 +23,19 @@ import java.util.Objects;
import java.util.function.Consumer;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.springframework.http.CacheControl;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.test.util.AssertionErrors;
import org.springframework.util.CollectionUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import static org.springframework.test.util.AssertionErrors.fail;
/**
* Assertions on headers of the response.
@@ -73,8 +73,8 @@ public class HeaderAssertions {
public WebTestClient.ResponseSpec valueEquals(String headerName, long value) {
String actual = getHeaders().getFirst(headerName);
this.exchangeResult.assertWithDiagnostics(() ->
assertTrue("Response does not contain header '" + headerName + "'", actual != null));
return assertHeader(headerName, value, Long.parseLong(Objects.requireNonNull(actual)));
assertNotNull("Response does not contain header '" + headerName + "'", actual));
return assertHeader(headerName, value, Long.parseLong(actual));
}
/**
@@ -94,7 +94,7 @@ public class HeaderAssertions {
headers.setDate("expected", value);
headers.set("actual", headerValue);
assertEquals("Response header '" + headerName + "'='" + headerValue + "' " +
assertEquals(getMessage(headerName) + "='" + headerValue + "' " +
"does not match expected value '" + headers.getFirst("expected") + "'",
headers.getFirstDate("expected"), headers.getFirstDate("actual"));
});
@@ -109,7 +109,7 @@ public class HeaderAssertions {
public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
String value = getRequiredValue(name);
String message = getMessage(name) + "=[" + value + "] does not match [" + pattern + "]";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertTrue(message, value.matches(pattern)));
this.exchangeResult.assertWithDiagnostics(() -> assertTrue(message, value.matches(pattern)));
return this.responseSpec;
}
@@ -123,16 +123,16 @@ public class HeaderAssertions {
* @since 5.3
*/
public WebTestClient.ResponseSpec valuesMatch(String name, String... patterns) {
List<String> values = getRequiredValues(name);
this.exchangeResult.assertWithDiagnostics(() -> {
List<String> values = getRequiredValues(name);
AssertionErrors.assertTrue(
assertTrue(
getMessage(name) + " has fewer or more values " + values +
" than number of patterns to match with " + Arrays.toString(patterns),
values.size() == patterns.length);
for (int i = 0; i < values.size(); i++) {
String value = values.get(i);
String pattern = patterns[i];
AssertionErrors.assertTrue(
assertTrue(
getMessage(name) + "[" + i + "]='" + value + "' does not match '" + pattern + "'",
value.matches(pattern));
}
@@ -150,7 +150,7 @@ public class HeaderAssertions {
String value = getHeaders().getFirst(name);
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);
MatcherAssert.assertThat(message, value, matcher);
assertThat(message, value, matcher);
});
return this.responseSpec;
}
@@ -165,7 +165,7 @@ public class HeaderAssertions {
List<String> values = getHeaders().get(name);
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);
MatcherAssert.assertThat(message, values, matcher);
assertThat(message, values, matcher);
});
return this.responseSpec;
}
@@ -201,8 +201,7 @@ public class HeaderAssertions {
private List<String> getRequiredValues(String name) {
List<String> values = getHeaders().get(name);
if (CollectionUtils.isEmpty(values)) {
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.fail(getMessage(name) + " not found"));
this.exchangeResult.assertWithDiagnostics(() -> fail(getMessage(name) + " not found"));
}
return Objects.requireNonNull(values);
}
@@ -214,7 +213,7 @@ public class HeaderAssertions {
public WebTestClient.ResponseSpec exists(String name) {
if (!getHeaders().containsKey(name)) {
String message = getMessage(name) + " does not exist";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
this.exchangeResult.assertWithDiagnostics(() -> fail(message));
}
return this.responseSpec;
}
@@ -225,7 +224,7 @@ public class HeaderAssertions {
public WebTestClient.ResponseSpec doesNotExist(String name) {
if (getHeaders().containsKey(name)) {
String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
this.exchangeResult.assertWithDiagnostics(() -> fail(message));
}
return this.responseSpec;
}
@@ -272,7 +271,7 @@ public class HeaderAssertions {
MediaType actual = getHeaders().getContentType();
String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]";
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType))));
assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType))));
return this.responseSpec;
}
@@ -310,16 +309,16 @@ public class HeaderAssertions {
return this.exchangeResult.getResponseHeaders();
}
private String getMessage(String headerName) {
return "Response header '" + headerName + "'";
}
private WebTestClient.ResponseSpec assertHeader(String name, @Nullable Object expected, @Nullable Object actual) {
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);
AssertionErrors.assertEquals(message, expected, actual);
assertEquals(message, expected, actual);
});
return this.responseSpec;
}
private static String getMessage(String headerName) {
return "Response header '" + headerName + "'";
}
}