Add Redis 2.6 incrbyfloat to ValueOperations

DATAREDIS-181
This commit is contained in:
Jennifer Hickey
2013-06-17 17:09:36 -07:00
parent 128360d43c
commit cc851c674e
9 changed files with 114 additions and 113 deletions

View File

@@ -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<String, String> redisTemplate;
private ValueOperations<String, String> 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));
}
}

View File

@@ -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();
}
}

View File

@@ -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<Object> objFactory;
private RedisTemplate template;
public TemplateTest(ObjectFactory<Object> objFactory, RedisTemplate template) {
this.objFactory = objFactory;
this.template = template;
ConnectionFactoryTracker.add(template.getConnectionFactory());
}
@AfterClass
public static void cleanUp() {
ConnectionFactoryTracker.cleanUp();
}
@Parameters
public static Collection<Object[]> 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<String, String> 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<Object> res = (List<Object>) template.execute(new RedisCallback<List<Object>>() {
public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
connection.hGet("non-existing-key".getBytes(), "some-value".getBytes());
return connection.closePipeline();
}
}, true, true);
}
}

View File

@@ -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<K, V> {
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);