|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.cache; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import com.couchbase.client.spring.cache.CouchbaseCacheManager; |
| 25 | +import com.hazelcast.spring.cache.HazelcastCacheManager; |
| 26 | +import org.infinispan.spring.provider.SpringEmbeddedCacheManager; |
| 27 | + |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 29 | +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; |
| 30 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 31 | +import org.springframework.boot.test.context.runner.ContextConsumer; |
| 32 | +import org.springframework.cache.CacheManager; |
| 33 | +import org.springframework.cache.caffeine.CaffeineCacheManager; |
| 34 | +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; |
| 35 | +import org.springframework.cache.ehcache.EhCacheCacheManager; |
| 36 | +import org.springframework.cache.support.SimpleCacheManager; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.context.annotation.Configuration; |
| 39 | +import org.springframework.data.redis.cache.RedisCacheManager; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | + |
| 43 | +/** |
| 44 | + * Base class for {@link CacheAutoConfiguration} tests. |
| 45 | + * |
| 46 | + * @author Andy Wilkinson |
| 47 | + */ |
| 48 | +abstract class AbstractCacheAutoConfigurationTests { |
| 49 | + |
| 50 | + protected final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 51 | + .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); |
| 52 | + |
| 53 | + protected <T extends CacheManager> T getCacheManager( |
| 54 | + AssertableApplicationContext loaded, Class<T> type) { |
| 55 | + CacheManager cacheManager = loaded.getBean(CacheManager.class); |
| 56 | + assertThat(cacheManager).as("Wrong cache manager type").isInstanceOf(type); |
| 57 | + return type.cast(cacheManager); |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings("rawtypes") |
| 61 | + protected ContextConsumer<AssertableApplicationContext> verifyCustomizers( |
| 62 | + String... expectedCustomizerNames) { |
| 63 | + return (context) -> { |
| 64 | + CacheManager cacheManager = getCacheManager(context, CacheManager.class); |
| 65 | + List<String> expected = new ArrayList<>( |
| 66 | + Arrays.asList(expectedCustomizerNames)); |
| 67 | + Map<String, CacheManagerTestCustomizer> customizer = context |
| 68 | + .getBeansOfType(CacheManagerTestCustomizer.class); |
| 69 | + customizer.forEach((key, value) -> { |
| 70 | + if (expected.contains(key)) { |
| 71 | + expected.remove(key); |
| 72 | + assertThat(value.cacheManager).isSameAs(cacheManager); |
| 73 | + } |
| 74 | + else { |
| 75 | + assertThat(value.cacheManager).isNull(); |
| 76 | + } |
| 77 | + }); |
| 78 | + assertThat(expected).hasSize(0); |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + @Configuration |
| 83 | + static class CacheManagerCustomizersConfiguration { |
| 84 | + |
| 85 | + @Bean |
| 86 | + public CacheManagerCustomizer<CacheManager> allCacheManagerCustomizer() { |
| 87 | + return new CacheManagerTestCustomizer<CacheManager>() { |
| 88 | + |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + public CacheManagerCustomizer<ConcurrentMapCacheManager> simpleCacheManagerCustomizer() { |
| 94 | + return new CacheManagerTestCustomizer<ConcurrentMapCacheManager>() { |
| 95 | + |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + public CacheManagerCustomizer<SimpleCacheManager> genericCacheManagerCustomizer() { |
| 101 | + return new CacheManagerTestCustomizer<SimpleCacheManager>() { |
| 102 | + |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public CacheManagerCustomizer<CouchbaseCacheManager> couchbaseCacheManagerCustomizer() { |
| 108 | + return new CacheManagerTestCustomizer<CouchbaseCacheManager>() { |
| 109 | + |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + @Bean |
| 114 | + public CacheManagerCustomizer<RedisCacheManager> redisCacheManagerCustomizer() { |
| 115 | + return new CacheManagerTestCustomizer<RedisCacheManager>() { |
| 116 | + |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + public CacheManagerCustomizer<EhCacheCacheManager> ehcacheCacheManagerCustomizer() { |
| 122 | + return new CacheManagerTestCustomizer<EhCacheCacheManager>() { |
| 123 | + |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + @Bean |
| 128 | + public CacheManagerCustomizer<HazelcastCacheManager> hazelcastCacheManagerCustomizer() { |
| 129 | + return new CacheManagerTestCustomizer<HazelcastCacheManager>() { |
| 130 | + |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + @Bean |
| 135 | + public CacheManagerCustomizer<SpringEmbeddedCacheManager> infinispanCacheManagerCustomizer() { |
| 136 | + return new CacheManagerTestCustomizer<SpringEmbeddedCacheManager>() { |
| 137 | + |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + @Bean |
| 142 | + public CacheManagerCustomizer<CaffeineCacheManager> caffeineCacheManagerCustomizer() { |
| 143 | + return new CacheManagerTestCustomizer<CaffeineCacheManager>() { |
| 144 | + |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + static abstract class CacheManagerTestCustomizer<T extends CacheManager> |
| 151 | + implements CacheManagerCustomizer<T> { |
| 152 | + |
| 153 | + T cacheManager; |
| 154 | + |
| 155 | + @Override |
| 156 | + public void customize(T cacheManager) { |
| 157 | + if (this.cacheManager != null) { |
| 158 | + throw new IllegalStateException("Customized invoked twice"); |
| 159 | + } |
| 160 | + this.cacheManager = cacheManager; |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments