From c7ed5c3d4a7be36256ca1172933979155be5e5d5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 27 Feb 2018 20:45:54 +0000 Subject: [PATCH] Upgrade to EhCache 3.5.0 Closes gh-12256 --- .../AbstractCacheAutoConfigurationTests.java | 165 ++++++++++++ .../cache/CacheAutoConfigurationTests.java | 234 ++---------------- .../EhCache2CacheAutoConfigurationTests.java | 93 +++++++ .../EhCache3CacheAutoConfigurationTests.java | 77 ++++++ .../spring-boot-dependencies/pom.xml | 2 +- 5 files changed, 355 insertions(+), 216 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/AbstractCacheAutoConfigurationTests.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache2CacheAutoConfigurationTests.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache3CacheAutoConfigurationTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/AbstractCacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/AbstractCacheAutoConfigurationTests.java new file mode 100644 index 0000000000..59521bcf0d --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/AbstractCacheAutoConfigurationTests.java @@ -0,0 +1,165 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.cache; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import com.couchbase.client.spring.cache.CouchbaseCacheManager; +import com.hazelcast.spring.cache.HazelcastCacheManager; +import org.infinispan.spring.provider.SpringEmbeddedCacheManager; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ContextConsumer; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; +import org.springframework.cache.ehcache.EhCacheCacheManager; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheManager; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Base class for {@link CacheAutoConfiguration} tests. + * + * @author Andy Wilkinson + */ +abstract class AbstractCacheAutoConfigurationTests { + + protected final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); + + protected T getCacheManager( + AssertableApplicationContext loaded, Class type) { + CacheManager cacheManager = loaded.getBean(CacheManager.class); + assertThat(cacheManager).as("Wrong cache manager type").isInstanceOf(type); + return type.cast(cacheManager); + } + + @SuppressWarnings("rawtypes") + protected ContextConsumer verifyCustomizers( + String... expectedCustomizerNames) { + return (context) -> { + CacheManager cacheManager = getCacheManager(context, CacheManager.class); + List expected = new ArrayList<>( + Arrays.asList(expectedCustomizerNames)); + Map customizer = context + .getBeansOfType(CacheManagerTestCustomizer.class); + customizer.forEach((key, value) -> { + if (expected.contains(key)) { + expected.remove(key); + assertThat(value.cacheManager).isSameAs(cacheManager); + } + else { + assertThat(value.cacheManager).isNull(); + } + }); + assertThat(expected).hasSize(0); + }; + } + + @Configuration + static class CacheManagerCustomizersConfiguration { + + @Bean + public CacheManagerCustomizer allCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer simpleCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer genericCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer couchbaseCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer redisCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer ehcacheCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer hazelcastCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer infinispanCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + @Bean + public CacheManagerCustomizer caffeineCacheManagerCustomizer() { + return new CacheManagerTestCustomizer() { + + }; + } + + } + + static abstract class CacheManagerTestCustomizer + implements CacheManagerCustomizer { + + T cacheManager; + + @Override + public void customize(T cacheManager) { + if (this.cacheManager != null) { + throw new IllegalStateException("Customized invoked twice"); + } + this.cacheManager = cacheManager; + } + + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index f77524e8f6..0d517901c4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -16,11 +16,7 @@ package org.springframework.boot.autoconfigure.cache; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; -import java.util.List; -import java.util.Map; import javax.cache.Caching; import javax.cache.configuration.CompleteConfiguration; @@ -39,7 +35,6 @@ import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.spring.cache.HazelcastCacheManager; import net.sf.ehcache.Status; -import org.ehcache.jsr107.EhcacheCachingProvider; import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.jcache.embedded.JCachingProvider; import org.infinispan.spring.provider.SpringEmbeddedCacheManager; @@ -52,8 +47,6 @@ 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.assertj.AssertableApplicationContext; -import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.cache.Cache; @@ -64,7 +57,6 @@ import org.springframework.cache.caffeine.CaffeineCache; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; -import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.jcache.JCacheCacheManager; import org.springframework.cache.support.NoOpCacheManager; @@ -93,10 +85,7 @@ import static org.mockito.Mockito.verify; */ @RunWith(ModifiedClassPathRunner.class) @ClassPathExclusions("hazelcast-client-*.jar") -public class CacheAutoConfigurationTests { - - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); +public class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @Test public void noEnableCaching() { @@ -161,7 +150,8 @@ public class CacheAutoConfigurationTests { this.contextRunner .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "simple") - .run(dunno("allCacheManagerCustomizer", "simpleCacheManagerCustomizer")); + .run(verifyCustomizers("allCacheManagerCustomizer", + "simpleCacheManagerCustomizer")); } @Test @@ -206,7 +196,8 @@ public class CacheAutoConfigurationTests { this.contextRunner .withUserConfiguration(GenericCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "generic") - .run(dunno("allCacheManagerCustomizer", "genericCacheManagerCustomizer")); + .run(verifyCustomizers("allCacheManagerCustomizer", + "genericCacheManagerCustomizer")); } @Test @@ -237,8 +228,9 @@ public class CacheAutoConfigurationTests { public void couchbaseCacheWithCustomizers() { this.contextRunner .withUserConfiguration(CouchbaseCacheAndCustomizersConfiguration.class) - .withPropertyValues("spring.cache.type=" + "couchbase").run(dunno( - "allCacheManagerCustomizer", "couchbaseCacheManagerCustomizer")); + .withPropertyValues("spring.cache.type=" + "couchbase") + .run(verifyCustomizers("allCacheManagerCustomizer", + "couchbaseCacheManagerCustomizer")); } @Test @@ -324,8 +316,8 @@ public class CacheAutoConfigurationTests { @Test public void redisCacheWithCustomizers() { this.contextRunner.withUserConfiguration(RedisWithCustomizersConfiguration.class) - .withPropertyValues("spring.cache.type=" + "redis") - .run(dunno("allCacheManagerCustomizer", "redisCacheManagerCustomizer")); + .withPropertyValues("spring.cache.type=" + "redis").run(verifyCustomizers( + "allCacheManagerCustomizer", "redisCacheManagerCustomizer")); } @Test @@ -474,85 +466,6 @@ public class CacheAutoConfigurationTests { .hasMessageContaining(configLocation)); } - @Test - public void ehcacheCacheWithCaches() { - this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=ehcache").run((context) -> { - EhCacheCacheManager cacheManager = getCacheManager(context, - EhCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("cacheTest1", - "cacheTest2"); - assertThat(context.getBean(net.sf.ehcache.CacheManager.class)) - .isEqualTo(cacheManager.getCacheManager()); - }); - } - - @Test - public void ehcacheCacheWithCustomizers() { - this.contextRunner - .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) - .withPropertyValues("spring.cache.type=" + "ehcache") - .run(dunno("allCacheManagerCustomizer", "ehcacheCacheManagerCustomizer")); - } - - @Test - public void ehcacheCacheWithConfig() { - this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=ehcache", - "spring.cache.ehcache.config=cache/ehcache-override.xml") - .run((context) -> { - EhCacheCacheManager cacheManager = getCacheManager(context, - EhCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()) - .containsOnly("cacheOverrideTest1", "cacheOverrideTest2"); - }); - } - - @Test - public void ehcacheCacheWithExistingCacheManager() { - this.contextRunner.withUserConfiguration(EhCacheCustomCacheManager.class) - .withPropertyValues("spring.cache.type=ehcache").run((context) -> { - EhCacheCacheManager cacheManager = getCacheManager(context, - EhCacheCacheManager.class); - assertThat(cacheManager.getCacheManager()) - .isEqualTo(context.getBean("customEhCacheCacheManager")); - }); - } - - @Test - public void ehcache3AsJCacheWithCaches() { - String cachingProviderFqn = EhcacheCachingProvider.class.getName(); - this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=jcache", - "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar") - .run((context) -> { - JCacheCacheManager cacheManager = getCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); - } - - @Test - public void ehcache3AsJCacheWithConfig() { - String cachingProviderFqn = EhcacheCachingProvider.class.getName(); - String configLocation = "ehcache3.xml"; - this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=jcache", - "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.jcache.config=" + configLocation) - .run((context) -> { - JCacheCacheManager cacheManager = getCacheManager(context, - JCacheCacheManager.class); - - Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI()) - .isEqualTo(configResource.getURI()); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); - } - @Test public void hazelcastCacheExplicit() { this.contextRunner @@ -575,8 +488,9 @@ public class CacheAutoConfigurationTests { public void hazelcastCacheWithCustomizers() { this.contextRunner .withUserConfiguration(HazelcastCacheAndCustomizersConfiguration.class) - .withPropertyValues("spring.cache.type=" + "hazelcast").run(dunno( - "allCacheManagerCustomizer", "hazelcastCacheManagerCustomizer")); + .withPropertyValues("spring.cache.type=" + "hazelcast") + .run(verifyCustomizers("allCacheManagerCustomizer", + "hazelcastCacheManagerCustomizer")); } @Test @@ -700,8 +614,9 @@ public class CacheAutoConfigurationTests { public void infinispanCacheWithCustomizers() { this.contextRunner .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) - .withPropertyValues("spring.cache.type=" + "infinispan").run(dunno( - "allCacheManagerCustomizer", "infinispanCacheManagerCustomizer")); + .withPropertyValues("spring.cache.type=" + "infinispan") + .run(verifyCustomizers("allCacheManagerCustomizer", + "infinispanCacheManagerCustomizer")); } @Test @@ -798,8 +713,9 @@ public class CacheAutoConfigurationTests { public void caffeineCacheWithCustomizers() { this.contextRunner .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) - .withPropertyValues("spring.cache.type=" + "caffeine").run(dunno( - "allCacheManagerCustomizer", "caffeineCacheManagerCustomizer")); + .withPropertyValues("spring.cache.type=" + "caffeine") + .run(verifyCustomizers("allCacheManagerCustomizer", + "caffeineCacheManagerCustomizer")); } @Test @@ -839,35 +755,6 @@ public class CacheAutoConfigurationTests { .isEqualTo(1L); } - @SuppressWarnings("rawtypes") - private ContextConsumer dunno( - String... expectedCustomizerNames) { - return (context) -> { - CacheManager cacheManager = getCacheManager(context, CacheManager.class); - List expected = new ArrayList<>( - Arrays.asList(expectedCustomizerNames)); - Map customizer = context - .getBeansOfType(CacheManagerTestCustomizer.class); - customizer.forEach((key, value) -> { - if (expected.contains(key)) { - expected.remove(key); - assertThat(value.cacheManager).isSameAs(cacheManager); - } - else { - assertThat(value.cacheManager).isNull(); - } - }); - assertThat(expected).hasSize(0); - }; - } - - private T getCacheManager( - AssertableApplicationContext loaded, Class type) { - CacheManager cacheManager = loaded.getBean(CacheManager.class); - assertThat(cacheManager).as("Wrong cache manager type").isInstanceOf(type); - return type.cast(cacheManager); - } - @Configuration static class EmptyConfiguration { @@ -1122,87 +1009,4 @@ public class CacheAutoConfigurationTests { } - @Configuration - static class CacheManagerCustomizersConfiguration { - - @Bean - public CacheManagerCustomizer allCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer simpleCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer genericCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer couchbaseCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer redisCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer ehcacheCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer hazelcastCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer infinispanCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - @Bean - public CacheManagerCustomizer caffeineCacheManagerCustomizer() { - return new CacheManagerTestCustomizer() { - - }; - } - - } - - static abstract class CacheManagerTestCustomizer - implements CacheManagerCustomizer { - - private T cacheManager; - - @Override - public void customize(T cacheManager) { - if (this.cacheManager != null) { - throw new IllegalStateException("Customized invoked twice"); - } - this.cacheManager = cacheManager; - } - - } - } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache2CacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache2CacheAutoConfigurationTests.java new file mode 100644 index 0000000000..ee40de1de6 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache2CacheAutoConfigurationTests.java @@ -0,0 +1,93 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.cache; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.DefaultCacheAndCustomizersConfiguration; +import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.DefaultCacheConfiguration; +import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.EhCacheCustomCacheManager; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; +import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; +import org.springframework.cache.ehcache.EhCacheCacheManager; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CacheAutoConfigurationTests} with EhCache 2. + * + * @author Stephane Nicoll + * @author Andy Wilkinson + */ +@RunWith(ModifiedClassPathRunner.class) +@ClassPathExclusions("ehcache-3*.jar") +public class EhCache2CacheAutoConfigurationTests + extends AbstractCacheAutoConfigurationTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); + + @Test + public void ehCacheWithCaches() { + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=ehcache").run((context) -> { + EhCacheCacheManager cacheManager = getCacheManager(context, + EhCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("cacheTest1", + "cacheTest2"); + assertThat(context.getBean(net.sf.ehcache.CacheManager.class)) + .isEqualTo(cacheManager.getCacheManager()); + }); + } + + @Test + public void ehCacheWithCustomizers() { + this.contextRunner + .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) + .withPropertyValues("spring.cache.type=" + "ehcache") + .run(verifyCustomizers("allCacheManagerCustomizer", + "ehcacheCacheManagerCustomizer")); + } + + @Test + public void ehCacheWithConfig() { + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=ehcache", + "spring.cache.ehcache.config=cache/ehcache-override.xml") + .run((context) -> { + EhCacheCacheManager cacheManager = getCacheManager(context, + EhCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()) + .containsOnly("cacheOverrideTest1", "cacheOverrideTest2"); + }); + } + + @Test + public void ehCacheWithExistingCacheManager() { + this.contextRunner.withUserConfiguration(EhCacheCustomCacheManager.class) + .withPropertyValues("spring.cache.type=ehcache").run((context) -> { + EhCacheCacheManager cacheManager = getCacheManager(context, + EhCacheCacheManager.class); + assertThat(cacheManager.getCacheManager()) + .isEqualTo(context.getBean("customEhCacheCacheManager")); + }); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache3CacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache3CacheAutoConfigurationTests.java new file mode 100644 index 0000000000..8600e2e9d2 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache3CacheAutoConfigurationTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.cache; + +import org.ehcache.jsr107.EhcacheCachingProvider; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.DefaultCacheConfiguration; +import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; +import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; +import org.springframework.cache.jcache.JCacheCacheManager; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link CacheAutoConfigurationTests} with EhCache 3. + * + * @author Stephane Nicoll + * @author Andy Wilkinson + */ +@RunWith(ModifiedClassPathRunner.class) +@ClassPathExclusions("ehcache-2*.jar") +public class EhCache3CacheAutoConfigurationTests + extends AbstractCacheAutoConfigurationTests { + + @Test + public void ehcache3AsJCacheWithCaches() { + String cachingProviderFqn = EhcacheCachingProvider.class.getName(); + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, + "spring.cache.cacheNames[0]=foo", + "spring.cache.cacheNames[1]=bar") + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); + } + + @Test + public void ehcache3AsJCacheWithConfig() { + String cachingProviderFqn = EhcacheCachingProvider.class.getName(); + String configLocation = "ehcache3.xml"; + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, + "spring.cache.jcache.config=" + configLocation) + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, + JCacheCacheManager.class); + + Resource configResource = new ClassPathResource(configLocation); + assertThat(cacheManager.getCacheManager().getURI()) + .isEqualTo(configResource.getURI()); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); + } + +} diff --git a/spring-boot-project/spring-boot-dependencies/pom.xml b/spring-boot-project/spring-boot-dependencies/pom.xml index 76593437ea..b61d5496d3 100644 --- a/spring-boot-project/spring-boot-dependencies/pom.xml +++ b/spring-boot-project/spring-boot-dependencies/pom.xml @@ -47,7 +47,7 @@ 1.6.1 3.2.6 2.10.4 - 3.4.0 + 3.5.0 2.0.3 5.0.7 2.3.27-incubating