Fix for (hash) increment treats long values as int

While the (hash) increment() method accepts a long argument (and Redis
HINCRBY/INCRBY/DECRBY treats arguments as 64 bits), it is cast to a
32bits signed int in the JedisConnection class.

 - Removed offensive (& unnecessary) casts

 - Extended integration tests with incrBy, decrBy and hIncrBy to
   demonstrate handling of large numbers

 - Fixed typos in test support
   (CollectionTestParams, AbstractConnectionIntegrationTests)

Issue: DATAREDIS-117
This commit is contained in:
Tijn Porcelijn
2013-02-21 17:13:31 +01:00
committed by Tijn Porcelijn
parent cc354f8552
commit 27a7ab0738
5 changed files with 42 additions and 16 deletions

View File

@@ -76,7 +76,6 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
public void setUp() throws Exception {
ConnectionFactoryTracker.add(template.getConnectionFactory());
super.setUp();
}
@AfterClass
@@ -161,4 +160,4 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
assertNotNull(cache);
assertTrue(redisCM.getCacheNames().contains(cacheName));
}
}
}

View File

@@ -141,7 +141,7 @@ public abstract class AbstractConnectionIntegrationTests {
try {
connection.decr((String) null);
} catch (Exception ex) {
// excepted
// expected
}
}
@@ -389,4 +389,4 @@ public abstract class AbstractConnectionIntegrationTests {
assertTrue(hashMap.containsKey(key1));
assertTrue(hashMap.containsKey(key2));
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.data.redis.connection.jedis;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
@@ -60,6 +61,32 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
System.out.println(connection.exec());
}
@Test
public void testIncrDecrBy() {
String key = "test.count";
long largeNumber = 0x123456789L; // > 32bits
connection.set(key.getBytes(), "0".getBytes());
connection.incrBy(key.getBytes(), largeNumber);
assertEquals(largeNumber, Long.valueOf(new String(connection.get(key.getBytes()))).longValue());
connection.decrBy(key.getBytes(), largeNumber);
assertEquals(0, Long.valueOf(new String(connection.get(key.getBytes()))).longValue());
connection.decrBy(key.getBytes(), 2*largeNumber);
assertEquals(-2*largeNumber, Long.valueOf(new String(connection.get(key.getBytes()))).longValue());
}
@Test
public void testHashIncrDecrBy() {
byte[] key = "test.hcount".getBytes();
byte[] hkey = "hashkey".getBytes();
long largeNumber = 0x123456789L; // > 32bits
connection.hSet(key, hkey, "0".getBytes());
connection.hIncrBy(key, hkey, largeNumber);
assertEquals(largeNumber, Long.valueOf(new String(connection.hGet(key, hkey))).longValue());
connection.hIncrBy(key, hkey, -2*largeNumber);
assertEquals(-largeNumber, Long.valueOf(new String(connection.hGet(key, hkey))).longValue());
}
// @Test
// public void setAdd() {
// connection.sadd("s1", "1");
@@ -87,4 +114,4 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
// for (Object object : s3) {
// System.out.println(object);
// }
}
}

View File

@@ -169,7 +169,7 @@ public abstract class CollectionTestParams {
lettuceConnFactory.setHostName(SettingsUtils.getHost());
lettuceConnFactory.afterPropertiesSet();
RedisTemplate<String, String> stringTemplateLtc = new StringRedisTemplate(srConnFactory);
RedisTemplate<String, String> stringTemplateLtc = new StringRedisTemplate(lettuceConnFactory);
RedisTemplate<String, Person> personTemplateLtc = new RedisTemplate<String, Person>();
personTemplateLtc.setConnectionFactory(lettuceConnFactory);
personTemplateLtc.afterPropertiesSet();