|
|
|
|
@ -16,80 +16,86 @@
@@ -16,80 +16,86 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.cache; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import org.junit.After; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.boot.test.util.EnvironmentTestUtils; |
|
|
|
|
import org.springframework.cache.CacheManager; |
|
|
|
|
import org.springframework.cache.annotation.EnableCaching; |
|
|
|
|
import org.springframework.cache.caffeine.CaffeineCacheManager; |
|
|
|
|
import org.springframework.cache.concurrent.ConcurrentMapCacheManager; |
|
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
import static org.mockito.Mockito.verifyZeroInteractions; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Stephane Nicoll |
|
|
|
|
*/ |
|
|
|
|
public class CacheManagerCustomizersTests { |
|
|
|
|
|
|
|
|
|
private AnnotationConfigApplicationContext context; |
|
|
|
|
|
|
|
|
|
@After |
|
|
|
|
public void tearDown() { |
|
|
|
|
if (this.context != null) { |
|
|
|
|
this.context.close(); |
|
|
|
|
} |
|
|
|
|
@Test |
|
|
|
|
public void customizeWithNullCustomizersShouldDoNothing() { |
|
|
|
|
new CacheManagerCustomizers(null) |
|
|
|
|
.customize(mock(CacheManager.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void customizeSimpleCacheManager() { |
|
|
|
|
load(SimpleConfiguration.class, "spring.cache.type=simple"); |
|
|
|
|
ConcurrentMapCacheManager cacheManager = this.context |
|
|
|
|
.getBean(ConcurrentMapCacheManager.class); |
|
|
|
|
CacheManagerCustomizers customizers = new CacheManagerCustomizers( |
|
|
|
|
Collections.singletonList(new CacheNamesCacheManagerCustomizer())); |
|
|
|
|
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(); |
|
|
|
|
customizers.customize(cacheManager); |
|
|
|
|
assertThat(cacheManager.getCacheNames()).containsOnly("one", "two"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void customizeNoConfigurableApplicationContext() { |
|
|
|
|
CacheManagerCustomizers invoker = new CacheManagerCustomizers(); |
|
|
|
|
ApplicationContext context = mock(ApplicationContext.class); |
|
|
|
|
invoker.setApplicationContext(context); |
|
|
|
|
invoker.customize(mock(CacheManager.class)); |
|
|
|
|
verifyZeroInteractions(context); |
|
|
|
|
public void customizeShouldCheckGeneric() throws Exception { |
|
|
|
|
List<TestCustomizer<?>> list = new ArrayList<TestCustomizer<?>>(); |
|
|
|
|
list.add(new TestCustomizer<CacheManager>()); |
|
|
|
|
list.add(new TestConcurrentMapCacheManagerCustomizer()); |
|
|
|
|
CacheManagerCustomizers customizers = new CacheManagerCustomizers(list); |
|
|
|
|
customizers.customize(mock(CacheManager.class)); |
|
|
|
|
assertThat(list.get(0).getCount()).isEqualTo(1); |
|
|
|
|
assertThat(list.get(1).getCount()).isEqualTo(0); |
|
|
|
|
customizers.customize(mock(ConcurrentMapCacheManager.class)); |
|
|
|
|
assertThat(list.get(0).getCount()).isEqualTo(2); |
|
|
|
|
assertThat(list.get(1).getCount()).isEqualTo(1); |
|
|
|
|
customizers.customize(mock(CaffeineCacheManager.class)); |
|
|
|
|
assertThat(list.get(0).getCount()).isEqualTo(3); |
|
|
|
|
assertThat(list.get(1).getCount()).isEqualTo(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void load(Class<?> config, String... environment) { |
|
|
|
|
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); |
|
|
|
|
EnvironmentTestUtils.addEnvironment(applicationContext, environment); |
|
|
|
|
applicationContext.register(config); |
|
|
|
|
applicationContext.register(CacheAutoConfiguration.class); |
|
|
|
|
applicationContext.refresh(); |
|
|
|
|
this.context = applicationContext; |
|
|
|
|
static class CacheNamesCacheManagerCustomizer |
|
|
|
|
implements CacheManagerCustomizer<ConcurrentMapCacheManager> { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void customize(ConcurrentMapCacheManager cacheManager) { |
|
|
|
|
cacheManager.setCacheNames(Arrays.asList("one", "two")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
@EnableCaching |
|
|
|
|
static class SimpleConfiguration { |
|
|
|
|
private static class TestCustomizer<T extends CacheManager> |
|
|
|
|
implements CacheManagerCustomizer<T> { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() { |
|
|
|
|
return new CacheManagerCustomizer<ConcurrentMapCacheManager>() { |
|
|
|
|
private int count; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void customize(ConcurrentMapCacheManager cacheManager) { |
|
|
|
|
cacheManager.setCacheNames(Arrays.asList("one", "two")); |
|
|
|
|
} |
|
|
|
|
@Override |
|
|
|
|
public void customize(T cacheManager) { |
|
|
|
|
this.count++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
public int getCount() { |
|
|
|
|
return this.count; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static class TestConcurrentMapCacheManagerCustomizer |
|
|
|
|
extends TestCustomizer<ConcurrentMapCacheManager> { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|