Skip to content

Commit 20cfd3e

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-469 - Throw DataRetrievalFailureException for atomic numbers when key is removed.
We now throw DataRetrievalFailureException in case of get() when the underlying key storing the value of an AtomicInteger, AtomicLong or AtomicDouble is removed from Redis. Original pull request: #182.
1 parent d097a64 commit 20cfd3e

File tree

6 files changed

+110
-8
lines changed

6 files changed

+110
-8
lines changed

src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicDouble.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Date;
2121
import java.util.concurrent.TimeUnit;
2222

23+
import org.springframework.dao.DataRetrievalFailureException;
2324
import org.springframework.data.redis.connection.DataType;
2425
import org.springframework.data.redis.connection.RedisConnectionFactory;
2526
import org.springframework.data.redis.core.BoundKeyOperations;
@@ -38,6 +39,7 @@
3839
*
3940
* @author Jennifer Hickey
4041
* @author Thomas Darimont
42+
* @author Christoph Strobl
4143
*/
4244
public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations<String> {
4345

@@ -143,7 +145,13 @@ private RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> t
143145
* @return the current value
144146
*/
145147
public double get() {
146-
return operations.get(key);
148+
149+
Double value = operations.get(key);
150+
if (value != null) {
151+
return value.doubleValue();
152+
}
153+
154+
throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
147155
}
148156

149157
/**

src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicInteger.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Date;
2121
import java.util.concurrent.TimeUnit;
2222

23+
import org.springframework.dao.DataRetrievalFailureException;
2324
import org.springframework.data.redis.connection.DataType;
2425
import org.springframework.data.redis.connection.RedisConnectionFactory;
2526
import org.springframework.data.redis.core.BoundKeyOperations;
@@ -39,6 +40,7 @@
3940
* @see java.util.concurrent.atomic.AtomicInteger
4041
* @author Costin Leau
4142
* @author Thomas Darimont
43+
* @author Christoph Strobl
4244
*/
4345
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {
4446

@@ -141,7 +143,13 @@ private RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer>
141143
* @return the current value
142144
*/
143145
public int get() {
144-
return Integer.valueOf(operations.get(key));
146+
147+
Integer value = operations.get(key);
148+
if (value != null) {
149+
return value.intValue();
150+
}
151+
152+
throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
145153
}
146154

147155
/**

src/main/java/org/springframework/data/redis/support/atomic/RedisAtomicLong.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Date;
2121
import java.util.concurrent.TimeUnit;
2222

23+
import org.springframework.dao.DataRetrievalFailureException;
2324
import org.springframework.data.redis.connection.DataType;
2425
import org.springframework.data.redis.connection.RedisConnectionFactory;
2526
import org.springframework.data.redis.core.BoundKeyOperations;
@@ -39,6 +40,7 @@
3940
* @see java.util.concurrent.atomic.AtomicLong
4041
* @author Costin Leau
4142
* @author Thomas Darimont
43+
* @author Christoph Strobl
4244
*/
4345
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> {
4446

@@ -149,7 +151,13 @@ private RedisAtomicLong(String redisCounter, RedisOperations<String, Long> templ
149151
* @return the current value
150152
*/
151153
public long get() {
152-
return operations.get(key);
154+
155+
Long value = operations.get(key);
156+
if (value != null) {
157+
return value.longValue();
158+
}
159+
160+
throw new DataRetrievalFailureException(String.format("The key '%s' seems to no longer exist.", key));
153161
}
154162

155163
/**

src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicDoubleTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.junit.runner.RunWith;
3131
import org.junit.runners.Parameterized;
3232
import org.junit.runners.Parameterized.Parameters;
33+
import org.springframework.dao.DataRetrievalFailureException;
3334
import org.springframework.data.redis.ConnectionFactoryTracker;
3435
import org.springframework.data.redis.RedisTestProfileValueSource;
3536
import org.springframework.data.redis.connection.ConnectionUtils;
@@ -44,6 +45,7 @@
4445
*
4546
* @author Jennifer Hickey
4647
* @author Thomas Darimont
48+
* @author Christoph Strobl
4749
*/
4850
@RunWith(Parameterized.class)
4951
public class RedisAtomicDoubleTests extends AbstractRedisAtomicsTests {
@@ -210,4 +212,28 @@ public void testShouldBeAbleToUseRedisAtomicDoubleWithProperlyConfiguredRedisTem
210212

211213
assertThat(ral.get(), is(32.23));
212214
}
215+
216+
/**
217+
* @see DATAREDIS-469
218+
*/
219+
@Test
220+
public void getThrowsExceptionWhenKeyHasBeenRemoved() {
221+
222+
expectedException.expect(DataRetrievalFailureException.class);
223+
expectedException.expectMessage("'test' seems to no longer exist");
224+
225+
// setup long
226+
RedisAtomicDouble test = new RedisAtomicDouble("test", factory, 1);
227+
assertThat(test.get(), equalTo(1D)); // this passes
228+
229+
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
230+
template.setConnectionFactory(factory);
231+
template.setKeySerializer(new StringRedisSerializer());
232+
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
233+
template.afterPropertiesSet();
234+
235+
template.delete("test");
236+
237+
test.get();
238+
}
213239
}

src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicIntegerTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.junit.runner.RunWith;
3131
import org.junit.runners.Parameterized;
3232
import org.junit.runners.Parameterized.Parameters;
33+
import org.springframework.dao.DataRetrievalFailureException;
3334
import org.springframework.data.redis.ConnectionFactoryTracker;
3435
import org.springframework.data.redis.connection.ConnectionUtils;
3536
import org.springframework.data.redis.connection.RedisConnection;
@@ -44,6 +45,7 @@
4445
* @author Costin Leau
4546
* @author Jennifer Hickey
4647
* @author Thomas Darimont
48+
* @author Christoph Strobl
4749
*/
4850
@RunWith(Parameterized.class)
4951
public class RedisAtomicIntegerTests extends AbstractRedisAtomicsTests {
@@ -180,4 +182,28 @@ public void testShouldBeAbleToUseRedisAtomicIntegerWithProperlyConfiguredRedisTe
180182

181183
assertThat(ral.get(), is(32));
182184
}
185+
186+
/**
187+
* @see DATAREDIS-469
188+
*/
189+
@Test
190+
public void getThrowsExceptionWhenKeyHasBeenRemoved() {
191+
192+
expectedException.expect(DataRetrievalFailureException.class);
193+
expectedException.expectMessage("'test' seems to no longer exist");
194+
195+
// setup long
196+
RedisAtomicInteger test = new RedisAtomicInteger("test", factory, 1);
197+
assertThat(test.get(), equalTo(1)); // this passes
198+
199+
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
200+
template.setConnectionFactory(factory);
201+
template.setKeySerializer(new StringRedisSerializer());
202+
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
203+
template.afterPropertiesSet();
204+
205+
template.delete("test");
206+
207+
test.get();
208+
}
183209
}

src/test/java/org/springframework/data/redis/support/atomic/RedisAtomicLongTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.runner.RunWith;
2828
import org.junit.runners.Parameterized;
2929
import org.junit.runners.Parameterized.Parameters;
30+
import org.springframework.dao.DataRetrievalFailureException;
3031
import org.springframework.data.redis.ConnectionFactoryTracker;
3132
import org.springframework.data.redis.connection.ConnectionUtils;
3233
import org.springframework.data.redis.connection.RedisConnection;
@@ -41,6 +42,7 @@
4142
* @author Costin Leau
4243
* @author Jennifer Hickey
4344
* @author Thomas Darimont
45+
* @author Christoph Strobl
4446
*/
4547
@RunWith(Parameterized.class)
4648
public class RedisAtomicLongTests extends AbstractRedisAtomicsTests {
@@ -151,4 +153,28 @@ public void testShouldBeAbleToUseRedisAtomicLongWithProperlyConfiguredRedisTempl
151153

152154
assertThat(ral.get(), is(32L));
153155
}
156+
157+
/**
158+
* @see DATAREDIS-469
159+
*/
160+
@Test
161+
public void getThrowsExceptionWhenKeyHasBeenRemoved() {
162+
163+
expectedException.expect(DataRetrievalFailureException.class);
164+
expectedException.expectMessage("'test' seems to no longer exist");
165+
166+
// setup long
167+
RedisAtomicLong test = new RedisAtomicLong("test", factory, 1);
168+
assertThat(test.get(), equalTo(1L)); // this passes
169+
170+
RedisTemplate<String, Long> template = new RedisTemplate<String, Long>();
171+
template.setConnectionFactory(factory);
172+
template.setKeySerializer(new StringRedisSerializer());
173+
template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
174+
template.afterPropertiesSet();
175+
176+
template.delete("test");
177+
178+
test.get();
179+
}
154180
}

0 commit comments

Comments
 (0)