Polishing.

Add tests that assert initialization constraints, use early return to default value in case of null and align code format.

Original Pull Request: #2637
This commit is contained in:
Christoph Strobl
2023-07-12 13:51:41 +02:00
parent 5d885e24a3
commit 7add798886
3 changed files with 48 additions and 8 deletions

View File

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

View File

@@ -45,6 +45,7 @@ public class RedisCollectionFactoryBean implements SmartFactoryBean<RedisStore>,
*
* @author Costin Leau
* @author Mark Paluch
* @author Christoph Strobl
*/
public enum CollectionType {
LIST {
@@ -95,6 +96,10 @@ public class RedisCollectionFactoryBean implements SmartFactoryBean<RedisStore>,
*/
static CollectionType findCollectionType(@Nullable DataType dataType, Supplier<CollectionType> 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<RedisStore>,
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);

View File

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