@ -54,7 +54,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations ;
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider ;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration ;
import org.springframework.boot.test.context.ApplicationContext Test er;
import org.springframework.boot.test.context.ApplicationContext Runn er;
import org.springframework.boot.test.context.AssertableApplicationContext ;
import org.springframework.boot.test.context.ContextConsumer ;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions ;
@ -100,58 +100,59 @@ public class CacheAutoConfigurationTests {
@Rule
public final ExpectedException thrown = ExpectedException . none ( ) ;
private final ApplicationContext Tester context = new ApplicationContextTest er( )
private final ApplicationContext Runner contextRunner = new ApplicationContextRunn er( )
. withConfiguration ( AutoConfigurations . of ( CacheAutoConfiguration . class ) ) ;
@Test
public void noEnableCaching ( ) {
this . context . withUserConfiguration ( EmptyConfiguration . class ) . run ( ( loaded ) - > {
assertThat ( loaded ) . doesNotHaveBean ( CacheManager . class ) ;
} ) ;
this . contextRunner . withUserConfiguration ( EmptyConfiguration . class )
. run ( ( context ) - > {
assertThat ( context ) . doesNotHaveBean ( CacheManager . class ) ;
} ) ;
}
@Test
public void cacheManagerBackOff ( ) {
this . context . withUserConfiguration ( CustomCacheManagerConfiguration . class )
. run ( ( loaded ) - > {
assertThat ( getCacheManager ( loaded , ConcurrentMapCacheManager . class )
this . context Runner . withUserConfiguration ( CustomCacheManagerConfiguration . class )
. run ( ( context ) - > {
assertThat ( getCacheManager ( context , ConcurrentMapCacheManager . class )
. getCacheNames ( ) ) . containsOnly ( "custom1" ) ;
} ) ;
}
@Test
public void cacheManagerFromSupportBackOff ( ) {
this . context
this . context Runner
. withUserConfiguration ( CustomCacheManagerFromSupportConfiguration . class )
. run ( ( loaded ) - > {
assertThat ( getCacheManager ( loaded , ConcurrentMapCacheManager . class )
. run ( ( context ) - > {
assertThat ( getCacheManager ( context , ConcurrentMapCacheManager . class )
. getCacheNames ( ) ) . containsOnly ( "custom1" ) ;
} ) ;
}
@Test
public void cacheResolverFromSupportBackOff ( ) throws Exception {
this . context
this . context Runner
. withUserConfiguration ( CustomCacheResolverFromSupportConfiguration . class )
. run ( ( loaded ) - > {
assertThat ( loaded ) . doesNotHaveBean ( CacheManager . class ) ;
. run ( ( context ) - > {
assertThat ( context ) . doesNotHaveBean ( CacheManager . class ) ;
} ) ;
}
@Test
public void customCacheResolverCanBeDefined ( ) throws Exception {
this . context . withUserConfiguration ( SpecificCacheResolverConfiguration . class )
. withPropertyValues ( "spring.cache.type=simple" ) . run ( ( loaded ) - > {
getCacheManager ( loaded , ConcurrentMapCacheManager . class ) ;
assertThat ( loaded ) . getBeans ( CacheResolver . class ) . hasSize ( 1 ) ;
this . context Runner . withUserConfiguration ( SpecificCacheResolverConfiguration . class )
. withPropertyValues ( "spring.cache.type=simple" ) . run ( ( context ) - > {
getCacheManager ( context , ConcurrentMapCacheManager . class ) ;
assertThat ( context ) . getBeans ( CacheResolver . class ) . hasSize ( 1 ) ;
} ) ;
}
@Test
public void notSupportedCachingMode ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=foobar" ) . run ( ( loaded ) - > {
assertThat ( loaded ) . getFailure ( )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=foobar" ) . run ( ( context ) - > {
assertThat ( context ) . getFailure ( )
. isInstanceOf ( BeanCreationException . class )
. hasMessageContaining (
"Failed to bind properties under 'spring.cache.type'" ) ;
@ -160,28 +161,29 @@ public class CacheAutoConfigurationTests {
@Test
public void simpleCacheExplicit ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=simple" ) . run ( ( loaded ) - > {
assertThat ( getCacheManager ( loaded , ConcurrentMapCacheManager . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=simple" ) . run ( ( context ) - > {
assertThat ( getCacheManager ( context , ConcurrentMapCacheManager . class )
. getCacheNames ( ) ) . isEmpty ( ) ;
} ) ;
}
@Test
public void simpleCacheWithCustomizers ( ) {
this . context . withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
this . contextRunner
. withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "simple" )
. run ( dunno ( "allCacheManagerCustomizer" , "simpleCacheManagerCustomizer" ) ) ;
}
@Test
public void simpleCacheExplicitWithCacheNames ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=simple" ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
ConcurrentMapCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
ConcurrentMapCacheManager cacheManager = getCacheManager ( context ,
ConcurrentMapCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
} ) ;
@ -189,23 +191,23 @@ public class CacheAutoConfigurationTests {
@Test
public void genericCacheWithCaches ( ) {
this . context . withUserConfiguration ( GenericCacheConfiguration . class )
. run ( ( loaded ) - > {
SimpleCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( GenericCacheConfiguration . class )
. run ( ( context ) - > {
SimpleCacheManager cacheManager = getCacheManager ( context ,
SimpleCacheManager . class ) ;
assertThat ( cacheManager . getCache ( "first" ) )
. isEqualTo ( loaded . getBean ( "firstCache" ) ) ;
. isEqualTo ( context . getBean ( "firstCache" ) ) ;
assertThat ( cacheManager . getCache ( "second" ) )
. isEqualTo ( loaded . getBean ( "secondCache" ) ) ;
. isEqualTo ( context . getBean ( "secondCache" ) ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . hasSize ( 2 ) ;
} ) ;
}
@Test
public void genericCacheExplicit ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=generic" ) . run ( ( loaded ) - > {
assertThat ( loaded ) . getFailure ( )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=generic" ) . run ( ( context ) - > {
assertThat ( context ) . getFailure ( )
. isInstanceOf ( BeanCreationException . class )
. hasMessageContaining (
"No cache manager could be auto-configured" )
@ -215,30 +217,31 @@ public class CacheAutoConfigurationTests {
@Test
public void genericCacheWithCustomizers ( ) {
this . context . withUserConfiguration ( GenericCacheAndCustomizersConfiguration . class )
this . contextRunner
. withUserConfiguration ( GenericCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "generic" )
. run ( dunno ( "allCacheManagerCustomizer" , "genericCacheManagerCustomizer" ) ) ;
}
@Test
public void genericCacheExplicitWithCaches ( ) {
this . context . withUserConfiguration ( GenericCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=generic" ) . run ( ( loaded ) - > {
SimpleCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( GenericCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=generic" ) . run ( ( context ) - > {
SimpleCacheManager cacheManager = getCacheManager ( context ,
SimpleCacheManager . class ) ;
assertThat ( cacheManager . getCache ( "first" ) )
. isEqualTo ( loaded . getBean ( "firstCache" ) ) ;
. isEqualTo ( context . getBean ( "firstCache" ) ) ;
assertThat ( cacheManager . getCache ( "second" ) )
. isEqualTo ( loaded . getBean ( "secondCache" ) ) ;
. isEqualTo ( context . getBean ( "secondCache" ) ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . hasSize ( 2 ) ;
} ) ;
}
@Test
public void couchbaseCacheExplicit ( ) {
this . context . withUserConfiguration ( CouchbaseCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=couchbase" ) . run ( ( loaded ) - > {
CouchbaseCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( CouchbaseCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=couchbase" ) . run ( ( context ) - > {
CouchbaseCacheManager cacheManager = getCacheManager ( context ,
CouchbaseCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . isEmpty ( ) ;
} ) ;
@ -246,7 +249,7 @@ public class CacheAutoConfigurationTests {
@Test
public void couchbaseCacheWithCustomizers ( ) {
this . context
this . context Runner
. withUserConfiguration ( CouchbaseCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "couchbase" ) . run ( dunno (
"allCacheManagerCustomizer" , "couchbaseCacheManagerCustomizer" ) ) ;
@ -254,45 +257,45 @@ public class CacheAutoConfigurationTests {
@Test
public void couchbaseCacheExplicitWithCaches ( ) {
this . context . withUserConfiguration ( CouchbaseCacheConfiguration . class )
this . context Runner . withUserConfiguration ( CouchbaseCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=couchbase" ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
CouchbaseCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
CouchbaseCacheManager cacheManager = getCacheManager ( context ,
CouchbaseCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
Cache cache = cacheManager . getCache ( "foo" ) ;
assertThat ( cache ) . isInstanceOf ( CouchbaseCache . class ) ;
assertThat ( ( ( CouchbaseCache ) cache ) . getTtl ( ) ) . isEqualTo ( 0 ) ;
assertThat ( ( ( CouchbaseCache ) cache ) . getNativeCache ( ) )
. isEqualTo ( loaded . getBean ( "bucket" ) ) ;
. isEqualTo ( context . getBean ( "bucket" ) ) ;
} ) ;
}
@Test
public void couchbaseCacheExplicitWithTtl ( ) {
this . context . withUserConfiguration ( CouchbaseCacheConfiguration . class )
this . context Runner . withUserConfiguration ( CouchbaseCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=couchbase" ,
"spring.cache.cacheNames=foo,bar" ,
"spring.cache.couchbase.expiration=2000" )
. run ( ( loaded ) - > {
CouchbaseCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
CouchbaseCacheManager cacheManager = getCacheManager ( context ,
CouchbaseCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
Cache cache = cacheManager . getCache ( "foo" ) ;
assertThat ( cache ) . isInstanceOf ( CouchbaseCache . class ) ;
assertThat ( ( ( CouchbaseCache ) cache ) . getTtl ( ) ) . isEqualTo ( 2 ) ;
assertThat ( ( ( CouchbaseCache ) cache ) . getNativeCache ( ) )
. isEqualTo ( loaded . getBean ( "bucket" ) ) ;
. isEqualTo ( context . getBean ( "bucket" ) ) ;
} ) ;
}
@Test
public void redisCacheExplicit ( ) {
this . context . withUserConfiguration ( RedisCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=redis" ) . run ( ( loaded ) - > {
RedisCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( RedisCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=redis" ) . run ( ( context ) - > {
RedisCacheManager cacheManager = getCacheManager ( context ,
RedisCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . isEmpty ( ) ;
assertThat (
@ -304,19 +307,20 @@ public class CacheAutoConfigurationTests {
@Test
public void redisCacheWithCustomizers ( ) {
this . context . withUserConfiguration ( RedisCacheAndCustomizersConfiguration . class )
this . contextRunner
. withUserConfiguration ( RedisCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "redis" )
. run ( dunno ( "allCacheManagerCustomizer" , "redisCacheManagerCustomizer" ) ) ;
}
@Test
public void redisCacheExplicitWithCaches ( ) {
this . context . withUserConfiguration ( RedisCacheConfiguration . class )
this . context Runner . withUserConfiguration ( RedisCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=redis" ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
RedisCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
RedisCacheManager cacheManager = getCacheManager ( context ,
RedisCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
} ) ;
@ -324,9 +328,9 @@ public class CacheAutoConfigurationTests {
@Test
public void noOpCacheExplicit ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=none" ) . run ( ( loaded ) - > {
NoOpCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=none" ) . run ( ( context ) - > {
NoOpCacheManager cacheManager = getCacheManager ( context ,
NoOpCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . isEmpty ( ) ;
} ) ;
@ -334,9 +338,9 @@ public class CacheAutoConfigurationTests {
@Test
public void jCacheCacheNoProviderExplicit ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ) . run ( ( loaded ) - > {
assertThat ( loaded ) . getFailure ( )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ) . run ( ( context ) - > {
assertThat ( context ) . getFailure ( )
. isInstanceOf ( BeanCreationException . class )
. hasMessageContaining (
"No cache manager could be auto-configured" )
@ -347,14 +351,14 @@ public class CacheAutoConfigurationTests {
@Test
public void jCacheCacheWithProvider ( ) {
String cachingProviderFqn = MockCachingProvider . class . getName ( ) ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . isEmpty ( ) ;
assertThat ( loaded . getBean ( javax . cache . CacheManager . class ) )
assertThat ( context . getBean ( javax . cache . CacheManager . class ) )
. isEqualTo ( cacheManager . getCacheManager ( ) ) ;
} ) ;
}
@ -362,13 +366,13 @@ public class CacheAutoConfigurationTests {
@Test
public void jCacheCacheWithCaches ( ) {
String cachingProviderFqn = MockCachingProvider . class . getName ( ) ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
} ) ;
@ -377,16 +381,16 @@ public class CacheAutoConfigurationTests {
@Test
public void jCacheCacheWithCachesAndCustomConfig ( ) {
String cachingProviderFqn = MockCachingProvider . class . getName ( ) ;
this . context . withUserConfiguration ( JCacheCustomConfiguration . class )
this . context Runner . withUserConfiguration ( JCacheCustomConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.cacheNames[0]=one" ,
"spring.cache.cacheNames[1]=two" )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "one" , "two" ) ;
CompleteConfiguration < ? , ? > defaultCacheConfiguration = loaded
CompleteConfiguration < ? , ? > defaultCacheConfiguration = context
. getBean ( CompleteConfiguration . class ) ;
verify ( cacheManager . getCacheManager ( ) ) . createCache ( "one" ,
defaultCacheConfiguration ) ;
@ -397,23 +401,23 @@ public class CacheAutoConfigurationTests {
@Test
public void jCacheCacheWithExistingJCacheManager ( ) {
this . context . withUserConfiguration ( JCacheCustomCacheManager . class )
. withPropertyValues ( "spring.cache.type=jcache" ) . run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( JCacheCustomCacheManager . class )
. withPropertyValues ( "spring.cache.type=jcache" ) . run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheManager ( ) )
. isEqualTo ( loaded . getBean ( "customJCacheCacheManager" ) ) ;
. isEqualTo ( context . getBean ( "customJCacheCacheManager" ) ) ;
} ) ;
}
@Test
public void jCacheCacheWithUnknownProvider ( ) {
String wrongCachingProviderClassName = "org.acme.FooBar" ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + wrongCachingProviderClassName )
. run ( ( loaded ) - > {
assertThat ( loaded ) . getFailure ( )
. run ( ( context ) - > {
assertThat ( context ) . getFailure ( )
. isInstanceOf ( BeanCreationException . class )
. hasMessageContaining ( wrongCachingProviderClassName ) ;
} ) ;
@ -423,12 +427,12 @@ public class CacheAutoConfigurationTests {
public void jCacheCacheWithConfig ( ) {
String cachingProviderFqn = MockCachingProvider . class . getName ( ) ;
String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml" ;
this . context . withUserConfiguration ( JCacheCustomConfiguration . class )
this . context Runner . withUserConfiguration ( JCacheCustomConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.jcache.config=" + configLocation )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
Resource configResource = new ClassPathResource ( configLocation ) ;
assertThat ( cacheManager . getCacheManager ( ) . getURI ( ) )
@ -440,12 +444,12 @@ public class CacheAutoConfigurationTests {
public void jCacheCacheWithWrongConfig ( ) {
String cachingProviderFqn = MockCachingProvider . class . getName ( ) ;
String configLocation = "org/springframework/boot/autoconfigure/cache/does-not-exist.xml" ;
this . context . withUserConfiguration ( JCacheCustomConfiguration . class )
this . context Runner . withUserConfiguration ( JCacheCustomConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.jcache.config=" + configLocation )
. run ( ( loaded ) - > {
assertThat ( loaded ) . getFailure ( )
. run ( ( context ) - > {
assertThat ( context ) . getFailure ( )
. isInstanceOf ( BeanCreationException . class )
. hasMessageContaining ( "does not exist" )
. hasMessageContaining ( configLocation ) ;
@ -454,31 +458,32 @@ public class CacheAutoConfigurationTests {
@Test
public void ehcacheCacheWithCaches ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=ehcache" ) . run ( ( loaded ) - > {
EhCacheCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=ehcache" ) . run ( ( context ) - > {
EhCacheCacheManager cacheManager = getCacheManager ( context ,
EhCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "cacheTest1" ,
"cacheTest2" ) ;
assertThat ( loaded . getBean ( net . sf . ehcache . CacheManager . class ) )
assertThat ( context . getBean ( net . sf . ehcache . CacheManager . class ) )
. isEqualTo ( cacheManager . getCacheManager ( ) ) ;
} ) ;
}
@Test
public void ehcacheCacheWithCustomizers ( ) {
this . context . withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
this . contextRunner
. withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "ehcache" )
. run ( dunno ( "allCacheManagerCustomizer" , "ehcacheCacheManagerCustomizer" ) ) ;
}
@Test
public void ehcacheCacheWithConfig ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=ehcache" ,
"spring.cache.ehcache.config=cache/ehcache-override.xml" )
. run ( ( loaded ) - > {
EhCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
EhCacheCacheManager cacheManager = getCacheManager ( context ,
EhCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) )
. containsOnly ( "cacheOverrideTest1" , "cacheOverrideTest2" ) ;
@ -487,25 +492,25 @@ public class CacheAutoConfigurationTests {
@Test
public void ehcacheCacheWithExistingCacheManager ( ) {
this . context . withUserConfiguration ( EhCacheCustomCacheManager . class )
. withPropertyValues ( "spring.cache.type=ehcache" ) . run ( ( loaded ) - > {
EhCacheCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( EhCacheCustomCacheManager . class )
. withPropertyValues ( "spring.cache.type=ehcache" ) . run ( ( context ) - > {
EhCacheCacheManager cacheManager = getCacheManager ( context ,
EhCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheManager ( ) )
. isEqualTo ( loaded . getBean ( "customEhCacheCacheManager" ) ) ;
. isEqualTo ( context . getBean ( "customEhCacheCacheManager" ) ) ;
} ) ;
}
@Test
public void ehcache3AsJCacheWithCaches ( ) {
String cachingProviderFqn = EhcacheCachingProvider . class . getName ( ) ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
} ) ;
@ -515,12 +520,12 @@ public class CacheAutoConfigurationTests {
public void ehcache3AsJCacheWithConfig ( ) throws IOException {
String cachingProviderFqn = EhcacheCachingProvider . class . getName ( ) ;
String configLocation = "ehcache3.xml" ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.jcache.config=" + configLocation )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
Resource configResource = new ClassPathResource ( configLocation ) ;
@ -532,25 +537,25 @@ public class CacheAutoConfigurationTests {
@Test
public void hazelcastCacheExplicit ( ) {
this . context
this . context Runner
. withConfiguration (
AutoConfigurations . of ( HazelcastAutoConfiguration . class ) )
. withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=hazelcast" ) . run ( ( loaded ) - > {
HazelcastCacheManager cacheManager = getCacheManager ( loaded ,
. withPropertyValues ( "spring.cache.type=hazelcast" ) . run ( ( context ) - > {
HazelcastCacheManager cacheManager = getCacheManager ( context ,
HazelcastCacheManager . class ) ;
// NOTE: the hazelcast implementation knows about a cache in a lazy
// manner.
cacheManager . getCache ( "defaultCache" ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "defaultCache" ) ;
assertThat ( loaded . getBean ( HazelcastInstance . class ) )
assertThat ( context . getBean ( HazelcastInstance . class ) )
. isEqualTo ( cacheManager . getHazelcastInstance ( ) ) ;
} ) ;
}
@Test
public void hazelcastCacheWithCustomizers ( ) {
this . context
this . context Runner
. withUserConfiguration ( HazelcastCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "hazelcast" ) . run ( dunno (
"allCacheManagerCustomizer" , "hazelcastCacheManagerCustomizer" ) ) ;
@ -558,28 +563,28 @@ public class CacheAutoConfigurationTests {
@Test
public void hazelcastCacheWithExistingHazelcastInstance ( ) {
this . context . withUserConfiguration ( HazelcastCustomHazelcastInstance . class )
. withPropertyValues ( "spring.cache.type=hazelcast" ) . run ( ( loaded ) - > {
HazelcastCacheManager cacheManager = getCacheManager ( loaded ,
this . context Runner . withUserConfiguration ( HazelcastCustomHazelcastInstance . class )
. withPropertyValues ( "spring.cache.type=hazelcast" ) . run ( ( context ) - > {
HazelcastCacheManager cacheManager = getCacheManager ( context ,
HazelcastCacheManager . class ) ;
assertThat ( cacheManager . getHazelcastInstance ( ) )
. isEqualTo ( loaded . getBean ( "customHazelcastInstance" ) ) ;
. isEqualTo ( context . getBean ( "customHazelcastInstance" ) ) ;
} ) ;
}
@Test
public void hazelcastCacheWithHazelcastAutoConfiguration ( ) throws IOException {
String hazelcastConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml" ;
this . context
this . context Runner
. withConfiguration (
AutoConfigurations . of ( HazelcastAutoConfiguration . class ) )
. withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=hazelcast" ,
"spring.hazelcast.config=" + hazelcastConfig )
. run ( ( loaded ) - > {
HazelcastCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
HazelcastCacheManager cacheManager = getCacheManager ( context ,
HazelcastCacheManager . class ) ;
HazelcastInstance hazelcastInstance = loaded
HazelcastInstance hazelcastInstance = context
. getBean ( HazelcastInstance . class ) ;
assertThat ( cacheManager . getHazelcastInstance ( ) )
. isSameAs ( hazelcastInstance ) ;
@ -594,13 +599,13 @@ public class CacheAutoConfigurationTests {
public void hazelcastAsJCacheWithCaches ( ) {
String cachingProviderFqn = HazelcastCachingProvider . class . getName ( ) ;
try {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . containsOnly ( "foo" ,
"bar" ) ;
@ -617,12 +622,12 @@ public class CacheAutoConfigurationTests {
String cachingProviderFqn = HazelcastCachingProvider . class . getName ( ) ;
try {
String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml" ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn ,
"spring.cache.jcache.config=" + configLocation )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
Resource configResource = new ClassPathResource ( configLocation ) ;
assertThat ( cacheManager . getCacheManager ( ) . getURI ( ) )
@ -638,21 +643,22 @@ public class CacheAutoConfigurationTests {
@Test
public void hazelcastAsJCacheWithExistingHazelcastInstance ( ) throws IOException {
String cachingProviderFqn = HazelcastCachingProvider . class . getName ( ) ;
this . context
this . context Runner
. withConfiguration (
AutoConfigurations . of ( HazelcastAutoConfiguration . class ) )
. withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderFqn )
. run ( ( loaded ) - > {
JCacheCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
JCacheCacheManager cacheManager = getCacheManager ( context ,
JCacheCacheManager . class ) ;
javax . cache . CacheManager jCacheManager = cacheManager
. getCacheManager ( ) ;
assertThat ( jCacheManager ) . isInstanceOf (
com . hazelcast . cache . HazelcastCacheManager . class ) ;
assertThat ( loaded . getBeansOfType ( HazelcastInstance . class ) ) . hasSize ( 1 ) ;
HazelcastInstance hazelcastInstance = loaded
assertThat ( context . getBeansOfType ( HazelcastInstance . class ) )
. hasSize ( 1 ) ;
HazelcastInstance hazelcastInstance = context
. getBean ( HazelcastInstance . class ) ;
assertThat ( ( ( com . hazelcast . cache . HazelcastCacheManager ) jCacheManager )
. getHazelcastInstance ( ) ) . isSameAs ( hazelcastInstance ) ;
@ -663,11 +669,11 @@ public class CacheAutoConfigurationTests {
@Test
public void infinispanCacheWithConfig ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=infinispan" ,
"spring.cache.infinispan.config=infinispan.xml" )
. run ( ( loaded ) - > {
SpringEmbeddedCacheManager cacheManager = getCacheManager ( loaded ,
. run ( ( context ) - > {
SpringEmbeddedCacheManager cacheManager = getCacheManager ( context ,
SpringEmbeddedCacheManager . class ) ;
assertThat ( cacheManager . getCacheNames ( ) ) . contains ( "foo" , "bar" ) ;
} ) ;
@ -675,46 +681,47 @@ public class CacheAutoConfigurationTests {
@Test
public void infinispanCacheWithCustomizers ( ) {
this . context . withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
this . contextRunner
. withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "infinispan" ) . run ( dunno (
"allCacheManagerCustomizer" , "infinispanCacheManagerCustomizer" ) ) ;
}
@Test
public void infinispanCacheWithCaches ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=infinispan" ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
assertThat ( getCacheManager ( loaded , SpringEmbeddedCacheManager . class )
. run ( ( context ) - > {
assertThat ( getCacheManager ( context , SpringEmbeddedCacheManager . class )
. getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
} ) ;
}
@Test
public void infinispanCacheWithCachesAndCustomConfig ( ) {
this . context . withUserConfiguration ( InfinispanCustomConfiguration . class )
this . context Runner . withUserConfiguration ( InfinispanCustomConfiguration . class )
. withPropertyValues ( "spring.cache.type=infinispan" ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
assertThat ( getCacheManager ( loaded , SpringEmbeddedCacheManager . class )
. run ( ( context ) - > {
assertThat ( getCacheManager ( context , SpringEmbeddedCacheManager . class )
. getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
verify ( loaded . getBean ( ConfigurationBuilder . class ) , times ( 2 ) ) . build ( ) ;
verify ( context . getBean ( ConfigurationBuilder . class ) , times ( 2 ) ) . build ( ) ;
} ) ;
}
@Test
public void infinispanAsJCacheWithCaches ( ) {
String cachingProviderClassName = JCachingProvider . class . getName ( ) ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderClassName ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
assertThat ( getCacheManager ( loaded , JCacheCacheManager . class )
. run ( ( context ) - > {
assertThat ( getCacheManager ( context , JCacheCacheManager . class )
. getCacheNames ( ) ) . containsOnly ( "foo" , "bar" ) ;
} ) ;
}
@ -723,13 +730,13 @@ public class CacheAutoConfigurationTests {
public void infinispanAsJCacheWithConfig ( ) throws IOException {
String cachingProviderClassName = JCachingProvider . class . getName ( ) ;
String configLocation = "infinispan.xml" ;
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderClassName ,
"spring.cache.jcache.config=" + configLocation )
. run ( ( loaded ) - > {
. run ( ( context ) - > {
Resource configResource = new ClassPathResource ( configLocation ) ;
assertThat ( getCacheManager ( loaded , JCacheCacheManager . class )
assertThat ( getCacheManager ( context , JCacheCacheManager . class )
. getCacheManager ( ) . getURI ( ) )
. isEqualTo ( configResource . getURI ( ) ) ;
} ) ;
@ -739,14 +746,15 @@ public class CacheAutoConfigurationTests {
public void jCacheCacheWithCachesAndCustomizer ( ) {
String cachingProviderClassName = HazelcastCachingProvider . class . getName ( ) ;
try {
this . context . withUserConfiguration ( JCacheWithCustomizerConfiguration . class )
this . contextRunner
. withUserConfiguration ( JCacheWithCustomizerConfiguration . class )
. withPropertyValues ( "spring.cache.type=jcache" ,
"spring.cache.jcache.provider=" + cachingProviderClassName ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
. run ( ( loaded ) - > {
. run ( ( context ) - > {
// see customizer
assertThat ( getCacheManager ( loaded , JCacheCacheManager . class )
assertThat ( getCacheManager ( context , JCacheCacheManager . class )
. getCacheNames ( ) ) . containsOnly ( "foo" , "custom1" ) ;
} ) ;
}
@ -757,11 +765,11 @@ public class CacheAutoConfigurationTests {
@Test
public void caffeineCacheWithExplicitCaches ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=caffeine" ,
"spring.cache.cacheNames=foo" )
. run ( ( loaded ) - > {
CaffeineCacheManager manager = getCacheManager ( loaded ,
. run ( ( context ) - > {
CaffeineCacheManager manager = getCacheManager ( context ,
CaffeineCacheManager . class ) ;
assertThat ( manager . getCacheNames ( ) ) . containsOnly ( "foo" ) ;
Cache foo = manager . getCache ( "foo" ) ;
@ -774,14 +782,15 @@ public class CacheAutoConfigurationTests {
@Test
public void caffeineCacheWithCustomizers ( ) {
this . context . withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
this . contextRunner
. withUserConfiguration ( DefaultCacheAndCustomizersConfiguration . class )
. withPropertyValues ( "spring.cache.type=" + "caffeine" ) . run ( dunno (
"allCacheManagerCustomizer" , "caffeineCacheManagerCustomizer" ) ) ;
}
@Test
public void caffeineCacheWithExplicitCacheBuilder ( ) {
this . context . withUserConfiguration ( CaffeineCacheBuilderConfiguration . class )
this . context Runner . withUserConfiguration ( CaffeineCacheBuilderConfiguration . class )
. withPropertyValues ( "spring.cache.type=caffeine" ,
"spring.cache.cacheNames=foo,bar" )
. run ( this : : validateCaffeineCacheWithStats ) ;
@ -789,7 +798,7 @@ public class CacheAutoConfigurationTests {
@Test
public void caffeineCacheExplicitWithSpec ( ) {
this . context . withUserConfiguration ( CaffeineCacheSpecConfiguration . class )
this . context Runner . withUserConfiguration ( CaffeineCacheSpecConfiguration . class )
. withPropertyValues ( "spring.cache.type=caffeine" ,
"spring.cache.cacheNames[0]=foo" ,
"spring.cache.cacheNames[1]=bar" )
@ -798,7 +807,7 @@ public class CacheAutoConfigurationTests {
@Test
public void caffeineCacheExplicitWithSpecString ( ) {
this . context . withUserConfiguration ( DefaultCacheConfiguration . class )
this . context Runner . withUserConfiguration ( DefaultCacheConfiguration . class )
. withPropertyValues ( "spring.cache.type=caffeine" ,
"spring.cache.caffeine.spec=recordStats" ,
"spring.cache.cacheNames[0]=foo" ,
@ -819,11 +828,11 @@ public class CacheAutoConfigurationTests {
@SuppressWarnings ( "rawtypes" )
private ContextConsumer < AssertableApplicationContext > dunno (
String . . . expectedCustomizerNames ) {
return ( loaded ) - > {
CacheManager cacheManager = getCacheManager ( loaded , CacheManager . class ) ;
return ( context ) - > {
CacheManager cacheManager = getCacheManager ( context , CacheManager . class ) ;
List < String > expected = new ArrayList < > (
Arrays . asList ( expectedCustomizerNames ) ) ;
Map < String , CacheManagerTestCustomizer > customizer = loaded
Map < String , CacheManagerTestCustomizer > customizer = context
. getBeansOfType ( CacheManagerTestCustomizer . class ) ;
customizer . forEach ( ( key , value ) - > {
if ( expected . contains ( key ) ) {