36
36
*
37
37
* @author Costin Leau
38
38
* @author Mark Paluch
39
+ * @author Christoph Strobl
39
40
*/
40
41
public class RedisCollectionFactoryBeanTests {
41
42
@@ -52,15 +53,17 @@ public class RedisCollectionFactoryBeanTests {
52
53
53
54
@ BeforeEach
54
55
void setUp () {
56
+
55
57
this .template .delete ("key" );
56
58
this .template .delete ("nosrt" );
57
59
}
58
60
59
61
@ AfterEach
60
62
void tearDown () throws Exception {
63
+
61
64
// clean up the whole db
62
65
template .execute ((RedisCallback <Object >) connection -> {
63
- connection .flushDb ();
66
+ connection .serverCommands (). flushDb ();
64
67
return null ;
65
68
});
66
69
}
@@ -70,6 +73,7 @@ private RedisStore createCollection(String key) {
70
73
}
71
74
72
75
private RedisStore createCollection (String key , CollectionType type ) {
76
+
73
77
RedisCollectionFactoryBean fb = new RedisCollectionFactoryBean ();
74
78
fb .setKey (key );
75
79
fb .setTemplate (template );
@@ -80,7 +84,8 @@ private RedisStore createCollection(String key, CollectionType type) {
80
84
}
81
85
82
86
@ Test
83
- void testNone () throws Exception {
87
+ void testNone () {
88
+
84
89
RedisStore store = createCollection ("nosrt" , CollectionType .PROPERTIES );
85
90
assertThat (store ).isInstanceOf (RedisProperties .class );
86
91
@@ -121,6 +126,7 @@ void testExisting() {
121
126
122
127
@ Test
123
128
void testExistingCol () {
129
+
124
130
String key = "set" ;
125
131
String val = "value" ;
126
132
@@ -148,7 +154,6 @@ void testIncompatibleCollections() {
148
154
template .opsForList ().leftPush ("key" , "value" );
149
155
assertThatIllegalArgumentException ().isThrownBy (() -> createCollection ("key" , CollectionType .SET ))
150
156
.withMessageContaining ("Cannot create collection type 'SET' for a key containing 'LIST'" );
151
-
152
157
}
153
158
154
159
@ Test // GH-2633
@@ -157,6 +162,33 @@ void shouldFailForStreamCreation() {
157
162
template .opsForStream ().add ("key" , Map .of ("k" , "v" ));
158
163
assertThatIllegalArgumentException ().isThrownBy (() -> createCollection ("key" , CollectionType .LIST ))
159
164
.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 );
160
174
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
+ });
161
193
}
162
194
}
0 commit comments