|
17 | 17 | package org.springframework.boot.autoconfigure.cache;
|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
| 20 | +import java.util.Arrays; |
20 | 21 | import java.util.Collections;
|
21 | 22 | import java.util.List;
|
| 23 | +import java.util.Map; |
22 | 24 |
|
23 | 25 | import javax.cache.Caching;
|
24 | 26 | import javax.cache.configuration.CompleteConfiguration;
|
|
32 | 34 | import com.hazelcast.core.Hazelcast;
|
33 | 35 | import com.hazelcast.core.HazelcastInstance;
|
34 | 36 | import com.hazelcast.spring.cache.HazelcastCacheManager;
|
| 37 | +import org.ehcache.jsr107.EhcacheCachingProvider; |
35 | 38 | import org.junit.jupiter.api.Test;
|
36 | 39 |
|
37 | 40 | import org.springframework.beans.factory.BeanCreationException;
|
|
41 | 44 | import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider.MockCacheManager;
|
42 | 45 | import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
|
43 | 46 | import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
| 47 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 48 | +import org.springframework.boot.test.context.runner.ContextConsumer; |
44 | 49 | import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
45 | 50 | import org.springframework.cache.Cache;
|
46 | 51 | import org.springframework.cache.CacheManager;
|
|
81 | 86 | * @author Ryon Day
|
82 | 87 | */
|
83 | 88 | @ClassPathExclusions("hazelcast-client-*.jar")
|
84 |
| -class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { |
| 89 | +class CacheAutoConfigurationTests { |
| 90 | + |
| 91 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 92 | + .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); |
85 | 93 |
|
86 | 94 | @Test
|
87 | 95 | void noEnableCaching() {
|
@@ -425,6 +433,34 @@ void jCacheCacheUseBeanClassLoader() {
|
425 | 433 | });
|
426 | 434 | }
|
427 | 435 |
|
| 436 | + @Test |
| 437 | + void ehcache3AsJCacheWithCaches() { |
| 438 | + String cachingProviderFqn = EhcacheCachingProvider.class.getName(); |
| 439 | + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) |
| 440 | + .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, |
| 441 | + "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") |
| 442 | + .run((context) -> { |
| 443 | + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); |
| 444 | + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); |
| 445 | + }); |
| 446 | + } |
| 447 | + |
| 448 | + @Test |
| 449 | + void ehcache3AsJCacheWithConfig() { |
| 450 | + String cachingProviderFqn = EhcacheCachingProvider.class.getName(); |
| 451 | + String configLocation = "ehcache3.xml"; |
| 452 | + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) |
| 453 | + .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, |
| 454 | + "spring.cache.jcache.config=" + configLocation) |
| 455 | + .run((context) -> { |
| 456 | + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); |
| 457 | + |
| 458 | + Resource configResource = new ClassPathResource(configLocation); |
| 459 | + assertThat(cacheManager.getCacheManager().getURI()).isEqualTo(configResource.getURI()); |
| 460 | + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); |
| 461 | + }); |
| 462 | + } |
| 463 | + |
428 | 464 | @Test
|
429 | 465 | void hazelcastCacheExplicit() {
|
430 | 466 | this.contextRunner.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
|
@@ -620,6 +656,32 @@ private RedisCacheConfiguration getDefaultRedisCacheConfiguration(RedisCacheMana
|
620 | 656 | return (RedisCacheConfiguration) ReflectionTestUtils.getField(cacheManager, "defaultCacheConfig");
|
621 | 657 | }
|
622 | 658 |
|
| 659 | + private <T extends CacheManager> T getCacheManager(AssertableApplicationContext loaded, Class<T> type) { |
| 660 | + CacheManager cacheManager = loaded.getBean(CacheManager.class); |
| 661 | + assertThat(cacheManager).as("Wrong cache manager type").isInstanceOf(type); |
| 662 | + return type.cast(cacheManager); |
| 663 | + } |
| 664 | + |
| 665 | + @SuppressWarnings("rawtypes") |
| 666 | + private ContextConsumer<AssertableApplicationContext> verifyCustomizers(String... expectedCustomizerNames) { |
| 667 | + return (context) -> { |
| 668 | + CacheManager cacheManager = getCacheManager(context, CacheManager.class); |
| 669 | + List<String> expected = new ArrayList<>(Arrays.asList(expectedCustomizerNames)); |
| 670 | + Map<String, CacheManagerTestCustomizer> customizer = context |
| 671 | + .getBeansOfType(CacheManagerTestCustomizer.class); |
| 672 | + customizer.forEach((key, value) -> { |
| 673 | + if (expected.contains(key)) { |
| 674 | + expected.remove(key); |
| 675 | + assertThat(value.cacheManager).isSameAs(cacheManager); |
| 676 | + } |
| 677 | + else { |
| 678 | + assertThat(value.cacheManager).isNull(); |
| 679 | + } |
| 680 | + }); |
| 681 | + assertThat(expected).hasSize(0); |
| 682 | + }; |
| 683 | + } |
| 684 | + |
623 | 685 | @Configuration(proxyBeanMethods = false)
|
624 | 686 | static class EmptyConfiguration {
|
625 | 687 |
|
@@ -878,4 +940,71 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
|
878 | 940 |
|
879 | 941 | }
|
880 | 942 |
|
| 943 | + @Configuration(proxyBeanMethods = false) |
| 944 | + static class CacheManagerCustomizersConfiguration { |
| 945 | + |
| 946 | + @Bean |
| 947 | + CacheManagerCustomizer<CacheManager> allCacheManagerCustomizer() { |
| 948 | + return new CacheManagerTestCustomizer<>() { |
| 949 | + |
| 950 | + }; |
| 951 | + } |
| 952 | + |
| 953 | + @Bean |
| 954 | + CacheManagerCustomizer<ConcurrentMapCacheManager> simpleCacheManagerCustomizer() { |
| 955 | + return new CacheManagerTestCustomizer<>() { |
| 956 | + |
| 957 | + }; |
| 958 | + } |
| 959 | + |
| 960 | + @Bean |
| 961 | + CacheManagerCustomizer<SimpleCacheManager> genericCacheManagerCustomizer() { |
| 962 | + return new CacheManagerTestCustomizer<>() { |
| 963 | + |
| 964 | + }; |
| 965 | + } |
| 966 | + |
| 967 | + @Bean |
| 968 | + CacheManagerCustomizer<CouchbaseCacheManager> couchbaseCacheManagerCustomizer() { |
| 969 | + return new CacheManagerTestCustomizer<CouchbaseCacheManager>() { |
| 970 | + |
| 971 | + }; |
| 972 | + } |
| 973 | + |
| 974 | + @Bean |
| 975 | + CacheManagerCustomizer<RedisCacheManager> redisCacheManagerCustomizer() { |
| 976 | + return new CacheManagerTestCustomizer<>() { |
| 977 | + |
| 978 | + }; |
| 979 | + } |
| 980 | + |
| 981 | + @Bean |
| 982 | + CacheManagerCustomizer<HazelcastCacheManager> hazelcastCacheManagerCustomizer() { |
| 983 | + return new CacheManagerTestCustomizer<>() { |
| 984 | + |
| 985 | + }; |
| 986 | + } |
| 987 | + |
| 988 | + @Bean |
| 989 | + CacheManagerCustomizer<CaffeineCacheManager> caffeineCacheManagerCustomizer() { |
| 990 | + return new CacheManagerTestCustomizer<>() { |
| 991 | + |
| 992 | + }; |
| 993 | + } |
| 994 | + |
| 995 | + } |
| 996 | + |
| 997 | + abstract static class CacheManagerTestCustomizer<T extends CacheManager> implements CacheManagerCustomizer<T> { |
| 998 | + |
| 999 | + private T cacheManager; |
| 1000 | + |
| 1001 | + @Override |
| 1002 | + public void customize(T cacheManager) { |
| 1003 | + if (this.cacheManager != null) { |
| 1004 | + throw new IllegalStateException("Customized invoked twice"); |
| 1005 | + } |
| 1006 | + this.cacheManager = cacheManager; |
| 1007 | + } |
| 1008 | + |
| 1009 | + } |
881 | 1010 | }
|
0 commit comments