Add framework for running tests based on Redis version

This commit is contained in:
Jennifer Hickey
2013-06-12 17:29:41 -07:00
parent d3809213c8
commit 1f87bc24d5
9 changed files with 92 additions and 21 deletions

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2011-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;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.test.annotation.ProfileValueSource;
/**
* Implementation of {@link ProfileValueSource} that handles profile value name
* "redisVersion" by checking the current version of Redis. 2.4.x will be
* returned as "2.4" and 2.6.x will be returned as "2.6". Any other version
* found will cause an {@link UnsupportedOperationException}
*
* System property values will be returned for any key other than "redisVersion"
*
* @author Jennifer Hickey
*
*/
public class RedisTestProfileValueSource implements ProfileValueSource {
private static final String REDIS_24 = "2.4";
private static final String REDIS_26 = "2.6";
private static final String REDIS_VERSION_KEY = "redisVersion";
private static Version redisVersion;
public RedisTestProfileValueSource() {
if (redisVersion == null) {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(
SettingsUtils.getHost(), SettingsUtils.getPort());
connectionFactory.afterPropertiesSet();
RedisConnection connection = connectionFactory.getConnection();
redisVersion = RedisVersionUtils.getRedisVersion(connection);
connection.close();
}
}
public String get(String key) {
if (REDIS_VERSION_KEY.equals(key)) {
if (redisVersion.compareTo(RedisVersionUtils.parseVersion(REDIS_26)) >= 0) {
return REDIS_26;
}
if (redisVersion.compareTo(RedisVersionUtils.parseVersion(REDIS_24)) >= 0) {
return REDIS_24;
}
throw new UnsupportedOperationException("Only Redis 2.4 and higher are supported");
}
return System.getProperty(key);
}
}

View File

@@ -43,14 +43,14 @@ public abstract class RedisVersionUtils {
return getRedisVersion(connection).compareTo(parseVersion(version)) <= 0;
}
private static Version parseVersion(String version) {
public static Version parseVersion(String version) {
Matcher matcher = VERSION_MATCHER.matcher(version);
if (matcher.matches()) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(4);
return new Version(Integer.parseInt(major), Integer.parseInt(minor),
Integer.parseInt(patch));
return new Version(Integer.parseInt(major), minor != null ? Integer.parseInt(minor) : 0,
patch != null ? Integer.parseInt(patch) : 0);
}
throw new IllegalArgumentException("Specified version cannot be parsed");
}

View File

