From dd0ce54425cff4338f2237d833eafd3558d02011 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sun, 2 Jul 2017 15:08:09 +0100 Subject: [PATCH] Improve the type-safety of ContextLoader for servlet and reactive web --- ...HealthIndicatorAutoConfigurationTests.java | 8 +- .../cache/CacheAutoConfigurationTests.java | 583 ++++++++++-------- ...HazelcastAutoConfigurationClientTests.java | 58 +- ...HazelcastAutoConfigurationServerTests.java | 81 +-- .../HazelcastAutoConfigurationTests.java | 8 +- .../DataSourceAutoConfigurationTests.java | 107 ++-- .../jms/JmsAutoConfigurationTests.java | 369 ++++++----- .../ActiveMQAutoConfigurationTests.java | 74 ++- .../ArtemisAutoConfigurationTests.java | 204 +++--- .../HttpHandlerAutoConfigurationTests.java | 5 +- .../servlet/WebMvcAutoConfigurationTests.java | 27 +- .../WebServicesAutoConfigurationTests.java | 5 +- .../TestDatabaseAutoConfigurationTests.java | 5 +- ...abaseAutoConfigurationNoEmbeddedTests.java | 5 +- .../boot/test/context/ContextConsumer.java | 11 +- .../boot/test/context/ContextLoader.java | 249 ++------ .../test/context/StandardContextLoader.java | 300 +++++++++ .../StandardContextLoaderTests.java} | 101 +-- 18 files changed, 1240 insertions(+), 960 deletions(-) create mode 100644 spring-boot-test/src/main/java/org/springframework/boot/test/context/StandardContextLoader.java rename spring-boot-test/src/test/java/org/springframework/boot/test/{rule/ContextLoaderTests.java => context/StandardContextLoaderTests.java} (66%) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java index 3c0001cf621..5b6192d15ed 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java @@ -58,6 +58,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.ContextConsumer; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.core.CassandraOperations; @@ -79,8 +80,9 @@ import static org.mockito.Mockito.mock; */ public class HealthIndicatorAutoConfigurationTests { - public final ContextLoader contextLoader = new ContextLoader().autoConfig( - HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class); + public final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(HealthIndicatorAutoConfiguration.class, + ManagementServerProperties.class); @Test public void defaultHealthIndicator() { @@ -378,7 +380,7 @@ public class HealthIndicatorAutoConfigurationTests { .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); } - private ContextConsumer hasSingleHealthIndicator( + private ContextConsumer hasSingleHealthIndicator( Class type) { return context -> { Map beans = context diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 588ce5aff4a..0605e988f47 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -73,6 +73,7 @@ import org.springframework.cache.jcache.JCacheCacheManager; import org.springframework.cache.support.NoOpCacheManager; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -100,8 +101,8 @@ public class CacheAutoConfigurationTests { @Rule public final ExpectedException thrown = ExpectedException.none(); - private final ContextLoader contextLoader = new ContextLoader().autoConfig( - CacheAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(CacheAutoConfiguration.class); @Test public void noEnableCaching() { @@ -143,27 +144,27 @@ public class CacheAutoConfigurationTests { public void customCacheResolverCanBeDefined() throws Exception { this.contextLoader.config(SpecificCacheResolverConfiguration.class) .env("spring.cache.type=simple").load(context -> { - validateCacheManager(context, ConcurrentMapCacheManager.class); - assertThat(context.getBeansOfType(CacheResolver.class)).hasSize(1); - }); + validateCacheManager(context, ConcurrentMapCacheManager.class); + assertThat(context.getBeansOfType(CacheResolver.class)).hasSize(1); + }); } @Test public void notSupportedCachingMode() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=foobar").loadAndFail(BeanCreationException.class, - ex -> assertThat(ex.getMessage()).contains( - "Failed to bind properties under 'spring.cache.type'")); + ex -> assertThat(ex.getMessage()).contains( + "Failed to bind properties under 'spring.cache.type'")); } @Test public void simpleCacheExplicit() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=simple").load(context -> { - ConcurrentMapCacheManager cacheManager = validateCacheManager(context, - ConcurrentMapCacheManager.class); - assertThat(cacheManager.getCacheNames()).isEmpty(); - }); + ConcurrentMapCacheManager cacheManager = validateCacheManager(context, + ConcurrentMapCacheManager.class); + assertThat(cacheManager.getCacheNames()).isEmpty(); + }); } @Test @@ -176,11 +177,12 @@ public class CacheAutoConfigurationTests { public void simpleCacheExplicitWithCacheNames() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=simple", "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - ConcurrentMapCacheManager cacheManager = validateCacheManager(context, - ConcurrentMapCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + ConcurrentMapCacheManager cacheManager = validateCacheManager(context, + ConcurrentMapCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test @@ -200,8 +202,8 @@ public class CacheAutoConfigurationTests { public void genericCacheExplicit() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=generic").loadAndFail(BeanCreationException.class, - ex -> assertThat(ex.getMessage()).contains( - "No cache manager could be auto-configured", "GENERIC")); + ex -> assertThat(ex.getMessage()).contains( + "No cache manager could be auto-configured", "GENERIC")); } @Test @@ -214,24 +216,24 @@ public class CacheAutoConfigurationTests { public void genericCacheExplicitWithCaches() { this.contextLoader.config(GenericCacheConfiguration.class) .env("spring.cache.type=generic").load(context -> { - SimpleCacheManager cacheManager = validateCacheManager(context, - SimpleCacheManager.class); - assertThat(cacheManager.getCache("first")) - .isEqualTo(context.getBean("firstCache")); - assertThat(cacheManager.getCache("second")) - .isEqualTo(context.getBean("secondCache")); - assertThat(cacheManager.getCacheNames()).hasSize(2); - }); + SimpleCacheManager cacheManager = validateCacheManager(context, + SimpleCacheManager.class); + assertThat(cacheManager.getCache("first")) + .isEqualTo(context.getBean("firstCache")); + assertThat(cacheManager.getCache("second")) + .isEqualTo(context.getBean("secondCache")); + assertThat(cacheManager.getCacheNames()).hasSize(2); + }); } @Test public void couchbaseCacheExplicit() { this.contextLoader.config(CouchbaseCacheConfiguration.class) .env("spring.cache.type=couchbase").load(context -> { - CouchbaseCacheManager cacheManager = validateCacheManager(context, - CouchbaseCacheManager.class); - assertThat(cacheManager.getCacheNames()).isEmpty(); - }); + CouchbaseCacheManager cacheManager = validateCacheManager(context, + CouchbaseCacheManager.class); + assertThat(cacheManager.getCacheNames()).isEmpty(); + }); } @Test @@ -244,44 +246,46 @@ public class CacheAutoConfigurationTests { public void couchbaseCacheExplicitWithCaches() { this.contextLoader.config(CouchbaseCacheConfiguration.class) .env("spring.cache.type=couchbase", "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - CouchbaseCacheManager cacheManager = validateCacheManager(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(context.getBean("bucket")); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + CouchbaseCacheManager cacheManager = validateCacheManager(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(context.getBean("bucket")); + }); } @Test public void couchbaseCacheExplicitWithTtl() { this.contextLoader.config(CouchbaseCacheConfiguration.class) .env("spring.cache.type=couchbase", "spring.cache.cacheNames=foo,bar", - "spring.cache.couchbase.expiration=2000").load(context -> { - CouchbaseCacheManager cacheManager = validateCacheManager(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(context.getBean("bucket")); - }); + "spring.cache.couchbase.expiration=2000") + .load(context -> { + CouchbaseCacheManager cacheManager = validateCacheManager(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(context.getBean("bucket")); + }); } @Test public void redisCacheExplicit() { this.contextLoader.config(RedisCacheConfiguration.class) .env("spring.cache.type=redis").load(context -> { - RedisCacheManager cacheManager = validateCacheManager(context, - RedisCacheManager.class); - assertThat(cacheManager.getCacheNames()).isEmpty(); - assertThat((Boolean) new DirectFieldAccessor(cacheManager) - .getPropertyValue("usePrefix")).isTrue(); - }); + RedisCacheManager cacheManager = validateCacheManager(context, + RedisCacheManager.class); + assertThat(cacheManager.getCacheNames()).isEmpty(); + assertThat((Boolean) new DirectFieldAccessor(cacheManager) + .getPropertyValue("usePrefix")).isTrue(); + }); } @Test @@ -294,97 +298,104 @@ public class CacheAutoConfigurationTests { public void redisCacheExplicitWithCaches() { this.contextLoader.config(RedisCacheConfiguration.class) .env("spring.cache.type=redis", "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - RedisCacheManager cacheManager = validateCacheManager(context, - RedisCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + RedisCacheManager cacheManager = validateCacheManager(context, + RedisCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test public void noOpCacheExplicit() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=none").load(context -> { - NoOpCacheManager cacheManager = validateCacheManager(context, - NoOpCacheManager.class); - assertThat(cacheManager.getCacheNames()).isEmpty(); - }); + NoOpCacheManager cacheManager = validateCacheManager(context, + NoOpCacheManager.class); + assertThat(cacheManager.getCacheNames()).isEmpty(); + }); } @Test public void jCacheCacheNoProviderExplicit() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=jcache").loadAndFail(ex -> { - assertThat(ex).isInstanceOf(BeanCreationException.class); - assertThat(ex.getMessage()).contains( - "No cache manager could be auto-configured", "JCACHE"); - }); + assertThat(ex).isInstanceOf(BeanCreationException.class); + assertThat(ex.getMessage()).contains( + "No cache manager could be auto-configured", "JCACHE"); + }); } @Test public void jCacheCacheWithProvider() { String cachingProviderFqn = MockCachingProvider.class.getName(); this.contextLoader.config(DefaultCacheConfiguration.class) - .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" - + cachingProviderFqn).load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).isEmpty(); - assertThat(context.getBean(javax.cache.CacheManager.class)) - .isEqualTo(cacheManager.getCacheManager()); - }); + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn) + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).isEmpty(); + assertThat(context.getBean(javax.cache.CacheManager.class)) + .isEqualTo(cacheManager.getCacheManager()); + }); } @Test public void jCacheCacheWithCaches() { String cachingProviderFqn = MockCachingProvider.class.getName(); this.contextLoader.config(DefaultCacheConfiguration.class) - .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test public void jCacheCacheWithCachesAndCustomConfig() { String cachingProviderFqn = MockCachingProvider.class.getName(); this.contextLoader.config(JCacheCustomConfiguration.class) - .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=one", - "spring.cache.cacheNames[1]=two").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("one", "two"); - CompleteConfiguration defaultCacheConfiguration = context - .getBean(CompleteConfiguration.class); - verify(cacheManager.getCacheManager()).createCache("one", - defaultCacheConfiguration); - verify(cacheManager.getCacheManager()).createCache("two", - defaultCacheConfiguration); - }); + "spring.cache.cacheNames[1]=two") + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("one", "two"); + CompleteConfiguration defaultCacheConfiguration = context + .getBean(CompleteConfiguration.class); + verify(cacheManager.getCacheManager()).createCache("one", + defaultCacheConfiguration); + verify(cacheManager.getCacheManager()).createCache("two", + defaultCacheConfiguration); + }); } @Test public void jCacheCacheWithExistingJCacheManager() { this.contextLoader.config(JCacheCustomCacheManager.class) .env("spring.cache.type=jcache").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheManager()) - .isEqualTo(context.getBean("customJCacheCacheManager")); - }); + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheManager()) + .isEqualTo(context.getBean("customJCacheCacheManager")); + }); } @Test public void jCacheCacheWithUnknownProvider() { String wrongCachingProviderFqn = "org.acme.FooBar"; this.contextLoader.config(DefaultCacheConfiguration.class) - .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" - + wrongCachingProviderFqn).loadAndFail(BeanCreationException.class, - ex -> assertThat(ex.getMessage().contains(wrongCachingProviderFqn))); + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + wrongCachingProviderFqn) + .loadAndFail(BeanCreationException.class, ex -> assertThat( + ex.getMessage().contains(wrongCachingProviderFqn))); } @Test @@ -392,14 +403,16 @@ public class CacheAutoConfigurationTests { String cachingProviderFqn = MockCachingProvider.class.getName(); String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; this.contextLoader.config(JCacheCustomConfiguration.class) - .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.jcache.config=" + configLocation).load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI()) - .isEqualTo(configResource.getURI()); - }); + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, + "spring.cache.jcache.config=" + configLocation) + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + Resource configResource = new ClassPathResource(configLocation); + assertThat(cacheManager.getCacheManager().getURI()) + .isEqualTo(configResource.getURI()); + }); } @Test @@ -410,21 +423,22 @@ public class CacheAutoConfigurationTests { .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation) - .loadAndFail(BeanCreationException.class, ex -> - assertThat(ex.getMessage()).contains("does not exist", configLocation)); + .loadAndFail(BeanCreationException.class, + ex -> assertThat(ex.getMessage()).contains("does not exist", + configLocation)); } @Test public void ehcacheCacheWithCaches() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=ehcache").load(context -> { - EhCacheCacheManager cacheManager = validateCacheManager(context, - EhCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly( - "cacheTest1", "cacheTest2"); - assertThat(context.getBean(net.sf.ehcache.CacheManager.class)) - .isEqualTo(cacheManager.getCacheManager()); - }); + EhCacheCacheManager cacheManager = validateCacheManager(context, + EhCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("cacheTest1", + "cacheTest2"); + assertThat(context.getBean(net.sf.ehcache.CacheManager.class)) + .isEqualTo(cacheManager.getCacheManager()); + }); } @Test @@ -441,8 +455,8 @@ public class CacheAutoConfigurationTests { .load(context -> { EhCacheCacheManager cacheManager = validateCacheManager(context, EhCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("cacheOverrideTest1", - "cacheOverrideTest2"); + assertThat(cacheManager.getCacheNames()) + .containsOnly("cacheOverrideTest1", "cacheOverrideTest2"); }); } @@ -450,11 +464,11 @@ public class CacheAutoConfigurationTests { public void ehcacheCacheWithExistingCacheManager() { this.contextLoader.config(EhCacheCustomCacheManager.class) .env("spring.cache.type=ehcache").load(context -> { - EhCacheCacheManager cacheManager = validateCacheManager(context, - EhCacheCacheManager.class); - assertThat(cacheManager.getCacheManager()) - .isEqualTo(context.getBean("customEhCacheCacheManager")); - }); + EhCacheCacheManager cacheManager = validateCacheManager(context, + EhCacheCacheManager.class); + assertThat(cacheManager.getCacheManager()) + .isEqualTo(context.getBean("customEhCacheCacheManager")); + }); } @Test @@ -464,11 +478,12 @@ public class CacheAutoConfigurationTests { .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test @@ -478,15 +493,16 @@ public class CacheAutoConfigurationTests { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.jcache.config=" + configLocation).load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - - Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI()) - .isEqualTo(configResource.getURI()); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.jcache.config=" + configLocation) + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + + Resource configResource = new ClassPathResource(configLocation); + assertThat(cacheManager.getCacheManager().getURI()) + .isEqualTo(configResource.getURI()); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test @@ -494,14 +510,15 @@ public class CacheAutoConfigurationTests { this.contextLoader.autoConfigFirst(HazelcastAutoConfiguration.class) .config(DefaultCacheConfiguration.class) .env("spring.cache.type=hazelcast").load(context -> { - HazelcastCacheManager cacheManager = validateCacheManager(context, - HazelcastCacheManager.class); - // NOTE: the hazelcast implementation knows about a cache in a lazy manner. - cacheManager.getCache("defaultCache"); - assertThat(cacheManager.getCacheNames()).containsOnly("defaultCache"); - assertThat(context.getBean(HazelcastInstance.class)) - .isEqualTo(cacheManager.getHazelcastInstance()); - }); + HazelcastCacheManager cacheManager = validateCacheManager(context, + HazelcastCacheManager.class); + // NOTE: the hazelcast implementation knows about a cache in a lazy + // manner. + cacheManager.getCache("defaultCache"); + assertThat(cacheManager.getCacheNames()).containsOnly("defaultCache"); + assertThat(context.getBean(HazelcastInstance.class)) + .isEqualTo(cacheManager.getHazelcastInstance()); + }); } @Test @@ -514,28 +531,32 @@ public class CacheAutoConfigurationTests { public void hazelcastCacheWithExistingHazelcastInstance() { this.contextLoader.config(HazelcastCustomHazelcastInstance.class) .env("spring.cache.type=hazelcast").load(context -> { - HazelcastCacheManager cacheManager = validateCacheManager(context, - HazelcastCacheManager.class); - assertThat(cacheManager.getHazelcastInstance()) - .isEqualTo(context.getBean("customHazelcastInstance")); - }); + HazelcastCacheManager cacheManager = validateCacheManager(context, + HazelcastCacheManager.class); + assertThat(cacheManager.getHazelcastInstance()) + .isEqualTo(context.getBean("customHazelcastInstance")); + }); } @Test public void hazelcastCacheWithHazelcastAutoConfiguration() throws IOException { String hazelcastConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; this.contextLoader.autoConfigFirst(HazelcastAutoConfiguration.class) - .config(DefaultCacheConfiguration.class).env("spring.cache.type=hazelcast", - "spring.hazelcast.config=" + hazelcastConfig).load(context -> { - HazelcastCacheManager cacheManager = validateCacheManager(context, - HazelcastCacheManager.class); - HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); - assertThat(cacheManager.getHazelcastInstance()).isSameAs(hazelcastInstance); - assertThat(hazelcastInstance.getConfig().getConfigurationFile()) - .isEqualTo(new ClassPathResource(hazelcastConfig).getFile()); - assertThat(cacheManager.getCache("foobar")).isNotNull(); - assertThat(cacheManager.getCacheNames()).containsOnly("foobar"); - }); + .config(DefaultCacheConfiguration.class) + .env("spring.cache.type=hazelcast", + "spring.hazelcast.config=" + hazelcastConfig) + .load(context -> { + HazelcastCacheManager cacheManager = validateCacheManager(context, + HazelcastCacheManager.class); + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(cacheManager.getHazelcastInstance()) + .isSameAs(hazelcastInstance); + assertThat(hazelcastInstance.getConfig().getConfigurationFile()) + .isEqualTo(new ClassPathResource(hazelcastConfig).getFile()); + assertThat(cacheManager.getCache("foobar")).isNotNull(); + assertThat(cacheManager.getCacheNames()).containsOnly("foobar"); + }); } @Test @@ -546,12 +567,14 @@ public class CacheAutoConfigurationTests { .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", + "bar"); + assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1); + }); } finally { Caching.getCachingProvider(cachingProviderFqn).close(); @@ -566,15 +589,16 @@ public class CacheAutoConfigurationTests { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.jcache.config=" + configLocation).load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - - Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI()) - .isEqualTo(configResource.getURI()); - assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1); - }); + "spring.cache.jcache.config=" + configLocation) + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + + Resource configResource = new ClassPathResource(configLocation); + assertThat(cacheManager.getCacheManager().getURI()) + .isEqualTo(configResource.getURI()); + assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1); + }); } finally { Caching.getCachingProvider(cachingProviderFqn).close(); @@ -585,31 +609,37 @@ public class CacheAutoConfigurationTests { public void hazelcastAsJCacheWithExistingHazelcastInstance() throws IOException { String cachingProviderFqn = HazelcastCachingProvider.class.getName(); this.contextLoader.autoConfig(HazelcastAutoConfiguration.class) - .config(DefaultCacheConfiguration.class).env("spring.cache.type=jcache", - "spring.cache.jcache.provider=" + cachingProviderFqn).load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - javax.cache.CacheManager jCacheManager = cacheManager.getCacheManager(); - assertThat(jCacheManager) - .isInstanceOf(com.hazelcast.cache.HazelcastCacheManager.class); - assertThat(context.getBeansOfType(HazelcastInstance.class)).hasSize(1); - HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); - assertThat(((com.hazelcast.cache.HazelcastCacheManager) jCacheManager) - .getHazelcastInstance()).isSameAs(hazelcastInstance); - assertThat(hazelcastInstance.getName()).isEqualTo("default-instance"); - assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1); - }); + .config(DefaultCacheConfiguration.class) + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn) + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + javax.cache.CacheManager jCacheManager = cacheManager + .getCacheManager(); + assertThat(jCacheManager).isInstanceOf( + com.hazelcast.cache.HazelcastCacheManager.class); + assertThat(context.getBeansOfType(HazelcastInstance.class)) + .hasSize(1); + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(((com.hazelcast.cache.HazelcastCacheManager) jCacheManager) + .getHazelcastInstance()).isSameAs(hazelcastInstance); + assertThat(hazelcastInstance.getName()).isEqualTo("default-instance"); + assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1); + }); } @Test public void infinispanCacheWithConfig() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=infinispan", - "spring.cache.infinispan.config=infinispan.xml").load(context -> { - SpringEmbeddedCacheManager cacheManager = validateCacheManager(context, - SpringEmbeddedCacheManager.class); - assertThat(cacheManager.getCacheNames()).contains("foo", "bar"); - }); + "spring.cache.infinispan.config=infinispan.xml") + .load(context -> { + SpringEmbeddedCacheManager cacheManager = validateCacheManager( + context, SpringEmbeddedCacheManager.class); + assertThat(cacheManager.getCacheNames()).contains("foo", "bar"); + }); } @Test @@ -622,25 +652,27 @@ public class CacheAutoConfigurationTests { public void infinispanCacheWithCaches() { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=infinispan", "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - SpringEmbeddedCacheManager cacheManager = validateCacheManager(context, - SpringEmbeddedCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + SpringEmbeddedCacheManager cacheManager = validateCacheManager( + context, SpringEmbeddedCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test public void infinispanCacheWithCachesAndCustomConfig() { this.contextLoader.config(InfinispanCustomConfiguration.class) .env("spring.cache.type=infinispan", "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - SpringEmbeddedCacheManager cacheManager = validateCacheManager(context, - SpringEmbeddedCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - ConfigurationBuilder defaultConfigurationBuilder = context - .getBean(ConfigurationBuilder.class); - verify(defaultConfigurationBuilder, times(2)).build(); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + SpringEmbeddedCacheManager cacheManager = validateCacheManager( + context, SpringEmbeddedCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + ConfigurationBuilder defaultConfigurationBuilder = context + .getBean(ConfigurationBuilder.class); + verify(defaultConfigurationBuilder, times(2)).build(); + }); } @Test @@ -650,11 +682,12 @@ public class CacheAutoConfigurationTests { .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); - }); + "spring.cache.cacheNames[1]=bar") + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); + }); } @Test @@ -664,30 +697,33 @@ public class CacheAutoConfigurationTests { this.contextLoader.config(DefaultCacheConfiguration.class) .env("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.jcache.config=" + configLocation).load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); + "spring.cache.jcache.config=" + configLocation) + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); - Resource configResource = new ClassPathResource(configLocation); - assertThat(cacheManager.getCacheManager().getURI()) - .isEqualTo(configResource.getURI()); - }); + Resource configResource = new ClassPathResource(configLocation); + assertThat(cacheManager.getCacheManager().getURI()) + .isEqualTo(configResource.getURI()); + }); } @Test public void jCacheCacheWithCachesAndCustomizer() { String cachingProviderFqn = HazelcastCachingProvider.class.getName(); try { - this.contextLoader.config(JCacheWithCustomizerConfiguration.class).env( - "spring.cache.type=jcache", - "spring.cache.jcache.provider=" + cachingProviderFqn, - "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar").load(context -> { - JCacheCacheManager cacheManager = validateCacheManager(context, - JCacheCacheManager.class); - // see customizer - assertThat(cacheManager.getCacheNames()).containsOnly("foo", "custom1"); - }); + this.contextLoader.config(JCacheWithCustomizerConfiguration.class) + .env("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn, + "spring.cache.cacheNames[0]=foo", + "spring.cache.cacheNames[1]=bar") + .load(context -> { + JCacheCacheManager cacheManager = validateCacheManager(context, + JCacheCacheManager.class); + // see customizer + assertThat(cacheManager.getCacheNames()).containsOnly("foo", + "custom1"); + }); } finally { Caching.getCachingProvider(cachingProviderFqn).close(); @@ -697,17 +733,17 @@ public class CacheAutoConfigurationTests { @Test public void caffeineCacheWithExplicitCaches() { this.contextLoader.config(DefaultCacheConfiguration.class) - .env("spring.cache.type=caffeine", - "spring.cache.cacheNames=foo").load(context -> { - CaffeineCacheManager cacheManager = validateCacheManager(context, - CaffeineCacheManager.class); - assertThat(cacheManager.getCacheNames()).containsOnly("foo"); - Cache foo = cacheManager.getCache("foo"); - foo.get("1"); - // See next tests: no spec given so stats should be disabled - assertThat(((CaffeineCache) foo).getNativeCache().stats().missCount()) - .isEqualTo(0L); - }); + .env("spring.cache.type=caffeine", "spring.cache.cacheNames=foo") + .load(context -> { + CaffeineCacheManager cacheManager = validateCacheManager(context, + CaffeineCacheManager.class); + assertThat(cacheManager.getCacheNames()).containsOnly("foo"); + Cache foo = cacheManager.getCache("foo"); + foo.get("1"); + // See next tests: no spec given so stats should be disabled + assertThat(((CaffeineCache) foo).getNativeCache().stats().missCount()) + .isEqualTo(0L); + }); } @Test @@ -718,26 +754,24 @@ public class CacheAutoConfigurationTests { @Test public void caffeineCacheWithExplicitCacheBuilder() { - this.contextLoader.config(CaffeineCacheBuilderConfiguration.class).env( - "spring.cache.type=caffeine", "spring.cache.cacheNames=foo,bar") + this.contextLoader.config(CaffeineCacheBuilderConfiguration.class) + .env("spring.cache.type=caffeine", "spring.cache.cacheNames=foo,bar") .load(this::validateCaffeineCacheWithStats); } @Test public void caffeineCacheExplicitWithSpec() { - this.contextLoader.config(CaffeineCacheSpecConfiguration.class).env( - "spring.cache.type=caffeine", "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar") + this.contextLoader.config(CaffeineCacheSpecConfiguration.class) + .env("spring.cache.type=caffeine", "spring.cache.cacheNames[0]=foo", + "spring.cache.cacheNames[1]=bar") .load(this::validateCaffeineCacheWithStats); } @Test public void caffeineCacheExplicitWithSpecString() { - this.contextLoader.config(DefaultCacheConfiguration.class) - .env("spring.cache.type=caffeine", - "spring.cache.caffeine.spec=recordStats", - "spring.cache.cacheNames[0]=foo", - "spring.cache.cacheNames[1]=bar") + this.contextLoader.config(DefaultCacheConfiguration.class).env( + "spring.cache.type=caffeine", "spring.cache.caffeine.spec=recordStats", + "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") .load(this::validateCaffeineCacheWithStats); } @@ -761,24 +795,27 @@ public class CacheAutoConfigurationTests { @SuppressWarnings("rawtypes") private void testCustomizers(Class config, String cacheType, String... expectedCustomizerNames) { - this.contextLoader.config(config) - .env("spring.cache.type=" + cacheType).load(context -> { - CacheManager cacheManager = validateCacheManager(context, CacheManager.class); - List expected = new ArrayList<>(); - expected.addAll(Arrays.asList(expectedCustomizerNames)); - Map map = context - .getBeansOfType(CacheManagerTestCustomizer.class); - for (Map.Entry entry : map.entrySet()) { - if (expected.contains(entry.getKey())) { - expected.remove(entry.getKey()); - assertThat(entry.getValue().cacheManager).isSameAs(cacheManager); - } - else { - assertThat(entry.getValue().cacheManager).isNull(); - } - } - assertThat(expected).hasSize(0); - }); + this.contextLoader.config(config).env("spring.cache.type=" + cacheType) + .load(context -> { + CacheManager cacheManager = validateCacheManager(context, + CacheManager.class); + List expected = new ArrayList<>(); + expected.addAll(Arrays.asList(expectedCustomizerNames)); + Map map = context + .getBeansOfType(CacheManagerTestCustomizer.class); + for (Map.Entry entry : map + .entrySet()) { + if (expected.contains(entry.getKey())) { + expected.remove(entry.getKey()); + assertThat(entry.getValue().cacheManager) + .isSameAs(cacheManager); + } + else { + assertThat(entry.getValue().cacheManager).isNull(); + } + } + assertThat(expected).hasSize(0); + }); } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java index 8664db32e06..121db866bb0 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -29,6 +29,7 @@ import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -59,29 +60,36 @@ public class HazelcastAutoConfigurationClientTests { } } - private final ContextLoader contextLoader = new ContextLoader() - .autoConfig(HazelcastAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(HazelcastAutoConfiguration.class); @Test public void systemProperty() throws IOException { - this.contextLoader.systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, - "classpath:org/springframework/boot/autoconfigure/hazelcast/" - + "hazelcast-client-specific.xml").load(context -> { - HazelcastInstance hazelcastInstance = context - .getBean(HazelcastInstance.class); - assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); - assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); - }); + this.contextLoader + .systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, + "classpath:org/springframework/boot/autoconfigure/hazelcast/" + + "hazelcast-client-specific.xml") + .load(context -> { + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance) + .isInstanceOf(HazelcastClientProxy.class); + assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); + }); } @Test public void explicitConfigFile() throws IOException { - this.contextLoader.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/" - + "hazelcast/hazelcast-client-specific.xml").load(context -> { - HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); - assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); - assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); - }); + this.contextLoader + .env("spring.hazelcast.config=org/springframework/boot/autoconfigure/" + + "hazelcast/hazelcast-client-specific.xml") + .load(context -> { + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance) + .isInstanceOf(HazelcastClientProxy.class); + assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); + }); } @Test @@ -90,26 +98,28 @@ public class HazelcastAutoConfigurationClientTests { .load(context -> { HazelcastInstance hazelcastInstance = context .getBean(HazelcastInstance.class); - assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); + assertThat(hazelcastInstance) + .isInstanceOf(HazelcastClientProxy.class); assertThat(hazelcastInstance.getName()).startsWith("hz.client_"); }); } @Test public void unknownConfigFile() { - this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml") - .loadAndFail(BeanCreationException.class, ex -> - assertThat(ex.getMessage()).contains("foo/bar/unknown.xml")); + this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml").loadAndFail( + BeanCreationException.class, + ex -> assertThat(ex.getMessage()).contains("foo/bar/unknown.xml")); } @Test public void clientConfigTakesPrecedence() { this.contextLoader.config(HazelcastServerAndClientConfig.class) .env("spring.hazelcast.config=this-is-ignored.xml").load(context -> { - HazelcastInstance hazelcastInstance = context - .getBean(HazelcastInstance.class); - assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); - }); + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance) + .isInstanceOf(HazelcastClientProxy.class); + }); } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java index cabb4cab1ce..54ffc96c9e6 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @@ -45,8 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat; @ClassPathExclusions("hazelcast-client-*.jar") public class HazelcastAutoConfigurationServerTests { - private final ContextLoader contextLoader = new ContextLoader() - .autoConfig(HazelcastAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(HazelcastAutoConfiguration.class); @Test public void defaultConfigFile() throws IOException { @@ -61,8 +62,9 @@ public class HazelcastAutoConfigurationServerTests { @Test public void systemProperty() throws IOException { - this.contextLoader.systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY, - "classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml") + this.contextLoader + .systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY, + "classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml") .load(context -> { HazelcastInstance hazelcastInstance = context @@ -75,32 +77,36 @@ public class HazelcastAutoConfigurationServerTests { @Test public void explicitConfigFile() throws IOException { - this.contextLoader.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" - + "hazelcast-specific.xml").load(context -> { - HazelcastInstance hazelcastInstance = context - .getBean(HazelcastInstance.class); - assertThat(hazelcastInstance.getConfig().getConfigurationFile()).isEqualTo( - new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast" - + "/hazelcast-specific.xml").getFile()); - }); + this.contextLoader + .env("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + + "hazelcast-specific.xml") + .load(context -> { + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance.getConfig().getConfigurationFile()) + .isEqualTo(new ClassPathResource( + "org/springframework/boot/autoconfigure/hazelcast" + + "/hazelcast-specific.xml").getFile()); + }); } @Test public void explicitConfigUrl() throws IOException { - this.contextLoader - .env("spring.hazelcast.config=hazelcast-default.xml").load(context -> { - HazelcastInstance hazelcastInstance = context - .getBean(HazelcastInstance.class); - assertThat(hazelcastInstance.getConfig().getConfigurationUrl()) - .isEqualTo(new ClassPathResource("hazelcast-default.xml").getURL()); - }); + this.contextLoader.env("spring.hazelcast.config=hazelcast-default.xml") + .load(context -> { + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance.getConfig().getConfigurationUrl()) + .isEqualTo(new ClassPathResource("hazelcast-default.xml") + .getURL()); + }); } @Test public void unknownConfigFile() { - this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml") - .loadAndFail(BeanCreationException.class, ex -> - assertThat(ex.getMessage()).contains("foo/bar/unknown.xml")); + this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml").loadAndFail( + BeanCreationException.class, + ex -> assertThat(ex.getMessage()).contains("foo/bar/unknown.xml")); } @Test @@ -109,15 +115,16 @@ public class HazelcastAutoConfigurationServerTests { HazelcastInstance existingHazelcastInstance = Hazelcast .newHazelcastInstance(config); try { - this.contextLoader.config(HazelcastConfigWithName.class).env( - "spring.hazelcast.config=this-is-ignored.xml").load(context -> { - HazelcastInstance hazelcastInstance = context - .getBean(HazelcastInstance.class); - assertThat(hazelcastInstance.getConfig().getInstanceName()) - .isEqualTo("my-test-instance"); - // Should reuse any existing instance by default. - assertThat(hazelcastInstance).isEqualTo(existingHazelcastInstance); - }); + this.contextLoader.config(HazelcastConfigWithName.class) + .env("spring.hazelcast.config=this-is-ignored.xml").load(context -> { + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + assertThat(hazelcastInstance.getConfig().getInstanceName()) + .isEqualTo("my-test-instance"); + // Should reuse any existing instance by default. + assertThat(hazelcastInstance) + .isEqualTo(existingHazelcastInstance); + }); } finally { existingHazelcastInstance.shutdown(); @@ -128,12 +135,12 @@ public class HazelcastAutoConfigurationServerTests { public void configInstanceWithoutName() { this.contextLoader.config(HazelcastConfigNoName.class) .env("spring.hazelcast.config=this-is-ignored.xml").load(context -> { - HazelcastInstance hazelcastInstance = context - .getBean(HazelcastInstance.class); - Map queueConfigs = hazelcastInstance.getConfig() - .getQueueConfigs(); - assertThat(queueConfigs).hasSize(1).containsKey("another-queue"); - }); + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); + Map queueConfigs = hazelcastInstance.getConfig() + .getQueueConfigs(); + assertThat(queueConfigs).hasSize(1).containsKey("another-queue"); + }); } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java index 1371c8f95a7..e62b26fd96f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java @@ -22,6 +22,7 @@ import com.hazelcast.core.HazelcastInstance; import org.junit.Test; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.io.ClassPathResource; import static org.assertj.core.api.Assertions.assertThat; @@ -33,14 +34,15 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class HazelcastAutoConfigurationTests { - private final ContextLoader contextLoader = new ContextLoader() - .autoConfig(HazelcastAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(HazelcastAutoConfiguration.class); @Test public void defaultConfigFile() throws IOException { // no hazelcast-client.xml and hazelcast.xml is present in root classpath this.contextLoader.load(context -> { - HazelcastInstance hazelcastInstance = context.getBean(HazelcastInstance.class); + HazelcastInstance hazelcastInstance = context + .getBean(HazelcastInstance.class); assertThat(hazelcastInstance.getConfig().getConfigurationUrl()) .isEqualTo(new ClassPathResource("hazelcast.xml").getURL()); }); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java index 7294e80b770..b1b8f867666 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java @@ -41,6 +41,7 @@ import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.boot.test.context.ContextConsumer; import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.HidePackagesClassLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; @@ -57,15 +58,16 @@ import static org.mockito.Mockito.mock; */ public class DataSourceAutoConfigurationTests { - private final ContextLoader contextLoader = new ContextLoader() - .autoConfig(DataSourceAutoConfiguration.class) + private final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(DataSourceAutoConfiguration.class) .env("spring.datasource.initialize=false", - "spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt()); + "spring.datasource.url:jdbc:hsqldb:mem:testdb-" + + new Random().nextInt()); @Test public void testDefaultDataSourceExists() throws Exception { - this.contextLoader.load(context -> - assertThat(context.getBean(DataSource.class)).isNotNull()); + this.contextLoader.load( + context -> assertThat(context.getBean(DataSource.class)).isNotNull()); } @Test @@ -93,25 +95,25 @@ public class DataSourceAutoConfigurationTests { @Test public void testBadDriverClass() throws Exception { this.contextLoader.env("spring.datasource.driverClassName:org.none.jdbcDriver") - .loadAndFail(BeanCreationException.class, ex -> - assertThat(ex.getMessage()).contains("org.none.jdbcDriver")); + .loadAndFail(BeanCreationException.class, + ex -> assertThat(ex.getMessage()) + .contains("org.none.jdbcDriver")); } @Test public void hikariValidatesConnectionByDefault() throws Exception { assertDataSource(HikariDataSource.class, - Collections.singletonList("org.apache.tomcat"), - dataSource -> - // Use Connection#isValid() - assertThat(dataSource.getConnectionTestQuery()).isNull() - ); + Collections.singletonList("org.apache.tomcat"), dataSource -> + // Use Connection#isValid() + assertThat(dataSource.getConnectionTestQuery()).isNull()); } @Test public void tomcatIsFallback() throws Exception { assertDataSource(org.apache.tomcat.jdbc.pool.DataSource.class, - Collections.singletonList("com.zaxxer.hikari"), dataSource -> - assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb")); + Collections.singletonList("com.zaxxer.hikari"), + dataSource -> assertThat(dataSource.getUrl()) + .startsWith("jdbc:hsqldb:mem:testdb")); } @Test @@ -127,8 +129,9 @@ public class DataSourceAutoConfigurationTests { @Test public void commonsDbcp2IsFallback() throws Exception { assertDataSource(BasicDataSource.class, - Arrays.asList("com.zaxxer.hikari", "org.apache.tomcat"), dataSource -> - assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb")); + Arrays.asList("com.zaxxer.hikari", "org.apache.tomcat"), + dataSource -> assertThat(dataSource.getUrl()) + .startsWith("jdbc:hsqldb:mem:testdb")); } @Test @@ -136,7 +139,8 @@ public class DataSourceAutoConfigurationTests { assertDataSource(org.apache.commons.dbcp2.BasicDataSource.class, Arrays.asList("com.zaxxer.hikari", "org.apache.tomcat"), dataSource -> { assertThat(dataSource.getTestOnBorrow()).isEqualTo(true); - assertThat(dataSource.getValidationQuery()).isNull(); // Use Connection#isValid() + assertThat(dataSource.getValidationQuery()).isNull(); // Use + // Connection#isValid() }); } @@ -144,13 +148,14 @@ public class DataSourceAutoConfigurationTests { public void testEmbeddedTypeDefaultsUsername() throws Exception { this.contextLoader.env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver", "spring.datasource.url:jdbc:hsqldb:mem:testdb").load(context -> { - DataSource bean = context.getBean(DataSource.class); - assertThat(bean).isNotNull(); - @SuppressWarnings("resource") - HikariDataSource pool = (HikariDataSource) bean; - assertThat(pool.getDriverClassName()).isEqualTo("org.hsqldb.jdbcDriver"); - assertThat(pool.getUsername()).isEqualTo("sa"); - }); + DataSource bean = context.getBean(DataSource.class); + assertThat(bean).isNotNull(); + @SuppressWarnings("resource") + HikariDataSource pool = (HikariDataSource) bean; + assertThat(pool.getDriverClassName()) + .isEqualTo("org.hsqldb.jdbcDriver"); + assertThat(pool.getUsername()).isEqualTo("sa"); + }); } /** @@ -159,24 +164,28 @@ public class DataSourceAutoConfigurationTests { */ @Test public void explicitTypeNoSupportedDataSource() { - this.contextLoader.classLoader( - new HidePackagesClassLoader("org.apache.tomcat", "com.zaxxer.hikari", - "org.apache.commons.dbcp", "org.apache.commons.dbcp2")) + this.contextLoader + .classLoader(new HidePackagesClassLoader("org.apache.tomcat", + "com.zaxxer.hikari", "org.apache.commons.dbcp", + "org.apache.commons.dbcp2")) .env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver", "spring.datasource.url:jdbc:hsqldb:mem:testdb", - "spring.datasource.type:" + SimpleDriverDataSource.class.getName()) + "spring.datasource.type:" + + SimpleDriverDataSource.class.getName()) .load(testExplicitType()); } @Test public void explicitTypeSupportedDataSource() { - this.contextLoader.env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver", - "spring.datasource.url:jdbc:hsqldb:mem:testdb", - "spring.datasource.type:" + SimpleDriverDataSource.class.getName()) + this.contextLoader + .env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver", + "spring.datasource.url:jdbc:hsqldb:mem:testdb", + "spring.datasource.type:" + + SimpleDriverDataSource.class.getName()) .load(testExplicitType()); } - private ContextConsumer testExplicitType() { + private ContextConsumer testExplicitType() { return context -> { assertThat(context.getBeansOfType(DataSource.class)).hasSize(1); DataSource bean = context.getBean(DataSource.class); @@ -187,14 +196,15 @@ public class DataSourceAutoConfigurationTests { @Test public void testExplicitDriverClassClearsUsername() throws Exception { - this.contextLoader.env("spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(), + this.contextLoader.env( + "spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(), "spring.datasource.url:jdbc:foo://localhost").load(context -> { - DataSource dataSource = context.getBean(DataSource.class); - assertThat(dataSource).isNotNull(); - assertThat(((HikariDataSource) dataSource).getDriverClassName()) - .isEqualTo(DatabaseTestDriver.class.getName()); - assertThat(((HikariDataSource) dataSource).getUsername()).isNull(); - }); + DataSource dataSource = context.getBean(DataSource.class); + assertThat(dataSource).isNotNull(); + assertThat(((HikariDataSource) dataSource).getDriverClassName()) + .isEqualTo(DatabaseTestDriver.class.getName()); + assertThat(((HikariDataSource) dataSource).getUsername()).isNull(); + }); } @Test @@ -208,22 +218,21 @@ public class DataSourceAutoConfigurationTests { @Test public void testDataSourceIsInitializedEarly() { this.contextLoader.config(TestInitializedDataSourceConfiguration.class) - .env("spring.datasource.initialize=true").load(context -> - assertThat(context.getBean( - TestInitializedDataSourceConfiguration.class).called).isTrue()); + .env("spring.datasource.initialize=true") + .load(context -> assertThat(context + .getBean(TestInitializedDataSourceConfiguration.class).called) + .isTrue()); } private void assertDataSource(Class expectedType, List hiddenPackages, Consumer consumer) { HidePackagesClassLoader classLoader = new HidePackagesClassLoader( hiddenPackages.toArray(new String[hiddenPackages.size()])); - this.contextLoader - .classLoader(classLoader) - .load(context -> { - DataSource bean = context.getBean(DataSource.class); - assertThat(bean).isInstanceOf(expectedType); - consumer.accept(expectedType.cast(bean)); - }); + this.contextLoader.classLoader(classLoader).load(context -> { + DataSource bean = context.getBean(DataSource.class); + assertThat(bean).isInstanceOf(expectedType); + consumer.accept(expectedType.cast(bean)); + }); } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java index 08ead25cbfc..fec974b9119 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java @@ -28,6 +28,7 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @@ -61,8 +62,9 @@ public class JmsAutoConfigurationTests { private static final String ACTIVEMQ_NETWORK_URL = "tcp://localhost:61616"; - private final ContextLoader contextLoader = new ContextLoader().autoConfig( - ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard() + .autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class); @Test public void testDefaultJmsConfiguration() { @@ -82,9 +84,10 @@ public class JmsAutoConfigurationTests { @Test public void testConnectionFactoryBackOff() { - this.contextLoader.config(TestConfiguration2.class).load(context -> - assertThat(context.getBean(ActiveMQConnectionFactory.class) - .getBrokerURL()).isEqualTo("foobar")); + this.contextLoader.config(TestConfiguration2.class) + .load(context -> assertThat( + context.getBean(ActiveMQConnectionFactory.class).getBrokerURL()) + .isEqualTo("foobar")); } @Test @@ -108,15 +111,16 @@ public class JmsAutoConfigurationTests { public void testJmsTemplateBackOffEverything() { this.contextLoader.config(TestConfiguration2.class, TestConfiguration3.class, TestConfiguration5.class).load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - assertThat(jmsTemplate.getPriority()).isEqualTo(999); - assertThat(context.getBean(ActiveMQConnectionFactory.class).getBrokerURL()) - .isEqualTo("foobar"); - JmsMessagingTemplate messagingTemplate = context - .getBean(JmsMessagingTemplate.class); - assertThat(messagingTemplate.getDefaultDestinationName()).isEqualTo("fooBar"); - assertThat(messagingTemplate.getJmsTemplate()).isEqualTo(jmsTemplate); - }); + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + assertThat(jmsTemplate.getPriority()).isEqualTo(999); + assertThat(context.getBean(ActiveMQConnectionFactory.class) + .getBrokerURL()).isEqualTo("foobar"); + JmsMessagingTemplate messagingTemplate = context + .getBean(JmsMessagingTemplate.class); + assertThat(messagingTemplate.getDefaultDestinationName()) + .isEqualTo("fooBar"); + assertThat(messagingTemplate.getJmsTemplate()).isEqualTo(jmsTemplate); + }); } @Test @@ -131,13 +135,14 @@ public class JmsAutoConfigurationTests { @Test public void testJmsListenerContainerFactoryBackOff() { - this.contextLoader.config(TestConfiguration6.class, - EnableJmsConfiguration.class).load(context -> { - JmsListenerContainerFactory jmsListenerContainerFactory = context.getBean( - "jmsListenerContainerFactory", JmsListenerContainerFactory.class); - assertThat(jmsListenerContainerFactory.getClass()) - .isEqualTo(SimpleJmsListenerContainerFactory.class); - }); + this.contextLoader.config(TestConfiguration6.class, EnableJmsConfiguration.class) + .load(context -> { + JmsListenerContainerFactory jmsListenerContainerFactory = context + .getBean("jmsListenerContainerFactory", + JmsListenerContainerFactory.class); + assertThat(jmsListenerContainerFactory.getClass()) + .isEqualTo(SimpleJmsListenerContainerFactory.class); + }); } @Test @@ -146,52 +151,57 @@ public class JmsAutoConfigurationTests { .env("spring.jms.listener.autoStartup=false", "spring.jms.listener.acknowledgeMode=client", "spring.jms.listener.concurrency=2", - "spring.jms.listener.maxConcurrency=10").load(context -> { - JmsListenerContainerFactory jmsListenerContainerFactory = context.getBean( - "jmsListenerContainerFactory", JmsListenerContainerFactory.class); - assertThat(jmsListenerContainerFactory.getClass()) - .isEqualTo(DefaultJmsListenerContainerFactory.class); - DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) - .createListenerContainer(mock(JmsListenerEndpoint.class)); - assertThat(listenerContainer.isAutoStartup()).isFalse(); - assertThat(listenerContainer.getSessionAcknowledgeMode()) - .isEqualTo(Session.CLIENT_ACKNOWLEDGE); - assertThat(listenerContainer.getConcurrentConsumers()).isEqualTo(2); - assertThat(listenerContainer.getMaxConcurrentConsumers()).isEqualTo(10); - }); + "spring.jms.listener.maxConcurrency=10") + .load(context -> { + JmsListenerContainerFactory jmsListenerContainerFactory = context + .getBean("jmsListenerContainerFactory", + JmsListenerContainerFactory.class); + assertThat(jmsListenerContainerFactory.getClass()) + .isEqualTo(DefaultJmsListenerContainerFactory.class); + DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) + .createListenerContainer(mock(JmsListenerEndpoint.class)); + assertThat(listenerContainer.isAutoStartup()).isFalse(); + assertThat(listenerContainer.getSessionAcknowledgeMode()) + .isEqualTo(Session.CLIENT_ACKNOWLEDGE); + assertThat(listenerContainer.getConcurrentConsumers()).isEqualTo(2); + assertThat(listenerContainer.getMaxConcurrentConsumers()) + .isEqualTo(10); + }); } @Test public void testDefaultContainerFactoryWithJtaTransactionManager() { - this.contextLoader.config(TestConfiguration7.class, - EnableJmsConfiguration.class).load(context -> { - JmsListenerContainerFactory jmsListenerContainerFactory = context.getBean( - "jmsListenerContainerFactory", JmsListenerContainerFactory.class); - assertThat(jmsListenerContainerFactory.getClass()) - .isEqualTo(DefaultJmsListenerContainerFactory.class); - DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) - .createListenerContainer(mock(JmsListenerEndpoint.class)); - assertThat(listenerContainer.isSessionTransacted()).isFalse(); - assertThat(new DirectFieldAccessor(listenerContainer) - .getPropertyValue("transactionManager")) - .isSameAs(context.getBean(JtaTransactionManager.class)); - }); + this.contextLoader.config(TestConfiguration7.class, EnableJmsConfiguration.class) + .load(context -> { + JmsListenerContainerFactory jmsListenerContainerFactory = context + .getBean("jmsListenerContainerFactory", + JmsListenerContainerFactory.class); + assertThat(jmsListenerContainerFactory.getClass()) + .isEqualTo(DefaultJmsListenerContainerFactory.class); + DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) + .createListenerContainer(mock(JmsListenerEndpoint.class)); + assertThat(listenerContainer.isSessionTransacted()).isFalse(); + assertThat(new DirectFieldAccessor(listenerContainer) + .getPropertyValue("transactionManager")).isSameAs( + context.getBean(JtaTransactionManager.class)); + }); } @Test public void testDefaultContainerFactoryNonJtaTransactionManager() { - this.contextLoader.config(TestConfiguration8.class, - EnableJmsConfiguration.class).load(context -> { - JmsListenerContainerFactory jmsListenerContainerFactory = context.getBean( - "jmsListenerContainerFactory", JmsListenerContainerFactory.class); - assertThat(jmsListenerContainerFactory.getClass()) - .isEqualTo(DefaultJmsListenerContainerFactory.class); - DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) - .createListenerContainer(mock(JmsListenerEndpoint.class)); - assertThat(listenerContainer.isSessionTransacted()).isTrue(); - assertThat(new DirectFieldAccessor(listenerContainer) - .getPropertyValue("transactionManager")).isNull(); - }); + this.contextLoader.config(TestConfiguration8.class, EnableJmsConfiguration.class) + .load(context -> { + JmsListenerContainerFactory jmsListenerContainerFactory = context + .getBean("jmsListenerContainerFactory", + JmsListenerContainerFactory.class); + assertThat(jmsListenerContainerFactory.getClass()) + .isEqualTo(DefaultJmsListenerContainerFactory.class); + DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) + .createListenerContainer(mock(JmsListenerEndpoint.class)); + assertThat(listenerContainer.isSessionTransacted()).isTrue(); + assertThat(new DirectFieldAccessor(listenerContainer) + .getPropertyValue("transactionManager")).isNull(); + }); } @Test @@ -213,32 +223,35 @@ public class JmsAutoConfigurationTests { public void testDefaultContainerFactoryWithMessageConverters() { this.contextLoader.config(MessageConvertersConfiguration.class, EnableJmsConfiguration.class).load(context -> { - JmsListenerContainerFactory jmsListenerContainerFactory = context.getBean( - "jmsListenerContainerFactory", JmsListenerContainerFactory.class); - assertThat(jmsListenerContainerFactory.getClass()) - .isEqualTo(DefaultJmsListenerContainerFactory.class); - DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) - .createListenerContainer(mock(JmsListenerEndpoint.class)); - assertThat(listenerContainer.getMessageConverter()) - .isSameAs(context.getBean("myMessageConverter")); - }); + JmsListenerContainerFactory jmsListenerContainerFactory = context + .getBean("jmsListenerContainerFactory", + JmsListenerContainerFactory.class); + assertThat(jmsListenerContainerFactory.getClass()) + .isEqualTo(DefaultJmsListenerContainerFactory.class); + DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) + .createListenerContainer(mock(JmsListenerEndpoint.class)); + assertThat(listenerContainer.getMessageConverter()) + .isSameAs(context.getBean("myMessageConverter")); + }); } @Test public void testCustomContainerFactoryWithConfigurer() { this.contextLoader.config(TestConfiguration9.class, EnableJmsConfiguration.class) .env("spring.jms.listener.autoStartup=false").load(context -> { - assertThat(context.containsBean("jmsListenerContainerFactory")).isTrue(); - JmsListenerContainerFactory jmsListenerContainerFactory = context.getBean( - "customListenerContainerFactory", JmsListenerContainerFactory.class); - assertThat(jmsListenerContainerFactory) - .isInstanceOf(DefaultJmsListenerContainerFactory.class); - DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) - .createListenerContainer(mock(JmsListenerEndpoint.class)); - assertThat(listenerContainer.getCacheLevel()) - .isEqualTo(DefaultMessageListenerContainer.CACHE_CONSUMER); - assertThat(listenerContainer.isAutoStartup()).isFalse(); - }); + assertThat(context.containsBean("jmsListenerContainerFactory")) + .isTrue(); + JmsListenerContainerFactory jmsListenerContainerFactory = context + .getBean("customListenerContainerFactory", + JmsListenerContainerFactory.class); + assertThat(jmsListenerContainerFactory) + .isInstanceOf(DefaultJmsListenerContainerFactory.class); + DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory) + .createListenerContainer(mock(JmsListenerEndpoint.class)); + assertThat(listenerContainer.getCacheLevel()) + .isEqualTo(DefaultMessageListenerContainer.CACHE_CONSUMER); + assertThat(listenerContainer.isAutoStartup()).isFalse(); + }); } @Test @@ -252,33 +265,37 @@ public class JmsAutoConfigurationTests { @Test public void testJmsTemplateWithDestinationResolver() { - this.contextLoader.config(DestinationResolversConfiguration.class).load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - assertThat(jmsTemplate.getDestinationResolver()) - .isSameAs(context.getBean("myDestinationResolver")); - }); + this.contextLoader.config(DestinationResolversConfiguration.class) + .load(context -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + assertThat(jmsTemplate.getDestinationResolver()) + .isSameAs(context.getBean("myDestinationResolver")); + }); } @Test public void testJmsTemplateFullCustomization() { - this.contextLoader.config(MessageConvertersConfiguration.class).env( - "spring.jms.template.default-destination=testQueue", - "spring.jms.template.delivery-delay=500", - "spring.jms.template.delivery-mode=non-persistent", - "spring.jms.template.priority=6", "spring.jms.template.time-to-live=6000", - "spring.jms.template.receive-timeout=2000").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - assertThat(jmsTemplate.getMessageConverter()) - .isSameAs(context.getBean("myMessageConverter")); - assertThat(jmsTemplate.isPubSubDomain()).isFalse(); - assertThat(jmsTemplate.getDefaultDestinationName()).isEqualTo("testQueue"); - assertThat(jmsTemplate.getDeliveryDelay()).isEqualTo(500); - assertThat(jmsTemplate.getDeliveryMode()).isEqualTo(1); - assertThat(jmsTemplate.getPriority()).isEqualTo(6); - assertThat(jmsTemplate.getTimeToLive()).isEqualTo(6000); - assertThat(jmsTemplate.isExplicitQosEnabled()).isTrue(); - assertThat(jmsTemplate.getReceiveTimeout()).isEqualTo(2000); - }); + this.contextLoader.config(MessageConvertersConfiguration.class) + .env("spring.jms.template.default-destination=testQueue", + "spring.jms.template.delivery-delay=500", + "spring.jms.template.delivery-mode=non-persistent", + "spring.jms.template.priority=6", + "spring.jms.template.time-to-live=6000", + "spring.jms.template.receive-timeout=2000") + .load(context -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + assertThat(jmsTemplate.getMessageConverter()) + .isSameAs(context.getBean("myMessageConverter")); + assertThat(jmsTemplate.isPubSubDomain()).isFalse(); + assertThat(jmsTemplate.getDefaultDestinationName()) + .isEqualTo("testQueue"); + assertThat(jmsTemplate.getDeliveryDelay()).isEqualTo(500); + assertThat(jmsTemplate.getDeliveryMode()).isEqualTo(1); + assertThat(jmsTemplate.getPriority()).isEqualTo(6); + assertThat(jmsTemplate.getTimeToLive()).isEqualTo(6000); + assertThat(jmsTemplate.isExplicitQosEnabled()).isTrue(); + assertThat(jmsTemplate.getReceiveTimeout()).isEqualTo(2000); + }); } @Test @@ -301,111 +318,125 @@ public class JmsAutoConfigurationTests { public void testPubSubDomainActive() { this.contextLoader.config(TestConfiguration.class) .env("spring.jms.pubSubDomain:true").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - DefaultMessageListenerContainer defaultMessageListenerContainer = context - .getBean(DefaultJmsListenerContainerFactory.class) - .createListenerContainer(mock(JmsListenerEndpoint.class)); - assertThat(jmsTemplate.isPubSubDomain()).isTrue(); - assertThat(defaultMessageListenerContainer.isPubSubDomain()).isTrue(); - }); + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + DefaultMessageListenerContainer defaultMessageListenerContainer = context + .getBean(DefaultJmsListenerContainerFactory.class) + .createListenerContainer(mock(JmsListenerEndpoint.class)); + assertThat(jmsTemplate.isPubSubDomain()).isTrue(); + assertThat(defaultMessageListenerContainer.isPubSubDomain()).isTrue(); + }); } @Test public void testPubSubDomainOverride() { this.contextLoader.config(TestConfiguration.class) .env("spring.jms.pubSubDomain:false").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertThat(jmsTemplate).isNotNull(); - assertThat(jmsTemplate.isPubSubDomain()).isFalse(); - assertThat(connectionFactory).isNotNull(); - assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); - }); + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertThat(jmsTemplate).isNotNull(); + assertThat(jmsTemplate.isPubSubDomain()).isFalse(); + assertThat(connectionFactory).isNotNull(); + assertThat(connectionFactory) + .isEqualTo(jmsTemplate.getConnectionFactory()); + }); } @Test public void testActiveMQOverriddenStandalone() { this.contextLoader.config(TestConfiguration.class) .env("spring.activemq.inMemory:false").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertThat(jmsTemplate).isNotNull(); - assertThat(connectionFactory).isNotNull(); - assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); - assertThat(((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()) - .getBrokerURL()).isEqualTo(ACTIVEMQ_NETWORK_URL); - }); + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertThat(jmsTemplate).isNotNull(); + assertThat(connectionFactory).isNotNull(); + assertThat(connectionFactory) + .isEqualTo(jmsTemplate.getConnectionFactory()); + assertThat(((ActiveMQConnectionFactory) jmsTemplate + .getConnectionFactory()).getBrokerURL()) + .isEqualTo(ACTIVEMQ_NETWORK_URL); + }); } @Test public void testActiveMQOverriddenRemoteHost() { this.contextLoader.config(TestConfiguration.class) - .env("spring.activemq.brokerUrl:tcp://remote-host:10000").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertThat(jmsTemplate).isNotNull(); - assertThat(connectionFactory).isNotNull(); - assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); - assertThat(((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()) - .getBrokerURL()).isEqualTo("tcp://remote-host:10000"); - }); + .env("spring.activemq.brokerUrl:tcp://remote-host:10000") + .load(context -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertThat(jmsTemplate).isNotNull(); + assertThat(connectionFactory).isNotNull(); + assertThat(connectionFactory) + .isEqualTo(jmsTemplate.getConnectionFactory()); + assertThat(((ActiveMQConnectionFactory) jmsTemplate + .getConnectionFactory()).getBrokerURL()) + .isEqualTo("tcp://remote-host:10000"); + }); } @Test public void testActiveMQOverriddenPool() { this.contextLoader.config(TestConfiguration.class) .env("spring.activemq.pool.enabled:true").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - PooledConnectionFactory pool = context.getBean(PooledConnectionFactory.class); - assertThat(jmsTemplate).isNotNull(); - assertThat(pool).isNotNull(); - assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory()); - ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool - .getConnectionFactory(); - assertThat(factory.getBrokerURL()).isEqualTo(ACTIVEMQ_EMBEDDED_URL); - }); + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + PooledConnectionFactory pool = context + .getBean(PooledConnectionFactory.class); + assertThat(jmsTemplate).isNotNull(); + assertThat(pool).isNotNull(); + assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory()); + ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool + .getConnectionFactory(); + assertThat(factory.getBrokerURL()).isEqualTo(ACTIVEMQ_EMBEDDED_URL); + }); } @Test public void testActiveMQOverriddenPoolAndStandalone() { this.contextLoader.config(TestConfiguration.class) .env("spring.activemq.pool.enabled:true", - "spring.activemq.inMemory:false").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - PooledConnectionFactory pool = context.getBean(PooledConnectionFactory.class); - assertThat(jmsTemplate).isNotNull(); - assertThat(pool).isNotNull(); - assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory()); - ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool - .getConnectionFactory(); - assertThat(factory.getBrokerURL()).isEqualTo(ACTIVEMQ_NETWORK_URL); - }); + "spring.activemq.inMemory:false") + .load(context -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + PooledConnectionFactory pool = context + .getBean(PooledConnectionFactory.class); + assertThat(jmsTemplate).isNotNull(); + assertThat(pool).isNotNull(); + assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory()); + ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool + .getConnectionFactory(); + assertThat(factory.getBrokerURL()).isEqualTo(ACTIVEMQ_NETWORK_URL); + }); } @Test public void testActiveMQOverriddenPoolAndRemoteServer() { - this.contextLoader.config(TestConfiguration.class).env( - "spring.activemq.pool.enabled:true", - "spring.activemq.brokerUrl:tcp://remote-host:10000").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - PooledConnectionFactory pool = context.getBean(PooledConnectionFactory.class); - assertThat(jmsTemplate).isNotNull(); - assertThat(pool).isNotNull(); - assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory()); - ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool - .getConnectionFactory(); - assertThat(factory.getBrokerURL()).isEqualTo("tcp://remote-host:10000"); - }); + this.contextLoader.config(TestConfiguration.class) + .env("spring.activemq.pool.enabled:true", + "spring.activemq.brokerUrl:tcp://remote-host:10000") + .load(context -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + PooledConnectionFactory pool = context + .getBean(PooledConnectionFactory.class); + assertThat(jmsTemplate).isNotNull(); + assertThat(pool).isNotNull(); + assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory()); + ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool + .getConnectionFactory(); + assertThat(factory.getBrokerURL()) + .isEqualTo("tcp://remote-host:10000"); + }); } @Test public void enableJmsAutomatically() throws Exception { this.contextLoader.config(NoEnableJmsConfiguration.class).load(context -> { - context.getBean(JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME); - context.getBean(JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME); + context.getBean( + JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME); + context.getBean( + JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME); }); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java index 64e177c29e3..32faaf6ae63 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java @@ -25,6 +25,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,60 +42,71 @@ import static org.mockito.Mockito.mockingDetails; */ public class ActiveMQAutoConfigurationTests { - private final ContextLoader contextLoader = new ContextLoader().autoConfig( - ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard() + .autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class); @Test public void brokerIsEmbeddedByDefault() { this.contextLoader.config(EmptyConfiguration.class).load(context -> { - ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); + ConnectionFactory connectionFactory = context + .getBean(ConnectionFactory.class); assertThat(connectionFactory).isInstanceOf(ActiveMQConnectionFactory.class); - String brokerUrl = ((ActiveMQConnectionFactory) connectionFactory).getBrokerURL(); + String brokerUrl = ((ActiveMQConnectionFactory) connectionFactory) + .getBrokerURL(); assertThat(brokerUrl).isEqualTo("vm://localhost?broker.persistent=false"); }); } @Test public void configurationBacksOffWhenCustomConnectionFactoryExists() { - this.contextLoader.config(CustomConnectionFactoryConfiguration.class).load( - context -> assertThat(mockingDetails(context - .getBean(ConnectionFactory.class)).isMock()).isTrue()); + this.contextLoader.config(CustomConnectionFactoryConfiguration.class) + .load(context -> assertThat( + mockingDetails(context.getBean(ConnectionFactory.class)).isMock()) + .isTrue()); } @Test public void customPooledConnectionFactoryConfiguration() { - this.contextLoader.config(EmptyConfiguration.class).env( - "spring.activemq.pool.enabled:true", - "spring.activemq.pool.maxConnections:256", - "spring.activemq.pool.idleTimeout:512", - "spring.activemq.pool.expiryTimeout:4096", - "spring.activemq.pool.configuration.maximumActiveSessionPerConnection:1024", - "spring.activemq.pool.configuration.timeBetweenExpirationCheckMillis:2048").load(context -> { - ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); - assertThat(connectionFactory).isInstanceOf(PooledConnectionFactory.class); - PooledConnectionFactory pooledConnectionFactory = (PooledConnectionFactory) connectionFactory; - assertThat(pooledConnectionFactory.getMaxConnections()).isEqualTo(256); - assertThat(pooledConnectionFactory.getIdleTimeout()).isEqualTo(512); - assertThat(pooledConnectionFactory.getMaximumActiveSessionPerConnection()) - .isEqualTo(1024); - assertThat(pooledConnectionFactory.getTimeBetweenExpirationCheckMillis()) - .isEqualTo(2048); - assertThat(pooledConnectionFactory.getExpiryTimeout()).isEqualTo(4096); - }); + this.contextLoader.config(EmptyConfiguration.class) + .env("spring.activemq.pool.enabled:true", + "spring.activemq.pool.maxConnections:256", + "spring.activemq.pool.idleTimeout:512", + "spring.activemq.pool.expiryTimeout:4096", + "spring.activemq.pool.configuration.maximumActiveSessionPerConnection:1024", + "spring.activemq.pool.configuration.timeBetweenExpirationCheckMillis:2048") + .load(context -> { + ConnectionFactory connectionFactory = context + .getBean(ConnectionFactory.class); + assertThat(connectionFactory) + .isInstanceOf(PooledConnectionFactory.class); + PooledConnectionFactory pooledConnectionFactory = (PooledConnectionFactory) connectionFactory; + assertThat(pooledConnectionFactory.getMaxConnections()) + .isEqualTo(256); + assertThat(pooledConnectionFactory.getIdleTimeout()).isEqualTo(512); + assertThat(pooledConnectionFactory + .getMaximumActiveSessionPerConnection()).isEqualTo(1024); + assertThat( + pooledConnectionFactory.getTimeBetweenExpirationCheckMillis()) + .isEqualTo(2048); + assertThat(pooledConnectionFactory.getExpiryTimeout()) + .isEqualTo(4096); + }); } @Test public void pooledConnectionFactoryConfiguration() throws JMSException { this.contextLoader.config(EmptyConfiguration.class) .env("spring.activemq.pool.enabled:true").load(context -> { - ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); - assertThat(connectionFactory).isInstanceOf(PooledConnectionFactory.class); - context.close(); - assertThat(connectionFactory.createConnection()).isNull(); - }); + ConnectionFactory connectionFactory = context + .getBean(ConnectionFactory.class); + assertThat(connectionFactory) + .isInstanceOf(PooledConnectionFactory.class); + context.close(); + assertThat(connectionFactory.createConnection()).isNull(); + }); } - @Configuration static class EmptyConfiguration { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java index 4f5afde4d15..38d8b318d00 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java @@ -44,6 +44,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; import org.springframework.boot.test.context.ContextLoader; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.core.JmsTemplate; @@ -65,64 +66,72 @@ public class ArtemisAutoConfigurationTests { @Rule public final TemporaryFolder folder = new TemporaryFolder(); - private final ContextLoader contextLoader = new ContextLoader().autoConfig( - ArtemisAutoConfiguration.class, JmsAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard() + .autoConfig(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class); @Test public void nativeConnectionFactory() { this.contextLoader.config(EmptyConfiguration.class) .env("spring.artemis.mode:native").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); - assertNettyConnectionFactory(connectionFactory, "localhost", 61616); - assertThat(connectionFactory.getUser()).isNull(); - assertThat(connectionFactory.getPassword()).isNull(); - }); + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertThat(connectionFactory) + .isEqualTo(jmsTemplate.getConnectionFactory()); + assertNettyConnectionFactory(connectionFactory, "localhost", 61616); + assertThat(connectionFactory.getUser()).isNull(); + assertThat(connectionFactory.getPassword()).isNull(); + }); } @Test public void nativeConnectionFactoryCustomHost() { - this.contextLoader.config(EmptyConfiguration.class) - .env("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144", - "spring.artemis.port:9876").load(context -> { - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertNettyConnectionFactory(connectionFactory, "192.168.1.144", 9876); - }); + this.contextLoader + .config(EmptyConfiguration.class).env("spring.artemis.mode:native", + "spring.artemis.host:192.168.1.144", "spring.artemis.port:9876") + .load(context -> { + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertNettyConnectionFactory(connectionFactory, "192.168.1.144", + 9876); + }); } @Test public void nativeConnectionFactoryCredentials() { - this.contextLoader.config(EmptyConfiguration.class) - .env("spring.artemis.mode:native", "spring.artemis.user:user", - "spring.artemis.password:secret").load(context -> { - JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); - assertNettyConnectionFactory(connectionFactory, "localhost", 61616); - assertThat(connectionFactory.getUser()).isEqualTo("user"); - assertThat(connectionFactory.getPassword()).isEqualTo("secret"); - }); + this.contextLoader + .config(EmptyConfiguration.class).env("spring.artemis.mode:native", + "spring.artemis.user:user", "spring.artemis.password:secret") + .load(context -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertThat(connectionFactory) + .isEqualTo(jmsTemplate.getConnectionFactory()); + assertNettyConnectionFactory(connectionFactory, "localhost", 61616); + assertThat(connectionFactory.getUser()).isEqualTo("user"); + assertThat(connectionFactory.getPassword()).isEqualTo("secret"); + }); } @Test public void embeddedConnectionFactory() { this.contextLoader.config(EmptyConfiguration.class) .env("spring.artemis.mode:embedded").load(context -> { - ArtemisProperties properties = context.getBean(ArtemisProperties.class); - assertThat(properties.getMode()).isEqualTo(ArtemisMode.EMBEDDED); - assertThat(context.getBeansOfType(EmbeddedJMS.class)).hasSize(1); - org.apache.activemq.artemis.core.config.Configuration configuration = context - .getBean(org.apache.activemq.artemis.core.config.Configuration.class); - assertThat(configuration.isPersistenceEnabled()).isFalse(); - assertThat(configuration.isSecurityEnabled()).isFalse(); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertInVmConnectionFactory(connectionFactory); - }); + ArtemisProperties properties = context + .getBean(ArtemisProperties.class); + assertThat(properties.getMode()).isEqualTo(ArtemisMode.EMBEDDED); + assertThat(context.getBeansOfType(EmbeddedJMS.class)).hasSize(1); + org.apache.activemq.artemis.core.config.Configuration configuration = context + .getBean( + org.apache.activemq.artemis.core.config.Configuration.class); + assertThat(configuration.isPersistenceEnabled()).isFalse(); + assertThat(configuration.isSecurityEnabled()).isFalse(); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertInVmConnectionFactory(connectionFactory); + }); } @Test @@ -146,11 +155,11 @@ public class ArtemisAutoConfigurationTests { // No mode is specified this.contextLoader.config(EmptyConfiguration.class) .env("spring.artemis.embedded.enabled:false").load(context -> { - assertThat(context.getBeansOfType(EmbeddedJMS.class)).isEmpty(); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertNettyConnectionFactory(connectionFactory, "localhost", 61616); - }); + assertThat(context.getBeansOfType(EmbeddedJMS.class)).isEmpty(); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertNettyConnectionFactory(connectionFactory, "localhost", 61616); + }); } @Test @@ -158,26 +167,28 @@ public class ArtemisAutoConfigurationTests { // No mode is specified this.contextLoader.config(EmptyConfiguration.class) .env("spring.artemis.mode:embedded", - "spring.artemis.embedded.enabled:false").load(context -> { - assertThat(context.getBeansOfType(EmbeddedJMS.class)).isEmpty(); - ActiveMQConnectionFactory connectionFactory = context - .getBean(ActiveMQConnectionFactory.class); - assertInVmConnectionFactory(connectionFactory); - }); + "spring.artemis.embedded.enabled:false") + .load(context -> { + assertThat(context.getBeansOfType(EmbeddedJMS.class)).isEmpty(); + ActiveMQConnectionFactory connectionFactory = context + .getBean(ActiveMQConnectionFactory.class); + assertInVmConnectionFactory(connectionFactory); + }); } @Test public void embeddedServerWithDestinations() { this.contextLoader.config(EmptyConfiguration.class) .env("spring.artemis.embedded.queues=Queue1,Queue2", - "spring.artemis.embedded.topics=Topic1").load(context -> { - DestinationChecker checker = new DestinationChecker(context); - checker.checkQueue("Queue1", true); - checker.checkQueue("Queue2", true); - checker.checkQueue("QueueWillNotBeAutoCreated", true); - checker.checkTopic("Topic1", true); - checker.checkTopic("TopicWillBeAutoCreated", true); - }); + "spring.artemis.embedded.topics=Topic1") + .load(context -> { + DestinationChecker checker = new DestinationChecker(context); + checker.checkQueue("Queue1", true); + checker.checkQueue("Queue2", true); + checker.checkQueue("QueueWillNotBeAutoCreated", true); + checker.checkTopic("Topic1", true); + checker.checkTopic("TopicWillBeAutoCreated", true); + }); } @Test @@ -194,11 +205,11 @@ public class ArtemisAutoConfigurationTests { // Ignored with custom config this.contextLoader.config(CustomJmsConfiguration.class) .env("spring.artemis.embedded.queues=Queue1,Queue2").load(context -> { - DestinationChecker checker = new DestinationChecker(context); - checker.checkQueue("custom", true); // See CustomJmsConfiguration - checker.checkQueue("Queue1", true); - checker.checkQueue("Queue2", true); - }); + DestinationChecker checker = new DestinationChecker(context); + checker.checkQueue("custom", true); // See CustomJmsConfiguration + checker.checkQueue("Queue1", true); + checker.checkQueue("Queue2", true); + }); } @Test @@ -216,8 +227,7 @@ public class ArtemisAutoConfigurationTests { final String msgId = UUID.randomUUID().toString(); // Start the server and post a message to some queue - this.contextLoader - .config(EmptyConfiguration.class).env( + this.contextLoader.config(EmptyConfiguration.class).env( "spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath()) @@ -226,7 +236,8 @@ public class ArtemisAutoConfigurationTests { JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); jmsTemplate.send("TestQueue", new MessageCreator() { @Override - public Message createMessage(Session session) throws JMSException { + public Message createMessage(Session session) + throws JMSException { return session.createTextMessage(msgId); } }); @@ -245,41 +256,54 @@ public class ArtemisAutoConfigurationTests { @Test public void severalEmbeddedBrokers() { - this.contextLoader.config(EmptyConfiguration.class).env( - "spring.artemis.embedded.queues=Queue1").load(rootContext -> { - this.contextLoader.env("spring.artemis.embedded.queues=Queue2").load(anotherContext -> { - ArtemisProperties properties = rootContext.getBean(ArtemisProperties.class); - ArtemisProperties anotherProperties = anotherContext - .getBean(ArtemisProperties.class); - assertThat(properties.getEmbedded().getServerId() < anotherProperties - .getEmbedded().getServerId()).isTrue(); - DestinationChecker checker = new DestinationChecker(anotherContext); - checker.checkQueue("Queue1", true); - checker.checkQueue("Queue2", true); - DestinationChecker anotherChecker = new DestinationChecker(anotherContext); - anotherChecker.checkQueue("Queue2", true); - anotherChecker.checkQueue("Queue1", true); - }); - }); + this.contextLoader.config(EmptyConfiguration.class) + .env("spring.artemis.embedded.queues=Queue1").load(rootContext -> { + this.contextLoader.env("spring.artemis.embedded.queues=Queue2") + .load(anotherContext -> { + ArtemisProperties properties = rootContext + .getBean(ArtemisProperties.class); + ArtemisProperties anotherProperties = anotherContext + .getBean(ArtemisProperties.class); + assertThat( + properties.getEmbedded().getServerId() < anotherProperties + .getEmbedded().getServerId()).isTrue(); + DestinationChecker checker = new DestinationChecker( + anotherContext); + checker.checkQueue("Queue1", true); + checker.checkQueue("Queue2", true); + DestinationChecker anotherChecker = new DestinationChecker( + anotherContext); + anotherChecker.checkQueue("Queue2", true); + anotherChecker.checkQueue("Queue1", true); + }); + }); } @Test public void connectToASpecificEmbeddedBroker() { this.contextLoader.config(EmptyConfiguration.class) .env("spring.artemis.embedded.serverId=93", - "spring.artemis.embedded.queues=Queue1").load(context -> { - this.contextLoader.config(EmptyConfiguration.class).env( - "spring.artemis.mode=embedded", - "spring.artemis.embedded.serverId=93", /* Connect to the "main" broker */ - "spring.artemis.embedded.enabled=false" /* do not start a specific one */) - .load(anotherContext -> { + "spring.artemis.embedded.queues=Queue1") + .load(context -> { + this.contextLoader.config(EmptyConfiguration.class).env( + "spring.artemis.mode=embedded", + "spring.artemis.embedded.serverId=93", /* + * Connect to the + * "main" broker + */ + "spring.artemis.embedded.enabled=false" /* + * do not start a + * specific one + */) + .load(anotherContext -> { DestinationChecker checker = new DestinationChecker(context); checker.checkQueue("Queue1", true); - DestinationChecker anotherChecker = new DestinationChecker(anotherContext); + DestinationChecker anotherChecker = new DestinationChecker( + anotherContext); anotherChecker.checkQueue("Queue1", true); }); - }); + }); } private TransportConfiguration assertInVmConnectionFactory( diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java index 8d59b4c0ba2..50050602289 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web.reactive; import org.junit.Test; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.server.reactive.HttpHandler; @@ -38,8 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class HttpHandlerAutoConfigurationTests { - private final ContextLoader contextLoader = new ContextLoader().webReactive() - .autoConfig(HttpHandlerAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .reactiveWeb().autoConfig(HttpHandlerAutoConfiguration.class); @Test public void shouldNotProcessIfExistingHttpHandler() { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 83d6c320a0f..248740bb712 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -61,7 +61,7 @@ import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; -import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.HttpPutFormContentFilter; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExceptionResolver; @@ -114,11 +114,12 @@ public class WebMvcAutoConfigurationTests { private static final MockServletWebServerFactory webServerFactory = new MockServletWebServerFactory(); - private final ContextLoader contextLoader = new ContextLoader() + private final ContextLoader contextLoader = ContextLoader + .servletWeb() .autoConfig(WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class) - .config(Config.class).webServlet(); + .config(Config.class); @Test public void handlerAdaptersCreated() { @@ -574,11 +575,10 @@ public class WebMvcAutoConfigurationTests { public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage() { this.contextLoader .env("spring.resources.static-locations:classpath:/no-welcome-page/"); - this.contextLoader.webServlet().load(context -> { + this.contextLoader.load(context -> { assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) .hasSize(1); - // TODO This cast is ugly. Fix it with generics and ContextLoader subclasses - MockMvcBuilders.webAppContextSetup((WebApplicationContext) context).build() + MockMvcBuilders.webAppContextSetup(context).build() .perform(get("/").accept(MediaType.TEXT_HTML)) .andExpect(status().isNotFound()); }); @@ -603,8 +603,7 @@ public class WebMvcAutoConfigurationTests { this.contextLoader.load(context -> { assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) .hasSize(1); - MockMvc mockMvc = MockMvcBuilders - .webAppContextSetup((WebApplicationContext) context).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)) .andExpect(status().isOk()).andExpect(forwardedUrl("index.html")); mockMvc.perform(get("/").accept("*/*")).andExpect(status().isOk()) @@ -619,8 +618,7 @@ public class WebMvcAutoConfigurationTests { this.contextLoader.load(context -> { assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) .hasSize(1); - MockMvc mockMvc = MockMvcBuilders - .webAppContextSetup((WebApplicationContext) context).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); }); @@ -633,8 +631,7 @@ public class WebMvcAutoConfigurationTests { this.contextLoader.load(context -> { assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) .hasSize(1); - MockMvc mockMvc = MockMvcBuilders - .webAppContextSetup((WebApplicationContext) context).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/")).andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); }); @@ -648,8 +645,7 @@ public class WebMvcAutoConfigurationTests { this.contextLoader.load(context -> { assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) .hasSize(1); - MockMvc mockMvc = MockMvcBuilders - .webAppContextSetup((WebApplicationContext) context).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").header(HttpHeaders.ACCEPT, "")) .andExpect(status().isOk()).andExpect(forwardedUrl("index.html")); }); @@ -663,8 +659,7 @@ public class WebMvcAutoConfigurationTests { this.contextLoader.load(context -> { assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) .hasSize(1); - MockMvc mockMvc = MockMvcBuilders - .webAppContextSetup((WebApplicationContext) context).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)) .andExpect(status().isOk()).andExpect(forwardedUrl("index.html")); }); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java index 90175a97167..343c9f5a577 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -36,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class WebServicesAutoConfigurationTests { - private final ContextLoader contextLoader = new ContextLoader().webServlet() - .autoConfig(WebServicesAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .servletWeb().autoConfig(WebServicesAutoConfiguration.class); @Rule public ExpectedException thrown = ExpectedException.none(); diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java index dd0d1742506..73f129794a6 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java @@ -21,6 +21,7 @@ import javax.sql.DataSource; import org.junit.Test; import org.springframework.boot.test.context.ContextLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; @@ -37,8 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class TestDatabaseAutoConfigurationTests { - private final ContextLoader contextLoader = new ContextLoader() - .autoConfig(TestDatabaseAutoConfiguration.class); + private final ContextLoader contextLoader = ContextLoader + .standard().autoConfig(TestDatabaseAutoConfiguration.class); @Test public void replaceWithNoDataSourceAvailable() { diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java index 5f5bdc7c84c..5f17c3d91df 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java @@ -26,6 +26,7 @@ import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfigur import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -43,8 +44,8 @@ import static org.mockito.Mockito.mock; @ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" }) public class TestDatabaseAutoConfigurationNoEmbeddedTests { - private final ContextLoader contextLoader = new ContextLoader() - .config(ExistingDataSourceConfiguration.class) + private final ContextLoader contextLoader = ContextLoader + .standard().config(ExistingDataSourceConfiguration.class) .autoConfig(TestDatabaseAutoConfiguration.class); @Test diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java index da3cec8e2da..82d4af43096 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java @@ -20,22 +20,21 @@ import org.springframework.context.ConfigurableApplicationContext; /** * Callback interface used in tests to process a running - * {@link ConfigurableApplicationContext} with the ability to throw a (checked) - * exception. + * {@link ConfigurableApplicationContext} with the ability to throw a (checked) exception. * * @author Stephane Nicoll * @author Andy Wilkinson * @since 2.0.0 + * @param the type of the context that can be consumed */ @FunctionalInterface -public interface ContextConsumer { +public interface ContextConsumer { /** - * Performs this operation on the supplied {@link ConfigurableApplicationContext - * ApplicationContext}. + * Performs this operation on the supplied {@code context}. * @param context the application context to consume * @throws Throwable any exception that might occur in assertions */ - void accept(ConfigurableApplicationContext context) throws Throwable; + void accept(T context) throws Throwable; } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java index 623ca2978df..baa5be801a7 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java @@ -16,32 +16,15 @@ package org.springframework.boot.test.context; -import java.io.Closeable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; import java.util.function.Consumer; -import java.util.function.Supplier; -import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.AnnotationConfigRegistry; -import org.springframework.core.io.DefaultResourceLoader; import org.springframework.mock.web.MockServletContext; -import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import static org.assertj.core.api.Assertions.assertThat; - /** * Manage the lifecycle of an {@link ApplicationContext}. Such helper is best used as a * field of a test class, describing the shared configuration required for the test: @@ -91,20 +74,45 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Stephane Nicoll * @author Andy Wilkinson * @since 2.0.0 + * @param the type of the context to be loaded */ -public class ContextLoader { - - private final Map systemProperties = new HashMap<>(); - - private final List env = new ArrayList<>(); - - private final Set> userConfigurations = new LinkedHashSet<>(); +public interface ContextLoader { - private final LinkedList> autoConfigurations = new LinkedList<>(); + /** + * Creates a {@code ContextLoader} that will load a standard + * {@link AnnotationConfigApplicationContext}. + * + * @return the context loader + */ + static ContextLoader standard() { + return new StandardContextLoader<>( + () -> new AnnotationConfigApplicationContext()); + } - private Supplier contextSupplier = () -> new AnnotationConfigApplicationContext(); + /** + * Creates a {@code ContextLoader} that will load a + * {@link AnnotationConfigWebApplicationContext}. + * + * @return the context loader + */ + static ContextLoader servletWeb() { + return new StandardContextLoader<>(() -> { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.setServletContext(new MockServletContext()); + return context; + }); + } - private ClassLoader classLoader; + /** + * Creates a {@code ContextLoader} that will load a + * {@link GenericReactiveWebApplicationContext}. + * + * @return the context loader + */ + static ContextLoader reactiveWeb() { + return new StandardContextLoader<>( + () -> new GenericReactiveWebApplicationContext()); + } /** * Set the specified system property prior to loading the context and restore its @@ -114,16 +122,7 @@ public class ContextLoader { * @param value the value (can be null to remove any existing customization) * @return this instance */ - public ContextLoader systemProperty(String key, String value) { - Assert.notNull(key, "Key must not be null"); - if (value != null) { - this.systemProperties.put(key, value); - } - else { - this.systemProperties.remove(key); - } - return this; - } + public ContextLoader systemProperty(String key, String value); /** * Add the specified property pairs. Key-value pairs can be specified with colon (":") @@ -133,36 +132,21 @@ public class ContextLoader { * environment * @return this instance */ - public ContextLoader env(String... pairs) { - if (!ObjectUtils.isEmpty(pairs)) { - this.env.addAll(Arrays.asList(pairs)); - } - return this; - } + public ContextLoader env(String... pairs); /** * Add the specified user configuration classes. * @param configs the user configuration classes to add * @return this instance */ - public ContextLoader config(Class... configs) { - if (!ObjectUtils.isEmpty(configs)) { - this.userConfigurations.addAll(Arrays.asList(configs)); - } - return this; - } + public ContextLoader config(Class... configs); /** * Add the specified auto-configuration classes. * @param autoConfigurations the auto-configuration classes to add * @return this instance */ - public ContextLoader autoConfig(Class... autoConfigurations) { - if (!ObjectUtils.isEmpty(autoConfigurations)) { - this.autoConfigurations.addAll(Arrays.asList(autoConfigurations)); - } - return this; - } + public ContextLoader autoConfig(Class... autoConfigurations); /** * Add the specified auto-configurations at the beginning (in that order) so that it @@ -172,10 +156,7 @@ public class ContextLoader { * @param autoConfigurations the auto-configuration to add * @return this instance */ - public ContextLoader autoConfigFirst(Class... autoConfigurations) { - this.autoConfigurations.addAll(0, Arrays.asList(autoConfigurations)); - return this; - } + public ContextLoader autoConfigFirst(Class... autoConfigurations); /** * Customize the {@link ClassLoader} that the {@link ApplicationContext} should use. @@ -185,58 +166,15 @@ public class ContextLoader { * @return this instance * @see HidePackagesClassLoader */ - public ContextLoader classLoader(ClassLoader classLoader) { - this.classLoader = classLoader; - return this; - } - - /** - * Configures the loader to create an {@link ApplicationContext} suitable for use in a - * reactive web application. - * @return this instance - */ - public ContextLoader webReactive() { - this.contextSupplier = () -> { - return new GenericReactiveWebApplicationContext(); - }; - return this; - } - - /** - * Configures the loader to create an {@link ApplicationContext} suitable for use in a - * servlet web application. - * @return this instance - */ - public ContextLoader webServlet() { - this.contextSupplier = () -> { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.setServletContext(new MockServletContext()); - return context; - }; - return this; - } + public ContextLoader classLoader(ClassLoader classLoader); /** * Create and refresh a new {@link ApplicationContext} based on the current state of - * this loader. The context is consumed by the specified {@link ContextConsumer} and - * closed upon completion. + * this loader. The context is consumed by the specified {@code consumers} and closed + * upon completion. * @param consumer the consumer of the created {@link ApplicationContext} */ - public void load(ContextConsumer consumer) { - try (ApplicationContextLifecycleHandler handler = new ApplicationContextLifecycleHandler()) { - try { - ConfigurableApplicationContext ctx = handler.load(); - consumer.accept(ctx); - } - catch (RuntimeException ex) { - throw ex; - } - catch (Throwable ex) { - throw new IllegalStateException( - "An unexpected error occurred: " + ex.getMessage(), ex); - } - } - } + public void load(ContextConsumer consumer); /** * Create and refresh a new {@link ApplicationContext} based on the current state of @@ -245,9 +183,7 @@ public class ContextLoader { * specified {@link Consumer} with no expectation on the type of the exception. * @param consumer the consumer of the failure */ - public void loadAndFail(Consumer consumer) { - loadAndFail(Throwable.class, consumer); - } + public void loadAndFail(Consumer consumer); /** * Create and refresh a new {@link ApplicationContext} based on the current state of @@ -257,98 +193,9 @@ public class ContextLoader { * exception type matches, it is consumed by the specified {@link Consumer}. * @param exceptionType the expected type of the failure * @param consumer the consumer of the failure - * @param the expected type of the failure + * @param the expected type of the failure */ - public void loadAndFail(Class exceptionType, - Consumer consumer) { - try (ApplicationContextLifecycleHandler handler = new ApplicationContextLifecycleHandler()) { - handler.load(); - throw new AssertionError("ApplicationContext should have failed"); - } - catch (Throwable ex) { - assertThat(ex).as("Wrong application context failure exception") - .isInstanceOf(exceptionType); - consumer.accept(exceptionType.cast(ex)); - } - } - - private ConfigurableApplicationContext createApplicationContext() { - ConfigurableApplicationContext context = ContextLoader.this.contextSupplier.get(); - if (this.classLoader != null) { - ((DefaultResourceLoader) context).setClassLoader(this.classLoader); - } - if (!ObjectUtils.isEmpty(this.env)) { - TestPropertyValues.of(this.env.toArray(new String[this.env.size()])) - .applyTo(context); - } - AnnotationConfigRegistry registry = ((AnnotationConfigRegistry) context); - if (!ObjectUtils.isEmpty(this.userConfigurations)) { - registry.register(this.userConfigurations - .toArray(new Class[this.userConfigurations.size()])); - } - if (!ObjectUtils.isEmpty(this.autoConfigurations)) { - LinkedHashSet> linkedHashSet = new LinkedHashSet<>( - this.autoConfigurations); - registry.register( - linkedHashSet.toArray(new Class[this.autoConfigurations.size()])); - } - return context; - } - - /** - * Handles the lifecycle of the {@link ApplicationContext}. - */ - private class ApplicationContextLifecycleHandler implements Closeable { - - private final Map customSystemProperties; - - private final Map previousSystemProperties = new HashMap<>(); - - private ConfigurableApplicationContext context; - - ApplicationContextLifecycleHandler() { - this.customSystemProperties = new HashMap<>( - ContextLoader.this.systemProperties); - } - - public ConfigurableApplicationContext load() { - setCustomSystemProperties(); - ConfigurableApplicationContext context = createApplicationContext(); - context.refresh(); - this.context = context; - return context; - } - - @Override - public void close() { - try { - if (this.context != null) { - this.context.close(); - } - } - finally { - unsetCustomSystemProperties(); - } - } - - private void setCustomSystemProperties() { - this.customSystemProperties.forEach((key, value) -> { - String previous = System.setProperty(key, value); - this.previousSystemProperties.put(key, previous); - }); - } - - private void unsetCustomSystemProperties() { - this.previousSystemProperties.forEach((key, value) -> { - if (value != null) { - System.setProperty(key, value); - } - else { - System.clearProperty(key); - } - }); - } - - } + public void loadAndFail(Class exceptionType, + Consumer consumer); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/StandardContextLoader.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/StandardContextLoader.java new file mode 100644 index 00000000000..e15c5608938 --- /dev/null +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/StandardContextLoader.java @@ -0,0 +1,300 @@ +/* + * Copyright 2012-2017 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.test.context; + +import java.io.Closeable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Supplier; + +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigRegistry; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Standard implementation of {@link ContextLoader}. + * + * @author Stephane Nicoll + * @author Andy Wilkinson + * @param the type of the context to be loaded + */ +final class StandardContextLoader + implements ContextLoader { + + private final Map systemProperties = new HashMap<>(); + + private final List env = new ArrayList<>(); + + private final Set> userConfigurations = new LinkedHashSet<>(); + + private final LinkedList> autoConfigurations = new LinkedList<>(); + + private final Supplier contextSupplier; + + private ClassLoader classLoader; + + StandardContextLoader(Supplier contextSupplier) { + this.contextSupplier = contextSupplier; + } + + /** + * Set the specified system property prior to loading the context and restore its + * previous value once the consumer has been invoked and the context closed. If the + * {@code value} is {@code null} this removes any prior customization for that key. + * @param key the system property + * @param value the value (can be null to remove any existing customization) + * @return this instance + */ + @Override + public StandardContextLoader systemProperty(String key, String value) { + Assert.notNull(key, "Key must not be null"); + if (value != null) { + this.systemProperties.put(key, value); + } + else { + this.systemProperties.remove(key); + } + return this; + } + + /** + * Add the specified property pairs. Key-value pairs can be specified with colon (":") + * or equals ("=") separators. Override matching keys that might have been specified + * previously. + * @param pairs the key-value pairs for properties that need to be added to the + * environment + * @return this instance + */ + @Override + public StandardContextLoader env(String... pairs) { + if (!ObjectUtils.isEmpty(pairs)) { + this.env.addAll(Arrays.asList(pairs)); + } + return this; + } + + /** + * Add the specified user configuration classes. + * @param configs the user configuration classes to add + * @return this instance + */ + @Override + public StandardContextLoader config(Class... configs) { + if (!ObjectUtils.isEmpty(configs)) { + this.userConfigurations.addAll(Arrays.asList(configs)); + } + return this; + } + + /** + * Add the specified auto-configuration classes. + * @param autoConfigurations the auto-configuration classes to add + * @return this instance + */ + @Override + public StandardContextLoader autoConfig(Class... autoConfigurations) { + if (!ObjectUtils.isEmpty(autoConfigurations)) { + this.autoConfigurations.addAll(Arrays.asList(autoConfigurations)); + } + return this; + } + + /** + * Add the specified auto-configurations at the beginning (in that order) so that it + * is applied before any other existing auto-configurations, but after any user + * configuration. If {@code A} and {@code B} are specified, {@code A} will be + * processed, then {@code B} and finally the rest of the existing auto-configuration. + * @param autoConfigurations the auto-configuration to add + * @return this instance + */ + @Override + public StandardContextLoader autoConfigFirst(Class... autoConfigurations) { + this.autoConfigurations.addAll(0, Arrays.asList(autoConfigurations)); + return this; + } + + /** + * Customize the {@link ClassLoader} that the {@link ApplicationContext} should use. + * Customizing the {@link ClassLoader} is an effective manner to hide resources from + * the classpath. + * @param classLoader the classloader to use (can be null to use the default) + * @return this instance + * @see HidePackagesClassLoader + */ + @Override + public StandardContextLoader classLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + return this; + } + + /** + * Create and refresh a new {@link ApplicationContext} based on the current state of + * this loader. The context is consumed by the specified {@link ContextConsumer} and + * closed upon completion. + * @param consumer the consumer of the created {@link ApplicationContext} + */ + @Override + public void load(ContextConsumer consumer) { + try (ApplicationContextLifecycleHandler handler = new ApplicationContextLifecycleHandler()) { + try { + T ctx = handler.load(); + consumer.accept(ctx); + } + catch (RuntimeException ex) { + throw ex; + } + catch (Throwable ex) { + throw new IllegalStateException( + "An unexpected error occurred: " + ex.getMessage(), ex); + } + } + } + + /** + * Create and refresh a new {@link ApplicationContext} based on the current state of + * this loader that this expected to fail. If the context does not fail, an + * {@link AssertionError} is thrown. Otherwise the exception is consumed by the + * specified {@link Consumer} with no expectation on the type of the exception. + * @param consumer the consumer of the failure + */ + @Override + public void loadAndFail(Consumer consumer) { + loadAndFail(Throwable.class, consumer); + } + + /** + * Create and refresh a new {@link ApplicationContext} based on the current state of + * this loader that this expected to fail. If the context does not fail, an + * {@link AssertionError} is thrown. If the exception does not match the specified + * {@code exceptionType}, an {@link AssertionError} is thrown as well. If the + * exception type matches, it is consumed by the specified {@link Consumer}. + * @param exceptionType the expected type of the failure + * @param consumer the consumer of the failure + * @param the expected type of the failure + */ + @Override + public void loadAndFail(Class exceptionType, + Consumer consumer) { + try (ApplicationContextLifecycleHandler handler = new ApplicationContextLifecycleHandler()) { + handler.load(); + throw new AssertionError("ApplicationContext should have failed"); + } + catch (Throwable ex) { + assertThat(ex).as("Wrong application context failure exception") + .isInstanceOf(exceptionType); + consumer.accept(exceptionType.cast(ex)); + } + } + + private T configureApplicationContext() { + T context = StandardContextLoader.this.contextSupplier.get(); + if (this.classLoader != null) { + if (context instanceof DefaultResourceLoader) { + ((DefaultResourceLoader) context).setClassLoader(this.classLoader); + } + else { + throw new IllegalStateException("Cannot configure ClassLoader: " + context + + " is not a DefaultResourceLoader sub-class"); + } + } + if (!ObjectUtils.isEmpty(this.env)) { + TestPropertyValues.of(this.env.toArray(new String[this.env.size()])) + .applyTo(context); + } + if (!ObjectUtils.isEmpty(this.userConfigurations)) { + context.register(this.userConfigurations + .toArray(new Class[this.userConfigurations.size()])); + } + if (!ObjectUtils.isEmpty(this.autoConfigurations)) { + LinkedHashSet> linkedHashSet = new LinkedHashSet<>( + this.autoConfigurations); + context.register( + linkedHashSet.toArray(new Class[this.autoConfigurations.size()])); + } + return context; + } + + /** + * Handles the lifecycle of the {@link ApplicationContext}. + */ + private class ApplicationContextLifecycleHandler implements Closeable { + + private final Map customSystemProperties; + + private final Map previousSystemProperties = new HashMap<>(); + + private ConfigurableApplicationContext context; + + ApplicationContextLifecycleHandler() { + this.customSystemProperties = new HashMap<>( + StandardContextLoader.this.systemProperties); + } + + public T load() { + setCustomSystemProperties(); + T context = configureApplicationContext(); + context.refresh(); + this.context = context; + return context; + } + + @Override + public void close() { + try { + if (this.context != null) { + this.context.close(); + } + } + finally { + unsetCustomSystemProperties(); + } + } + + private void setCustomSystemProperties() { + this.customSystemProperties.forEach((key, value) -> { + String previous = System.setProperty(key, value); + this.previousSystemProperties.put(key, previous); + }); + } + + private void unsetCustomSystemProperties() { + this.previousSystemProperties.forEach((key, value) -> { + if (value != null) { + System.setProperty(key, value); + } + else { + System.clearProperty(key); + } + }); + } + + } + +} diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/StandardContextLoaderTests.java similarity index 66% rename from spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/StandardContextLoaderTests.java index 087caad7aa5..7208cc434d5 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/StandardContextLoaderTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.rule; +package org.springframework.boot.test.context; import java.util.UUID; @@ -24,27 +24,28 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; -import org.springframework.boot.test.context.ContextLoader; -import org.springframework.boot.test.context.HidePackagesClassLoader; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.test.context.support.AbstractContextLoader; import org.springframework.util.ClassUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; /** - * Tests for {@link ContextLoader}. + * Tests for {@link AbstractContextLoader}. * * @author Stephane Nicoll */ -public class ContextLoaderTests { +public class StandardContextLoaderTests { @Rule public final ExpectedException thrown = ExpectedException.none(); - private final ContextLoader contextLoader = new ContextLoader(); + private final StandardContextLoader contextLoader = new StandardContextLoader<>( + () -> new AnnotationConfigApplicationContext()); @Test public void systemPropertyIsSetAndRemoved() { @@ -61,9 +62,9 @@ public class ContextLoaderTests { public void systemPropertyIsRemovedIfContextFailed() { String key = "test." + UUID.randomUUID().toString(); assertThat(System.getProperties().containsKey(key)).isFalse(); - this.contextLoader.systemProperty(key, "value") - .config(ConfigC.class).loadAndFail(e -> { - }); + this.contextLoader.systemProperty(key, "value").config(ConfigC.class) + .loadAndFail(e -> { + }); assertThat(System.getProperties().containsKey(key)).isFalse(); } @@ -87,10 +88,10 @@ public class ContextLoaderTests { public void systemPropertyCanBeSetToNullValue() { String key = "test." + UUID.randomUUID().toString(); assertThat(System.getProperties().containsKey(key)).isFalse(); - this.contextLoader.systemProperty(key, "value") - .systemProperty(key, null).load(context -> { - assertThat(System.getProperties().containsKey(key)).isFalse(); - }); + this.contextLoader.systemProperty(key, "value").systemProperty(key, null) + .load(context -> { + assertThat(System.getProperties().containsKey(key)).isFalse(); + }); } @Test @@ -102,8 +103,8 @@ public class ContextLoaderTests { @Test public void envIsAdditive() { this.contextLoader.env("test.foo=1").env("test.bar=2").load(context -> { - ConfigurableEnvironment environment = context.getBean( - ConfigurableEnvironment.class); + ConfigurableEnvironment environment = context + .getBean(ConfigurableEnvironment.class); assertThat(environment.getProperty("test.foo", Integer.class)).isEqualTo(1); assertThat(environment.getProperty("test.bar", Integer.class)).isEqualTo(2); }); @@ -111,45 +112,42 @@ public class ContextLoaderTests { @Test public void envOverridesExistingKey() { - this.contextLoader.env("test.foo=1").env("test.foo=2").load(context -> - assertThat(context.getBean(ConfigurableEnvironment.class) + this.contextLoader.env("test.foo=1").env("test.foo=2") + .load(context -> assertThat(context.getBean(ConfigurableEnvironment.class) .getProperty("test.foo", Integer.class)).isEqualTo(2)); } @Test public void configurationIsProcessedInOrder() { - this.contextLoader.config(ConfigA.class, AutoConfigA.class).load(context -> - assertThat(context.getBean("a")).isEqualTo("autoconfig-a")); + this.contextLoader.config(ConfigA.class, AutoConfigA.class).load( + context -> assertThat(context.getBean("a")).isEqualTo("autoconfig-a")); } @Test public void configurationIsProcessedBeforeAutoConfiguration() { - this.contextLoader.autoConfig(AutoConfigA.class) - .config(ConfigA.class).load(context -> - assertThat(context.getBean("a")).isEqualTo("autoconfig-a")); + this.contextLoader.autoConfig(AutoConfigA.class).config(ConfigA.class).load( + context -> assertThat(context.getBean("a")).isEqualTo("autoconfig-a")); } @Test public void configurationIsAdditive() { - this.contextLoader.config(AutoConfigA.class) - .config(AutoConfigB.class).load(context -> { - assertThat(context.containsBean("a")).isTrue(); - assertThat(context.containsBean("b")).isTrue(); - }); + this.contextLoader.config(AutoConfigA.class).config(AutoConfigB.class) + .load(context -> { + assertThat(context.containsBean("a")).isTrue(); + assertThat(context.containsBean("b")).isTrue(); + }); } @Test public void autoConfigureFirstIsAppliedProperly() { - this.contextLoader.autoConfig(ConfigA.class) - .autoConfigFirst(AutoConfigA.class).load(context -> - assertThat(context.getBean("a")).isEqualTo("a")); + this.contextLoader.autoConfig(ConfigA.class).autoConfigFirst(AutoConfigA.class) + .load(context -> assertThat(context.getBean("a")).isEqualTo("a")); } @Test public void autoConfigureFirstWithSeveralConfigsIsAppliedProperly() { this.contextLoader.autoConfig(ConfigA.class, ConfigB.class) - .autoConfigFirst(AutoConfigA.class, AutoConfigB.class) - .load(context -> { + .autoConfigFirst(AutoConfigA.class, AutoConfigB.class).load(context -> { assertThat(context.getBean("a")).isEqualTo("a"); assertThat(context.getBean("b")).isEqualTo(1); }); @@ -157,18 +155,18 @@ public class ContextLoaderTests { @Test public void autoConfigurationIsAdditive() { - this.contextLoader.autoConfig(AutoConfigA.class) - .autoConfig(AutoConfigB.class).load(context -> { - assertThat(context.containsBean("a")).isTrue(); - assertThat(context.containsBean("b")).isTrue(); - }); + this.contextLoader.autoConfig(AutoConfigA.class).autoConfig(AutoConfigB.class) + .load(context -> { + assertThat(context.containsBean("a")).isTrue(); + assertThat(context.containsBean("b")).isTrue(); + }); } @Test public void loadAndFailWithExpectedException() { - this.contextLoader.config(ConfigC.class) - .loadAndFail(BeanCreationException.class, ex -> - assertThat(ex.getMessage()).contains("Error creating bean with name 'c'")); + this.contextLoader.config(ConfigC.class).loadAndFail(BeanCreationException.class, + ex -> assertThat(ex.getMessage()) + .contains("Error creating bean with name 'c'")); } @Test @@ -182,16 +180,19 @@ public class ContextLoaderTests { @Test public void classLoaderIsUsed() { - this.contextLoader.classLoader(new HidePackagesClassLoader( - Gson.class.getPackage().getName())).load(context -> { - try { - ClassUtils.forName(Gson.class.getName(), context.getClassLoader()); - fail("Should have thrown a ClassNotFoundException"); - } - catch (ClassNotFoundException e) { - // expected - } - }); + this.contextLoader + .classLoader( + new HidePackagesClassLoader(Gson.class.getPackage().getName())) + .load(context -> { + try { + ClassUtils.forName(Gson.class.getName(), + context.getClassLoader()); + fail("Should have thrown a ClassNotFoundException"); + } + catch (ClassNotFoundException e) { + // expected + } + }); } @Configuration