DATAREDIS-469 - Polishing.

Return zero (0) in getAndSet to prevent NullPointerExceptions. Remove trailing whitespace. Add tests for existing methods. Simplify tests.

Original pull request: #182.
This commit is contained in:
Mark Paluch
2016-05-13 14:34:21 +02:00
parent 20cfd3e76c
commit 83359a9c4f
6 changed files with 252 additions and 99 deletions

View File

@@ -42,21 +42,30 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Integration test of {@link RedisAtomicDouble}
*
*
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
@RunWith(Parameterized.class)
public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
private RedisAtomicDouble doubleCounter;
private RedisConnectionFactory factory;
private RedisTemplate<String, Double> template;
public RedisAtomicDoubleTests(RedisConnectionFactory factory) {
doubleCounter = new RedisAtomicDouble(getClass().getSimpleName() + ":double", factory);
this.doubleCounter = new RedisAtomicDouble(getClass().getSimpleName() + ":double", factory);
this.factory = factory;
this.template = new RedisTemplate<String, Double>();
this.template.setConnectionFactory(factory);
this.template.setKeySerializer(new StringRedisSerializer());
this.template.setValueSerializer(new GenericToStringSerializer<Double>(Double.class));
this.template.afterPropertiesSet();
ConnectionFactoryTracker.add(factory);
}
@@ -201,12 +210,6 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
@Test
public void testShouldBeAbleToUseRedisAtomicDoubleWithProperlyConfiguredRedisTemplate() {
RedisTemplate<String, Double> template = new RedisTemplate<String, Double>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Double>(Double.class));
template.afterPropertiesSet();
RedisAtomicDouble ral = new RedisAtomicDouble("DATAREDIS-317.atomicDouble", template);
ral.set(32.23);
@@ -222,18 +225,27 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
expectedException.expect(DataRetrievalFailureException.class);
expectedException.expectMessage("'test' seems to no longer exist");
// setup long
// setup double
RedisAtomicDouble test = new RedisAtomicDouble("test", factory, 1);
assertThat(test.get(), equalTo(1D)); // this passes
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
template.afterPropertiesSet();
template.delete("test");
test.get();
}
/**
* @see DATAREDIS-469
*/
@Test
public void getAndSetReturnsZeroWhenKeyHasBeenRemoved() {
// setup double
RedisAtomicDouble test = new RedisAtomicDouble("test", factory, 1);
assertThat(test.get(), equalTo(1D)); // this passes
template.delete("test");
assertThat(test.getAndSet(2), is(0D));
}
}

View File

