DATAREDIS-72

+ Spring 3.1 cache abstraction for Redis
This commit is contained in:
Costin Leau
2011-07-15 18:05:28 +03:00
parent 1a87f45947
commit 63b1e862b7
10 changed files with 441 additions and 42 deletions

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.cache;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cache.Cache;
/**
* Test for native cache implementations.
*
* @author Costin Leau
*/
public abstract class AbstractNativeCacheTest<T> {
private T nativeCache;
private Cache cache;
protected final static String CACHE_NAME = "testCache";
@Before
public void setUp() throws Exception {
nativeCache = createNativeCache();
cache = createCache(nativeCache);
cache.clear();
}
protected abstract T createNativeCache() throws Exception;
protected abstract Cache createCache(T nativeCache);
protected abstract Object getObject();
@Test
public void testCacheName() throws Exception {
assertEquals(CACHE_NAME, cache.getName());
}
@Test
public void testNativeCache() throws Exception {
assertSame(nativeCache, cache.getNativeCache());
}
@Test
public void testCachePut() throws Exception {
Object key = getObject();
Object value = getObject();
assertNull(cache.get(key));
cache.put(key, value);
assertEquals(value, cache.get(key).get());
}
@Test
public void testCacheClear() throws Exception {
Object key1 = getObject();
Object value1 = getObject();
Object key2 = getObject();
Object value2 = getObject();
assertNull(cache.get(key1));
cache.put(key1, value1);
assertNull(cache.get(key2));
cache.put(key2, value2);
cache.clear();
assertNull(cache.get(key2));
assertNull(cache.get(key1));
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.cache;
import java.util.Collection;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.cache.Cache;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.collections.CollectionTestParams;
import org.springframework.data.redis.support.collections.ObjectFactory;
/**
* @author Costin Leau
*/
@RunWith(Parameterized.class)
public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
private ObjectFactory<Object> objFactory;
private RedisTemplate template;
public RedisCacheTest(ObjectFactory<Object> objFactory, RedisTemplate template) {
this.objFactory = objFactory;
this.template = template;
ConnectionFactoryTracker.add(template.getConnectionFactory());
}
@Parameters
public static Collection<Object[]> testParams() {
return CollectionTestParams.testParams();
}
@Override
protected Cache createCache(RedisTemplate nativeCache) {
return new RedisCache(CACHE_NAME, nativeCache);
}
@Override
protected RedisTemplate createNativeCache() throws Exception {
return template;
}
@Before
public void setUp() throws Exception {
ConnectionFactoryTracker.add(template.getConnectionFactory());
super.setUp();
}
@AfterClass
public static void cleanUp() {
ConnectionFactoryTracker.cleanUp();
}
@Override
protected Object getObject() {
return objFactory.instance();
}
}

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.data.redis.core;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
@@ -25,7 +25,6 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.collections.CollectionTestParams;
import org.springframework.data.redis.support.collections.ObjectFactory;