diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCache.java b/src/main/java/org/springframework/data/redis/cache/RedisCache.java index c900387fe..41cd0330b 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -37,7 +37,6 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.util.ByteUtils; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -82,8 +81,7 @@ public class RedisCache extends AbstractValueAdaptingCache { */ protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) { - super(RedisAssertions.requireNonNull(cacheConfiguration, "CacheConfiguration must not be null") - .getAllowCacheNullValues()); + super(cacheConfiguration.getAllowCacheNullValues()); Assert.notNull(name, "Name must not be null"); Assert.notNull(cacheWriter, "CacheWriter must not be null"); diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java index 1df8208ef..23d51456c 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java @@ -27,7 +27,6 @@ import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -103,10 +102,11 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager private RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowRuntimeCacheCreation) { - this.defaultCacheConfiguration = RedisAssertions.requireNonNull(defaultCacheConfiguration, - "DefaultCacheConfiguration must not be null"); + Assert.notNull(defaultCacheConfiguration, "DefaultCacheConfiguration must not be null"); + Assert.notNull(cacheWriter, "CacheWriter must not be null"); - this.cacheWriter = RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null"); + this.defaultCacheConfiguration = defaultCacheConfiguration; + this.cacheWriter = cacheWriter; this.initialCacheConfiguration = new LinkedHashMap<>(); this.allowRuntimeCacheCreation = allowRuntimeCacheCreation; } @@ -423,7 +423,10 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager * @see org.springframework.data.redis.cache.RedisCacheWriter */ public static RedisCacheManagerBuilder fromCacheWriter(RedisCacheWriter cacheWriter) { - return new RedisCacheManagerBuilder(RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null")); + + Assert.notNull(cacheWriter, "CacheWriter must not be null"); + + return new RedisCacheManagerBuilder(cacheWriter); } /** @@ -534,7 +537,10 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager * @since 2.3 */ public RedisCacheManagerBuilder cacheWriter(RedisCacheWriter cacheWriter) { - this.cacheWriter = RedisAssertions.requireNonNull(cacheWriter, "CacheWriter must not be null"); + + Assert.notNull(cacheWriter, "CacheWriter must not be null"); + + this.cacheWriter = cacheWriter; return this; } @@ -558,8 +564,10 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager */ public RedisCacheManagerBuilder initialCacheNames(Set cacheNames) { - RedisAssertions.requireNonNull(cacheNames, "CacheNames must not be null") - .forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration)); + Assert.notNull(cacheNames, "CacheNames must not be null"); + Assert.noNullElements(cacheNames, "CacheNames must not be null"); + + cacheNames.forEach(it -> withCacheConfiguration(it, defaultCacheConfiguration)); return this; } @@ -603,9 +611,9 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager public RedisCacheManagerBuilder withInitialCacheConfigurations( Map cacheConfigurations) { - RedisAssertions.requireNonNull(cacheConfigurations, "CacheConfigurations must not be null") - .forEach((cacheName, cacheConfiguration) -> RedisAssertions.requireNonNull(cacheConfiguration, - "RedisCacheConfiguration for cache [%s] must not be null", cacheName)); + Assert.notNull(cacheConfigurations, "CacheConfigurations must not be null!"); + cacheConfigurations.forEach((cacheName, configuration) -> Assert.notNull(configuration, + String.format("RedisCacheConfiguration for cache %s must not be null!", cacheName))); this.initialCaches.putAll(cacheConfigurations); diff --git a/src/main/java/org/springframework/data/redis/connection/RedisClusterConfiguration.java b/src/main/java/org/springframework/data/redis/connection/RedisClusterConfiguration.java index 620e132d0..057527ed0 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisClusterConfiguration.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisClusterConfiguration.java @@ -25,7 +25,6 @@ import java.util.Set; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.NumberUtils; @@ -161,7 +160,10 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon * @param node must not be {@literal null}. */ public void addClusterNode(RedisNode node) { - this.clusterNodes.add(RedisAssertions.requireNonNull(node, "ClusterNode must not be null")); + + Assert.notNull(node, "ClusterNode must not be null"); + + this.clusterNodes.add(node); } /** @@ -211,7 +213,10 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon @Override public void setPassword(RedisPassword password) { - this.password = RedisAssertions.requireNonNull(password, "RedisPassword must not be null"); + + Assert.notNull(password, "RedisPassword must not be null"); + + this.password = password; } @Override diff --git a/src/main/java/org/springframework/data/redis/connection/RedisClusterNode.java b/src/main/java/org/springframework/data/redis/connection/RedisClusterNode.java index 7f84a2110..99cd93707 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisClusterNode.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisClusterNode.java @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -76,7 +75,9 @@ public class RedisClusterNode extends RedisNode { this(SlotRange.empty()); - this.id = RedisAssertions.requireNonNull(id, "Id must not be null"); + Assert.notNull(id, "Id must not be null"); + + this.id = id; } /** @@ -86,8 +87,10 @@ public class RedisClusterNode extends RedisNode { */ public RedisClusterNode(SlotRange slotRange) { + Assert.notNull(slotRange, "SlotRange must not be null"); + this.flags = Collections.emptySet(); - this.slotRange = RedisAssertions.requireNonNull(slotRange,"SlotRange must not be null"); + this.slotRange = slotRange; } /** @@ -101,8 +104,10 @@ public class RedisClusterNode extends RedisNode { super(host, port); + Assert.notNull(slotRange, "SlotRange must not be null"); + this.flags = Collections.emptySet(); - this.slotRange = RedisAssertions.requireNonNull(slotRange,"SlotRange must not be null"); + this.slotRange = slotRange; } /** diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java index fdbc882a9..6d5431654 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java @@ -64,7 +64,6 @@ import org.springframework.data.redis.connection.*; import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration; import org.springframework.data.redis.connection.RedisConfiguration.WithDatabaseIndex; import org.springframework.data.redis.connection.RedisConfiguration.WithPassword; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.data.util.Optionals; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -670,8 +669,11 @@ public class LettuceConnectionFactory implements RedisConnectionFactory, Reactiv */ public AbstractRedisClient getRequiredNativeClient() { - return RedisAssertions.requireState(getNativeClient(), - "Client not yet initialized; Did you forget to call initialize the bean"); + AbstractRedisClient client = getNativeClient(); + + Assert.state(client != null, "Client not yet initialized; Did you forget to call initialize the bean"); + + return client; } @Nullable diff --git a/src/main/java/org/springframework/data/redis/core/DefaultReactiveZSetOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultReactiveZSetOperations.java index 40869055e..5fc762f51 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultReactiveZSetOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultReactiveZSetOperations.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.function.Function; import org.reactivestreams.Publisher; + import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.domain.Range; import org.springframework.data.redis.connection.Limit; @@ -38,7 +39,6 @@ import org.springframework.data.redis.connection.zset.Weights; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.util.ByteUtils; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -745,8 +745,13 @@ class DefaultReactiveZSetOperations implements ReactiveZSetOperations new InvalidDataAccessApiUsageException("Deserialized sorted set value is null")); + V value = readValue(buffer); + + if (value == null) { + throw new InvalidDataAccessApiUsageException("Deserialized sorted set value is null"); + } + + return value; } private TypedTuple readTypedTuple(Tuple raw) { diff --git a/src/main/java/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.java b/src/main/java/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.java index 00c79c6f8..d25ea9c39 100644 --- a/src/main/java/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.java +++ b/src/main/java/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.java @@ -20,8 +20,8 @@ import org.springframework.core.serializer.DefaultDeserializer; import org.springframework.core.serializer.DefaultSerializer; import org.springframework.core.serializer.support.DeserializingConverter; import org.springframework.core.serializer.support.SerializingConverter; -import org.springframework.data.redis.util.RedisAssertions; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * Java Serialization {@link RedisSerializer}. @@ -77,8 +77,11 @@ public class JdkSerializationRedisSerializer implements RedisSerializer public JdkSerializationRedisSerializer(Converter serializer, Converter deserializer) { - this.serializer = RedisAssertions.requireNonNull(serializer, "Serializer must not be null"); - this.deserializer = RedisAssertions.requireNonNull(deserializer, "Deserializer must not be null"); + Assert.notNull(serializer, "Serializer must not be null"); + Assert.notNull(deserializer, "Deserializer must not be null"); + + this.serializer = serializer; + this.deserializer = deserializer; } @Override diff --git a/src/main/java/org/springframework/data/redis/util/RedisAssertions.java b/src/main/java/org/springframework/data/redis/util/RedisAssertions.java index 2b0272de2..a218f762a 100644 --- a/src/main/java/org/springframework/data/redis/util/RedisAssertions.java +++ b/src/main/java/org/springframework/data/redis/util/RedisAssertions.java @@ -25,7 +25,9 @@ import org.springframework.util.Assert; * * @author John Blum * @since 3.1.0 + * @deprecated since 3.3, will be removed in a future revision in favor of Spring's {@link Assert} utility. */ +@Deprecated(since = "3.3", forRemoval = true) public abstract class RedisAssertions { /** diff --git a/src/test/java/org/springframework/data/redis/util/RedisAssertionsUnitTests.java b/src/test/java/org/springframework/data/redis/util/RedisAssertionsUnitTests.java deleted file mode 100644 index d9fbe94d1..000000000 --- a/src/test/java/org/springframework/data/redis/util/RedisAssertionsUnitTests.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2017-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. - * 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.data.redis.util; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; -import static org.assertj.core.api.Assertions.assertThatIllegalStateException; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -import java.util.function.Supplier; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import org.springframework.dao.InvalidDataAccessApiUsageException; - -/** - * Unit Tests for {@link RedisAssertions}. - * - * @author John Blum - */ -@ExtendWith(MockitoExtension.class) -class RedisAssertionsUnitTests { - - @Mock - private Supplier mockSupplier; - - @Test - void requireNonNullWithMessageAndArgumentsIsSuccessful() { - assertThat(RedisAssertions.requireNonNull("test", "Test message")).isEqualTo("test"); - } - - @Test - void requireNonNullWithMessageAndArgumentsThrowsIllegalArgumentException() { - - assertThatIllegalArgumentException() - .isThrownBy(() -> RedisAssertions.requireNonNull(null, "This is a %s", "test")) - .withMessage("This is a test") - .withNoCause(); - } - - @Test - void requireNonNullWithSupplierIsSuccessful() { - - assertThat(RedisAssertions.requireNonNull("mock", this.mockSupplier)).isEqualTo("mock"); - - verifyNoInteractions(this.mockSupplier); - } - - @Test - void requireNonNullWithSupplierThrowsIllegalArgumentException() { - - doReturn("Mock message").when(this.mockSupplier).get(); - - assertThatIllegalArgumentException() - .isThrownBy(() -> RedisAssertions.requireNonNull(null, this.mockSupplier)) - .withMessage("Mock message") - .withNoCause(); - - verify(this.mockSupplier, times(1)).get(); - verifyNoMoreInteractions(this.mockSupplier); - } - - @Test - void requireNonNullWithRuntimeExceptionSupplierIsSuccessful() { - - assertThat(RedisAssertions.requireNonNull("mock", () -> new InvalidDataAccessApiUsageException("TEST"))) - .isEqualTo("mock"); - } - - @Test - @SuppressWarnings("all") - void requireNonNullWithThrowsRuntimeException() { - - assertThatExceptionOfType(InvalidDataAccessApiUsageException.class) - .isThrownBy(() -> RedisAssertions.requireNonNull(null, - () -> new InvalidDataAccessApiUsageException("TEST"))) - .withMessage("TEST") - .withNoCause(); - } - - @Test - void requireStateWithMessageAndArgumentsIsSuccessful() { - assertThat(RedisAssertions.requireState("test", "Mock message")).isEqualTo("test"); - } - - @Test - void requireStateWithMessageAndArgumentsThrowsIllegalStateException() { - - assertThatIllegalStateException() - .isThrownBy(() -> RedisAssertions.requireState(null, "This is a %s", "test")) - .withMessage("This is a test") - .withNoCause(); - } - - @Test - void requireStateWithSupplierIsSuccessful() { - - assertThat(RedisAssertions.requireState("test", this.mockSupplier)).isEqualTo("test"); - - verifyNoInteractions(this.mockSupplier); - } - - @Test - void requiredStateWithSupplierThrowsIllegalStateException() { - - doReturn("Mock message").when(this.mockSupplier).get(); - - assertThatIllegalStateException() - .isThrownBy(() -> RedisAssertions.requireState(null, this.mockSupplier)) - .withMessage("Mock message") - .withNoCause(); - - verify(this.mockSupplier, times(1)).get(); - verifyNoMoreInteractions(this.mockSupplier); - } -}