SPR-8082
SPR-7833
+ add support for CacheDefinitions declarations inside XML
+ more integration tests
This commit is contained in:
Costin Leau
2011-11-09 17:53:51 +00:00
parent e4c88553d8
commit dc88a7c8ba
20 changed files with 497 additions and 175 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.cache.config;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.UUID;
import org.junit.Before;
@@ -51,6 +52,10 @@ public abstract class AbstractAnnotationTests {
cs = ctx.getBean("service", CacheableService.class);
ccs = ctx.getBean("classService", CacheableService.class);
cm = ctx.getBean(CacheManager.class);
Collection<String> cn = cm.getCacheNames();
assertTrue(cn.contains("default"));
assertTrue(cn.contains("secondary"));
assertTrue(cn.contains("primary"));
}
public void testCacheable(CacheableService service) throws Exception {
@@ -201,6 +206,124 @@ public abstract class AbstractAnnotationTests {
assertEquals(three, Integer.valueOf(cache.get(three).get().toString()));
}
public void testMultiCache(CacheableService service) {
Object o1 = new Object();
Object o2 = new Object();
Cache primary = cm.getCache("primary");
Cache secondary = cm.getCache("secondary");
assertNull(primary.get(o1));
assertNull(secondary.get(o1));
Object r1 = service.multiCache(o1);
assertSame(r1, primary.get(o1).get());
assertSame(r1, secondary.get(o1).get());
Object r2 = service.multiCache(o1);
Object r3 = service.multiCache(o1);
assertSame(r1, r2);
assertSame(r1, r3);
assertNull(primary.get(o2));
assertNull(secondary.get(o2));
Object r4 = service.multiCache(o2);
assertSame(r4, primary.get(o2).get());
assertSame(r4, secondary.get(o2).get());
}
public void testMultiEvict(CacheableService service) {
Object o1 = new Object();
Object r1 = service.multiCache(o1);
Object r2 = service.multiCache(o1);
Cache primary = cm.getCache("primary");
Cache secondary = cm.getCache("secondary");
assertSame(r1, r2);
assertSame(r1, primary.get(o1).get());
assertSame(r1, secondary.get(o1).get());
service.multiEvict(o1);
assertNull(primary.get(o1));
assertNull(secondary.get(o1));
Object r3 = service.multiCache(o1);
Object r4 = service.multiCache(o1);
assertNotSame(r1, r3);
assertSame(r3, r4);
assertSame(r3, primary.get(o1).get());
assertSame(r4, secondary.get(o1).get());
}
public void testMultiPut(CacheableService service) {
Object o = Integer.valueOf(1);
Cache primary = cm.getCache("primary");
Cache secondary = cm.getCache("secondary");
assertNull(primary.get(o));
assertNull(secondary.get(o));
Object r1 = service.multiUpdate(o);
assertSame(r1, primary.get(o).get());
assertSame(r1, secondary.get(o).get());
o = Integer.valueOf(2);
assertNull(primary.get(o));
assertNull(secondary.get(o));
Object r2 = service.multiUpdate(o);
assertSame(r2, primary.get(o).get());
assertSame(r2, secondary.get(o).get());
}
public void testMultiCacheAndEvict(CacheableService service) {
String methodName = "multiCacheAndEvict";
Cache primary = cm.getCache("primary");
Cache secondary = cm.getCache("secondary");
Object key = Integer.valueOf(1);
secondary.put(key, key);
assertNull(secondary.get(methodName));
assertSame(key, secondary.get(key).get());
Object r1 = service.multiCacheAndEvict(key);
assertSame(r1, service.multiCacheAndEvict(key));
// assert the method name is used
assertSame(r1, primary.get(methodName).get());
assertNull(secondary.get(methodName));
assertNull(secondary.get(key));
}
public void testMultiConditionalCacheAndEvict(CacheableService service) {
Cache primary = cm.getCache("primary");
Cache secondary = cm.getCache("secondary");
Object key = Integer.valueOf(1);
secondary.put(key, key);
assertNull(primary.get(key));
assertSame(key, secondary.get(key).get());
Object r1 = service.multiConditionalCacheAndEvict(key);
Object r3 = service.multiConditionalCacheAndEvict(key);
assertTrue(!r1.equals(r3));
assertNull(primary.get(key));
Object key2 = Integer.valueOf(3);
Object r2 = service.multiConditionalCacheAndEvict(key2);
assertSame(r2, service.multiConditionalCacheAndEvict(key2));
// assert the method name is used
assertSame(r2, primary.get(key2).get());
assertNull(secondary.get(key2));
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
@@ -329,4 +452,54 @@ public abstract class AbstractAnnotationTests {
public void testClassConditionalUpdate() {
testConditionalCacheUpdate(ccs);
}
@Test
public void testMultiCache() {
testMultiCache(cs);
}
@Test
public void testClassMultiCache() {
testMultiCache(ccs);
}
@Test
public void testMultiEvict() {
testMultiEvict(cs);
}
@Test
public void testClassMultiEvict() {
testMultiEvict(ccs);
}
@Test
public void testMultiPut() {
testMultiPut(cs);
}
@Test
public void testClassMultiPut() {
testMultiPut(ccs);
}
@Test
public void testMultiCacheAndEvict() {
testMultiCacheAndEvict(cs);
}
@Test
public void testClassMultiCacheAndEvict() {
testMultiCacheAndEvict(ccs);
}
@Test
public void testMultiConditionalCacheAndEvict() {
testMultiConditionalCacheAndEvict(cs);
}
@Test
public void testClassMultiConditionalCacheAndEvict() {
testMultiConditionalCacheAndEvict(ccs);
}
}

View File

@@ -18,8 +18,9 @@ package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheDefinitions;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CacheUpdate;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
/**
@@ -62,12 +63,12 @@ public class AnnotatedClassCacheableService implements CacheableService {
return counter.getAndIncrement();
}
@CacheUpdate("default")
@CachePut("default")
public Object update(Object arg1) {
return counter.getAndIncrement();
}
@CacheUpdate(value = "default", condition = "#arg.equals(3)")
@CachePut(value = "default", condition = "#arg.equals(3)")
public Object conditionalUpdate(Object arg) {
return arg;
}
@@ -88,4 +89,31 @@ public class AnnotatedClassCacheableService implements CacheableService {
public Long throwUnchecked(Object arg1) {
throw new UnsupportedOperationException();
}
}
// multi annotations
@CacheDefinitions(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
public Object multiCache(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0") })
public Object multiEvict(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
public Object multiCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
public Object multiConditionalCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(put = { @CachePut("primary"), @CachePut("secondary") })
public Object multiUpdate(Object arg1) {
return arg1;
}
}

View File

@@ -24,7 +24,7 @@ import org.springframework.cache.interceptor.CacheInterceptor;
/**
* @author Costin Leau
*/
public abstract class AbstractCacheAdviceNamespaceTests extends AbstractAnnotationTests {
public class CacheAdviceNamespaceTests extends AbstractAnnotationTests {
@Override

View File

@@ -50,4 +50,14 @@ public interface CacheableService<T> {
T throwUnchecked(Object arg1);
// multi annotations
T multiCache(Object arg1);
T multiEvict(Object arg1);
T multiCacheAndEvict(Object arg1);
T multiConditionalCacheAndEvict(Object arg1);
T multiUpdate(Object arg1);
}

View File

@@ -18,8 +18,9 @@ package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheDefinitions;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CacheUpdate;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
/**
@@ -65,12 +66,12 @@ public class DefaultCacheableService implements CacheableService<Long> {
return counter.getAndIncrement();
}
@CacheUpdate("default")
@CachePut("default")
public Long update(Object arg1) {
return counter.getAndIncrement();
}
@CacheUpdate(value = "default", condition = "#arg.equals(3)")
@CachePut(value = "default", condition = "#arg.equals(3)")
public Long conditionalUpdate(Object arg) {
return Long.valueOf(arg.toString());
}
@@ -94,4 +95,31 @@ public class DefaultCacheableService implements CacheableService<Long> {
public Long throwUnchecked(Object arg1) {
throw new UnsupportedOperationException(arg1.toString());
}
// multi annotations
@CacheDefinitions(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
public Long multiCache(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(evict = { @CacheEvict("primary"), @CacheEvict(value = "secondary", key = "#p0") })
public Long multiEvict(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(cacheable = { @Cacheable(value = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
public Long multiCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(cacheable = { @Cacheable(value = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
public Long multiConditionalCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
}
@CacheDefinitions(put = { @CachePut("primary"), @CachePut("secondary") })
public Long multiUpdate(Object arg1) {
return Long.valueOf(arg1.toString());
}
}