Skip to content

Commit 7add798

Browse files
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
1 parent 5d885e2 commit 7add798

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/main/java/org/springframework/data/redis/connection/DataType.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
* @author Costin Leau
2626
* @author Mark Paluch
27+
* @author Christoph Strobl
2728
*/
2829
public enum DataType {
2930

@@ -36,9 +37,9 @@ public enum DataType {
3637
private static final Map<String, DataType> codeLookup = new ConcurrentHashMap<>(7);
3738

3839
static {
39-
for (DataType type : EnumSet.allOf(DataType.class))
40+
for (DataType type : EnumSet.allOf(DataType.class)) {
4041
codeLookup.put(type.code, type);
41-
42+
}
4243
}
4344

4445
private final String code;
@@ -63,9 +64,11 @@ public String code() {
6364
* @return actual enum corresponding to the given code
6465
*/
6566
public static DataType fromCode(String code) {
67+
6668
DataType data = codeLookup.get(code);
67-
if (data == null)
68-
throw new IllegalArgumentException("unknown data type code");
69+
if (data == null) {
70+
throw new IllegalArgumentException("unknown data type code %s".formatted(code));
71+
}
6972
return data;
7073
}
7174
}

src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class RedisCollectionFactoryBean implements SmartFactoryBean<RedisStore>,
4545
*
4646
* @author Costin Leau
4747
* @author Mark Paluch
48+
* @author Christoph Strobl
4849
*/
4950
public enum CollectionType {
5051
LIST {
@@ -95,6 +96,10 @@ public DataType dataType() {
9596
*/
9697
static CollectionType findCollectionType(@Nullable DataType dataType, Supplier<CollectionType> ifNotFound) {
9798

99+
if (dataType == null) {
100+
return ifNotFound.get();
101+
}
102+
98103
for (CollectionType collectionType : values()) {
99104
if (collectionType.dataType() == dataType) {
100105
return collectionType;
@@ -135,7 +140,7 @@ public void afterPropertiesSet() {
135140

136141
if (keyType != null && DataType.NONE != keyType && this.type.dataType() != keyType) {
137142
throw new IllegalArgumentException(
138-
String.format("Cannot create collection type '%s' for a key containing '%s'", this.type, keyType));
143+
"Cannot create collection type '%s' for a key containing '%s'".formatted(this.type, keyType));
139144
}
140145

141146
return createStore(this.type, key, template);

src/test/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBeanTests.java

+35-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Costin Leau
3838
* @author Mark Paluch
39+
* @author Christoph Strobl
3940
*/
4041
public class RedisCollectionFactoryBeanTests {
4142

@@ -52,15 +53,17 @@ public class RedisCollectionFactoryBeanTests {
5253

5354
@BeforeEach
5455
void setUp() {
56+
5557
this.template.delete("key");
5658
this.template.delete("nosrt");
5759
}
5860

5961
@AfterEach
6062
void tearDown() throws Exception {
63+
6164
// clean up the whole db
6265
template.execute((RedisCallback<Object>) connection -> {
63-
connection.flushDb();
66+
connection.serverCommands().flushDb();
6467
return null;
6568
});
6669
}
@@ -70,6 +73,7 @@ private RedisStore createCollection(String key) {
7073
}
7174

7275
private RedisStore createCollection(String key, CollectionType type) {
76+
7377
RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean();
7478
fb.setKey(key);
7579
fb.setTemplate(template);
@@ -80,7 +84,8 @@ private RedisStore createCollection(String key, CollectionType type) {
8084
}
8185

8286
@Test
83-
void testNone() throws Exception {
87+
void testNone() {
88+
8489
RedisStore store = createCollection("nosrt", CollectionType.PROPERTIES);
8590
assertThat(store).isInstanceOf(RedisProperties.class);
8691

@@ -121,6 +126,7 @@ void testExisting() {
121126

122127
@Test
123128
void testExistingCol() {
129+
124130
String key = "set";
125131
String val = "value";
126132

@@ -148,7 +154,6 @@ void testIncompatibleCollections() {
148154
template.opsForList().leftPush("key", "value");
149155
assertThatIllegalArgumentException().isThrownBy(() -> createCollection("key", CollectionType.SET))
150156
.withMessageContaining("Cannot create collection type 'SET' for a key containing 'LIST'");
151-
152157
}
153158

154159
@Test // GH-2633
@@ -157,6 +162,33 @@ void shouldFailForStreamCreation() {
157162
template.opsForStream().add("key", Map.of("k", "v"));
158163
assertThatIllegalArgumentException().isThrownBy(() -> createCollection("key", CollectionType.LIST))
159164
.withMessageContaining("Cannot create store on keys of type 'STREAM'");
165+
}
166+
167+
@Test // Gh-2633
168+
void shouldFailWhenNotInitialized() {
169+
170+
RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean();
171+
fb.setKey("key");
172+
fb.setTemplate(template);
173+
fb.setType(CollectionType.SET);
160174

175+
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> fb.getObject());
176+
}
177+
178+
@Test // Gh-2633
179+
void usesBeanNameIfNoKeyProvided() {
180+
181+
template.delete("key");
182+
template.opsForHash().put("key", "k", "v");
183+
184+
RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean();
185+
fb.setBeanName("key");
186+
fb.setTemplate(template);
187+
fb.afterPropertiesSet();
188+
189+
assertThat(fb.getObject()).satisfies(value -> {
190+
assertThat(value).isInstanceOf(RedisMap.class);
191+
assertThat((RedisMap)value).containsEntry("k", "v");
192+
});
161193
}
162194
}

0 commit comments

Comments
 (0)