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.
This commit is contained in:
@@ -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'
|
||||
defaultTasks 'build'
|
||||
|
||||
@@ -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<ValueWrapper>() {
|
||||
|
||||
@@ -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> T get(Object key, Class<T> type) {
|
||||
|
||||
ValueWrapper wrapper = get(key);
|
||||
return wrapper == null ? null : (T) wrapper.get();
|
||||
}
|
||||
|
||||
|
||||
public void put(final Object key, final Object value) {
|
||||
|
||||
@@ -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<RedisTemplate> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user