@@ -44,6 +44,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.Address;
import org.springframework.data.redis.Person;
import org.springframework.data.redis.RedisTestProfileValueSource;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
@@ -64,7 +65,7 @@ import org.springframework.test.annotation.ProfileValueSourceConfiguration;
* @author Jennifer Hickey
*
*/
@ProfileValueSourceConfiguration
@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class)
public abstract class AbstractConnectionIntegrationTests {
protected StringRedisConnection connection;
@@ -115,6 +116,7 @@ public abstract class AbstractConnectionIntegrationTests {
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpire() {
connection.set("exp", "true");
assertTrue(connection.pExpire("exp", 100));
@@ -122,11 +124,13 @@ public abstract class AbstractConnectionIntegrationTests {
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpireKeyNotExists() {
assertFalse(connection.pExpire("nonexistent", 100));
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpireAt() {
connection.set("exp", "true");
assertTrue(connection.pExpireAt("exp", System.currentTimeMillis() + 200));
@@ -134,6 +138,7 @@ public abstract class AbstractConnectionIntegrationTests {
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpireAtKeyNotExists() {
assertFalse(connection.pExpireAt("nonexistent", System.currentTimeMillis() + 200));
}
@@ -542,6 +547,7 @@ public abstract class AbstractConnectionIntegrationTests {
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPTtlNoExpire() {
connection.set("whatup", "yo");
actual.add(connection.pTtl("whatup"));
@@ -549,6 +555,7 @@ public abstract class AbstractConnectionIntegrationTests {
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPTtl() {
connection.set("whatup", "yo");
connection.pExpire("whatup", 9000l);

View File

@@ -115,6 +115,7 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
verifyResults(Arrays.asList(new Object[] { "bar" }), actual);
}
@IfProfileValue(name = "redisVersion", value = "2.6")
@Test
public void testPExpire() {
connection.set("exp", "true");
@@ -123,12 +124,14 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
assertTrue(waitFor(new KeyExpired("exp"), 1000l));
}
@IfProfileValue(name = "redisVersion", value = "2.6")
@Test
public void testPExpireKeyNotExists() {
actual.add(connection.pExpire("nonexistent", 100));
verifyResults(Arrays.asList(new Object[] { 0l }), actual);
}
@IfProfileValue(name = "redisVersion", value = "2.6")
@Test
public void testPExpireAt() {
connection.set("exp2", "true");
@@ -137,12 +140,14 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
assertTrue(waitFor(new KeyExpired("exp2"), 1000l));
}
@IfProfileValue(name = "redisVersion", value = "2.6")
@Test
public void testPExpireAtKeyNotExists() {
actual.add(connection.pExpireAt("nonexistent", System.currentTimeMillis() + 200));
verifyResults(Arrays.asList(new Object[] { 0l }), actual);
}
@IfProfileValue(name = "redisVersion", value = "2.6")
@Test
public void testPTtl() {
connection.set("whatup", "yo");

View File

@@ -292,6 +292,7 @@ public class LettuceConnectionPipelineIntegrationTests extends
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpire() {
connection.set("exp", "true");
actual.add(connection.pExpire("exp", 100));
@@ -300,12 +301,14 @@ public class LettuceConnectionPipelineIntegrationTests extends
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpireKeyNotExists() {
actual.add(connection.pExpire("nonexistent", 100));
verifyResults(Arrays.asList(new Object[] { false }), actual);
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpireAt() {
connection.set("exp2", "true");
actual.add(connection.pExpireAt("exp2", System.currentTimeMillis() + 200));
@@ -314,12 +317,14 @@ public class LettuceConnectionPipelineIntegrationTests extends
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPExpireAtKeyNotExists() {
actual.add(connection.pExpireAt("nonexistent", System.currentTimeMillis() + 200));
verifyResults(Arrays.asList(new Object[] { false }), actual);
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testPTtl() {
connection.set("whatup", "yo");
actual.add(connection.pExpire("whatup", 9000l));

View File

@@ -25,6 +25,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -77,8 +78,8 @@ public class SrpConnectionIntegrationTests extends AbstractConnectionIntegration
}
@Test
@IfProfileValue(name = "redisVersion", values = {"2.4", "2.6"})
public void testGetRangeSetRange() {
assumeTrue(RedisVersionUtils.atLeast("2.4.0", connection));
super.testGetRangeSetRange();
}

View File

@@ -37,6 +37,7 @@ import org.springframework.data.redis.connection.DefaultStringTuple;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.connection.StringRedisConnection.StringTuple;
import org.springframework.data.redis.serializer.SerializationUtils;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -135,11 +136,8 @@ public class SrpConnectionPipelineIntegrationTests extends
}
@Test
@IfProfileValue(name = "redisVersion", values = {"2.4", "2.6"})
public void testGetRangeSetRange() {
connection.closePipeline();
boolean getRangeSupported = RedisVersionUtils.atLeast("2.4.0", connection);
connection.openPipeline();
assumeTrue(getRangeSupported);
super.testGetRangeSetRange();
}

View File

@@ -26,6 +26,7 @@ import java.util.List;
import org.junit.Test;
import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.data.redis.connection.RedisPipelineException;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test of {@link SrpConnection} transactions within a pipeline
@@ -83,12 +84,8 @@ public class SrpConnectionPipelineTxIntegrationTests extends
}
@Test
@IfProfileValue(name = "redisVersion", values = {"2.4", "2.6"})
public void testGetRangeSetRange() {
connection.exec();
connection.closePipeline();
boolean getRangeSupported = RedisVersionUtils.atLeast("2.4.0", connection);
initConnection();
assumeTrue(getRangeSupported);
connection.set("rangekey", "supercalifrag");
actual.add(connection.getRange("rangekey", 0l, 2l));
connection.setRange("rangekey", "ck", 2);

View File

@@ -15,15 +15,13 @@
*/
package org.springframework.data.redis.connection.srp;
import static org.junit.Assume.assumeTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.test.annotation.IfProfileValue;
/**
* Integration test of {@link SrpConnection} functionality within a transaction
@@ -88,11 +86,8 @@ public class SrpConnectionTransactionIntegrationTests extends
}
@Test
@IfProfileValue(name = "redisVersion", values = {"2.4", "2.6"})
public void testGetRangeSetRange() {
connection.exec();
boolean getRangeSupported = RedisVersionUtils.atLeast("2.4.0", connection);
connection.multi();
assumeTrue(getRangeSupported);
connection.set("rangekey", "supercalifrag");
actual.add(connection.getRange("rangekey", 0l, 2l));
connection.setRange("rangekey", "ck", 2);