提交 8aa0b077 编写于 作者: J Juergen Hoeller

Consistent early eviction tests in regular and transactional scenarios

See gh-23192
上级 be65bef9
......@@ -38,7 +38,7 @@ public class AspectJCacheAnnotationTests extends AbstractCacheAnnotationTests {
}
@Test
public void testKeyStrategy() throws Exception {
public void testKeyStrategy() {
AnnotationCacheAspect aspect = ctx.getBean(
"org.springframework.cache.config.internalCacheAspect", AnnotationCacheAspect.class);
assertThat(aspect.getKeyGenerator()).isSameAs(ctx.getBean("keyGenerator"));
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
......@@ -33,11 +33,13 @@ import org.springframework.cache.annotation.Caching;
public class AnnotatedClassCacheableService implements CacheableService<Object> {
private final AtomicLong counter = new AtomicLong();
public static final AtomicLong nullInvocations = new AtomicLong();
@Override
public Object cache(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......@@ -48,7 +50,7 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
@Override
@Cacheable(cacheNames = "testCache", sync = true)
public Object cacheSync(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......@@ -68,13 +70,14 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
}
@Override
@Cacheable(cacheNames = "testCache", unless = "#result > 10")
public Object unless(int arg) {
return arg;
}
@Override
@CacheEvict("testCache")
public void invalidate(Object arg1) {
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
}
@Override
......@@ -83,11 +86,6 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
throw new RuntimeException("exception thrown - evict should NOT occur");
}
@Override
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", beforeInvocation = true)
public void evictEarly(Object arg1) {
......@@ -95,68 +93,68 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0", beforeInvocation = true)
public void invalidateEarly(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true, beforeInvocation = true)
public void evictAllEarly(Object arg1) {
throw new RuntimeException("exception thrown - evict should still occur");
}
@Override
@Cacheable(cacheNames = "testCache", key = "#p0")
public Object key(Object arg1, Object arg2) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable("testCache")
public Object varArgsKey(Object... args) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.caches[0].name")
public Object name(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
public Object rootVars(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", keyGenerator = "customKyeGenerator")
public Object customKeyGenerator(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", keyGenerator = "unknownBeanName")
public Object unknownCustomKeyGenerator(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", cacheManager = "customCacheManager")
public Object customCacheManager(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", cacheManager = "unknownBeanName")
public Object unknownCustomCacheManager(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@CachePut("testCache")
public Object update(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......@@ -203,25 +201,25 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
@Override
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
public Object multiCache(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(cacheNames = "secondary", key = "#a0"), @CacheEvict(cacheNames = "primary", key = "#p0 + 'A'") })
public Object multiEvict(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Caching(cacheable = { @Cacheable(cacheNames = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
public Object multiCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Caching(cacheable = { @Cacheable(cacheNames = "primary", condition = "#a0 == 3") }, evict = { @CacheEvict("secondary") })
public Object multiConditionalCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
......@@ -17,7 +17,7 @@
package org.springframework.cache.config;
/**
* Basic service interface.
* Basic service interface for caching tests.
*
* @author Costin Leau
* @author Phillip Webb
......@@ -33,17 +33,15 @@ public interface CacheableService<T> {
T cacheSyncNull(Object arg1);
void invalidate(Object arg1);
void evict(Object arg1, Object arg2);
void evictWithException(Object arg1);
void evictEarly(Object arg1);
void evictAll(Object arg1);
void evictWithException(Object arg1);
void evict(Object arg1, Object arg2);
void invalidateEarly(Object arg1, Object arg2);
void evictAllEarly(Object arg1);
T conditional(int field);
......@@ -83,7 +81,6 @@ public interface CacheableService<T> {
T throwUncheckedSync(Object arg1);
// multi annotations
T multiCache(Object arg1);
T multiEvict(Object arg1);
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
......@@ -25,7 +25,7 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
/**
* Simple cacheable service
* Simple cacheable service.
*
* @author Costin Leau
* @author Phillip Webb
......@@ -34,12 +34,14 @@ import org.springframework.cache.annotation.Caching;
public class DefaultCacheableService implements CacheableService<Long> {
private final AtomicLong counter = new AtomicLong();
private final AtomicLong nullInvocations = new AtomicLong();
@Override
@Cacheable("testCache")
public Long cache(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......@@ -51,7 +53,7 @@ public class DefaultCacheableService implements CacheableService<Long> {
@Override
@Cacheable(cacheNames = "testCache", sync = true)
public Long cacheSync(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......@@ -61,8 +63,8 @@ public class DefaultCacheableService implements CacheableService<Long> {
}
@Override
@CacheEvict("testCache")
public void invalidate(Object arg1) {
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
}
@Override
......@@ -71,11 +73,6 @@ public class DefaultCacheableService implements CacheableService<Long> {
throw new RuntimeException("exception thrown - evict should NOT occur");
}
@Override
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", beforeInvocation = true)
public void evictEarly(Object arg1) {
......@@ -83,26 +80,26 @@ public class DefaultCacheableService implements CacheableService<Long> {
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0", beforeInvocation = true)
public void invalidateEarly(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true, beforeInvocation = true)
public void evictAllEarly(Object arg1) {
throw new RuntimeException("exception thrown - evict should still occur");
}
@Override
@Cacheable(cacheNames = "testCache", condition = "#p0 == 3")
public Long conditional(int classField) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", sync = true, condition = "#p0 == 3")
public Long conditionalSync(int field) {
return counter.getAndIncrement();
public Long conditionalSync(int classField) {
return this.counter.getAndIncrement();
}
@Override
......@@ -114,55 +111,55 @@ public class DefaultCacheableService implements CacheableService<Long> {
@Override
@Cacheable(cacheNames = "testCache", key = "#p0")
public Long key(Object arg1, Object arg2) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache")
public Long varArgsKey(Object... args) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName")
public Long name(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
public Long rootVars(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", keyGenerator = "customKeyGenerator")
public Long customKeyGenerator(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", keyGenerator = "unknownBeanName")
public Long unknownCustomKeyGenerator(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", cacheManager = "customCacheManager")
public Long customCacheManager(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Cacheable(cacheNames = "testCache", cacheManager = "unknownBeanName")
public Long unknownCustomCacheManager(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@CachePut("testCache")
public Long update(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......@@ -174,13 +171,13 @@ public class DefaultCacheableService implements CacheableService<Long> {
@Override
@Cacheable("testCache")
public Long nullValue(Object arg1) {
nullInvocations.incrementAndGet();
this.nullInvocations.incrementAndGet();
return null;
}
@Override
public Number nullInvocations() {
return nullInvocations.get();
return this.nullInvocations.get();
}
@Override
......@@ -212,25 +209,25 @@ public class DefaultCacheableService implements CacheableService<Long> {
@Override
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
public Long multiCache(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(cacheNames = "secondary", key = "#p0"), @CacheEvict(cacheNames = "primary", key = "#p0 + 'A'") })
public Long multiEvict(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Caching(cacheable = { @Cacheable(cacheNames = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
public Long multiCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
@Caching(cacheable = { @Cacheable(cacheNames = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
public Long multiConditionalCacheAndEvict(Object arg1) {
return counter.getAndIncrement();
return this.counter.getAndIncrement();
}
@Override
......
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
......@@ -39,12 +39,17 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.tests.transaction.CallCountingTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
/**
* @author Stephane Nicoll
* @author Juergen Hoeller
*/
public class JCacheEhCacheAnnotationTests extends AbstractCacheAnnotationTests {
private final TransactionTemplate txTemplate = new TransactionTemplate(new CallCountingTransactionManager());
private CacheManager jCacheManager;
......@@ -76,6 +81,26 @@ public class JCacheEhCacheAnnotationTests extends AbstractCacheAnnotationTests {
public void testCustomCacheManager() {
}
@Test
public void testEvictWithTransaction() {
txTemplate.execute(() -> testEvict(this.cs, false));
}
@Test
public void testEvictEarlyWithTransaction() {
txTemplate.execute(() -> testEvictEarly(this.cs));
}
@Test
public void testEvictAllWithTransaction() {
txTemplate.execute(() -> testEvictAll(this.cs, false));
}
@Test
public void testEvictAllEarlyWithTransaction() {
txTemplate.execute(() -> testEvictAllEarly(this.cs));
}
@Configuration
@EnableCaching
......@@ -87,7 +112,9 @@ public class JCacheEhCacheAnnotationTests extends AbstractCacheAnnotationTests {
@Override
@Bean
public org.springframework.cache.CacheManager cacheManager() {
return new JCacheCacheManager(jCacheManager());
JCacheCacheManager cm = new JCacheCacheManager(jCacheManager());
cm.setTransactionAware(true);
return cm;
}
@Bean
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
......@@ -36,6 +36,7 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
public static final AtomicLong nullInvocations = new AtomicLong();
@Override
public Object cache(Object arg1) {
return this.counter.getAndIncrement();
......@@ -75,8 +76,8 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
}
@Override
@CacheEvict("testCache")
public void invalidate(Object arg1) {
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
}
@Override
......@@ -85,11 +86,6 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
throw new RuntimeException("exception thrown - evict should NOT occur");
}
@Override
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", beforeInvocation = true)
public void evictEarly(Object arg1) {
......@@ -97,13 +93,13 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0", beforeInvocation = true)
public void invalidateEarly(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true, beforeInvocation = true)
public void evictAllEarly(Object arg1) {
throw new RuntimeException("exception thrown - evict should still occur");
}
......@@ -238,4 +234,5 @@ public class AnnotatedClassCacheableService implements CacheableService<Object>
arg1.setId(Long.MIN_VALUE);
return arg1;
}
}
......@@ -37,7 +37,7 @@ public class CacheAdviceNamespaceTests extends AbstractCacheAnnotationTests {
}
@Test
public void testKeyStrategy() throws Exception {
public void testKeyStrategy() {
CacheInterceptor bean = this.ctx.getBean("cacheAdviceClass", CacheInterceptor.class);
assertThat(bean.getKeyGenerator()).isSameAs(this.ctx.getBean("keyGenerator"));
}
......
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
......@@ -33,17 +33,15 @@ public interface CacheableService<T> {
T cacheSyncNull(Object arg1);
void invalidate(Object arg1);
void evict(Object arg1, Object arg2);
void evictWithException(Object arg1);
void evictEarly(Object arg1);
void evictAll(Object arg1);
void evictWithException(Object arg1);
void evict(Object arg1, Object arg2);
void invalidateEarly(Object arg1, Object arg2);
void evictAllEarly(Object arg1);
T conditional(int field);
......
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
......@@ -63,8 +63,8 @@ public class DefaultCacheableService implements CacheableService<Long> {
}
@Override
@CacheEvict("testCache")
public void invalidate(Object arg1) {
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
}
@Override
......@@ -73,11 +73,6 @@ public class DefaultCacheableService implements CacheableService<Long> {
throw new RuntimeException("exception thrown - evict should NOT occur");
}
@Override
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", beforeInvocation = true)
public void evictEarly(Object arg1) {
......@@ -85,13 +80,13 @@ public class DefaultCacheableService implements CacheableService<Long> {
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0")
public void evict(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true)
public void evictAll(Object arg1) {
}
@Override
@CacheEvict(cacheNames = "testCache", key = "#p0", beforeInvocation = true)
public void invalidateEarly(Object arg1, Object arg2) {
@CacheEvict(cacheNames = "testCache", allEntries = true, beforeInvocation = true)
public void evictAllEarly(Object arg1) {
throw new RuntimeException("exception thrown - evict should still occur");
}
......
......@@ -27,12 +27,11 @@
<cache:cacheable method="nullValue" cache="testCache"/>
</cache:caching>
<cache:caching>
<cache:cache-evict method="invalidate" cache="testCache"/>
<cache:cache-evict method="evict" key="#p0" cache="testCache"/>
<cache:cache-evict method="evictWithException" cache="testCache"/>
<cache:cache-evict method="evictEarly" cache="testCache" before-invocation="true"/>
<cache:cache-evict method="invalidateEarly" key="#p0" cache="testCache" before-invocation="true"/>
<cache:cache-evict method="evictAll" cache="testCache" all-entries="true"/>
<cache:cache-evict method="evictAllEarly" cache="testCache" all-entries="true" before-invocation="true"/>
</cache:caching>
<cache:caching cache="testCache">
<cache:cache-put method="update"/>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册