From 2870df0cdce41af80c41441064b1810084a6cf90 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Wed, 15 Jan 2014 19:36:22 +0100 Subject: [PATCH] DATAREDIS-251 - Fix tests to work against Redis 2.8.x. Since Redis 2.8.x keyOps#getExpire() returns -2 for empty collections (RedisList/RedisSet/RedisMap). We're now adding a dummy entry to keep the test version agnostic. Previously getExpire() returned -1. We also have to set the RedisAtomic(Integer|Long) to a value other than 0 to get the value stored appropriately. For reference see the discussion in http://redis.io/commands/ttl --- .../redis/support/BoundKeyOperationsTest.java | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java b/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java index 2e7ce65f6..af8bb4a8f 100644 --- a/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java +++ b/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java @@ -15,6 +15,15 @@ */ package org.springframework.data.redis.support; +import static org.junit.Assert.*; +import static org.junit.Assume.*; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + import org.junit.After; import org.junit.AfterClass; import org.junit.Test; @@ -26,15 +35,8 @@ import org.springframework.data.redis.ObjectFactory; import org.springframework.data.redis.connection.ConnectionUtils; import org.springframework.data.redis.core.BoundKeyOperations; import org.springframework.data.redis.core.RedisTemplate; - -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.*; -import static org.junit.Assume.assumeTrue; +import org.springframework.data.redis.support.atomic.RedisAtomicInteger; +import org.springframework.data.redis.support.atomic.RedisAtomicLong; /** * @author Costin Leau @@ -43,7 +45,7 @@ import static org.junit.Assume.assumeTrue; */ @RunWith(Parameterized.class) public class BoundKeyOperationsTest { - private BoundKeyOperations keyOps; + private BoundKeyOperations keyOps; private ObjectFactory objFactory; private RedisTemplate template; @@ -56,8 +58,7 @@ public class BoundKeyOperationsTest { } @After - public void stop() { - } + public void stop() {} @AfterClass public static void cleanUp() { @@ -79,12 +80,10 @@ public class BoundKeyOperationsTest { // at start of test run and underlying key wiped out by other tests try { keyOps.getClass().getMethod("set", int.class).invoke(keyOps, 0); - }catch(NoSuchMethodException e) { - } + } catch (NoSuchMethodException e) {} try { keyOps.getClass().getMethod("set", long.class).invoke(keyOps, 0l); - }catch(NoSuchMethodException e) { - } + } catch (NoSuchMethodException e) {} Object newName = objFactory.instance(); keyOps.rename(newName); assertEquals(newName, keyOps.getKey()); @@ -92,44 +91,51 @@ public class BoundKeyOperationsTest { assertEquals(key, keyOps.getKey()); } - /** - * @see DATAREDIS-251 - */ + /** + * @see DATAREDIS-251 + */ @Test public void testExpire() throws Exception { - if(keyOps instanceof List || keyOps instanceof Set){ - ((Collection) keyOps).add("dummy"); - }else if(keyOps instanceof Map){ - ((Map)keyOps).put("dummy","dummy"); - } + populateBoundKey(); - assertEquals(Long.valueOf(-1), keyOps.getExpire()); + assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), Long.valueOf(-1), keyOps.getExpire()); if (keyOps.expire(10, TimeUnit.SECONDS)) { long expire = keyOps.getExpire().longValue(); assertTrue(expire <= 10 && expire > 5); } } - /** - * @see DATAREDIS-251 - */ + /** + * @see DATAREDIS-251 + */ @Test public void testPersist() throws Exception { assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory())); - if(keyOps instanceof List || keyOps instanceof Set){ - ((Collection) keyOps).add("dummy"); - }else if(keyOps instanceof Map){ - ((Map)keyOps).put("dummy","dummy"); - } + populateBoundKey(); keyOps.persist(); - assertEquals(Long.valueOf(-1), keyOps.getExpire()); + + assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), Long.valueOf(-1), keyOps.getExpire()); if (keyOps.expire(10, TimeUnit.SECONDS)) { assertTrue(keyOps.getExpire().longValue() > 0); } keyOps.persist(); - assertEquals(-1, keyOps.getExpire().longValue()); + assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), -1, keyOps.getExpire().longValue()); } -} \ No newline at end of file + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void populateBoundKey() { + + if (keyOps instanceof List || keyOps instanceof Set) { + ((Collection) keyOps).add("dummy"); + } else if (keyOps instanceof Map) { + ((Map) keyOps).put("dummy", "dummy"); + } else if (keyOps instanceof RedisAtomicInteger) { + ((RedisAtomicInteger) keyOps).set(42); + } else if (keyOps instanceof RedisAtomicLong) { + ((RedisAtomicLong) keyOps).set(42L); + } + } +}