From 226d6b4c87ccc69c85e0ed7acfd355761328b2bb Mon Sep 17 00:00:00 2001 From: Jennifer Hickey Date: Mon, 29 Jul 2013 16:10:12 -0700 Subject: [PATCH] Enable testing RedisTemplate with multiple serializers --- ...lateTests.java => RedisTemplateTests.java} | 171 ++++++++++++------ .../core/StringRedisTemplateTests-context.xml | 22 --- 2 files changed, 112 insertions(+), 81 deletions(-) rename src/test/java/org/springframework/data/redis/core/{StringRedisTemplateTests.java => RedisTemplateTests.java} (63%) delete mode 100644 src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml diff --git a/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java b/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java similarity index 63% rename from src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java rename to src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java index 2e014f833..374d2b611 100644 --- a/src/test/java/org/springframework/data/redis/core/StringRedisTemplateTests.java +++ b/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java @@ -17,9 +17,11 @@ package org.springframework.data.redis.core; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assume.assumeTrue; import static org.springframework.data.redis.SpinBarrier.waitFor; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; @@ -32,9 +34,12 @@ import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.data.redis.ObjectFactory; import org.springframework.data.redis.RedisTestProfileValueSource; import org.springframework.data.redis.SettingsUtils; import org.springframework.data.redis.TestCondition; @@ -43,25 +48,31 @@ import org.springframework.data.redis.connection.StringRedisConnection; import org.springframework.data.redis.connection.srp.SrpConnectionFactory; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; import org.springframework.data.redis.serializer.GenericToStringSerializer; -import org.springframework.test.annotation.IfProfileValue; -import org.springframework.test.annotation.ProfileValueSourceConfiguration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.data.redis.serializer.RedisSerializer; /** * - * Integration test of {@link StringRedisTemplate} + * Integration test of {@link RedisTemplate} * * @author Jennifer Hickey * */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class) -public class StringRedisTemplateTests { +@RunWith(Parameterized.class) +public class RedisTemplateTests { @Autowired - private StringRedisTemplate redisTemplate; + private RedisTemplate redisTemplate; + + private ObjectFactory keyFactory; + + private ObjectFactory valueFactory; + + public RedisTemplateTests(RedisTemplate redisTemplate, ObjectFactory keyFactory, + ObjectFactory valueFactory) { + this.redisTemplate = redisTemplate; + this.keyFactory = keyFactory; + this.valueFactory = valueFactory; + } @After public void tearDown() { @@ -73,37 +84,51 @@ public class StringRedisTemplateTests { }); } - @Test - @IfProfileValue(name = "redisVersion", value = "2.6") - public void testDumpAndRestoreNoTtl() { - redisTemplate.boundValueOps("testing").set("123"); - byte[] serializedValue = redisTemplate.dump("testing"); - assertNotNull(serializedValue); - redisTemplate.delete("testing"); - redisTemplate.restore("testing", serializedValue, 0, TimeUnit.SECONDS); - assertEquals("123", redisTemplate.boundValueOps("testing").get()); + @Parameters + public static Collection testParams() { + return AbstractOperationsTestParams.testParams(); } @Test - @IfProfileValue(name = "redisVersion", value = "2.6") - public void testRestoreTtl() { - redisTemplate.boundValueOps("testing").set("123"); - byte[] serializedValue = redisTemplate.dump("testing"); + public void testDumpAndRestoreNoTtl() { + assumeTrue(RedisTestProfileValueSource.matches("redisVersion", "2.6")); + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + redisTemplate.boundValueOps(key1).set(value1); + byte[] serializedValue = redisTemplate.dump(key1); assertNotNull(serializedValue); - redisTemplate.delete("testing"); - redisTemplate.restore("testing", serializedValue, 200, TimeUnit.MILLISECONDS); - assertEquals("123", redisTemplate.boundValueOps("testing").get()); + redisTemplate.delete(key1); + redisTemplate.restore(key1, serializedValue, 0, TimeUnit.SECONDS); + assertEquals(value1, redisTemplate.boundValueOps(key1).get()); + } + + @Test + public void testRestoreTtl() { + assumeTrue(RedisTestProfileValueSource.matches("redisVersion", "2.6")); + final K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + redisTemplate.boundValueOps(key1).set(value1); + byte[] serializedValue = redisTemplate.dump(key1); + assertNotNull(serializedValue); + redisTemplate.delete(key1); + redisTemplate.restore(key1, serializedValue, 200, TimeUnit.MILLISECONDS); + assertEquals(value1, redisTemplate.boundValueOps(key1).get()); waitFor(new TestCondition() { public boolean passes() { - return (!redisTemplate.hasKey("testing")); + return (!redisTemplate.hasKey(key1)); } }, 400); } + @SuppressWarnings("unchecked") @Test public void testKeys() throws Exception { - redisTemplate.opsForValue().set("foo", "bar"); - assertNotNull(redisTemplate.keys("*")); + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + assumeTrue(key1 instanceof String || key1 instanceof byte[]); + redisTemplate.opsForValue().set(key1, value1); + K keyPattern = key1 instanceof String ? (K) "*" : (K)"*".getBytes(); + assertNotNull(redisTemplate.keys(keyPattern)); } @SuppressWarnings("rawtypes") @@ -116,6 +141,7 @@ public class StringRedisTemplateTests { @Test public void testStringTemplateExecutesWithStringConn() { + assumeTrue(redisTemplate instanceof StringRedisTemplate); String value = redisTemplate.execute(new RedisCallback() { public String doInRedis(RedisConnection connection) { StringRedisConnection stringConn = (StringRedisConnection) connection; @@ -128,40 +154,52 @@ public class StringRedisTemplateTests { @Test public void testExecDeserializes() { + final K key1 = keyFactory.instance(); + final V value1 = valueFactory.instance(); + final K listKey = keyFactory.instance(); + final V listValue = valueFactory.instance(); + final K setKey = keyFactory.instance(); + final V setValue = valueFactory.instance(); + final K zsetKey = keyFactory.instance(); + final V zsetValue = valueFactory.instance(); + final K mapKey = keyFactory.instance(); + final Object hashKey = redisTemplate.getHashKeySerializer().deserialize("test".getBytes()); + final Object hashValue = redisTemplate.getHashValueSerializer().deserialize("passed".getBytes()); List results = redisTemplate.execute(new SessionCallback>() { @SuppressWarnings({ "rawtypes", "unchecked" }) public List execute(RedisOperations operations) throws DataAccessException { operations.multi(); - operations.opsForValue().set("foo", "bar"); + operations.opsForValue().set(key1, value1); // byte[] - operations.opsForValue().get("foo"); - operations.opsForList().leftPush("foolist", "something"); + operations.opsForValue().get(key1); + operations.opsForList().leftPush(listKey, listValue); // List - operations.opsForList().range("foolist", 0l, 1l); - operations.opsForSet().add("fooset", "a"); + operations.opsForList().range(listKey, 0l, 1l); + operations.opsForSet().add(setKey, setValue); // Set - operations.opsForSet().members("fooset"); - operations.opsForZSet().add("foozset", "Joe", 1d); + operations.opsForSet().members(setKey); + operations.opsForZSet().add(zsetKey, zsetValue, 1d); // Set - operations.opsForZSet().rangeWithScores("foozset", 0l, -1l); - operations.opsForHash().put("foomap", "test", "passed"); + operations.opsForZSet().rangeWithScores(zsetKey, 0l, -1l); + operations.opsForHash().put(mapKey, hashKey, hashValue); // Map - operations.opsForHash().entries("foomap"); + operations.opsForHash().entries(mapKey); return operations.exec(); } }); - List list = Collections.singletonList("something"); - Set stringSet = new HashSet(Collections.singletonList("a")); - Set> tupleSet = new LinkedHashSet>( - Collections.singletonList(new DefaultTypedTuple("Joe", 1d))); - Map map = new LinkedHashMap(); - map.put("test", "passed"); - assertEquals(Arrays.asList(new Object[] {"bar", 1l, list, true, stringSet, true, tupleSet, true, map}), + List list = Collections.singletonList(listValue); + Set set = new HashSet(Collections.singletonList(setValue)); + Set> tupleSet = new LinkedHashSet>( + Collections.singletonList(new DefaultTypedTuple(zsetValue, 1d))); + Map map = new LinkedHashMap(); + map.put(hashKey, hashValue); + assertEquals(Arrays.asList(new Object[] {value1, 1l, list, true, set, true, tupleSet, true, map}), results); } @Test public void testExecCustomSerializer() { + assumeTrue(redisTemplate instanceof StringRedisTemplate); List results = redisTemplate.execute(new SessionCallback>() { @SuppressWarnings({ "rawtypes", "unchecked" }) public List execute(RedisOperations operations) throws DataAccessException { @@ -220,23 +258,30 @@ public class StringRedisTemplateTests { @SuppressWarnings("rawtypes") @Test public void testExecutePipelined() { + final K key1 = keyFactory.instance(); + final V value1 = valueFactory.instance(); + final K listKey = keyFactory.instance(); + final V listValue = valueFactory.instance(); + final V listValue2 = valueFactory.instance(); List results = redisTemplate.executePipelined(new RedisCallback() { public Object doInRedis(RedisConnection connection) throws DataAccessException { - StringRedisConnection stringRedisConn = (StringRedisConnection) connection; - stringRedisConn.set("foo", "bar"); - stringRedisConn.get("foo"); - stringRedisConn.rPush("foolist", "a"); - stringRedisConn.rPush("foolist", "b"); - stringRedisConn.lRange("foolist", 0, -1); + byte[] rawKey = serialize(key1, redisTemplate.getKeySerializer()); + byte[] rawListKey = serialize(listKey, redisTemplate.getKeySerializer()); + connection.set(rawKey, serialize(value1, redisTemplate.getValueSerializer())); + connection.get(rawKey); + connection.rPush(rawListKey, serialize(listValue, redisTemplate.getValueSerializer())); + connection.rPush(rawListKey, serialize(listValue2, redisTemplate.getValueSerializer())); + connection.lRange(rawListKey, 0, -1); return null; } }); - assertEquals(Arrays.asList(new Object[] {"bar", 1l, 2l, Arrays.asList(new String[] {"a", "b"})}), results); + assertEquals(Arrays.asList(new Object[] {value1, 1l, 2l, Arrays.asList(new Object[] {listValue, listValue2})}), results); } @SuppressWarnings("rawtypes") @Test public void testExecutePipelinedCustomSerializer() { + assumeTrue(redisTemplate instanceof StringRedisTemplate); List results = redisTemplate.executePipelined(new RedisCallback() { public Object doInRedis(RedisConnection connection) throws DataAccessException { StringRedisConnection stringRedisConn = (StringRedisConnection) connection; @@ -263,25 +308,28 @@ public class StringRedisTemplateTests { @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testExecutePipelinedTx() { + final K key1 = keyFactory.instance(); + final V value1 = valueFactory.instance(); List pipelinedResults = redisTemplate.executePipelined(new SessionCallback() { public Object execute(RedisOperations operations) throws DataAccessException { operations.multi(); - operations.opsForList().leftPush("foo", "bar"); - operations.opsForList().rightPop("foo"); - operations.opsForList().size("foo"); + operations.opsForList().leftPush(key1, value1); + operations.opsForList().rightPop(key1); + operations.opsForList().size(key1); operations.exec(); - operations.opsForValue().set("foo", "bar"); - operations.opsForValue().get("foo"); + operations.opsForValue().set(key1, value1); + operations.opsForValue().get(key1); return null; } }); // Should contain the List of deserialized exec results and the result of the last call to get() - assertEquals(Arrays.asList(new Object[] {Arrays.asList(new Object[] {1l, "bar", 0l}), "bar"}), pipelinedResults); + assertEquals(Arrays.asList(new Object[] {Arrays.asList(new Object[] {1l, value1, 0l}), value1}), pipelinedResults); } @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testExecutePipelinedTxCustomSerializer() { + assumeTrue(redisTemplate instanceof StringRedisTemplate); List pipelinedResults = redisTemplate.executePipelined(new SessionCallback() { public Object execute(RedisOperations operations) throws DataAccessException { operations.multi(); @@ -307,4 +355,9 @@ public class StringRedisTemplateTests { } }); } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private byte[] serialize(Object value, RedisSerializer serializer) { + return serializer.serialize(value); + } } diff --git a/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml b/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml deleted file mode 100644 index 4114b87f0..000000000 --- a/src/test/resources/org/springframework/data/redis/core/StringRedisTemplateTests-context.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file