From 1b312b6f3fb59d5e9a00ac3f4fef63d6971efa3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Tue, 16 Jan 2024 18:45:59 +0100 Subject: [PATCH] Polish --- .../reactive/server/DefaultWebTestClient.java | 37 +++++++------------ .../server/DefaultWebTestClientBuilder.java | 2 +- .../reactive/server/HttpHandlerConnector.java | 2 +- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index 5ce9b8f22316..7e23910cf330 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -185,9 +185,6 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec { private final Map attributes = new LinkedHashMap<>(4); - @Nullable - private Consumer httpRequestConsumer; - @Nullable private String uriTemplate; @@ -195,7 +192,7 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec { DefaultRequestBodyUriSpec(HttpMethod httpMethod) { this.httpMethod = httpMethod; - this.requestId = String.valueOf(requestIndex.incrementAndGet()); + this.requestId = String.valueOf(DefaultWebTestClient.this.requestIndex.incrementAndGet()); this.headers = new HttpHeaders(); this.headers.add(WebTestClient.WEBTESTCLIENT_REQUEST_ID, this.requestId); } @@ -203,19 +200,19 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec { @Override public RequestBodySpec uri(String uriTemplate, Object... uriVariables) { this.uriTemplate = uriTemplate; - return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); + return uri(DefaultWebTestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables)); } @Override public RequestBodySpec uri(String uriTemplate, Map uriVariables) { this.uriTemplate = uriTemplate; - return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); + return uri(DefaultWebTestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables)); } @Override public RequestBodySpec uri(Function uriFunction) { this.uriTemplate = null; - return uri(uriFunction.apply(uriBuilderFactory.builder())); + return uri(uriFunction.apply(DefaultWebTestClient.this.uriBuilderFactory.builder())); } @Override @@ -358,10 +355,10 @@ public ResponseSpec exchange() { initRequestBuilder().body(this.inserter).build() : initRequestBuilder().build()); - ClientResponse response = exchangeFunction.exchange(request).block(getResponseTimeout()); + ClientResponse response = DefaultWebTestClient.this.exchangeFunction.exchange(request).block(getResponseTimeout()); Assert.state(response != null, "No ClientResponse"); - ExchangeResult result = wiretapConnector.getExchangeResult( + ExchangeResult result = DefaultWebTestClient.this.wiretapConnector.getExchangeResult( this.requestId, this.uriTemplate, getResponseTimeout()); return new DefaultResponseSpec(result, response, @@ -369,34 +366,28 @@ public ResponseSpec exchange() { } private ClientRequest.Builder initRequestBuilder() { - ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri()) + return ClientRequest.create(this.httpMethod, initUri()) .headers(headersToUse -> { - if (!CollectionUtils.isEmpty(defaultHeaders)) { - headersToUse.putAll(defaultHeaders); + if (!CollectionUtils.isEmpty(DefaultWebTestClient.this.defaultHeaders)) { + headersToUse.putAll(DefaultWebTestClient.this.defaultHeaders); } if (!CollectionUtils.isEmpty(this.headers)) { headersToUse.putAll(this.headers); } }) .cookies(cookiesToUse -> { - if (!CollectionUtils.isEmpty(defaultCookies)) { - cookiesToUse.putAll(defaultCookies); + if (!CollectionUtils.isEmpty(DefaultWebTestClient.this.defaultCookies)) { + cookiesToUse.putAll(DefaultWebTestClient.this.defaultCookies); } if (!CollectionUtils.isEmpty(this.cookies)) { cookiesToUse.putAll(this.cookies); } }) .attributes(attributes -> attributes.putAll(this.attributes)); - - if (this.httpRequestConsumer != null) { - builder.httpRequest(this.httpRequestConsumer); - } - - return builder; } private URI initUri() { - return (this.uri != null ? this.uri : uriBuilderFactory.expand("")); + return (this.uri != null ? this.uri : DefaultWebTestClient.this.uriBuilderFactory.expand("")); } } @@ -520,9 +511,7 @@ public ResponseSpec expectAll(ResponseSpecConsumer... consumers) { // that is not a RuntimeException, but since ExceptionCollector may // throw a checked Exception, we handle this to appease the compiler // and in case someone uses a "sneaky throws" technique. - AssertionError assertionError = new AssertionError(ex.getMessage()); - assertionError.initCause(ex); - throw assertionError; + throw new AssertionError(ex.getMessage(), ex); } return this; } diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java index 136cc932740e..996db49b8b36 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java @@ -256,7 +256,7 @@ public WebTestClient.Builder exchangeStrategies(ExchangeStrategies strategies) { } @Override - @SuppressWarnings("deprecation") + @Deprecated public WebTestClient.Builder exchangeStrategies(Consumer configurer) { if (this.strategiesConfigurers == null) { this.strategiesConfigurers = new ArrayList<>(4); diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/HttpHandlerConnector.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/HttpHandlerConnector.java index 92df11075da2..b89b664de23d 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/HttpHandlerConnector.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/HttpHandlerConnector.java @@ -60,7 +60,7 @@ */ public class HttpHandlerConnector implements ClientHttpConnector { - private static Log logger = LogFactory.getLog(HttpHandlerConnector.class); + private static final Log logger = LogFactory.getLog(HttpHandlerConnector.class); private final HttpHandler handler;