@@ -41,21 +41,31 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Integration test of {@link RedisAtomicInteger}
*
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
*/
@RunWith(Parameterized.class)
public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
private RedisAtomicInteger intCounter;
private RedisConnectionFactory factory;
private RedisTemplate<String, Integer> template;
public RedisAtomicIntegerTests(RedisConnectionFactory factory) {
intCounter = new RedisAtomicInteger(getClass().getSimpleName() + ":int", factory);
this.intCounter = new RedisAtomicInteger(getClass().getSimpleName() + ":int", factory);
this.factory = factory;
this.template = new RedisTemplate<String, Integer>();
this.template.setConnectionFactory(factory);
this.template.setKeySerializer(new StringRedisSerializer());
this.template.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
this.template.afterPropertiesSet();
ConnectionFactoryTracker.add(factory);
}
@@ -77,7 +87,7 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
}
@Test
public void testCheckAndSet() throws Exception {
public void testCheckAndSet() {
// Txs not supported in Jredis
assumeTrue(!ConnectionUtils.isJredis(factory));
intCounter.set(0);
@@ -87,7 +97,7 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
}
@Test
public void testIncrementAndGet() throws Exception {
public void testIncrementAndGet() {
intCounter.set(0);
assertEquals(1, intCounter.incrementAndGet());
}
@@ -100,11 +110,55 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
}
@Test
public void testDecrementAndGet() throws Exception {
public void testDecrementAndGet() {
intCounter.set(1);
assertEquals(0, intCounter.decrementAndGet());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndIncrement() {
intCounter.set(1);
assertEquals(1, intCounter.getAndIncrement());
assertEquals(2, intCounter.get());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndAdd() {
intCounter.set(1);
assertEquals(1, intCounter.getAndAdd(5));
assertEquals(6, intCounter.get());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndDecrement() {
intCounter.set(1);
assertEquals(1, intCounter.getAndDecrement());
assertEquals(0, intCounter.get());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndSet() {
intCounter.set(1);
assertEquals(1, intCounter.getAndSet(5));
assertEquals(5, intCounter.get());
}
@Test
@Ignore("DATAREDIS-108 Test is intermittently failing")
public void testCompareSet() throws Exception {
@@ -171,12 +225,6 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
@Test
public void testShouldBeAbleToUseRedisAtomicIntegerWithProperlyConfiguredRedisTemplate() {
RedisTemplate<String, Integer> template = new RedisTemplate<String, Integer>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
template.afterPropertiesSet();
RedisAtomicInteger ral = new RedisAtomicInteger("DATAREDIS-317.atomicInteger", template);
ral.set(32);
@@ -192,18 +240,27 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
expectedException.expect(DataRetrievalFailureException.class);
expectedException.expectMessage("'test' seems to no longer exist");
// setup long
// setup integer
RedisAtomicInteger test = new RedisAtomicInteger("test", factory, 1);
assertThat(test.get(), equalTo(1)); // this passes
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
template.afterPropertiesSet();
template.delete("test");
test.get();
}
/**
* @see DATAREDIS-469
*/
@Test
public void getAndSetReturnsZeroWhenKeyHasBeenRemoved() {
// setup integer
RedisAtomicInteger test = new RedisAtomicInteger("test", factory, 1);
assertThat(test.get(), equalTo(1)); // this passes
template.delete("test");
assertThat(test.getAndSet(2), is(0));
}
}

View File

@@ -38,7 +38,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Integration test of {@link RedisAtomicLong}
*
*
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
@@ -49,10 +49,19 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
private RedisAtomicLong longCounter;
private RedisConnectionFactory factory;
private RedisTemplate<String, Long> template;
public RedisAtomicLongTests(RedisConnectionFactory factory) {
longCounter = new RedisAtomicLong(getClass().getSimpleName() + ":long", factory);
this.longCounter = new RedisAtomicLong(getClass().getSimpleName() + ":long", factory);
this.factory = factory;
this.template = new RedisTemplate<String, Long>();
this.template.setConnectionFactory(factory);
this.template.setKeySerializer(new StringRedisSerializer());
this.template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
this.template.afterPropertiesSet();
ConnectionFactoryTracker.add(factory);
}
@@ -102,8 +111,53 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
assertEquals(0, longCounter.decrementAndGet());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndIncrement() {
longCounter.set(1);
assertEquals(1, longCounter.getAndIncrement());
assertEquals(2, longCounter.get());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndAdd() {
longCounter.set(1);
assertEquals(1, longCounter.getAndAdd(5));
assertEquals(6, longCounter.get());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndDecrement() {
longCounter.set(1);
assertEquals(1, longCounter.getAndDecrement());
assertEquals(0, longCounter.get());
}
/**
* @see DATAREDIS-469
*/
@Test
public void testGetAndSet() {
longCounter.set(1);
assertEquals(1, longCounter.getAndSet(5));
assertEquals(5, longCounter.get());
}
@Test
public void testGetExistingValue() throws Exception {
longCounter.set(5);
RedisAtomicLong keyCopy = new RedisAtomicLong(longCounter.getKey(), factory);
assertEquals(longCounter.get(), keyCopy.get());
@@ -167,14 +221,23 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
RedisAtomicLong test = new RedisAtomicLong("test", factory, 1);
assertThat(test.get(), equalTo(1L)); // this passes
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
template.afterPropertiesSet();
template.delete("test");
test.get();
}
/**
* @see DATAREDIS-469
*/
@Test
public void getAndSetReturnsZeroWhenKeyHasBeenRemoved() {
// setup long
RedisAtomicLong test = new RedisAtomicLong("test", factory, 1);
assertThat(test.get(), equalTo(1L)); // this passes
template.delete("test");
assertThat(test.getAndSet(2), is(0L));
}
}