@ -2,6 +2,7 @@ package org.springframework.cache.config;
@@ -2,6 +2,7 @@ package org.springframework.cache.config;
import java.util.concurrent.atomic.AtomicLong ;
import org.junit.After ;
import org.junit.Test ;
import org.springframework.cache.Cache ;
@ -21,29 +22,37 @@ import org.springframework.context.annotation.Import;
@@ -21,29 +22,37 @@ import org.springframework.context.annotation.Import;
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
* /
public class EnableCachingIntegrationTests {
private ConfigurableApplicationContext context ;
@After
public void closeContext ( ) {
if ( this . context ! = null ) {
this . context . close ( ) ;
}
}
@Test
public void fooServiceWithInterface ( ) {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext ( FooConfig . class ) ;
FooService service = context . getBean ( FooService . class ) ;
fooGetSimple ( context , service ) ;
this . context = new AnnotationConfigApplicationContext ( FooConfig . class ) ;
FooService service = this . context . getBean ( FooService . class ) ;
fooGetSimple ( service ) ;
}
@Test
public void fooServiceWithInterfaceCglib ( ) {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext ( FooConfigCglib . class ) ;
FooService service = context . getBean ( FooService . class ) ;
fooGetSimple ( context , service ) ;
this . context = new AnnotationConfigApplicationContext ( FooConfigCglib . class ) ;
FooService service = this . context . getBean ( FooService . class ) ;
fooGetSimple ( service ) ;
}
private void fooGetSimple ( ApplicationContext context , FooService service ) {
CacheManager cacheManager = context . getBean ( CacheManager . class ) ;
Cache cache = cacheManager . getCache ( "testCache" ) ;
private void fooGetSimple ( FooService service ) {
Cache cache = getCache ( ) ;
Object key = new Object ( ) ;
assertCacheMiss ( key , cache ) ;
@ -52,6 +61,21 @@ public class EnableCachingIntegrationTests {
@@ -52,6 +61,21 @@ public class EnableCachingIntegrationTests {
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
static class SharedConfig extends CachingConfigurerSupport {
@Override
@ -81,8 +105,10 @@ public class EnableCachingIntegrationTests {
@@ -81,8 +105,10 @@ public class EnableCachingIntegrationTests {
}
}
private static interface FooService {
public Object getSimple ( Object key ) ;
private interface FooService {
Object getSimple ( Object key ) ;
Object getWithCondition ( Object key ) ;
}
@CacheConfig ( cacheNames = "testCache" )
@ -94,6 +120,35 @@ public class EnableCachingIntegrationTests {
@@ -94,6 +120,35 @@ public class EnableCachingIntegrationTests {
public Object getSimple ( Object key ) {
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 ;
}
}
}
}