Polishing.

Simplify code to use well-known Spring patterns.

Original pull request: #2752
See #2751
This commit is contained in:
Mark Paluch
2024-09-10 14:01:02 +02:00
parent c7ab023d40
commit f9dd9bc246
9 changed files with 58 additions and 167 deletions

View File

@@ -39,7 +39,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;
@@ -86,8 +85,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");

View File

@@ -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<String> 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<String, RedisCacheConfiguration> 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);

View File

@@ -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

View File

@@ -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;
}
/**

View File

@@ -63,7 +63,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;
@@ -666,8 +665,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

View File

@@ -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<K, V> implements ReactiveZSetOperations<K, V
private V readRequiredValue(ByteBuffer buffer) {
return RedisAssertions.requireNonNull(readValue(buffer),
() -> 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<V> readTypedTuple(Tuple raw) {

View File

@@ -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<Object>
public JdkSerializationRedisSerializer(Converter<Object, byte[]> serializer,
Converter<byte[], Object> 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

View File

@@ -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 {
/**

View File

@@ -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<String> 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);
}
}