From 2d3c2e3feba276d56c7968fd087bef42ef973416 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 30 Oct 2025 15:40:33 +0100 Subject: [PATCH] Revise contribution See gh-35380 --- .../util/function/SingletonSupplier.java | 12 ++- .../util/function/SingletonSupplierTests.java | 89 ++++++++++--------- ...LErrorCodeSQLExceptionTranslatorTests.java | 11 ++- 3 files changed, 63 insertions(+), 49 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/function/SingletonSupplier.java b/spring-core/src/main/java/org/springframework/util/function/SingletonSupplier.java index 572141a1675..79893dfb203 100644 --- a/spring-core/src/main/java/org/springframework/util/function/SingletonSupplier.java +++ b/spring-core/src/main/java/org/springframework/util/function/SingletonSupplier.java @@ -48,6 +48,7 @@ public class SingletonSupplier implements Supplier implements Supplier implements Supplier singletonSupplier = new SingletonSupplier<>(() -> null, () -> "Default"); + var singletonSupplier = new SingletonSupplier<>(() -> null, () -> "Default"); + assertThat(singletonSupplier.get()).isEqualTo("Default"); } @Test void shouldReturnNullForOfNullableWithNullInstance() { - SingletonSupplier singletonSupplier = SingletonSupplier.ofNullable((String) null); + var singletonSupplier = SingletonSupplier.ofNullable((String) null); + assertThat(singletonSupplier).isNull(); } @Test void shouldReturnNullForOfNullableWithNullSupplier() { - SingletonSupplier singletonSupplier = SingletonSupplier.ofNullable((Supplier) null); + var singletonSupplier = SingletonSupplier.ofNullable((Supplier) null); + assertThat(singletonSupplier).isNull(); } @Test void shouldReturnNullWhenAllSuppliersReturnNull() { - SingletonSupplier singletonSupplier = new SingletonSupplier<>(() -> null, () -> null); + var singletonSupplier = new SingletonSupplier<>(() -> null, () -> null); + assertThat(singletonSupplier.get()).isNull(); } @Test void shouldReturnNullWhenNoInstanceOrDefaultSupplier() { - SingletonSupplier singletonSupplier = new SingletonSupplier<>((String) null, null); + var singletonSupplier = new SingletonSupplier<>((String) null, null); + assertThat(singletonSupplier.get()).isNull(); } @Test void shouldReturnSingletonInstanceOnMultipleCalls() { - SingletonSupplier singletonSupplier = SingletonSupplier.of("Hello"); + var singletonSupplier = SingletonSupplier.of("Hello"); + assertThat(singletonSupplier.get()).isEqualTo("Hello"); assertThat(singletonSupplier.get()).isEqualTo("Hello"); } - @Test void shouldReturnSingletonInstanceOnMultipleSupplierCalls() { - SingletonSupplier singletonSupplier = SingletonSupplier.of(new HelloStringSupplier()); + var singletonSupplier = SingletonSupplier.of(new HelloStringSupplier()); + assertThat(singletonSupplier.get()).isEqualTo("Hello 0"); assertThat(singletonSupplier.get()).isEqualTo("Hello 0"); } @Test void shouldReturnSupplierForOfNullableWithNonNullInstance() { - SingletonSupplier singletonSupplier = SingletonSupplier.ofNullable("Hello"); + var singletonSupplier = SingletonSupplier.ofNullable("Hello"); + assertThat(singletonSupplier).isNotNull(); assertThat(singletonSupplier.get()).isEqualTo("Hello"); } @Test void shouldReturnSupplierForOfNullableWithNonNullSupplier() { - SingletonSupplier singletonSupplier = SingletonSupplier.ofNullable(() -> "Hello"); + var singletonSupplier = SingletonSupplier.ofNullable(() -> "Hello"); + assertThat(singletonSupplier).isNotNull(); assertThat(singletonSupplier.get()).isEqualTo("Hello"); } @Test void shouldThrowWhenObtainCalledAndNoInstanceAvailable() { - SingletonSupplier singletonSupplier = new SingletonSupplier<>((String) null, null); - assertThatThrownBy(singletonSupplier::obtain).isInstanceOf(IllegalStateException.class) - .hasMessage("No instance from Supplier"); + var singletonSupplier = new SingletonSupplier<>((String) null, null); + + assertThatIllegalStateException() + .isThrownBy(singletonSupplier::obtain) + .withMessage("No instance from Supplier"); } @Test void shouldUseDefaultSupplierWhenInstanceIsNull() { - SingletonSupplier singletonSupplier = new SingletonSupplier<>((String) null, () -> "defaultSupplier"); + var singletonSupplier = new SingletonSupplier<>((String) null, () -> "defaultSupplier"); + assertThat(singletonSupplier.get()).isEqualTo("defaultSupplier"); } @Test void shouldUseDefaultSupplierWhenInstanceSupplierReturnsNull() { - SingletonSupplier singletonSupplier = new SingletonSupplier<>((Supplier) null, () -> "defaultSupplier"); + var singletonSupplier = new SingletonSupplier<>((Supplier) null, () -> "defaultSupplier"); + assertThat(singletonSupplier.get()).isEqualTo("defaultSupplier"); } @Test void shouldUseInstanceSupplierWhenProvidedAndIgnoreDefaultSupplier() { - AtomicInteger defaultValue = new AtomicInteger(); - SingletonSupplier singletonSupplier = new SingletonSupplier<>(() -> -1, defaultValue::incrementAndGet); + var defaultValue = new AtomicInteger(); + var singletonSupplier = new SingletonSupplier<>(() -> -1, defaultValue::incrementAndGet); + assertThat(singletonSupplier.get()).isEqualTo(-1); - assertThat(defaultValue.get()).isEqualTo(0); + assertThat(defaultValue.get()).isZero(); } @Test void shouldUseInstanceWhenProvidedAndIgnoreDefaultSupplier() { - AtomicInteger defaultValue = new AtomicInteger(); - SingletonSupplier singletonSupplier = new SingletonSupplier<>(-1, defaultValue::incrementAndGet); + var defaultValue = new AtomicInteger(); + var singletonSupplier = new SingletonSupplier<>(-1, defaultValue::incrementAndGet); + assertThat(singletonSupplier.get()).isEqualTo(-1); - assertThat(defaultValue.get()).isEqualTo(0); + assertThat(defaultValue.get()).isZero(); } @Test void shouldReturnConsistentlyNullSingletonInstanceOnMultipleSupplierCalls() { - SingletonSupplier singletonSupplier = SingletonSupplier.of(new Supplier<>() { - - int count = 0; - - @Override - public String get() { - if (this.count++ == 0) { - return null; - } - return "Hello"; - } - }); + var count = new AtomicInteger(); + var singletonSupplier = SingletonSupplier.of(() -> (count.getAndIncrement() == 0 ? null : "Hello")); assertThat(singletonSupplier.get()).isNull(); assertThat(singletonSupplier.get()).isNull(); @@ -154,11 +158,12 @@ class SingletonSupplierTests { @RepeatedTest(100) void shouldReturnSingletonInstanceOnMultipleConcurrentSupplierCalls() throws Exception { int numberOfThreads = 4; - CountDownLatch ready = new CountDownLatch(numberOfThreads); - CountDownLatch start = new CountDownLatch(1); - List> futures = new ArrayList<>(); - SingletonSupplier singletonSupplier = SingletonSupplier.of(new HelloStringSupplier()); - ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads); + var ready = new CountDownLatch(numberOfThreads); + var start = new CountDownLatch(1); + var futures = new ArrayList>(); + var singletonSupplier = SingletonSupplier.of(new HelloStringSupplier()); + var executorService = Executors.newFixedThreadPool(numberOfThreads); + try { for (int i = 0; i < numberOfThreads; i++) { futures.add(executorService.submit(() -> { @@ -179,7 +184,7 @@ class SingletonSupplierTests { private static final class HelloStringSupplier implements Supplier { - private final AtomicInteger count = new AtomicInteger(); + final AtomicInteger count = new AtomicInteger(); @Override public String get() { diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java index fba4ada29b0..01926e0b359 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java @@ -39,7 +39,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; /** @@ -191,7 +190,7 @@ class SQLErrorCodeSQLExceptionTranslatorTests { } @Test - void dataSourceInitialization() throws Exception { + void dataSourceInitializationWhenConnectionCannotBeObtained() throws Exception { SQLException connectionException = new SQLException(); SQLException duplicateKeyException = new SQLException("test", "", 1); @@ -200,6 +199,11 @@ class SQLErrorCodeSQLExceptionTranslatorTests { translator = new SQLErrorCodeSQLExceptionTranslator(dataSource); assertThat(translator.translate("test", null, duplicateKeyException)).isNull(); + } + + @Test + void dataSourceInitialization() throws Exception { + SQLException duplicateKeyException = new SQLException("test", "", 1); DatabaseMetaData databaseMetaData = mock(); given(databaseMetaData.getDatabaseProductName()).willReturn("Oracle"); @@ -207,8 +211,9 @@ class SQLErrorCodeSQLExceptionTranslatorTests { Connection connection = mock(); given(connection.getMetaData()).willReturn(databaseMetaData); - reset(dataSource); + DataSource dataSource = mock(); given(dataSource.getConnection()).willReturn(connection); + translator = new SQLErrorCodeSQLExceptionTranslator(dataSource); assertThat(translator.translate("test", null, duplicateKeyException)) .isInstanceOf(DuplicateKeyException.class);