|
16 | 16 |
|
17 | 17 | package org.springframework.data.redis.cache; |
18 | 18 |
|
19 | | -import static org.hamcrest.core.IsInstanceOf.*; |
20 | | -import static org.hamcrest.core.IsNull.*; |
| 19 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 20 | +import static org.hamcrest.core.IsInstanceOf.instanceOf; |
| 21 | +import static org.hamcrest.core.IsNot.not; |
| 22 | +import static org.hamcrest.core.IsNull.nullValue; |
| 23 | +import static org.hamcrest.core.IsSame.sameInstance; |
21 | 24 | import static org.junit.Assert.assertFalse; |
22 | 25 | import static org.junit.Assert.assertNotNull; |
23 | 26 | import static org.junit.Assert.assertNull; |
24 | 27 | import static org.junit.Assert.assertThat; |
25 | 28 | import static org.junit.Assert.assertTrue; |
26 | | -import static org.junit.Assume.*; |
| 29 | +import static org.junit.Assume.assumeThat; |
27 | 30 | import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual; |
28 | 31 |
|
29 | 32 | import java.util.Collection; |
@@ -71,7 +74,7 @@ public static Collection<Object[]> testParams() { |
71 | 74 | } |
72 | 75 |
|
73 | 76 | @SuppressWarnings("unchecked") |
74 | | -protected Cache createCache(RedisTemplate nativeCache) { |
| 77 | +protected RedisCache createCache(RedisTemplate nativeCache) { |
75 | 78 | return new RedisCache(CACHE_NAME, CACHE_NAME.concat(":").getBytes(), nativeCache, TimeUnit.MINUTES.toSeconds(10)); |
76 | 79 | } |
77 | 80 |
|
@@ -246,4 +249,31 @@ public void testCacheGetShouldReturnNullIfNoCachedValueFound() { |
246 | 249 | Object invalidKey = template.getKeySerializer() == null ? "spring-data-redis".getBytes() : "spring-data-redis"; |
247 | 250 | assertThat(redisCache.get(invalidKey, value.getClass()), nullValue()); |
248 | 251 | } |
| 252 | + |
| 253 | +/** |
| 254 | + * @see DATAREDIS-344 |
| 255 | + */ |
| 256 | +@Test |
| 257 | +public void putIfAbsentShouldSetValueOnlyIfNotPresent() { |
| 258 | + |
| 259 | +assumeThat(cache, instanceOf(RedisCache.class)); |
| 260 | + |
| 261 | +RedisCache redisCache = (RedisCache)cache; |
| 262 | + |
| 263 | +Object key = getKey(); |
| 264 | +template.delete(key); |
| 265 | + |
| 266 | +Object value = getValue(); |
| 267 | +ValueWrapper wrapper = redisCache.putIfAbsent(key, value); |
| 268 | + |
| 269 | +assertThat(wrapper.get(), sameInstance(value)); |
| 270 | + |
| 271 | +ValueWrapper wrapper2 = redisCache.putIfAbsent(key, value); |
| 272 | + |
| 273 | +if (!(value instanceof Number)) { |
| 274 | +assertThat(wrapper2.get(), not(sameInstance(value))); |
| 275 | +} |
| 276 | + |
| 277 | +assertThat(wrapper2.get(), equalTo(value)); |
| 278 | +} |
249 | 279 | } |
0 commit comments