1919import static org .junit .Assume .*;
2020
2121import java .util .Collection ;
22- import java .util .List ;
2322import java .util .Map ;
24- import java .util .Set ;
2523import java .util .concurrent .TimeUnit ;
2624
2725import org .junit .After ;
2826import org .junit .AfterClass ;
27+ import org .junit .Before ;
2928import org .junit .Test ;
3029import org .junit .runner .RunWith ;
3130import org .junit .runners .Parameterized ;
3231import org .junit .runners .Parameterized .Parameters ;
32+ import org .springframework .dao .DataAccessException ;
3333import org .springframework .data .redis .ConnectionFactoryTracker ;
3434import org .springframework .data .redis .ObjectFactory ;
3535import org .springframework .data .redis .connection .ConnectionUtils ;
36+ import org .springframework .data .redis .connection .RedisConnection ;
3637import org .springframework .data .redis .core .BoundKeyOperations ;
38+ import org .springframework .data .redis .core .RedisCallback ;
3739import org .springframework .data .redis .core .RedisTemplate ;
3840import org .springframework .data .redis .support .atomic .RedisAtomicInteger ;
3941import org .springframework .data .redis .support .atomic .RedisAtomicLong ;
4244 * @author Costin Leau
4345 * @author Jennifer Hickey
4446 * @author Thomas Darimont
47+ * @author Christoph Strobl
4548 */
4649@ RunWith (Parameterized .class )
4750public class BoundKeyOperationsTest {
51+
52+ @ SuppressWarnings ("rawtypes" )//
4853private BoundKeyOperations keyOps ;
54+
4955private ObjectFactory <Object > objFactory ;
56+
57+ @ SuppressWarnings ("rawtypes" )//
5058private RedisTemplate template ;
5159
60+ @ SuppressWarnings ("rawtypes" )
5261public BoundKeyOperationsTest (BoundKeyOperations <Object > keyOps , ObjectFactory <Object > objFactory ,
5362RedisTemplate template ) {
5463this .objFactory = objFactory ;
@@ -57,8 +66,23 @@ public BoundKeyOperationsTest(BoundKeyOperations<Object> keyOps, ObjectFactory<O
5766ConnectionFactoryTracker .add (template .getConnectionFactory ());
5867}
5968
69+ @ Before
70+ public void setUp () {
71+ populateBoundKey ();
72+ }
73+
74+ @ SuppressWarnings ("unchecked" )
6075@ After
61- public void stop () {}
76+ public void tearDown () {
77+ template .execute (new RedisCallback <Object >() {
78+
79+ @ Override
80+ public Object doInRedis (RedisConnection connection ) throws DataAccessException {
81+ connection .flushDb ();
82+ return null ;
83+ }
84+ });
85+ }
6286
6387@ AfterClass
6488public static void cleanUp () {
@@ -70,23 +94,16 @@ public static Collection<Object[]> testParams() {
7094return BoundKeyParams .testParams ();
7195}
7296
97+ @ SuppressWarnings ("unchecked" )
7398@ Test
7499public void testRename () throws Exception {
75- // DATAREDIS-188 Infinite loop renaming a non-existent Collection when using Lettuce
76- assumeTrue (!ConnectionUtils .isLettuce (template .getConnectionFactory ()));
100+
77101Object key = keyOps .getKey ();
78- assertNotNull (key );
79- // RedisAtomicInteger/Long need to be reset, as they may be created
80- // at start of test run and underlying key wiped out by other tests
81- try {
82- keyOps .getClass ().getMethod ("set" , int .class ).invoke (keyOps , 0 );
83- } catch (NoSuchMethodException e ) {}
84- try {
85- keyOps .getClass ().getMethod ("set" , long .class ).invoke (keyOps , 0l );
86- } catch (NoSuchMethodException e ) {}
87102Object newName = objFactory .instance ();
103+
88104keyOps .rename (newName );
89105assertEquals (newName , keyOps .getKey ());
106+
90107keyOps .rename (key );
91108assertEquals (key , keyOps .getKey ());
92109}
@@ -97,9 +114,8 @@ public void testRename() throws Exception {
97114@ Test
98115public void testExpire () throws Exception {
99116
100- populateBoundKey ();
101-
102117assertEquals (keyOps .getClass ().getName () + " -> " + keyOps .getKey (), Long .valueOf (-1 ), keyOps .getExpire ());
118+
103119if (keyOps .expire (10 , TimeUnit .SECONDS )) {
104120long expire = keyOps .getExpire ().longValue ();
105121assertTrue (expire <= 10 && expire > 5 );
@@ -113,22 +129,20 @@ public void testExpire() throws Exception {
113129public void testPersist () throws Exception {
114130assumeTrue (!ConnectionUtils .isJredis (template .getConnectionFactory ()));
115131
116- populateBoundKey ();
117-
118132keyOps .persist ();
119133
120134assertEquals (keyOps .getClass ().getName () + " -> " + keyOps .getKey (), Long .valueOf (-1 ), keyOps .getExpire ());
121135if (keyOps .expire (10 , TimeUnit .SECONDS )) {
122136assertTrue (keyOps .getExpire ().longValue () > 0 );
123137}
138+
124139keyOps .persist ();
125140assertEquals (keyOps .getClass ().getName () + " -> " + keyOps .getKey (), -1 , keyOps .getExpire ().longValue ());
126141}
127142
128143@ SuppressWarnings ({ "unchecked" , "rawtypes" })
129144private void populateBoundKey () {
130-
131- if (keyOps instanceof List || keyOps instanceof Set ) {
145+ if (keyOps instanceof Collection ) {
132146((Collection ) keyOps ).add ("dummy" );
133147} else if (keyOps instanceof Map ) {
134148((Map ) keyOps ).put ("dummy" , "dummy" );
0 commit comments