Polishing

This commit is contained in:
Sam Brannen
2022-11-08 11:33:57 +01:00
parent aeb35663d4
commit 3438c47744
36 changed files with 190 additions and 228 deletions

View File

@@ -43,7 +43,6 @@ import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
/**
@@ -236,7 +235,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
*/
@Deprecated(since = "6.0")
public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
Assert.hasText(httpMethod, "HTTP method is required.");
return new DefaultBodyBuilder(HttpMethod.valueOf(httpMethod), toUri(uri, vars));
}

View File

@@ -549,11 +549,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setParameters(Map<String, ?> params) {
Assert.notNull(params, "Parameter map must not be null");
params.forEach((key, value) -> {
if (value instanceof String) {
setParameter(key, (String) value);
if (value instanceof String str) {
setParameter(key, str);
}
else if (value instanceof String[]) {
setParameter(key, (String[]) value);
else if (value instanceof String[] strings) {
setParameter(key, strings);
}
else {
throw new IllegalArgumentException(
@@ -598,11 +598,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void addParameters(Map<String, ?> params) {
Assert.notNull(params, "Parameter map must not be null");
params.forEach((key, value) -> {
if (value instanceof String) {
addParameter(key, (String) value);
if (value instanceof String str) {
addParameter(key, str);
}
else if (value instanceof String[]) {
addParameter(key, (String[]) value);
else if (value instanceof String[] strings) {
addParameter(key, strings);
}
else {
throw new IllegalArgumentException("Parameter map value must be single value " +
@@ -1083,8 +1083,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
header = new HeaderValueHolder();
this.headers.put(name, header);
}
if (value instanceof Collection) {
header.addValues((Collection<?>) value);
if (value instanceof Collection<?> collection) {
header.addValues(collection);
}
else if (value.getClass().isArray()) {
header.addValueArray(value);
@@ -1119,14 +1119,14 @@ public class MockHttpServletRequest implements HttpServletRequest {
public long getDateHeader(String name) {
HeaderValueHolder header = this.headers.get(name);
Object value = (header != null ? header.getValue() : null);
if (value instanceof Date) {
return ((Date) value).getTime();
if (value instanceof Date date) {
return date.getTime();
}
else if (value instanceof Number) {
return ((Number) value).longValue();
else if (value instanceof Number number) {
return number.longValue();
}
else if (value instanceof String) {
return parseDateHeader(name, (String) value);
else if (value instanceof String str) {
return parseDateHeader(name, str);
}
else if (value != null) {
throw new IllegalArgumentException(
@@ -1173,11 +1173,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
public int getIntHeader(String name) {
HeaderValueHolder header = this.headers.get(name);
Object value = (header != null ? header.getValue() : null);
if (value instanceof Number) {
return ((Number) value).intValue();
if (value instanceof Number number) {
return number.intValue();
}
else if (value instanceof String) {
return Integer.parseInt((String) value);
else if (value instanceof String str) {
return Integer.parseInt(str);
}
else if (value != null) {
throw new NumberFormatException("Value for header '" + name + "' is not a Number: " + value);
@@ -1248,8 +1248,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public boolean isUserInRole(String role) {
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext &&
((MockServletContext) this.servletContext).getDeclaredRoles().contains(role)));
return (this.userRoles.contains(role) ||
(this.servletContext instanceof MockServletContext mockContext &&
mockContext.getDeclaredRoles().contains(role)));
}
public void setUserPrincipal(@Nullable Principal userPrincipal) {
@@ -1321,7 +1322,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
public HttpSession getSession(boolean create) {
checkActive();
// Reset session if invalidated.
if (this.session instanceof MockHttpSession && ((MockHttpSession) this.session).isInvalid()) {
if (this.session instanceof MockHttpSession mockSession && mockSession.isInvalid()) {
this.session = null;
}
// Create new session if necessary.
@@ -1346,8 +1347,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) {
return ((MockHttpSession) this.session).changeSessionId();
if (this.session instanceof MockHttpSession mockSession) {
return mockSession.changeSessionId();
}
return this.session.getId();
}

View File

@@ -424,7 +424,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
buf.append("; Domain=").append(cookie.getDomain());
}
int maxAge = cookie.getMaxAge();
ZonedDateTime expires = (cookie instanceof MockCookie mockCookie? mockCookie.getExpires() : null);
ZonedDateTime expires = (cookie instanceof MockCookie mockCookie ? mockCookie.getExpires() : null);
if (maxAge >= 0) {
buf.append("; Max-Age=").append(maxAge);
buf.append("; Expires=");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 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.
@@ -187,7 +187,7 @@ public abstract class ProfileValueUtils {
String environmentValue = profileValueSource.get(ifProfileValue.name());
String[] annotatedValues = ifProfileValue.values();
if (StringUtils.hasLength(ifProfileValue.value())) {
Assert.isTrue(annotatedValues.length == 0, () -> "Setting both the 'value' and 'values' attributes " +
Assert.isTrue(annotatedValues.length == 0, "Setting both the 'value' and 'values' attributes " +
"of @IfProfileValue is not allowed: choose one or the other.");
annotatedValues = new String[] { ifProfileValue.value() };
}

View File

@@ -89,10 +89,8 @@ class WiretapConnector implements ClientHttpConnector {
*/
ExchangeResult getExchangeResult(String requestId, @Nullable String uriTemplate, Duration timeout) {
ClientExchangeInfo clientInfo = this.exchanges.remove(requestId);
Assert.state(clientInfo != null, () -> {
String header = WebTestClient.WEBTESTCLIENT_REQUEST_ID;
return "No match for " + header + "=" + requestId;
});
Assert.state(clientInfo != null, () -> "No match for %s=%s".formatted(
WebTestClient.WEBTESTCLIENT_REQUEST_ID, requestId));
return new ExchangeResult(clientInfo.getRequest(), clientInfo.getResponse(),
clientInfo.getRequest().getRecorder().getContent(),
clientInfo.getResponse().getRecorder().getContent(),