From 14bf8f13b1ccf04257b7a2df0ba99a79e0ad8e3c Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Mon, 13 Jan 2014 22:42:57 +0100 Subject: [PATCH] DATAREDIS-243 - Ensure compatibility with Spring Framework 4.0. Updated dependency of XStream since Spring framework 4.0 requires a newer version. Spring 4 added another method to get cached object to the Cache interface. The method has been added to RedisCache in order to provide compatibility, while calling get internally and casting values to required type. --- build.gradle | 4 +- .../data/redis/cache/RedisCache.java | 19 +++++- .../data/redis/cache/RedisCacheTest.java | 68 ++++++++++++++++++- 3 files changed, 86 insertions(+), 5 deletions(-) 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()); + } }