From e6d360c1c61b3fe6b7125a602f76fe4a32ff91f8 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 15 Jul 2023 12:53:12 +0200 Subject: [PATCH] Polishing --- .../ROOT/pages/web/webflux/config.adoc | 4 +- .../jdbc/support/SqlArrayValueTests.java | 4 +- .../dao/support/DataAccessUtils.java | 5 ++- ...tenceExceptionTranslationAdvisorTests.java | 2 +- ...edPersistenceExceptionTranslatorTests.java | 10 ++--- .../dao/support/DataAccessUtilsTests.java | 41 +++++-------------- .../MapPersistenceExceptionTranslator.java | 40 ++++++++++++++++++ .../http/server/ServletServerHttpRequest.java | 2 +- .../web/util/ForwardedHeaderUtils.java | 30 +++++++------- .../config/BlockingExecutionConfigurer.java | 4 +- .../RequestMappingHandlerAdapter.java | 2 +- 11 files changed, 82 insertions(+), 62 deletions(-) create mode 100644 spring-tx/src/test/java/org/springframework/dao/support/MapPersistenceExceptionTranslator.java diff --git a/framework-docs/modules/ROOT/pages/web/webflux/config.adoc b/framework-docs/modules/ROOT/pages/web/webflux/config.adoc index 144a65de46..a74d18f2ca 100644 --- a/framework-docs/modules/ROOT/pages/web/webflux/config.adoc +++ b/framework-docs/modules/ROOT/pages/web/webflux/config.adoc @@ -742,7 +742,7 @@ reliance on it. [[webflux-config-blocking-execution]] == Blocking Execution -The WebFlux Java config lets you to customize blocking execution in WebFlux. +The WebFlux Java config allows you to customize blocking execution in WebFlux. You can have blocking controller methods called on a separate thread by providing an `Executor` such as the @@ -777,7 +777,7 @@ Kotlin:: @Override fun configureBlockingExecution(configurer: BlockingExecutionConfigurer) { - val executor = ... + val executor = ... configurer.setExecutor(executor) } } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SqlArrayValueTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SqlArrayValueTests.java index 9207591dbf..10fbebeb07 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SqlArrayValueTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SqlArrayValueTests.java @@ -33,7 +33,7 @@ import static org.mockito.BDDMockito.verify; * @author Philippe Marschall * @since 6.1 */ -public class SqlArrayValueTests { +class SqlArrayValueTests { private final Connection con = mock(); @@ -47,7 +47,7 @@ public class SqlArrayValueTests { @Test - public void setValue() throws SQLException { + void setValue() throws SQLException { given(this.ps.getConnection()).willReturn(this.con); given(this.con.createArrayOf("smallint", elements)).willReturn(this.arr); diff --git a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java index 0fe3823184..fb6395c8ee 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java +++ b/spring-tx/src/main/java/org/springframework/dao/support/DataAccessUtils.java @@ -33,7 +33,8 @@ import org.springframework.util.NumberUtils; /** * Miscellaneous utility methods for DAO implementations. - * Useful with any data access technology. + * + *

Useful with any data access technology. * * @author Juergen Hoeller * @since 1.0.2 @@ -80,7 +81,7 @@ public abstract class DataAccessUtils { if (resultList.size() > 1) { throw new IncorrectResultSizeDataAccessException(1); } - return CollectionUtils.isEmpty(resultList) ? null : resultList.get(0); + return resultList.isEmpty() ? null : resultList.get(0); } } diff --git a/spring-tx/src/test/java/org/springframework/dao/annotation/PersistenceExceptionTranslationAdvisorTests.java b/spring-tx/src/test/java/org/springframework/dao/annotation/PersistenceExceptionTranslationAdvisorTests.java index 876d5405ea..811cdcb7e2 100644 --- a/spring-tx/src/test/java/org/springframework/dao/annotation/PersistenceExceptionTranslationAdvisorTests.java +++ b/spring-tx/src/test/java/org/springframework/dao/annotation/PersistenceExceptionTranslationAdvisorTests.java @@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; -import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator; +import org.springframework.dao.support.MapPersistenceExceptionTranslator; import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.stereotype.Repository; diff --git a/spring-tx/src/test/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslatorTests.java b/spring-tx/src/test/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslatorTests.java index a4d5e24ae9..6a4e1e837c 100644 --- a/spring-tx/src/test/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslatorTests.java +++ b/spring-tx/src/test/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 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. @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.OptimisticLockingFailureException; -import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator; import static org.assertj.core.api.Assertions.assertThat; @@ -28,18 +27,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Rod Johnson * @since 2.0 */ -public class ChainedPersistenceExceptionTranslatorTests { +class ChainedPersistenceExceptionTranslatorTests { @Test - public void empty() { + void empty() { ChainedPersistenceExceptionTranslator pet = new ChainedPersistenceExceptionTranslator(); - //MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); RuntimeException in = new RuntimeException("in"); assertThat(DataAccessUtils.translateIfNecessary(in, pet)).isSameAs(in); } @Test - public void exceptionTranslationWithTranslation() { + void exceptionTranslationWithTranslation() { MapPersistenceExceptionTranslator mpet1 = new MapPersistenceExceptionTranslator(); RuntimeException in1 = new RuntimeException("in"); InvalidDataAccessApiUsageException out1 = new InvalidDataAccessApiUsageException("out"); diff --git a/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java b/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java index 59b1dde4f4..f80e92df13 100644 --- a/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java +++ b/spring-tx/src/test/java/org/springframework/dao/support/DataAccessUtilsTests.java @@ -20,15 +20,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Optional; import java.util.function.Consumer; import org.junit.jupiter.api.Test; -import org.springframework.dao.DataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.dao.TypeMismatchDataAccessException; @@ -40,10 +37,10 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Juergen Hoeller * @since 20.10.2004 */ -public class DataAccessUtilsTests { +class DataAccessUtilsTests { @Test - public void withEmptyCollection() { + void withEmptyCollection() { Collection col = new HashSet<>(); assertThat(DataAccessUtils.uniqueResult(col)).isNull(); @@ -73,7 +70,7 @@ public class DataAccessUtilsTests { } @Test - public void withTooLargeCollection() { + void withTooLargeCollection() { Collection col = new HashSet<>(2); col.add("test1"); col.add("test2"); @@ -124,7 +121,7 @@ public class DataAccessUtilsTests { } @Test - public void withInteger() { + void withInteger() { Collection col = new HashSet<>(1); col.add(5); @@ -143,7 +140,7 @@ public class DataAccessUtilsTests { } @Test - public void withSameIntegerInstanceTwice() { + void withSameIntegerInstanceTwice() { Integer i = 5; Collection col = new ArrayList<>(1); col.add(i); @@ -158,7 +155,7 @@ public class DataAccessUtilsTests { } @Test - public void withEquivalentIntegerInstanceTwice() { + void withEquivalentIntegerInstanceTwice() { Collection col = Arrays.asList(555, 555); assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class) @@ -167,7 +164,7 @@ public class DataAccessUtilsTests { } @Test - public void withLong() { + void withLong() { Collection col = new HashSet<>(1); col.add(5L); @@ -186,7 +183,7 @@ public class DataAccessUtilsTests { } @Test - public void withString() { + void withString() { Collection col = new HashSet<>(1); col.add("test1"); @@ -208,7 +205,7 @@ public class DataAccessUtilsTests { } @Test - public void withDate() { + void withDate() { Date date = new Date(); Collection col = new HashSet<>(1); col.add(date); @@ -232,14 +229,14 @@ public class DataAccessUtilsTests { } @Test - public void exceptionTranslationWithNoTranslation() { + void exceptionTranslationWithNoTranslation() { MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); RuntimeException in = new RuntimeException(); assertThat(DataAccessUtils.translateIfNecessary(in, mpet)).isSameAs(in); } @Test - public void exceptionTranslationWithTranslation() { + void exceptionTranslationWithTranslation() { MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); RuntimeException in = new RuntimeException("in"); InvalidDataAccessApiUsageException out = new InvalidDataAccessApiUsageException("out"); @@ -261,20 +258,4 @@ public class DataAccessUtilsTests { return ex -> assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize); } - - public static class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator { - - // in to out - private final Map translations = new HashMap<>(); - - public void addTranslation(RuntimeException in, RuntimeException out) { - this.translations.put(in, out); - } - - @Override - public DataAccessException translateExceptionIfPossible(RuntimeException ex) { - return (DataAccessException) translations.get(ex); - } - } - } diff --git a/spring-tx/src/test/java/org/springframework/dao/support/MapPersistenceExceptionTranslator.java b/spring-tx/src/test/java/org/springframework/dao/support/MapPersistenceExceptionTranslator.java new file mode 100644 index 0000000000..f500c64c5a --- /dev/null +++ b/spring-tx/src/test/java/org/springframework/dao/support/MapPersistenceExceptionTranslator.java @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.dao.support; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.dao.DataAccessException; + +public class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator { + + // in to out + private final Map translations = new HashMap<>(); + + + public void addTranslation(RuntimeException in, RuntimeException out) { + this.translations.put(in, out); + } + + + @Override + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return (DataAccessException) this.translations.get(ex); + } + +} diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 8b91cb6f38..ee2af3a17e 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -103,7 +103,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { } /** - * Initialize a URI from the given Servet request. + * Initialize a URI from the given Servlet request. * @param servletRequest the request * @return the initialized URI * @since 6.1 diff --git a/spring-web/src/main/java/org/springframework/web/util/ForwardedHeaderUtils.java b/spring-web/src/main/java/org/springframework/web/util/ForwardedHeaderUtils.java index 1ef296021d..fa7df6d282 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ForwardedHeaderUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/ForwardedHeaderUtils.java @@ -25,21 +25,20 @@ import org.springframework.http.HttpHeaders; import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; - /** * Utility class to assist with processing "Forwarded" and "X-Forwarded-*" headers. * - *

Note:There are security considerations surrounding the use + *

Note: There are security considerations surrounding the use * of forwarded headers. Those should not be used unless the application is * behind a trusted proxy that inserts them and also explicitly removes any such * headers coming from an external source. * - *

In most cases, should not use this class directly but rely on - * {@link org.springframework.web.filter.ForwardedHeaderFilter} for Spring MVC, or + *

In most cases, you should not use this class directly but rather rely on + * {@link org.springframework.web.filter.ForwardedHeaderFilter} for Spring MVC or * {@link org.springframework.web.server.adapter.ForwardedHeaderTransformer} in - * order to extract the information from them as early as possible, and discard - * such headers. Underlying servers such as Tomcat, Jetty, Reactor Netty, also - * provides options to handle forwarded headers even earlier. + * order to extract the information from the headers as early as possible and discard + * such headers. Underlying servers such as Tomcat, Jetty, and Reactor Netty also + * provide options to handle forwarded headers even earlier. * * @author Rossen Stoyanchev * @since 6.1 @@ -56,10 +55,11 @@ public abstract class ForwardedHeaderUtils { /** - * Adapt the scheme+host+port of the given {@link URI} from the "Forwarded" header, - * see RFC 7239, or - * "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if "Forwarded" - * is not present. + * Adapt the scheme+host+port of the given {@link URI} from the "Forwarded" header + * (see RFC 7239) or from the + * "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers if + * "Forwarded" is not present. + * @param uri the request {@code URI} * @param headers the HTTP headers to consider * @return a {@link UriComponentsBuilder} that reflects the request URI and * additional updates from forwarded headers @@ -106,7 +106,7 @@ public abstract class ForwardedHeaderUtils { catch (NumberFormatException ex) { throw new IllegalArgumentException("Failed to parse a port from \"forwarded\"-type headers. " + "If not behind a trusted proxy, consider using ForwardedHeaderFilter " + - "with the removeOnly=true. Request headers: " + headers); + "with removeOnly=true. Request headers: " + headers); } uriComponentsBuilder.resetPortIfDefaultForScheme(); @@ -138,11 +138,11 @@ public abstract class ForwardedHeaderUtils { /** * Parse the first "Forwarded: for=..." or "X-Forwarded-For" header value to * an {@code InetSocketAddress} representing the address of the client. - * @param uri the request URI + * @param uri the request {@code URI} * @param headers the request headers that may contain forwarded headers - * @param remoteAddress the current remoteAddress + * @param remoteAddress the current remote address * @return an {@code InetSocketAddress} with the extracted host and port, or - * {@code null} if the headers are not present. + * {@code null} if the headers are not present * @see RFC 7239, Section 5.2 */ @Nullable diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/BlockingExecutionConfigurer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/BlockingExecutionConfigurer.java index c04cfec6cc..ca56c99549 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/BlockingExecutionConfigurer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/BlockingExecutionConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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 BlockingExecutionConfigurer { /** * Configure a predicate to decide if a controller method is blocking and * should be called on a separate thread if an executor is - * {@link #setExecutor configured}. + * {@linkplain #setExecutor configured}. *

The default predicate matches controller methods whose return type is * not recognized by the configured * {@link org.springframework.core.ReactiveAdapterRegistry}. diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java index 6a1130c09c..d94c32aeae 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java @@ -151,7 +151,7 @@ public class RequestMappingHandlerAdapter * Provide a predicate to decide which controller methods to invoke through * the configured {@link #setBlockingExecutor blockingExecutor}. *

If an executor is configured, the default predicate matches controller - * methods whose return type not recognized by the configured + * methods whose return type is not recognized by the configured * {@link org.springframework.core.ReactiveAdapterRegistry}. * @param predicate the predicate to use * @since 6.1