diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java index 992cd1eeb..568475257 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2016 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. @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.Date; import java.util.concurrent.TimeUnit; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.BoundKeyOperations; @@ -38,6 +39,7 @@ import org.springframework.util.Assert; * * @author Jennifer Hickey * @author Thomas Darimont + * @author Christoph Strobl */ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations { @@ -143,7 +145,13 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO * @return the current value */ public double get() { - return operations.get(key); + + Double value = operations.get(key); + if (value != null) { + return value.doubleValue(); + } + + throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key)); } /** diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java index 224460aac..03c841e26 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2014 the original author or authors. + * Copyright 2011-2016 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. @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.Date; import java.util.concurrent.TimeUnit; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.BoundKeyOperations; @@ -39,6 +40,7 @@ import org.springframework.util.Assert; * @see java.util.concurrent.atomic.AtomicInteger * @author Costin Leau * @author Thomas Darimont + * @author Christoph Strobl */ public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations { @@ -141,7 +143,13 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey * @return the current value */ public int get() { - return Integer.valueOf(operations.get(key)); + + Integer value = operations.get(key); + if (value != null) { + return value.intValue(); + } + + throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key)); } /** diff --git a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java index a2e738f36..b3b54c54d 100644 --- a/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java +++ b/src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2014 the original author or authors. + * Copyright 2011-2016 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. @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.Date; import java.util.concurrent.TimeUnit; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.BoundKeyOperations; @@ -39,6 +40,7 @@ import org.springframework.util.Assert; * @see java.util.concurrent.atomic.AtomicLong * @author Costin Leau * @author Thomas Darimont + * @author Christoph Strobl */ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations { @@ -149,7 +151,13 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe * @return the current value */ public long get() { - return operations.get(key); + + Long value = operations.get(key); + if (value != null) { + return value.longValue(); + } + + throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key)); } /** diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java index 09d1bf7ca..dd6c1fd72 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2016 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. @@ -30,6 +30,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.RedisTestProfileValueSource; import org.springframework.data.redis.connection.ConnectionUtils; @@ -44,6 +45,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; * * @author Jennifer Hickey * @author Thomas Darimont + * @author Christoph Strobl */ @RunWith(Parameterized.class) public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests { @@ -210,4 +212,28 @@ public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests { assertThat(ral.get(), is(32.23)); } + + /** + * @see DATAREDIS-469 + */ + @Test + public void getThrowsExceptionWhenKeyHasBeenRemoved() { + + expectedException.expect(DataRetrievalFailureException.class); + expectedException.expectMessage("'test' seems to no longer exist"); + + // setup long + RedisAtomicDouble test = new RedisAtomicDouble("test", factory, 1); + assertThat(test.get(), equalTo(1D)); // this passes + + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(factory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericToStringSerializer(Long.class)); + template.afterPropertiesSet(); + + template.delete("test"); + + test.get(); + } } diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java index bd28071eb..400bff906 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2016 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. @@ -30,6 +30,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.connection.ConnectionUtils; import org.springframework.data.redis.connection.RedisConnection; @@ -44,6 +45,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; * @author Costin Leau * @author Jennifer Hickey * @author Thomas Darimont + * @author Christoph Strobl */ @RunWith(Parameterized.class) public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests { @@ -180,4 +182,28 @@ public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests { assertThat(ral.get(), is(32)); } + + /** + * @see DATAREDIS-469 + */ + @Test + public void getThrowsExceptionWhenKeyHasBeenRemoved() { + + expectedException.expect(DataRetrievalFailureException.class); + expectedException.expectMessage("'test' seems to no longer exist"); + + // setup long + RedisAtomicInteger test = new RedisAtomicInteger("test", factory, 1); + assertThat(test.get(), equalTo(1)); // this passes + + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(factory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericToStringSerializer(Long.class)); + template.afterPropertiesSet(); + + template.delete("test"); + + test.get(); + } } diff --git a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java index aeee29e86..d19751c27 100644 --- a/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java +++ b/src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java @@ -27,6 +27,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import org.springframework.dao.DataRetrievalFailureException; import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.connection.ConnectionUtils; import org.springframework.data.redis.connection.RedisConnection; @@ -41,6 +42,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; * @author Costin Leau * @author Jennifer Hickey * @author Thomas Darimont + * @author Christoph Strobl */ @RunWith(Parameterized.class) public class RedisAtomicLongTests extends AbstractRedisAtomicsTests { @@ -151,4 +153,28 @@ public class RedisAtomicLongTests extends AbstractRedisAtomicsTests { assertThat(ral.get(), is(32L)); } + + /** + * @see DATAREDIS-469 + */ + @Test + public void getThrowsExceptionWhenKeyHasBeenRemoved() { + + expectedException.expect(DataRetrievalFailureException.class); + expectedException.expectMessage("'test' seems to no longer exist"); + + // setup long + RedisAtomicLong test = new RedisAtomicLong("test", factory, 1); + assertThat(test.get(), equalTo(1L)); // this passes + + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(factory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericToStringSerializer(Long.class)); + template.afterPropertiesSet(); + + template.delete("test"); + + test.get(); + } }