diff --git a/src/main/java/org/springframework/data/redis/connection/DataType.java b/src/main/java/org/springframework/data/redis/connection/DataType.java index 070a11be6..911859afe 100644 --- a/src/main/java/org/springframework/data/redis/connection/DataType.java +++ b/src/main/java/org/springframework/data/redis/connection/DataType.java @@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap; * * @author Costin Leau * @author Mark Paluch + * @author Christoph Strobl */ public enum DataType { @@ -36,9 +37,9 @@ public enum DataType { private static final Map codeLookup = new ConcurrentHashMap<>(7); static { - for (DataType type : EnumSet.allOf(DataType.class)) + for (DataType type : EnumSet.allOf(DataType.class)) { codeLookup.put(type.code, type); - + } } private final String code; @@ -63,9 +64,11 @@ public enum DataType { * @return actual enum corresponding to the given code */ public static DataType fromCode(String code) { + DataType data = codeLookup.get(code); - if (data == null) - throw new IllegalArgumentException("unknown data type code"); + if (data == null) { + throw new IllegalArgumentException("unknown data type code %s".formatted(code)); + } return data; } } diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java b/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java index 376181c1f..d97a63dc5 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java @@ -45,6 +45,7 @@ public class RedisCollectionFactoryBean implements SmartFactoryBean, * * @author Costin Leau * @author Mark Paluch + * @author Christoph Strobl */ public enum CollectionType { LIST { @@ -95,6 +96,10 @@ public class RedisCollectionFactoryBean implements SmartFactoryBean, */ static CollectionType findCollectionType(@Nullable DataType dataType, Supplier ifNotFound) { + if (dataType == null) { + return ifNotFound.get(); + } + for (CollectionType collectionType : values()) { if (collectionType.dataType() == dataType) { return collectionType; @@ -135,7 +140,7 @@ public class RedisCollectionFactoryBean implements SmartFactoryBean, if (keyType != null && DataType.NONE != keyType && this.type.dataType() != keyType) { throw new IllegalArgumentException( - String.format("Cannot create collection type '%s' for a key containing '%s'", this.type, keyType)); + "Cannot create collection type '%s' for a key containing '%s'".formatted(this.type, keyType)); } return createStore(this.type, key, template); diff --git a/src/test/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBeanTests.java b/src/test/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBeanTests.java index 3af400c75..6af4a1fdf 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBeanTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBeanTests.java @@ -36,6 +36,7 @@ import org.springframework.data.redis.test.extension.RedisStanalone; * * @author Costin Leau * @author Mark Paluch + * @author Christoph Strobl */ public class RedisCollectionFactoryBeanTests { @@ -52,15 +53,17 @@ public class RedisCollectionFactoryBeanTests { @BeforeEach void setUp() { + this.template.delete("key"); this.template.delete("nosrt"); } @AfterEach void tearDown() throws Exception { + // clean up the whole db template.execute((RedisCallback) connection -> { - connection.flushDb(); + connection.serverCommands().flushDb(); return null; }); } @@ -70,6 +73,7 @@ public class RedisCollectionFactoryBeanTests { } private RedisStore createCollection(String key, CollectionType type) { + RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean(); fb.setKey(key); fb.setTemplate(template); @@ -80,7 +84,8 @@ public class RedisCollectionFactoryBeanTests { } @Test - void testNone() throws Exception { + void testNone() { + RedisStore store = createCollection("nosrt", CollectionType.PROPERTIES); assertThat(store).isInstanceOf(RedisProperties.class); @@ -121,6 +126,7 @@ public class RedisCollectionFactoryBeanTests { @Test void testExistingCol() { + String key = "set"; String val = "value"; @@ -148,7 +154,6 @@ public class RedisCollectionFactoryBeanTests { template.opsForList().leftPush("key", "value"); assertThatIllegalArgumentException().isThrownBy(() -> createCollection("key", CollectionType.SET)) .withMessageContaining("Cannot create collection type 'SET' for a key containing 'LIST'"); - } @Test // GH-2633 @@ -157,6 +162,33 @@ public class RedisCollectionFactoryBeanTests { template.opsForStream().add("key", Map.of("k", "v")); assertThatIllegalArgumentException().isThrownBy(() -> createCollection("key", CollectionType.LIST)) .withMessageContaining("Cannot create store on keys of type 'STREAM'"); + } + @Test // Gh-2633 + void shouldFailWhenNotInitialized() { + + RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean(); + fb.setKey("key"); + fb.setTemplate(template); + fb.setType(CollectionType.SET); + + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> fb.getObject()); + } + + @Test // Gh-2633 + void usesBeanNameIfNoKeyProvided() { + + template.delete("key"); + template.opsForHash().put("key", "k", "v"); + + RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean(); + fb.setBeanName("key"); + fb.setTemplate(template); + fb.afterPropertiesSet(); + + assertThat(fb.getObject()).satisfies(value -> { + assertThat(value).isInstanceOf(RedisMap.class); + assertThat((RedisMap)value).containsEntry("k", "v"); + }); } }