diff --git a/src/test/java/org/springframework/data/redis/core/DefaultHashOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultHashOperationsTests.java new file mode 100644 index 000000000..aa14034cd --- /dev/null +++ b/src/test/java/org/springframework/data/redis/core/DefaultHashOperationsTests.java @@ -0,0 +1,107 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.core; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.data.redis.ObjectFactory; +import org.springframework.data.redis.SettingsUtils; +import org.springframework.data.redis.StringObjectFactory; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.srp.SrpConnectionFactory; +import static org.junit.Assert.assertEquals; + +/** + * Integration test of {@link DefaultHashOperations} + * + * @author Jennifer Hickey + * + * @param Key type + * @param Hash key type + * @param Hash value type + */ +@RunWith(Parameterized.class) +public class DefaultHashOperationsTests { + private RedisTemplate redisTemplate; + + private ObjectFactory keyFactory; + + private ObjectFactory hashKeyFactory; + + private ObjectFactory hashValueFactory; + + private HashOperations hashOps; + + public DefaultHashOperationsTests(RedisTemplate redisTemplate, ObjectFactory keyFactory, + ObjectFactory hashKeyFactory, ObjectFactory hashValueFactory) { + this.redisTemplate = redisTemplate; + this.keyFactory = keyFactory; + this.hashKeyFactory = hashKeyFactory; + this.hashValueFactory = hashValueFactory; + } + + @Parameters + public static Collection testParams() { + ObjectFactory stringFactory = new StringObjectFactory(); + SrpConnectionFactory srConnFactory = new SrpConnectionFactory(); + srConnFactory.setPort(SettingsUtils.getPort()); + srConnFactory.setHostName(SettingsUtils.getHost()); + srConnFactory.afterPropertiesSet(); + RedisTemplate stringTemplate = new StringRedisTemplate(); + stringTemplate.setConnectionFactory(srConnFactory); + stringTemplate.afterPropertiesSet(); + return Arrays.asList(new Object[][] { { stringTemplate, stringFactory, stringFactory, stringFactory }}); + } + + @Before + public void setUp() { + hashOps = redisTemplate.opsForHash(); + } + + @After + public void tearDown() { + redisTemplate.execute(new RedisCallback() { + public Object doInRedis(RedisConnection connection) { + connection.flushDb(); + return null; + } + }); + } + + @Test + public void testEntries() { + K key = keyFactory.instance(); + HK key1 = hashKeyFactory.instance(); + HV val1 = hashValueFactory.instance(); + HK key2 = hashKeyFactory.instance(); + HV val2 = hashValueFactory.instance(); + hashOps.put(key, key1, val1); + hashOps.put(key, key2, val2); + Map expected = new HashMap(); + expected.put(key1, val1); + expected.put(key2, val2); + assertEquals(expected, hashOps.entries(key)); + } +} diff --git a/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java new file mode 100644 index 000000000..a2ac93bde --- /dev/null +++ b/src/test/java/org/springframework/data/redis/core/DefaultListOperationsTests.java @@ -0,0 +1,148 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assume.assumeTrue; + +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.data.redis.ObjectFactory; +import org.springframework.data.redis.RedisTestProfileValueSource; +import org.springframework.data.redis.connection.RedisConnection; + +/** + * Integration test of {@link DefaultListOperations} + * @author Jennifer Hickey + * + * @param Key test + * @param Value test + */ +@RunWith(Parameterized.class) +public class DefaultListOperationsTests { + + private RedisTemplate redisTemplate; + + private ObjectFactory keyFactory; + + private ObjectFactory valueFactory; + + private ListOperations listOps; + + public DefaultListOperationsTests(RedisTemplate redisTemplate, ObjectFactory keyFactory, + ObjectFactory valueFactory) { + this.redisTemplate = redisTemplate; + this.keyFactory = keyFactory; + this.valueFactory = valueFactory; + } + + @Parameters + public static Collection testParams() { + return AbstractOperationsTestParams.testParams(); + } + + @Before + public void setUp() { + listOps = redisTemplate.opsForList(); + } + + @After + public void tearDown() { + redisTemplate.execute(new RedisCallback() { + public Object doInRedis(RedisConnection connection) { + connection.flushDb(); + return null; + } + }); + } + + @Test + public void testLeftPushWithPivot() { + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + V v3 = valueFactory.instance(); + assertEquals(Long.valueOf(1),listOps.leftPush(key, v1)); + assertEquals(Long.valueOf(2),listOps.leftPush(key, v2)); + assertEquals(Long.valueOf(3), listOps.leftPush(key, v1, v3)); + assertEquals(Arrays.asList(new Object[] {v2, v3, v1}),listOps.range(key, 0, -1)); + } + + @Test + public void testLeftPushIfPresent() { + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + assertEquals(Long.valueOf(0), listOps.leftPushIfPresent(key, v1)); + assertEquals(Long.valueOf(1),listOps.leftPush(key, v1)); + assertEquals(Long.valueOf(2),listOps.leftPushIfPresent(key, v2)); + assertEquals(Arrays.asList(new Object[] {v2, v1}),listOps.range(key, 0, -1)); + } + + @Test + public void testRightPopAndLeftPushTimeout() { + // 1 ms timeout gets upgraded to 1 sec timeout at the moment + assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true")); + K key = keyFactory.instance(); + K key2 = keyFactory.instance(); + V v1 = valueFactory.instance(); + assertNull(listOps.rightPopAndLeftPush(key, key2, 1, TimeUnit.MILLISECONDS)); + listOps.leftPush(key, v1); + assertEquals(v1, listOps.rightPopAndLeftPush(key, key2, 1, TimeUnit.MILLISECONDS)); + } + + @Test + public void testRightPopAndLeftPush() { + K key = keyFactory.instance(); + K key2 = keyFactory.instance(); + V v1 = valueFactory.instance(); + assertNull(listOps.rightPopAndLeftPush(key, key2)); + listOps.leftPush(key, v1); + assertEquals(v1, listOps.rightPopAndLeftPush(key, key2)); + } + + @Test + public void testRightPushWithPivot() { + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + V v3 = valueFactory.instance(); + assertEquals(Long.valueOf(1),listOps.rightPush(key, v1)); + assertEquals(Long.valueOf(2),listOps.rightPush(key, v2)); + assertEquals(Long.valueOf(3), listOps.rightPush(key, v1, v3)); + assertEquals(Arrays.asList(new Object[] {v1, v3, v2}),listOps.range(key, 0, -1)); + } + + @Test + public void testRightPushIfPresent() { + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + assertEquals(Long.valueOf(0), listOps.rightPushIfPresent(key, v1)); + assertEquals(Long.valueOf(1),listOps.rightPush(key, v1)); + assertEquals(Long.valueOf(2),listOps.rightPushIfPresent(key, v2)); + assertEquals(Arrays.asList(new Object[] {v1, v2}),listOps.range(key, 0, -1)); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java index 429aae971..04f1f21d3 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultSetOperationsTests.java @@ -22,6 +22,8 @@ import static org.junit.Assume.assumeTrue; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -123,4 +125,41 @@ public class DefaultSetOperationsTests { }catch(IllegalArgumentException e) { } } + + @Test + public void testMove() { + K key1 = keyFactory.instance(); + K key2 = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + setOps.add(key1, v1); + setOps.add(key1, v2); + setOps.move(key1, v1, key2); + assertEquals(new HashSet(Collections.singletonList(v2)), + setOps.members(key1)); + assertEquals(new HashSet(Collections.singletonList(v1)), + setOps.members(key2)); + } + + @Test + public void testPop() { + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + setOps.add(key, v1); + assertEquals(v1, setOps.pop(key)); + assertTrue(setOps.members(key).isEmpty()); + } + + @Test + public void testRandomMember() { + K key = keyFactory.instance(); + V v1 = valueFactory.instance(); + V v2 = valueFactory.instance(); + setOps.add(key, v1); + setOps.add(key, v2); + HashSet expected = new HashSet(); + expected.add(v1); + expected.add(v2); + assertTrue(expected.contains(setOps.randomMember(key))); + } } diff --git a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java index aa90bc1aa..cb7d66ad4 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java @@ -19,13 +19,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; +import static org.springframework.data.redis.SpinBarrier.waitFor; import java.text.DecimalFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; @@ -39,6 +42,7 @@ import org.springframework.data.redis.ObjectFactory; import org.springframework.data.redis.RedisTestProfileValueSource; import org.springframework.data.redis.SettingsUtils; import org.springframework.data.redis.StringObjectFactory; +import org.springframework.data.redis.TestCondition; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.srp.SrpConnectionFactory; import org.springframework.data.redis.serializer.GenericToStringSerializer; @@ -166,4 +170,113 @@ public class DefaultValueOperationsTests { keysAndValues.put(key2, value3); assertFalse(valueOps.multiSetIfAbsent(keysAndValues)); } + + @Test + public void testMultiSet() { + Map keysAndValues = new HashMap(); + K key1 = keyFactory.instance(); + K key2 = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + keysAndValues.put(key1, value1); + keysAndValues.put(key2, value2); + valueOps.multiSet(keysAndValues); + assertEquals(new ArrayList(keysAndValues.values()), valueOps.multiGet(keysAndValues.keySet())); + } + + @Test + public void testGetSet() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + valueOps.set(key1,value1); + assertEquals(value1, valueOps.get(key1)); + } + + @Test + public void testGetAndSet() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + valueOps.set(key1, value1); + assertEquals(value1, valueOps.getAndSet(key1, value2)); + } + + @Test + public void testSetWithExpiration() { + // 1 ms timeout gets upgraded to 1 sec timeout at the moment + assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true")); + final K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + valueOps.set(key1, value1, 1, TimeUnit.MILLISECONDS); + waitFor(new TestCondition() { + public boolean passes() { + return (!redisTemplate.hasKey(key1)); + } + }, 1000); + } + + @Test + public void testAppend() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + assumeTrue(value1 instanceof String); + valueOps.set(key1, value1); + assertEquals(Integer.valueOf(((String)value1).length() + 3), valueOps.append(key1, "aaa")); + assertEquals((String)value1 + "aaa",valueOps.get(key1)); + } + + @Test + public void testGetRange() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + assumeTrue(value1 instanceof String); + valueOps.set(key1, value1); + assertEquals(2,valueOps.get(key1, 0, 1).length()); + } + + @Test + public void testSetRange() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + assumeTrue(value1 instanceof String); + valueOps.set(key1, value1); + valueOps.set(key1, value2, 0); + assertEquals(value2, valueOps.get(key1)); + } + + @Test + public void testSetIfAbsent() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + assertTrue(valueOps.setIfAbsent(key1, value1)); + assertFalse(valueOps.setIfAbsent(key1, value2)); + } + + @Test + public void testSize() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + valueOps.set(key1, value1); + assertTrue(valueOps.size(key1) > 0); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testRawKeys() { + K key1 = keyFactory.instance(); + K key2 = keyFactory.instance(); + byte[][] rawKeys = ((DefaultValueOperations)valueOps).rawKeys(key1, key2); + assertEquals(2, rawKeys.length); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void testRawKeysCollection() { + K key1 = keyFactory.instance(); + K key2 = keyFactory.instance(); + byte[][] rawKeys = ((DefaultValueOperations)valueOps).rawKeys(Arrays.asList(new Object[] {key1, key2})); + assertEquals(2, rawKeys.length); + } } diff --git a/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java new file mode 100644 index 000000000..efc0e9170 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/core/DefaultZSetOperationsTests.java @@ -0,0 +1,157 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.core; + +import static org.junit.Assert.assertEquals; + +import java.util.Collection; +import java.util.Collections; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.data.redis.ObjectFactory; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.core.ZSetOperations.TypedTuple; + +/** + * Integration test of {@link DefaultZSetOperations} + * @author Jennifer Hickey + * + * @param Key type + * @param Value type + */ +@RunWith(Parameterized.class) +public class DefaultZSetOperationsTests { + + private RedisTemplate redisTemplate; + + private ObjectFactory keyFactory; + + private ObjectFactory valueFactory; + + private ZSetOperations zSetOps; + + public DefaultZSetOperationsTests(RedisTemplate redisTemplate, ObjectFactory keyFactory, + ObjectFactory valueFactory) { + this.redisTemplate = redisTemplate; + this.keyFactory = keyFactory; + this.valueFactory = valueFactory; + } + + @Parameters + public static Collection testParams() { + return AbstractOperationsTestParams.testParams(); + } + + @Before + public void setUp() { + zSetOps = redisTemplate.opsForZSet(); + } + + @After + public void tearDown() { + redisTemplate.execute(new RedisCallback() { + public Object doInRedis(RedisConnection connection) { + connection.flushDb(); + return null; + } + }); + } + + @Test + public void testCount() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + zSetOps.add(key1, value1, 2.5); + zSetOps.add(key1, value2, 5.5); + assertEquals(Long.valueOf(1), zSetOps.count(key1, 2.7, 5.7)); + } + + @Test + public void testIncrementScore() { + K key1 = keyFactory.instance(); + V value1 = valueFactory.instance(); + zSetOps.add(key1, value1, 2.5); + assertEquals(Double.valueOf(5.7), zSetOps.incrementScore(key1, value1, 3.2)); + Set> values = zSetOps.rangeWithScores(key1, 0, -1); + assertEquals(1,values.size()); + TypedTuple tuple = values.iterator().next(); + assertEquals(value1, tuple.getValue()); + assertEquals(Double.valueOf(5.7), tuple.getScore()); + } + + @Test + public void testRangeByScoreOffsetCount() { + K key = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + V value3 = valueFactory.instance(); + zSetOps.add(key, value1, 1.9); + zSetOps.add(key, value2, 3.7); + zSetOps.add(key, value3, 5.8); + assertEquals(Collections.singleton(value1), zSetOps.rangeByScore(key, 1.5, 4.7, 0, 1)); + } + + @Test + public void testRangeByScoreWithScoresOffsetCount() { + K key = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + V value3 = valueFactory.instance(); + zSetOps.add(key, value1, 1.9); + zSetOps.add(key, value2, 3.7); + zSetOps.add(key, value3, 5.8); + Set> tuples = zSetOps.rangeByScoreWithScores(key, 1.5, 4.7, 0, 1); + assertEquals(1, tuples.size()); + TypedTuple tuple = tuples.iterator().next(); + assertEquals(value1,tuple.getValue()); + assertEquals(Double.valueOf(1.9), tuple.getScore()); + } + + @Test + public void testReverseRangeByScoreOffsetCount() { + K key = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + V value3 = valueFactory.instance(); + zSetOps.add(key, value1, 1.9); + zSetOps.add(key, value2, 3.7); + zSetOps.add(key, value3, 5.8); + assertEquals(Collections.singleton(value2), zSetOps.reverseRangeByScore(key, 1.5, 4.7, 0, 1)); + } + + @Test + public void testReverseRangeByScoreWithScoresOffsetCount() { + K key = keyFactory.instance(); + V value1 = valueFactory.instance(); + V value2 = valueFactory.instance(); + V value3 = valueFactory.instance(); + zSetOps.add(key, value1, 1.9); + zSetOps.add(key, value2, 3.7); + zSetOps.add(key, value3, 5.8); + Set> tuples = zSetOps.reverseRangeByScoreWithScores(key, 1.5, 4.7, 0, 1); + assertEquals(1, tuples.size()); + TypedTuple tuple = tuples.iterator().next(); + assertEquals(value2,tuple.getValue()); + assertEquals(Double.valueOf(3.7), tuple.getScore()); + } +} diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java index 037b4ac3d..d02addfc7 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java @@ -17,16 +17,19 @@ package org.springframework.data.redis.support.collections; import static org.junit.Assert.*; import static org.junit.matchers.JUnitMatchers.*; +import static org.junit.Assume.assumeTrue; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; +import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; import org.springframework.data.redis.ObjectFactory; +import org.springframework.data.redis.RedisTestProfileValueSource; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.support.collections.DefaultRedisList; import org.springframework.data.redis.support.collections.RedisList; @@ -35,6 +38,7 @@ import org.springframework.data.redis.support.collections.RedisList; * Integration test for RedisList * * @author Costin Leau + * @author Jennifer Hickey */ public abstract class AbstractRedisListTests extends AbstractRedisCollectionTests { @@ -46,6 +50,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT * @param factory * @param template */ + @SuppressWarnings("rawtypes") public AbstractRedisListTests(ObjectFactory factory, RedisTemplate template) { super(factory, template); } @@ -98,6 +103,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT list.add(1, t3); } + @SuppressWarnings("unchecked") @Test public void addAllIndexCollectionHead() { T t1 = getT(); @@ -117,6 +123,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertEquals(t4, list.get(1)); } + @SuppressWarnings("unchecked") @Test public void addAllIndexCollectionTail() { T t1 = getT(); @@ -137,6 +144,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertEquals(t4, list.get(3)); } + @SuppressWarnings("unchecked") @Test(expected = IllegalArgumentException.class) public void addAllIndexCollectionMiddle() { T t1 = getT(); @@ -220,6 +228,16 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertNull(list.poll()); } + @Test + public void testPollTimeout() throws InterruptedException { + // 1 ms timeout gets upgraded to 1 sec timeout at the moment + assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true")); + T t1 = getT(); + list.add(t1); + assertEquals(t1, list.poll(1, TimeUnit.MILLISECONDS)); + assertNull(list.poll(1, TimeUnit.MILLISECONDS)); + } + @Test public void testRemove() { try { @@ -266,6 +284,16 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertEquals(t1, list.remove(0)); } + @Test + public void testSet() { + T t1 = getT(); + T t2 = getT(); + list.add(t1); + list.set(0, t1); + assertEquals(t1, list.set(0, t2)); + assertEquals(t2, list.get(0)); + } + @Test public void testTrim() { T t1 = getT(); @@ -280,6 +308,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertEquals(t1, list.get(0)); } + @SuppressWarnings("unchecked") @Test public void testCappedCollection() throws Exception { RedisList cappedList = new DefaultRedisList(template.boundListOps(collection.getKey() + ":capped"), 1); @@ -332,6 +361,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT } + @SuppressWarnings("unchecked") @Test public void testDrainToCollectionWithMaxElements() { T t1 = getT(); @@ -351,6 +381,7 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertThat(c, hasItems(t1, t2)); } + @SuppressWarnings("unchecked") @Test public void testDrainToCollection() { T t1 = getT(); @@ -431,6 +462,22 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT assertThat(list, hasItem(t1)); } + @Test + public void testPollLastTimeout() throws InterruptedException { + // 1 ms timeout gets upgraded to 1 sec timeout at the moment + assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true")); + T t1 = getT(); + T t2 = getT(); + + list.add(t1); + list.add(t2); + + T last = list.pollLast(1, TimeUnit.MILLISECONDS); + assertEquals(t2, last); + assertEquals(1, list.size()); + assertThat(list, hasItem(t1)); + } + @Test public void testPut() { testOffer(); diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisMapTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisMapTests.java index 266c21eb6..4005fa2de 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisMapTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisMapTests.java @@ -61,6 +61,7 @@ import org.springframework.data.redis.core.RedisTemplate; * Integration test for Redis Map. * * @author Costin Leau + * @author Jennifer Hickey */ @RunWith(Parameterized.class) public abstract class AbstractRedisMapTests { @@ -68,6 +69,7 @@ public abstract class AbstractRedisMapTests { protected RedisMap map; protected ObjectFactory keyFactory; protected ObjectFactory valueFactory; + @SuppressWarnings("rawtypes") protected RedisTemplate template; abstract RedisMap createMap(); @@ -77,6 +79,7 @@ public abstract class AbstractRedisMapTests { map = createMap(); } + @SuppressWarnings("rawtypes") public AbstractRedisMapTests(ObjectFactory keyFactory, ObjectFactory valueFactory, RedisTemplate template) { this.keyFactory = keyFactory; this.valueFactory = valueFactory; @@ -97,10 +100,12 @@ public abstract class AbstractRedisMapTests { return valueFactory.instance(); } + @SuppressWarnings({ "unchecked", "rawtypes" }) protected RedisStore copyStore(RedisStore store) { return new DefaultRedisMap(store.getKey(), store.getOperations()); } + @SuppressWarnings("unchecked") @After public void tearDown() throws Exception { // remove the collection entirely since clear() doesn't always work @@ -246,6 +251,7 @@ public abstract class AbstractRedisMapTests { assertTrue(map.isEmpty()); } + @SuppressWarnings("unchecked") @Test public void testKeySet() { map.clear(); @@ -337,6 +343,7 @@ public abstract class AbstractRedisMapTests { assertEquals(0, map.size()); } + @SuppressWarnings("unchecked") @Test public void testValues() { V v1 = getValue(); @@ -356,6 +363,7 @@ public abstract class AbstractRedisMapTests { assertThat(values, hasItems(v1, v2, v3)); } + @SuppressWarnings("unchecked") @Test public void testEntrySet() { assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory())); @@ -388,9 +396,9 @@ public abstract class AbstractRedisMapTests { assertThat(values, not(hasItem(v2))); } - - //@Test(expected = UnsupportedOperationException.class) - public void testConcurrentPutIfAbsent() { + @Test + public void testPutIfAbsent() { + assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory())); K k1 = getKey(); K k2 = getKey(); @@ -412,7 +420,6 @@ public abstract class AbstractRedisMapTests { public void testConcurrentRemove() { K k1 = getKey(); V v1 = getValue(); - V v2 = getValue(); map.put(k1, v1); assertFalse(map.remove(k1, v1)); diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTest.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTest.java index 043a241cf..456a8f3c1 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTest.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTest.java @@ -35,11 +35,13 @@ import org.springframework.data.redis.ObjectFactory; import org.springframework.data.redis.connection.ConnectionUtils; import org.springframework.data.redis.core.BoundZSetOperations; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ZSetOperations.TypedTuple; /** * Integration test for Redis ZSet. * * @author Costin Leau + * @author Jennifer Hickey */ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTests { @@ -51,6 +53,7 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe * @param factory * @param template */ + @SuppressWarnings("rawtypes") public AbstractRedisZSetTest(ObjectFactory factory, RedisTemplate template) { super(factory, template); } @@ -189,10 +192,12 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertEquals(1, zSet.getDefaultScore(), 0); } + @SuppressWarnings("unchecked") private RedisZSet createZSetFor(String key) { return new DefaultRedisZSet((BoundZSetOperations) zSet.getOperations().boundZSetOps(key)); } + @SuppressWarnings("unchecked") @Test public void testIntersectAndStore() { assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory())); @@ -239,6 +244,29 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertEquals(t3, iterator.next()); } + @Test + public void testRangeWithScores() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + zSet.add(t1, 1); + zSet.add(t2, 2); + zSet.add(t3, 3); + + Set> range = zSet.rangeWithScores(1, 2); + assertEquals(2, range.size()); + + Iterator> iterator = range.iterator(); + TypedTuple tuple1 = iterator.next(); + assertEquals(t2, tuple1.getValue()); + assertEquals(Double.valueOf(2), tuple1.getScore()); + + TypedTuple tuple2 = iterator.next(); + assertEquals(t3, tuple2.getValue()); + assertEquals(Double.valueOf(3), tuple2.getScore()); + } + @Test public void testReverseRange() { T t1 = getT(); @@ -256,6 +284,71 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertEquals(t1, iterator.next()); } + @Test + public void testReverseRangeWithScores() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + zSet.add(t1, 1); + zSet.add(t2, 2); + zSet.add(t3, 3); + + Set> range = zSet.reverseRangeWithScores(1, 2); + assertEquals(2, range.size()); + + Iterator> iterator = range.iterator(); + TypedTuple tuple1 = iterator.next(); + assertEquals(t2, tuple1.getValue()); + assertEquals(Double.valueOf(2), tuple1.getScore()); + + TypedTuple tuple2 = iterator.next(); + assertEquals(t1, tuple2.getValue()); + assertEquals(Double.valueOf(1), tuple2.getScore()); + } + + @Test + public void testReverseRangeByScore() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + zSet.add(t1, 1); + zSet.add(t2, 2); + zSet.add(t3, 3); + + Set range = zSet.reverseRangeByScore(1.5, 3.5); + assertEquals(2, range.size()); + Iterator iterator = range.iterator(); + assertEquals(t3, iterator.next()); + assertEquals(t2, iterator.next()); + } + + @Test + public void testReverseRangeByScoreWithScores() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + zSet.add(t1, 1); + zSet.add(t2, 2); + zSet.add(t3, 3); + + Set> range = zSet.reverseRangeByScoreWithScores(1.5, 3.5); + assertEquals(2, range.size()); + + Iterator> iterator = range.iterator(); + TypedTuple tuple1 = iterator.next(); + assertEquals(t3, tuple1.getValue()); + assertEquals(Double.valueOf(3), tuple1.getScore()); + + TypedTuple tuple2 = iterator.next(); + assertEquals(t2, tuple2.getValue()); + assertEquals(Double.valueOf(2), tuple2.getScore()); + } + + @SuppressWarnings("unchecked") + @Test public void testRangeByScore() { T t1 = getT(); T t2 = getT(); @@ -274,6 +367,29 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertEquals(t3, iterator.next()); } + @Test + public void testRangeByScoreWithScores() { + T t1 = getT(); + T t2 = getT(); + T t3 = getT(); + + zSet.add(t1, 1); + zSet.add(t2, 2); + zSet.add(t3, 3); + + Set> range = zSet.rangeByScoreWithScores(1.5, 3.5); + assertEquals(2, range.size()); + + Iterator> iterator = range.iterator(); + TypedTuple tuple1 = iterator.next(); + assertEquals(t2, tuple1.getValue()); + assertEquals(Double.valueOf(2), tuple1.getScore()); + + TypedTuple tuple2 = iterator.next(); + assertEquals(t3, tuple2.getValue()); + assertEquals(Double.valueOf(3), tuple2.getScore()); + } + @Test public void testRemove() { T t1 = getT(); @@ -315,6 +431,7 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertEquals(t4, iterator.next()); } + @SuppressWarnings("unchecked") @Test public void testUnionAndStore() { assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory())); @@ -366,7 +483,6 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertFalse(iterator.hasNext()); } - @SuppressWarnings("unchecked") @Test public void testToArray() { T t1 = getT(); @@ -383,7 +499,6 @@ public abstract class AbstractRedisZSetTest extends AbstractRedisCollectionTe assertArrayEquals(new Object[] { t1, t2, t3, t4 }, array); } - @SuppressWarnings("unchecked") @Test public void testToArrayWithGenerics() { T t1 = getT();