diff --git a/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java b/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java index 5df843583..00707faf0 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundValueOperations.java @@ -42,6 +42,8 @@ public interface BoundValueOperations extends BoundKeyOperations { Long increment(long delta); + Double increment(double delta); + Integer append(String value); Long size(); diff --git a/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java index 946fcdd95..49d8eb4da 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultBoundValueOperations.java @@ -52,7 +52,10 @@ class DefaultBoundValueOperations extends DefaultBoundKeyOperations imp return ops.increment(getKey(), delta); } - + public Double increment(double delta) { + return ops.increment(getKey(), delta); + } + public Integer append(String value) { return ops.append(getKey(), value); } diff --git a/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java index 57ea74272..1ffe3eb20 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java @@ -29,6 +29,7 @@ import org.springframework.data.redis.connection.RedisConnection; * Default implementation of {@link ValueOperations}. * * @author Costin Leau + * @author Jennifer Hickey */ class DefaultValueOperations extends AbstractOperations implements ValueOperations { @@ -61,24 +62,23 @@ class DefaultValueOperations extends AbstractOperations implements V public Long increment(K key, final long delta) { final byte[] rawKey = rawKey(key); - // TODO add conversion service in here ? return execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) { - if (delta == 1) { - return connection.incr(rawKey); - } - - if (delta == -1) { - return connection.decr(rawKey); - } - + return connection.incrBy(rawKey, delta); + } + }, true); + } + + public Double increment(K key, final double delta) { + final byte[] rawKey = rawKey(key); + return execute(new RedisCallback() { + public Double doInRedis(RedisConnection connection) { return connection.incrBy(rawKey, delta); } }, true); } - public Integer append(K key, String value) { final byte[] rawKey = rawKey(key); final byte[] rawString = rawString(value); diff --git a/src/main/java/org/springframework/data/redis/core/ValueOperations.java b/src/main/java/org/springframework/data/redis/core/ValueOperations.java index 3de41dbcf..b034f4d3b 100644 --- a/src/main/java/org/springframework/data/redis/core/ValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ValueOperations.java @@ -45,6 +45,8 @@ public interface ValueOperations { Long increment(K key, long delta); + Double increment(K key, double delta); + Integer append(K key, String value); String get(K key, long start, long end); diff --git a/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java new file mode 100644 index 000000000..de84403e0 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/core/DefaultValueOperationsTests.java @@ -0,0 +1,74 @@ +/* + * 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 org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.IfProfileValue; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Integration test of {@link DefaultValueOperations} + * + * @author Jennifer Hickey + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("RedisTemplateTests-context.xml") +public class DefaultValueOperationsTests { + + @Autowired + private RedisTemplate redisTemplate; + + private ValueOperations valueOps; + + @Before + public void setUp() { + valueOps = redisTemplate.opsForValue(); + } + + @After + public void tearDown() { + redisTemplate.getConnectionFactory().getConnection().flushDb(); + } + + @Test + public void testIncrementLong() throws Exception { + String key = "test.template.inc"; + valueOps.set(key, "10"); + assertEquals(Long.valueOf(0), valueOps.increment(key, -10)); + assertEquals(0, Integer.valueOf(valueOps.get(key)).intValue()); + valueOps.increment(key, -10); + assertEquals(-10, Integer.valueOf(valueOps.get(key)).intValue()); + } + + @Test + @IfProfileValue(name = "redisVersion", value = "2.6") + public void testIncrementDouble() { + String key = "test.template.inc"; + valueOps.set(key, "10.5"); + assertEquals(Double.valueOf(11.9), valueOps.increment(key, 1.4)); + assertEquals("11.9", valueOps.get(key)); + valueOps.increment(key, -10d); + assertEquals("1.9", valueOps.get(key)); + } +} diff --git a/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java b/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java index d97138350..8f803ec9f 100644 --- a/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java +++ b/src/test/java/org/springframework/data/redis/core/RedisTemplateTests.java @@ -15,6 +15,10 @@ */ package org.springframework.data.redis.core; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.springframework.data.redis.SpinBarrier.waitFor; + import java.util.concurrent.TimeUnit; import org.junit.After; @@ -26,10 +30,6 @@ import org.springframework.test.annotation.IfProfileValue; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertEquals; -import static org.springframework.data.redis.SpinBarrier.waitFor; - /** * * Integration test of {@link RedisTemplate} @@ -76,4 +76,18 @@ public class RedisTemplateTests { }, 400); } + @Test + public void testKeys() throws Exception { + redisTemplate.opsForValue().set("foo", "bar"); + assertNotNull(redisTemplate.keys("*")); + } + + @SuppressWarnings("rawtypes") + @Test(expected = IllegalArgumentException.class) + public void testTemplateNotInitialized() throws Exception { + RedisTemplate tpl = new RedisTemplate(); + tpl.setConnectionFactory(redisTemplate.getConnectionFactory()); + tpl.exec(); + } + } diff --git a/src/test/java/org/springframework/data/redis/core/TemplateTest.java b/src/test/java/org/springframework/data/redis/core/TemplateTest.java deleted file mode 100644 index a54a2f9a2..000000000 --- a/src/test/java/org/springframework/data/redis/core/TemplateTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.core; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.util.Collection; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.springframework.dao.DataAccessException; -import org.springframework.data.redis.ConnectionFactoryTracker; -import org.springframework.data.redis.connection.ConnectionUtils; -import org.springframework.data.redis.connection.RedisConnection; -import org.springframework.data.redis.support.collections.CollectionTestParams; -import org.springframework.data.redis.support.collections.ObjectFactory; - -/** - * @author Costin Leau - */ -@RunWith(Parameterized.class) -public class TemplateTest { - private ObjectFactory objFactory; - private RedisTemplate template; - - public TemplateTest(ObjectFactory objFactory, RedisTemplate template) { - this.objFactory = objFactory; - this.template = template; - ConnectionFactoryTracker.add(template.getConnectionFactory()); - } - - @AfterClass - public static void cleanUp() { - ConnectionFactoryTracker.cleanUp(); - } - - @Parameters - public static Collection testParams() { - return CollectionTestParams.testParams(); - } - - @Test(expected = IllegalArgumentException.class) - public void testTemplateNotInitialized() throws Exception { - RedisTemplate tpl = new RedisTemplate(); - tpl.setConnectionFactory(template.getConnectionFactory()); - tpl.exec(); - } - - @Test - public void testKeys() throws Exception { - assertTrue(template.keys("*") != null); - } - - @Test - public void testIncrement() throws Exception { - StringRedisTemplate sr = new StringRedisTemplate(template.getConnectionFactory()); - String key = "test.template.inc"; - ValueOperations valueOps = sr.opsForValue(); - valueOps.set(key, "10"); - valueOps.increment(key, -10); - assertEquals(0, Integer.valueOf(valueOps.get(key)).intValue()); - valueOps.increment(key, -10); - assertEquals(-10, Integer.valueOf(valueOps.get(key)).intValue()); - } - - //@Test - public void testGetNonExistingKey() throws Exception { - List res = (List) template.execute(new RedisCallback>() { - - public List doInRedis(RedisConnection connection) throws DataAccessException { - connection.hGet("non-existing-key".getBytes(), "some-value".getBytes()); - return connection.closePipeline(); - } - }, true, true); - } -} 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 07bf2e173..cf4697f43 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 @@ -46,12 +46,12 @@ import org.junit.runners.Parameterized; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.redis.ConnectionFactoryTracker; 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.core.RedisCallback; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.test.annotation.IfProfileValue; /** * Integration test for Redis Map. @@ -221,10 +221,10 @@ public abstract class AbstractRedisMapTests { assertEquals(Long.valueOf(Long.valueOf((String)v1) + 10), map.increment(k1, 10)); } - @IfProfileValue(name = "redisVersion", value = "2.6") @Test public void testIncrementDouble() { - assumeTrue(valueFactory instanceof DoubleObjectFactory); + assumeTrue(RedisVersionUtils.atLeast("2.6", template.getConnectionFactory().getConnection()) && + valueFactory instanceof DoubleObjectFactory); K k1 = getKey(); V v1 = getValue(); map.put(k1, v1); diff --git a/src/test/resources/org/springframework/data/redis/core/RedisTemplateTests-context.xml b/src/test/resources/org/springframework/data/redis/core/RedisTemplateTests-context.xml index 6ae329887..00cb8ae78 100644 --- a/src/test/resources/org/springframework/data/redis/core/RedisTemplateTests-context.xml +++ b/src/test/resources/org/springframework/data/redis/core/RedisTemplateTests-context.xml @@ -15,7 +15,7 @@ - +