|
|
@ -2,6 +2,7 @@ package org.springframework.cache.config; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong; |
|
|
|
import java.util.concurrent.atomic.AtomicLong; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.cache.Cache; |
|
|
|
import org.springframework.cache.Cache; |
|
|
@ -21,29 +22,37 @@ import org.springframework.context.annotation.Import; |
|
|
|
import static org.springframework.cache.CacheTestUtils.*; |
|
|
|
import static org.springframework.cache.CacheTestUtils.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tests that represent real use cases with advanced configuration |
|
|
|
* Tests that represent real use cases with advanced configuration. |
|
|
|
|
|
|
|
* |
|
|
|
* @author Stephane Nicoll |
|
|
|
* @author Stephane Nicoll |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class EnableCachingIntegrationTests { |
|
|
|
public class EnableCachingIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ConfigurableApplicationContext context; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@After |
|
|
|
|
|
|
|
public void closeContext() { |
|
|
|
|
|
|
|
if (this.context != null) { |
|
|
|
|
|
|
|
this.context.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void fooServiceWithInterface() { |
|
|
|
public void fooServiceWithInterface() { |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FooConfig.class); |
|
|
|
this.context = new AnnotationConfigApplicationContext(FooConfig.class); |
|
|
|
FooService service = context.getBean(FooService.class); |
|
|
|
FooService service = this.context.getBean(FooService.class); |
|
|
|
fooGetSimple(context, service); |
|
|
|
fooGetSimple(service); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void fooServiceWithInterfaceCglib() { |
|
|
|
public void fooServiceWithInterfaceCglib() { |
|
|
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FooConfigCglib.class); |
|
|
|
this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class); |
|
|
|
FooService service = context.getBean(FooService.class); |
|
|
|
FooService service = this.context.getBean(FooService.class); |
|
|
|
fooGetSimple(context, service); |
|
|
|
fooGetSimple(service); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void fooGetSimple(ApplicationContext context, FooService service) { |
|
|
|
private void fooGetSimple(FooService service) { |
|
|
|
CacheManager cacheManager = context.getBean(CacheManager.class); |
|
|
|
Cache cache = getCache(); |
|
|
|
|
|
|
|
|
|
|
|
Cache cache = cacheManager.getCache("testCache"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object key = new Object(); |
|
|
|
Object key = new Object(); |
|
|
|
assertCacheMiss(key, cache); |
|
|
|
assertCacheMiss(key, cache); |
|
|
@ -52,6 +61,21 @@ public class EnableCachingIntegrationTests { |
|
|
|
assertCacheHit(key, value, cache); |
|
|
|
assertCacheHit(key, value, cache); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void beanCondition() { |
|
|
|
|
|
|
|
this.context = new AnnotationConfigApplicationContext(BeanConditionConfig.class); |
|
|
|
|
|
|
|
Cache cache = getCache(); |
|
|
|
|
|
|
|
FooService service = context.getBean(FooService.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object key = new Object(); |
|
|
|
|
|
|
|
service.getWithCondition(key); |
|
|
|
|
|
|
|
assertCacheMiss(key, cache); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Cache getCache() { |
|
|
|
|
|
|
|
return this.context.getBean(CacheManager.class).getCache("testCache"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
static class SharedConfig extends CachingConfigurerSupport { |
|
|
|
static class SharedConfig extends CachingConfigurerSupport { |
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -81,8 +105,10 @@ public class EnableCachingIntegrationTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static interface FooService { |
|
|
|
private interface FooService { |
|
|
|
public Object getSimple(Object key); |
|
|
|
Object getSimple(Object key); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object getWithCondition(Object key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@CacheConfig(cacheNames = "testCache") |
|
|
|
@CacheConfig(cacheNames = "testCache") |
|
|
@ -94,6 +120,35 @@ public class EnableCachingIntegrationTests { |
|
|
|
public Object getSimple(Object key) { |
|
|
|
public Object getSimple(Object key) { |
|
|
|
return counter.getAndIncrement(); |
|
|
|
return counter.getAndIncrement(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
@Cacheable(condition = "@bar.enabled") |
|
|
|
|
|
|
|
public Object getWithCondition(Object key) { |
|
|
|
|
|
|
|
return counter.getAndIncrement(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@Import(FooConfig.class) |
|
|
|
|
|
|
|
@EnableCaching |
|
|
|
|
|
|
|
static class BeanConditionConfig { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
|
|
public Bar bar() { |
|
|
|
|
|
|
|
return new Bar(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class Bar { |
|
|
|
|
|
|
|
private final boolean enabled; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Bar(boolean enabled) { |
|
|
|
|
|
|
|
this.enabled = enabled; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isEnabled() { |
|
|
|
|
|
|
|
return enabled; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|