Commit ef5c2afc authored by Stephane Nicoll's avatar Stephane Nicoll

Polish

parent dd0ce544
...@@ -58,7 +58,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -58,7 +58,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.ContextConsumer; import org.springframework.boot.test.context.ContextConsumer;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.CassandraOperations;
...@@ -80,8 +80,8 @@ import static org.mockito.Mockito.mock; ...@@ -80,8 +80,8 @@ import static org.mockito.Mockito.mock;
*/ */
public class HealthIndicatorAutoConfigurationTests { public class HealthIndicatorAutoConfigurationTests {
public final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader public final StandardContextLoader contextLoader = ContextLoader.standard()
.standard().autoConfig(HealthIndicatorAutoConfiguration.class, .autoConfig(HealthIndicatorAutoConfiguration.class,
ManagementServerProperties.class); ManagementServerProperties.class);
@Test @Test
...@@ -380,7 +380,7 @@ public class HealthIndicatorAutoConfigurationTests { ...@@ -380,7 +380,7 @@ public class HealthIndicatorAutoConfigurationTests {
.load(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
} }
private ContextConsumer<AnnotationConfigApplicationContext> hasSingleHealthIndicator( private ContextConsumer hasSingleHealthIndicator(
Class<? extends HealthIndicator> type) { Class<? extends HealthIndicator> type) {
return context -> { return context -> {
Map<String, HealthIndicator> beans = context Map<String, HealthIndicator> beans = context
......
...@@ -56,6 +56,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException; ...@@ -56,6 +56,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider; import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
...@@ -73,7 +74,6 @@ import org.springframework.cache.jcache.JCacheCacheManager; ...@@ -73,7 +74,6 @@ import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.cache.support.NoOpCacheManager; import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.cache.support.SimpleCacheManager; import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -101,8 +101,8 @@ public class CacheAutoConfigurationTests { ...@@ -101,8 +101,8 @@ public class CacheAutoConfigurationTests {
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard().autoConfig(CacheAutoConfiguration.class); .autoConfig(CacheAutoConfiguration.class);
@Test @Test
public void noEnableCaching() { public void noEnableCaching() {
......
...@@ -29,7 +29,7 @@ import org.junit.Test; ...@@ -29,7 +29,7 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -60,8 +60,8 @@ public class HazelcastAutoConfigurationClientTests { ...@@ -60,8 +60,8 @@ public class HazelcastAutoConfigurationClientTests {
} }
} }
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard().autoConfig(HazelcastAutoConfiguration.class); .autoConfig(HazelcastAutoConfiguration.class);
@Test @Test
public void systemProperty() throws IOException { public void systemProperty() throws IOException {
......
...@@ -28,9 +28,9 @@ import org.junit.runner.RunWith; ...@@ -28,9 +28,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
...@@ -46,8 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -46,8 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("hazelcast-client-*.jar") @ClassPathExclusions("hazelcast-client-*.jar")
public class HazelcastAutoConfigurationServerTests { public class HazelcastAutoConfigurationServerTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard().autoConfig(HazelcastAutoConfiguration.class); .autoConfig(HazelcastAutoConfiguration.class);
@Test @Test
public void defaultConfigFile() throws IOException { public void defaultConfigFile() throws IOException {
......
...@@ -22,7 +22,7 @@ import com.hazelcast.core.HazelcastInstance; ...@@ -22,7 +22,7 @@ import com.hazelcast.core.HazelcastInstance;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class HazelcastAutoConfigurationTests { public class HazelcastAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader
.standard().autoConfig(HazelcastAutoConfiguration.class); .standard().autoConfig(HazelcastAutoConfiguration.class);
@Test @Test
......
...@@ -41,7 +41,7 @@ import org.springframework.boot.jdbc.DatabaseDriver; ...@@ -41,7 +41,7 @@ import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.test.context.ContextConsumer; import org.springframework.boot.test.context.ContextConsumer;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.boot.test.context.HidePackagesClassLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
...@@ -58,7 +58,7 @@ import static org.mockito.Mockito.mock; ...@@ -58,7 +58,7 @@ import static org.mockito.Mockito.mock;
*/ */
public class DataSourceAutoConfigurationTests { public class DataSourceAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader
.standard().autoConfig(DataSourceAutoConfiguration.class) .standard().autoConfig(DataSourceAutoConfiguration.class)
.env("spring.datasource.initialize=false", .env("spring.datasource.initialize=false",
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" "spring.datasource.url:jdbc:hsqldb:mem:testdb-"
...@@ -185,7 +185,7 @@ public class DataSourceAutoConfigurationTests { ...@@ -185,7 +185,7 @@ public class DataSourceAutoConfigurationTests {
.load(testExplicitType()); .load(testExplicitType());
} }
private ContextConsumer<AnnotationConfigApplicationContext> testExplicitType() { private ContextConsumer testExplicitType() {
return context -> { return context -> {
assertThat(context.getBeansOfType(DataSource.class)).hasSize(1); assertThat(context.getBeansOfType(DataSource.class)).hasSize(1);
DataSource bean = context.getBean(DataSource.class); DataSource bean = context.getBean(DataSource.class);
......
...@@ -28,7 +28,7 @@ import org.springframework.beans.DirectFieldAccessor; ...@@ -28,7 +28,7 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration; import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
...@@ -62,8 +62,7 @@ public class JmsAutoConfigurationTests { ...@@ -62,8 +62,7 @@ public class JmsAutoConfigurationTests {
private static final String ACTIVEMQ_NETWORK_URL = "tcp://localhost:61616"; private static final String ACTIVEMQ_NETWORK_URL = "tcp://localhost:61616";
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard()
.autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class); .autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class);
@Test @Test
......
...@@ -25,7 +25,7 @@ import org.junit.Test; ...@@ -25,7 +25,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -42,8 +42,7 @@ import static org.mockito.Mockito.mockingDetails; ...@@ -42,8 +42,7 @@ import static org.mockito.Mockito.mockingDetails;
*/ */
public class ActiveMQAutoConfigurationTests { public class ActiveMQAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard()
.autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class); .autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class);
@Test @Test
......
...@@ -43,8 +43,8 @@ import org.junit.rules.TemporaryFolder; ...@@ -43,8 +43,8 @@ import org.junit.rules.TemporaryFolder;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.JmsTemplate;
...@@ -66,8 +66,7 @@ public class ArtemisAutoConfigurationTests { ...@@ -66,8 +66,7 @@ public class ArtemisAutoConfigurationTests {
@Rule @Rule
public final TemporaryFolder folder = new TemporaryFolder(); public final TemporaryFolder folder = new TemporaryFolder();
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard()
.autoConfig(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class); .autoConfig(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class);
@Test @Test
......
...@@ -19,7 +19,7 @@ package org.springframework.boot.autoconfigure.web.reactive; ...@@ -19,7 +19,7 @@ package org.springframework.boot.autoconfigure.web.reactive;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; import org.springframework.boot.test.context.ReactiveWebContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
...@@ -39,8 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -39,8 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class HttpHandlerAutoConfigurationTests { public class HttpHandlerAutoConfigurationTests {
private final ContextLoader<GenericReactiveWebApplicationContext> contextLoader = ContextLoader private final ReactiveWebContextLoader contextLoader = ContextLoader.reactiveWeb()
.reactiveWeb().autoConfig(HttpHandlerAutoConfiguration.class); .autoConfig(HttpHandlerAutoConfiguration.class);
@Test @Test
public void shouldNotProcessIfExistingHttpHandler() { public void shouldNotProcessIfExistingHttpHandler() {
......
...@@ -38,6 +38,7 @@ import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; ...@@ -38,6 +38,7 @@ import org.springframework.boot.autoconfigure.validation.ValidatorAdapter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WelcomePageHandlerMapping; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WelcomePageHandlerMapping;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ServletWebContextLoader;
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
...@@ -61,7 +62,6 @@ import org.springframework.validation.Validator; ...@@ -61,7 +62,6 @@ import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HttpPutFormContentFilter; import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.HandlerExceptionResolver;
...@@ -114,8 +114,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -114,8 +114,7 @@ public class WebMvcAutoConfigurationTests {
private static final MockServletWebServerFactory webServerFactory = new MockServletWebServerFactory(); private static final MockServletWebServerFactory webServerFactory = new MockServletWebServerFactory();
private final ContextLoader<AnnotationConfigWebApplicationContext> contextLoader = ContextLoader private final ServletWebContextLoader contextLoader = ContextLoader.servletWeb()
.servletWeb()
.autoConfig(WebMvcAutoConfiguration.class, .autoConfig(WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class) PropertyPlaceholderAutoConfiguration.class)
...@@ -236,7 +235,6 @@ public class WebMvcAutoConfigurationTests { ...@@ -236,7 +235,6 @@ public class WebMvcAutoConfigurationTests {
assertThat(resolver.getStrategyMap().get("/**/*.js")) assertThat(resolver.getStrategyMap().get("/**/*.js"))
.isInstanceOf(FixedVersionStrategy.class); .isInstanceOf(FixedVersionStrategy.class);
}); });
;
} }
@Test @Test
...@@ -575,7 +573,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -575,7 +573,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage() { public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage() {
this.contextLoader this.contextLoader
.env("spring.resources.static-locations:classpath:/no-welcome-page/"); .env("spring.resources.static-locations:classpath:/no-welcome-page/");
this.contextLoader.load(context -> { this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1); .hasSize(1);
MockMvcBuilders.webAppContextSetup(context).build() MockMvcBuilders.webAppContextSetup(context).build()
...@@ -600,7 +598,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -600,7 +598,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() { public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() {
this.contextLoader this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/"); .env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> { this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1); .hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
...@@ -615,7 +613,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -615,7 +613,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingDoesNotHandleRequestsThatDoNotAcceptTextHtml() { public void welcomePageMappingDoesNotHandleRequestsThatDoNotAcceptTextHtml() {
this.contextLoader this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/"); .env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> { this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1); .hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
...@@ -628,7 +626,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -628,7 +626,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() { public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() {
this.contextLoader this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/"); .env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> { this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1); .hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
...@@ -642,7 +640,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -642,7 +640,7 @@ public class WebMvcAutoConfigurationTests {
throws Exception { throws Exception {
this.contextLoader this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/"); .env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> { this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1); .hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
...@@ -656,7 +654,7 @@ public class WebMvcAutoConfigurationTests { ...@@ -656,7 +654,7 @@ public class WebMvcAutoConfigurationTests {
throws Exception { throws Exception {
this.contextLoader this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page"); .env("spring.resources.static-locations:classpath:/welcome-page");
this.contextLoader.load(context -> { this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class)) assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1); .hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
......
...@@ -22,9 +22,9 @@ import org.junit.rules.ExpectedException; ...@@ -22,9 +22,9 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ServletWebContextLoader;
import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -37,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -37,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class WebServicesAutoConfigurationTests { public class WebServicesAutoConfigurationTests {
private final ContextLoader<AnnotationConfigWebApplicationContext> contextLoader = ContextLoader private final ServletWebContextLoader contextLoader = ContextLoader.servletWeb()
.servletWeb().autoConfig(WebServicesAutoConfiguration.class); .autoConfig(WebServicesAutoConfiguration.class);
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
......
...@@ -21,7 +21,7 @@ import javax.sql.DataSource; ...@@ -21,7 +21,7 @@ import javax.sql.DataSource;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
...@@ -38,8 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -38,8 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class TestDatabaseAutoConfigurationTests { public class TestDatabaseAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard().autoConfig(TestDatabaseAutoConfiguration.class); .autoConfig(TestDatabaseAutoConfiguration.class);
@Test @Test
public void replaceWithNoDataSourceAvailable() { public void replaceWithNoDataSourceAvailable() {
......
...@@ -24,9 +24,9 @@ import org.junit.runner.RunWith; ...@@ -24,9 +24,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration; import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.StandardContextLoader;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -44,8 +44,8 @@ import static org.mockito.Mockito.mock; ...@@ -44,8 +44,8 @@ import static org.mockito.Mockito.mock;
@ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" }) @ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" })
public class TestDatabaseAutoConfigurationNoEmbeddedTests { public class TestDatabaseAutoConfigurationNoEmbeddedTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader private final StandardContextLoader contextLoader = ContextLoader.standard()
.standard().config(ExistingDataSourceConfiguration.class) .config(ExistingDataSourceConfiguration.class)
.autoConfig(TestDatabaseAutoConfiguration.class); .autoConfig(TestDatabaseAutoConfiguration.class);
@Test @Test
......
/*
* 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;
/**
* Base implementation of {@link ContextLoader}.
*
* @param <T> the type of the context to be loaded
* @param <L> the type of the loader
* @author Stephane Nicoll
* @author Andy Wilkinson
*/
class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends AbstractContextLoader<T, ?>>
implements ContextLoader {
private final Map<String, String> systemProperties = new HashMap<>();
private final List<String> env = new ArrayList<>();
private final Set<Class<?>> userConfigurations = new LinkedHashSet<>();
private final LinkedList<Class<?>> autoConfigurations = new LinkedList<>();
private final Supplier<T> contextSupplier;
private ClassLoader classLoader;
protected AbstractContextLoader(Supplier<T> 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 L 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 self();
}
/**
* 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 L env(String... pairs) {
if (!ObjectUtils.isEmpty(pairs)) {
this.env.addAll(Arrays.asList(pairs));
}
return self();
}
/**
* Add the specified user configuration classes.
* @param configs the user configuration classes to add
* @return this instance
*/
@Override
public L config(Class<?>... configs) {
if (!ObjectUtils.isEmpty(configs)) {
this.userConfigurations.addAll(Arrays.asList(configs));
}
return self();
}
/**
* Add the specified auto-configuration classes.
* @param autoConfigurations the auto-configuration classes to add
* @return this instance
*/
@Override
public L autoConfig(Class<?>... autoConfigurations) {
if (!ObjectUtils.isEmpty(autoConfigurations)) {
this.autoConfigurations.addAll(Arrays.asList(autoConfigurations));
}
return self();
}
/**
* 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 L autoConfigFirst(Class<?>... autoConfigurations) {
this.autoConfigurations.addAll(0, Arrays.asList(autoConfigurations));
return self();
}
/**
* 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 L classLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
return self();
}
protected final L self() {
return (L) 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) {
doLoad(consumer::accept);
}
protected void doLoad(ContextHandler<T> contextHandler) {
try (ApplicationContextLifecycleHandler handler = new ApplicationContextLifecycleHandler()) {
try {
T ctx = handler.load();
contextHandler.handle(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<Throwable> 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 <E> the expected type of the failure
*/
@Override
public <E extends Throwable> void loadAndFail(Class<E> exceptionType,
Consumer<E> 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 = AbstractContextLoader.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)) {
((AnnotationConfigRegistry) context).register(this.userConfigurations
.toArray(new Class<?>[this.userConfigurations.size()]));
}
if (!ObjectUtils.isEmpty(this.autoConfigurations)) {
LinkedHashSet<Class<?>> linkedHashSet = new LinkedHashSet<>(
this.autoConfigurations);
((AnnotationConfigRegistry) context).register(
linkedHashSet.toArray(new Class<?>[this.autoConfigurations.size()]));
}
return context;
}
/**
* An internal callback interface that handles a concrete {@link ApplicationContext}
* type.
* @param <T> the type of the application context
*/
protected interface ContextHandler<T> {
void handle(T context) throws Throwable;
}
/**
* Handles the lifecycle of the {@link ApplicationContext}.
*/
private class ApplicationContextLifecycleHandler implements Closeable {
private final Map<String, String> customSystemProperties;
private final Map<String, String> previousSystemProperties = new HashMap<>();
private ConfigurableApplicationContext context;
ApplicationContextLifecycleHandler() {
this.customSystemProperties = new HashMap<>(
AbstractContextLoader.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);
}
});
}
}
}
...@@ -25,16 +25,15 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -25,16 +25,15 @@ import org.springframework.context.ConfigurableApplicationContext;
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 2.0.0 * @since 2.0.0
* @param <T> the type of the context that can be consumed
*/ */
@FunctionalInterface @FunctionalInterface
public interface ContextConsumer<T extends ConfigurableApplicationContext> { public interface ContextConsumer {
/** /**
* Performs this operation on the supplied {@code context}. * Performs this operation on the supplied {@code context}.
* @param context the application context to consume * @param context the application context to consume
* @throws Throwable any exception that might occur in assertions * @throws Throwable any exception that might occur in assertions
*/ */
void accept(T context) throws Throwable; void accept(ConfigurableApplicationContext context) throws Throwable;
} }
...@@ -20,7 +20,6 @@ import java.util.function.Consumer; ...@@ -20,7 +20,6 @@ import java.util.function.Consumer;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
...@@ -32,7 +31,7 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon ...@@ -32,7 +31,7 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* <pre class="code"> * <pre class="code">
* public class FooAutoConfigurationTests { * public class FooAutoConfigurationTests {
* *
* private final ContextLoader contextLoader = new ContextLoader() * private final ContextLoader contextLoader = ContextLoader.standard()
* .autoConfig(FooAutoConfiguration.class).env("spring.foo=bar"); * .autoConfig(FooAutoConfiguration.class).env("spring.foo=bar");
* *
* }</pre> * }</pre>
...@@ -67,6 +66,10 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon ...@@ -67,6 +66,10 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* automatically closed. * automatically closed.
* *
* <p> * <p>
* Web environment can easily be simulated using the {@link #servletWeb()} and
* {@link #reactiveWeb()} factory methods.
*
* <p>
* If a failure scenario has to be tested, {@link #loadAndFail(Consumer)} can be used * If a failure scenario has to be tested, {@link #loadAndFail(Consumer)} can be used
* instead: it expects the startup of the context to fail and call the {@link Consumer} * instead: it expects the startup of the context to fail and call the {@link Consumer}
* with the exception for further assertions. * with the exception for further assertions.
...@@ -74,9 +77,8 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon ...@@ -74,9 +77,8 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 2.0.0 * @since 2.0.0
* @param <T> the type of the context to be loaded
*/ */
public interface ContextLoader<T extends ConfigurableApplicationContext> { public interface ContextLoader {
/** /**
* Creates a {@code ContextLoader} that will load a standard * Creates a {@code ContextLoader} that will load a standard
...@@ -84,9 +86,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -84,9 +86,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* *
* @return the context loader * @return the context loader
*/ */
static ContextLoader<AnnotationConfigApplicationContext> standard() { static StandardContextLoader standard() {
return new StandardContextLoader<>( return new StandardContextLoader(AnnotationConfigApplicationContext::new);
() -> new AnnotationConfigApplicationContext());
} }
/** /**
...@@ -95,8 +96,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -95,8 +96,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* *
* @return the context loader * @return the context loader
*/ */
static ContextLoader<AnnotationConfigWebApplicationContext> servletWeb() { static ServletWebContextLoader servletWeb() {
return new StandardContextLoader<>(() -> { return new ServletWebContextLoader(() -> {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext()); context.setServletContext(new MockServletContext());
return context; return context;
...@@ -109,9 +110,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -109,9 +110,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* *
* @return the context loader * @return the context loader
*/ */
static ContextLoader<GenericReactiveWebApplicationContext> reactiveWeb() { static ReactiveWebContextLoader reactiveWeb() {
return new StandardContextLoader<>( return new ReactiveWebContextLoader(GenericReactiveWebApplicationContext::new);
() -> new GenericReactiveWebApplicationContext());
} }
/** /**
...@@ -122,7 +122,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -122,7 +122,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @param value the value (can be null to remove any existing customization) * @param value the value (can be null to remove any existing customization)
* @return this instance * @return this instance
*/ */
public ContextLoader<T> systemProperty(String key, String value); ContextLoader systemProperty(String key, String value);
/** /**
* Add the specified property pairs. Key-value pairs can be specified with colon (":") * Add the specified property pairs. Key-value pairs can be specified with colon (":")
...@@ -132,21 +132,21 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -132,21 +132,21 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* environment * environment
* @return this instance * @return this instance
*/ */
public ContextLoader<T> env(String... pairs); ContextLoader env(String... pairs);
/** /**
* Add the specified user configuration classes. * Add the specified user configuration classes.
* @param configs the user configuration classes to add * @param configs the user configuration classes to add
* @return this instance * @return this instance
*/ */
public ContextLoader<T> config(Class<?>... configs); ContextLoader config(Class<?>... configs);
/** /**
* Add the specified auto-configuration classes. * Add the specified auto-configuration classes.
* @param autoConfigurations the auto-configuration classes to add * @param autoConfigurations the auto-configuration classes to add
* @return this instance * @return this instance
*/ */
public ContextLoader<T> autoConfig(Class<?>... autoConfigurations); ContextLoader autoConfig(Class<?>... autoConfigurations);
/** /**
* Add the specified auto-configurations at the beginning (in that order) so that it * Add the specified auto-configurations at the beginning (in that order) so that it
...@@ -156,7 +156,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -156,7 +156,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @param autoConfigurations the auto-configuration to add * @param autoConfigurations the auto-configuration to add
* @return this instance * @return this instance
*/ */
public ContextLoader<T> autoConfigFirst(Class<?>... autoConfigurations); ContextLoader autoConfigFirst(Class<?>... autoConfigurations);
/** /**
* Customize the {@link ClassLoader} that the {@link ApplicationContext} should use. * Customize the {@link ClassLoader} that the {@link ApplicationContext} should use.
...@@ -166,15 +166,15 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -166,15 +166,15 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @return this instance * @return this instance
* @see HidePackagesClassLoader * @see HidePackagesClassLoader
*/ */
public ContextLoader<T> classLoader(ClassLoader classLoader); ContextLoader classLoader(ClassLoader classLoader);
/** /**
* Create and refresh a new {@link ApplicationContext} based on the current state of * Create and refresh a new {@link ApplicationContext} based on the current state of
* this loader. The context is consumed by the specified {@code consumers} and closed * this loader. The context is consumed by the specified {@code consumer} and closed
* upon completion. * upon completion.
* @param consumer the consumer of the created {@link ApplicationContext} * @param consumer the consumer of the created {@link ApplicationContext}
*/ */
public void load(ContextConsumer<T> consumer); void load(ContextConsumer consumer);
/** /**
* Create and refresh a new {@link ApplicationContext} based on the current state of * Create and refresh a new {@link ApplicationContext} based on the current state of
...@@ -183,7 +183,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -183,7 +183,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* specified {@link Consumer} with no expectation on the type of the exception. * specified {@link Consumer} with no expectation on the type of the exception.
* @param consumer the consumer of the failure * @param consumer the consumer of the failure
*/ */
public void loadAndFail(Consumer<Throwable> consumer); void loadAndFail(Consumer<Throwable> consumer);
/** /**
* Create and refresh a new {@link ApplicationContext} based on the current state of * Create and refresh a new {@link ApplicationContext} based on the current state of
...@@ -195,7 +195,6 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> { ...@@ -195,7 +195,6 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @param consumer the consumer of the failure * @param consumer the consumer of the failure
* @param <E> the expected type of the failure * @param <E> the expected type of the failure
*/ */
public <E extends Throwable> void loadAndFail(Class<E> exceptionType, <E extends Throwable> void loadAndFail(Class<E> exceptionType, Consumer<E> consumer);
Consumer<E> consumer);
} }
/*
* 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.util.function.Supplier;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
/**
* A {@link ContextLoader} that simulates a {@link GenericReactiveWebApplicationContext}
* which can be useful to test components that require a reactive web application.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @since 2.0.0
*/
public final class ReactiveWebContextLoader
extends AbstractContextLoader<GenericReactiveWebApplicationContext, ReactiveWebContextLoader> {
ReactiveWebContextLoader(
Supplier<GenericReactiveWebApplicationContext> contextSupplier) {
super(contextSupplier);
}
}
/*
* 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.util.function.Supplier;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
* A {@link ContextLoader} that simulates a {@link AnnotationConfigWebApplicationContext}
* which can be useful to test components that require a servlet web application.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @since 2.0.0
*/
public final class ServletWebContextLoader
extends AbstractContextLoader<AnnotationConfigWebApplicationContext, ServletWebContextLoader> {
ServletWebContextLoader(
Supplier<AnnotationConfigWebApplicationContext> contextSupplier) {
super(contextSupplier);
}
/**
* Create and refresh a new {@link ConfigurableWebApplicationContext} based on the
* current state of this loader. The context is consumed by the specified
* {@link WebMvcContextConsumer consumer} and closed upon completion.
* @param consumer the consumer of the created {@link ConfigurableWebApplicationContext}
*/
public void loadWeb(WebMvcContextConsumer consumer) {
doLoad(consumer::accept);
}
}
...@@ -16,285 +16,22 @@ ...@@ -16,285 +16,22 @@
package org.springframework.boot.test.context; 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 java.util.function.Supplier;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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}. * A {@link ContextLoader} that simulates a standard, non web environment.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson * @author Andy Wilkinson
* @param <T> the type of the context to be loaded * @since 2.0.0
*/ */
final class StandardContextLoader<T extends ConfigurableApplicationContext & AnnotationConfigRegistry> public class StandardContextLoader
implements ContextLoader<T> { extends AbstractContextLoader<AnnotationConfigApplicationContext, StandardContextLoader> {
private final Map<String, String> systemProperties = new HashMap<>();
private final List<String> env = new ArrayList<>();
private final Set<Class<?>> userConfigurations = new LinkedHashSet<>();
private final LinkedList<Class<?>> autoConfigurations = new LinkedList<>();
private final Supplier<T> contextSupplier;
private ClassLoader classLoader;
StandardContextLoader(Supplier<T> 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<T> 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<T> 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<T> 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<T> 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<T> 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<T> 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<T> 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<Throwable> 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 <E> the expected type of the failure
*/
@Override
public <E extends Throwable> void loadAndFail(Class<E> exceptionType,
Consumer<E> 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<Class<?>> 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<String, String> customSystemProperties;
private final Map<String, String> 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);
}
});
}
public StandardContextLoader(Supplier<AnnotationConfigApplicationContext> contextSupplier) {
super(contextSupplier);
} }
} }
/*
* 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 org.springframework.web.context.ConfigurableWebApplicationContext;
/**
* Specialized callback interface used in tests to process a running
* {@link ConfigurableWebApplicationContext} with the ability to throw a (checked)
* exception.
*
* @author Stephane Nicoll
* @see ContextConsumer
* @since 2.0.0
*/
@FunctionalInterface
public interface WebMvcContextConsumer {
/**
* 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(ConfigurableWebApplicationContext context) throws Throwable;
}
...@@ -28,14 +28,13 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext ...@@ -28,14 +28,13 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
* Tests for {@link AbstractContextLoader}. * Tests for {@link StandardContextLoader}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
...@@ -44,8 +43,8 @@ public class StandardContextLoaderTests { ...@@ -44,8 +43,8 @@ public class StandardContextLoaderTests {
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();
private final StandardContextLoader<AnnotationConfigApplicationContext> contextLoader = new StandardContextLoader<>( private final StandardContextLoader contextLoader = new StandardContextLoader(
() -> new AnnotationConfigApplicationContext()); AnnotationConfigApplicationContext::new);
@Test @Test
public void systemPropertyIsSetAndRemoved() { public void systemPropertyIsSetAndRemoved() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment