DATAREDIS-314 - Add SCAN commands to Collection types.

Add scan to RedisMap / RedisSet / RedisZSet.
Lua scritps have to be assembled in binary way in order to avoid script errors when executing.
Add missing rules/profiles to run properly in a 2.6.x redis environment.

Original pull request: #82.
This commit is contained in:
Christoph Strobl
2014-06-12 11:04:31 +02:00
committed by Thomas Darimont
parent 7b3a358752
commit 42ff3ee1e1
18 changed files with 456 additions and 51 deletions

View File

@@ -25,6 +25,7 @@ import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -35,6 +36,7 @@ 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.jedis.JedisConnectionFactory;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.test.annotation.IfProfileValue;
/**
@@ -48,6 +50,9 @@ import org.springframework.test.annotation.IfProfileValue;
*/
@RunWith(Parameterized.class)
public class DefaultHashOperationsTests<K, HK, HV> {
public @Rule MinimumRedisVersionRule versionRule = new MinimumRedisVersionRule();
private RedisTemplate<K, ?> redisTemplate;
private ObjectFactory<K> keyFactory;

View File

@@ -29,6 +29,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -36,6 +37,7 @@ 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;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.test.annotation.IfProfileValue;
/**
@@ -49,6 +51,8 @@ import org.springframework.test.annotation.IfProfileValue;
@RunWith(Parameterized.class)
public class DefaultZSetOperationsTests<K, V> {
public @Rule MinimumRedisVersionRule versionRule = new MinimumRedisVersionRule();
private RedisTemplate<K, V> redisTemplate;
private ObjectFactory<K> keyFactory;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 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.
@@ -15,23 +15,16 @@
*/
package org.springframework.data.redis.support.collections;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.springframework.data.redis.matcher.RedisTestMatchers.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
@@ -41,6 +34,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -53,19 +47,35 @@ import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.data.redis.test.util.RedisClientRule;
import org.springframework.data.redis.test.util.RedisDriver;
import org.springframework.data.redis.test.util.WithRedisDriver;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test for Redis Map.
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Christoph Strobl
* @auhtor Thomas Darimont
*/
@RunWith(Parameterized.class)
public abstract class AbstractRedisMapTests<K, V> {
public @Rule RedisClientRule clientRule = new RedisClientRule() {
public RedisConnectionFactory getConnectionFactory() {
return template.getConnectionFactory();
}
};
public @Rule MinimumRedisVersionRule versionRule = new MinimumRedisVersionRule();
protected RedisMap<K, V> map;
protected ObjectFactory<K> keyFactory;
protected ObjectFactory<V> valueFactory;
@@ -478,4 +488,29 @@ public abstract class AbstractRedisMapTests<K, V> {
public void testReplaceNullValue() {
map.replace(getKey(), null);
}
/**
* @see DATAREDIS-314
*/
@Test
@IfProfileValue(name = "redisVersion", value = "2.8+")
@WithRedisDriver({ RedisDriver.JEDIS, RedisDriver.LETTUCE })
public void testScanWorksCorrectly() {
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
map.put(k1, v1);
map.put(k2, v2);
Iterator<Entry<K, V>> it = map.scan();
while (it.hasNext()) {
Entry<K, V> entry = it.next();
assertThat(entry.getKey(), anyOf(equalTo(k1), equalTo(k2)));
assertThat(entry.getValue(), anyOf(equalTo(v1), equalTo(v2)));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 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.
@@ -17,7 +17,7 @@ package org.springframework.data.redis.support.collections;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual;
import static org.springframework.data.redis.matcher.RedisTestMatchers.*;
import java.util.ArrayList;
import java.util.Arrays;
@@ -26,20 +26,35 @@ import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.collections.DefaultRedisSet;
import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.data.redis.test.util.RedisClientRule;
import org.springframework.data.redis.test.util.RedisDriver;
import org.springframework.data.redis.test.util.WithRedisDriver;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test for Redis set.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Thomas Darimont
*/
public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTests<T> {
public @Rule RedisClientRule clientRule = new RedisClientRule() {
public RedisConnectionFactory getConnectionFactory() {
return template.getConnectionFactory();
}
};
public @Rule MinimumRedisVersionRule versionRule = new MinimumRedisVersionRule();
protected RedisSet<T> set;
/**
@@ -65,7 +80,6 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
return new DefaultRedisSet<T>((BoundSetOperations<String, T>) set.getOperations().boundSetOps(key));
}
@SuppressWarnings("unchecked")
@Test
public void testDiff() {
RedisSet<T> diffSet1 = createSetFor("test:set:diff1");
@@ -87,7 +101,6 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
assertThat(diff, hasItem(t1));
}
@SuppressWarnings("unchecked")
@Test
public void testDiffAndStore() {
RedisSet<T> diffSet1 = createSetFor("test:set:diff1");
@@ -114,7 +127,6 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
assertEquals(resultName, diff.getKey());
}
@SuppressWarnings("unchecked")
@Test
public void testIntersect() {
RedisSet<T> intSet1 = createSetFor("test:set:int1");
@@ -139,7 +151,6 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
assertThat(inter, hasItem(t2));
}
@SuppressWarnings("unchecked")
public void testIntersectAndStore() {
RedisSet<T> intSet1 = createSetFor("test:set:int1");
RedisSet<T> intSet2 = createSetFor("test:set:int2");
@@ -213,7 +224,6 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
assertEquals(resultName, union.getKey());
}
@SuppressWarnings("unchecked")
@Test
public void testIterator() {
T t1 = getT();
@@ -290,4 +300,22 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
assertEquals(0, result.size());
}
/**
* @see DATAREDIS-314
*/
@SuppressWarnings("unchecked")
@IfProfileValue(name = "redisVersion", value = "2.8+")
@Test
@WithRedisDriver({ RedisDriver.JEDIS, RedisDriver.LETTUCE })
public void testScanWorksCorrectly() {
Object[] expectedArray = new Object[] { getT(), getT(), getT() };
collection.addAll((List<T>) Arrays.asList(expectedArray));
Iterator<T> it = set.scan();
while (it.hasNext()) {
assertThat(it.next(), anyOf(equalTo(expectedArray[0]), equalTo(expectedArray[1]), equalTo(expectedArray[2])));
}
}
}

View File

@@ -15,15 +15,10 @@
*/
package org.springframework.data.redis.support.collections;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.springframework.data.redis.matcher.RedisTestMatchers.*;
import java.util.Arrays;
import java.util.Iterator;
@@ -31,21 +26,37 @@ import java.util.NoSuchElementException;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
import org.springframework.data.redis.test.util.RedisClientRule;
import org.springframework.data.redis.test.util.RedisDriver;
import org.springframework.data.redis.test.util.WithRedisDriver;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test for Redis ZSet.
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
*/
public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTests<T> {
public @Rule RedisClientRule clientRule = new RedisClientRule() {
public RedisConnectionFactory getConnectionFactory() {
return template.getConnectionFactory();
}
};
public @Rule MinimumRedisVersionRule versionRule = new MinimumRedisVersionRule();
protected RedisZSet<T> zSet;
/**
@@ -519,4 +530,28 @@ public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTe
Object[] array = collection.toArray(new Object[zSet.size()]);
assertArrayEquals(new Object[] { t1, t2, t3, t4 }, array);
}
/**
* @see DATAREDIS-314
*/
@IfProfileValue(name = "redisVersion", value = "2.8+")
@Test
@WithRedisDriver({ RedisDriver.JEDIS, RedisDriver.LETTUCE })
public void testScanWorksCorrectly() {
T t1 = getT();
T t2 = getT();
T t3 = getT();
T t4 = getT();
zSet.add(t1, 1);
zSet.add(t2, 2);
zSet.add(t3, 3);
zSet.add(t4, 4);
Iterator<T> it = zSet.scan();
while (it.hasNext()) {
assertThat(it.next(), anyOf(equalTo(t1), equalTo(t2), equalTo(t3), equalTo(t4)));
}
}
}

View File

@@ -52,6 +52,7 @@ import org.springframework.oxm.xstream.XStreamMarshaller;
/**
* @author Costin Leau
* @author Thomas Darimont
* @author Christoph Strobl
*/
public class RedisPropertiesTests extends RedisMapTests {
@@ -220,6 +221,12 @@ public class RedisPropertiesTests extends RedisMapTests {
assertTrue(keys.contains(key3));
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void testScanWorksCorrectly() {
super.testScanWorksCorrectly();
}
/**
* @see DATAREDIS-241
*/
@@ -371,4 +378,5 @@ public class RedisPropertiesTests extends RedisMapTests {
{ stringFactory, stringFactory, xGenericTemplateSrp }, { stringFactory, stringFactory, jsonPersonTemplateSrp },
{ stringFactory, stringFactory, jackson2JsonPersonTemplateSrp } });
}
}

View File

@@ -23,7 +23,7 @@ import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.Version;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.util.StringUtils;
@@ -45,9 +45,10 @@ public class MinimumRedisVersionRule implements TestRule {
private Version readServerVersion() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
connectionFactory.setHostName(SettingsUtils.getHost());
connectionFactory.setPort(SettingsUtils.getPort());
connectionFactory.setTimeout(100);
connectionFactory.afterPropertiesSet();
RedisConnection connection = connectionFactory.getConnection();

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2014 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.test.util;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
* A JUnit {@link TestRule} that checks whether a given {@link RedisConnectionFactory} is from the required
* {@link RedisDriver}.
*
* @author Christoph Strobl
* @author Thomas Darimont
*/
public abstract class RedisClientRule implements TestRule {
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
WithRedisDriver withRedisDriver = description.getAnnotation(WithRedisDriver.class);
RedisConnectionFactory redisConnectionFactory = getConnectionFactory();
if (withRedisDriver != null && redisConnectionFactory != null) {
boolean valid = true;
for (RedisDriver driver : withRedisDriver.value()) {
valid &= driver.matches(redisConnectionFactory);
if (!valid) {
throw new AssumptionViolatedException("not a vaild redis connection for driver: " + driver);
}
}
}
base.evaluate();
}
};
}
public abstract RedisConnectionFactory getConnectionFactory();
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2014 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.test.util;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
* Enumerates the supported Redis driver types.
*
* @author Thomas Darimont
*/
public enum RedisDriver {
JEDIS {
@Override
public boolean matches(RedisConnectionFactory connectionFactory) {
return ConnectionUtils.isJedis(connectionFactory);
}
},
LETTUCE {
@Override
public boolean matches(RedisConnectionFactory connectionFactory) {
return ConnectionUtils.isLettuce(connectionFactory);
}
},
SRP {
@Override
public boolean matches(RedisConnectionFactory connectionFactory) {
return ConnectionUtils.isSrp(connectionFactory);
}
},
JREDIS {
@Override
public boolean matches(RedisConnectionFactory connectionFactory) {
return ConnectionUtils.isJredis(connectionFactory);
}
};
/**
* @param connectionFactory
* @return true of the given {@link RedisConnectionFactory} is supported by the current redis driver.
*/
public abstract boolean matches(RedisConnectionFactory connectionFactory);
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2014 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.test.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation to declare the supported {@link RedisDriver} types.
*
* @author Thomas Darimont
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface WithRedisDriver {
RedisDriver[] value() default {};
}