diff --git a/src/test/java/org/springframework/data/redis/RedisTestProfileValueSource.java b/src/test/java/org/springframework/data/redis/RedisTestProfileValueSource.java new file mode 100644 index 000000000..f8beaef52 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/RedisTestProfileValueSource.java @@ -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); + } +} diff --git a/src/test/java/org/springframework/data/redis/RedisVersionUtils.java b/src/test/java/org/springframework/data/redis/RedisVersionUtils.java index 5ee4287c0..0638930b5 100644 --- a/src/test/java/org/springframework/data/redis/RedisVersionUtils.java +++ b/src/test/java/org/springframework/data/redis/RedisVersionUtils.java @@ -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"); } diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java index cc769366d..4f9fb5185 100644 --- a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java @@ -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); diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java index 6ec515f9c..242a45b28 100644 --- a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java @@ -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"); diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineIntegrationTests.java index 69bd44abe..d33421362 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionPipelineIntegrationTests.java @@ -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)); diff --git a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java index 6069ad6be..e519fa2d6 100644 --- a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java @@ -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(); } diff --git a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineIntegrationTests.java index 5b9bbff6d..80e78cb35 100644 --- a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineIntegrationTests.java @@ -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(); } diff --git a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineTxIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineTxIntegrationTests.java index 4c28c642d..4af3b14d0 100644 --- a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineTxIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionPipelineTxIntegrationTests.java @@ -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); diff --git a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionTransactionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionTransactionIntegrationTests.java index 10cf14a6d..20e1ea45e 100644 --- a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionTransactionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionTransactionIntegrationTests.java @@ -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);