diff --git a/build.gradle b/build.gradle index b6cf1abbb..90a594ae9 100644 --- a/build.gradle +++ b/build.gradle @@ -63,7 +63,7 @@ dependencies { testCompile "org.springframework:spring-test:$springVersion" testCompile "org.mockito:mockito-all:$mockitoVersion" testCompile("javax.annotation:jsr250-api:1.0", optional) - testCompile("com.thoughtworks.xstream:xstream:1.3", optional) + testCompile("com.thoughtworks.xstream:xstream:1.4.4", optional) } sourceCompatibility = 1.5 @@ -225,4 +225,4 @@ task wrapper(type: Wrapper) { } assemble.dependsOn = ['jar', 'sourcesJar'] -defaultTasks 'build' \ No newline at end of file +defaultTasks 'build' diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCache.java b/src/main/java/org/springframework/data/redis/cache/RedisCache.java index dde79da42..1c3ba0630 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 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. @@ -32,6 +32,7 @@ import org.springframework.util.Assert; * Cache implementation on top of Redis. * * @author Costin Leau + * @author Christoph Strobl */ @SuppressWarnings("unchecked") class RedisCache implements Cache { @@ -84,7 +85,6 @@ class RedisCache implements Cache { return template; } - public ValueWrapper get(final Object key) { return (ValueWrapper) template.execute(new RedisCallback() { @@ -96,6 +96,21 @@ class RedisCache implements Cache { } }, true); } + + /** + * Return the value to which this cache maps the specified key, generically specifying a type that return value will be cast to. + * + * @param key + * @param type + * @return + * + * @see DATAREDIS-243 + */ + public T get(Object key, Class type) { + + ValueWrapper wrapper = get(key); + return wrapper == null ? null : (T) wrapper.get(); + } public void put(final Object key, final Object value) { diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheTest.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheTest.java index 2277f72cc..6d099d5cd 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheTest.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 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. @@ -16,11 +16,14 @@ package org.springframework.data.redis.cache; +import static org.hamcrest.core.IsInstanceOf.*; +import static org.hamcrest.core.IsNull.*; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assume.*; import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual; import java.util.Collection; @@ -45,6 +48,7 @@ import org.springframework.data.redis.core.RedisTemplate; /** * @author Costin Leau * @author Jennifer Hickey + * @author Christoph Strobl */ @SuppressWarnings("rawtypes") @RunWith(Parameterized.class) @@ -184,4 +188,66 @@ public class RedisCacheTest extends AbstractNativeCacheTest { latch.await(); assertFalse(monitorStateException.get()); } + + /** + * @see DATAREDIS-243 + */ + @Test + public void testCacheGetShouldReturnCachedInstance() { + assumeThat(cache, instanceOf(RedisCache.class)); + + Object key = getKey(); + Object value = getValue(); + cache.put(key, value); + + assertThat(value, isEqual(((RedisCache)cache).get(key, Object.class))); + } + + /** + * @see DATAREDIS-243 + */ + @Test + public void testCacheGetShouldRetunInstanceOfCorrectType() { + assumeThat(cache, instanceOf(RedisCache.class)); + + Object key = getKey(); + Object value = getValue(); + cache.put(key, value); + + RedisCache redisCache = (RedisCache)cache; + assertThat(redisCache.get(key, value.getClass()), instanceOf(value.getClass())); + } + + /** + * @see DATAREDIS-243 + */ + @Test(expected = ClassCastException.class) + public void testCacheGetShouldThrowExceptionOnInvalidType() { + assumeThat(cache, instanceOf(RedisCache.class)); + + Object key = getKey(); + Object value = getValue(); + cache.put(key, value); + + RedisCache redisCache = (RedisCache)cache; + @SuppressWarnings("unused") + Cache retrievedObject = redisCache.get(key, Cache.class); + } + + /** + * @see DATAREDIS-243 + */ + @Test + public void testCacheGetShouldReturnNullIfNoCachedValueFound() { + assumeThat(cache, instanceOf(RedisCache.class)); + + Object key = getKey(); + Object value = getValue(); + cache.put(key, value); + + RedisCache redisCache = (RedisCache)cache; + + Object invalidKey = template.getKeySerializer() == null ? "spring-data-redis".getBytes() : "spring-data-redis"; + assertThat(redisCache.get(invalidKey, value.getClass()), nullValue()); + } }