Commit ef5c2afc authored by Stephane Nicoll's avatar Stephane Nicoll

Polish

parent dd0ce544
......@@ -58,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations;
......@@ -80,8 +80,8 @@ import static org.mockito.Mockito.mock;
*/
public class HealthIndicatorAutoConfigurationTests {
public final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard().autoConfig(HealthIndicatorAutoConfiguration.class,
public final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(HealthIndicatorAutoConfiguration.class,
ManagementServerProperties.class);
@Test
......@@ -380,7 +380,7 @@ public class HealthIndicatorAutoConfigurationTests {
.load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
}
private ContextConsumer<AnnotationConfigApplicationContext> hasSingleHealthIndicator(
private ContextConsumer hasSingleHealthIndicator(
Class<? extends HealthIndicator> type) {
return context -> {
Map<String, HealthIndicator> beans = context
......
......@@ -56,6 +56,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
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.ModifiedClassPathRunner;
import org.springframework.cache.Cache;
......@@ -73,7 +74,6 @@ 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;
......@@ -101,8 +101,8 @@ public class CacheAutoConfigurationTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard().autoConfig(CacheAutoConfiguration.class);
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(CacheAutoConfiguration.class);
@Test
public void noEnableCaching() {
......
......@@ -29,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -60,8 +60,8 @@ public class HazelcastAutoConfigurationClientTests {
}
}
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard().autoConfig(HazelcastAutoConfiguration.class);
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(HazelcastAutoConfiguration.class);
@Test
public void systemProperty() throws IOException {
......
......@@ -28,9 +28,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
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.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;
......@@ -46,8 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("hazelcast-client-*.jar")
public class HazelcastAutoConfigurationServerTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard().autoConfig(HazelcastAutoConfiguration.class);
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(HazelcastAutoConfiguration.class);
@Test
public void defaultConfigFile() throws IOException {
......
......@@ -22,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class HazelcastAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
private final StandardContextLoader contextLoader = ContextLoader
.standard().autoConfig(HazelcastAutoConfiguration.class);
@Test
......
......@@ -41,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -58,7 +58,7 @@ import static org.mockito.Mockito.mock;
*/
public class DataSourceAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
private final StandardContextLoader contextLoader = ContextLoader
.standard().autoConfig(DataSourceAutoConfiguration.class)
.env("spring.datasource.initialize=false",
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
......@@ -185,7 +185,7 @@ public class DataSourceAutoConfigurationTests {
.load(testExplicitType());
}
private ContextConsumer<AnnotationConfigApplicationContext> testExplicitType() {
private ContextConsumer testExplicitType() {
return context -> {
assertThat(context.getBeansOfType(DataSource.class)).hasSize(1);
DataSource bean = context.getBean(DataSource.class);
......
......@@ -28,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
......@@ -62,8 +62,7 @@ public class JmsAutoConfigurationTests {
private static final String ACTIVEMQ_NETWORK_URL = "tcp://localhost:61616";
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard()
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class);
@Test
......
......@@ -25,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -42,8 +42,7 @@ import static org.mockito.Mockito.mockingDetails;
*/
public class ActiveMQAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard()
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class);
@Test
......
......@@ -43,8 +43,8 @@ import org.junit.rules.TemporaryFolder;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.StandardContextLoader;
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;
......@@ -66,8 +66,7 @@ public class ArtemisAutoConfigurationTests {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard()
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class);
@Test
......
......@@ -19,7 +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.boot.test.context.ReactiveWebContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
......@@ -39,8 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class HttpHandlerAutoConfigurationTests {
private final ContextLoader<GenericReactiveWebApplicationContext> contextLoader = ContextLoader
.reactiveWeb().autoConfig(HttpHandlerAutoConfiguration.class);
private final ReactiveWebContextLoader contextLoader = ContextLoader.reactiveWeb()
.autoConfig(HttpHandlerAutoConfiguration.class);
@Test
public void shouldNotProcessIfExistingHttpHandler() {
......
......@@ -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.WelcomePageHandlerMapping;
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.servlet.filter.OrderedHttpPutFormContentFilter;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
......@@ -61,7 +62,6 @@ 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.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExceptionResolver;
......@@ -114,8 +114,7 @@ public class WebMvcAutoConfigurationTests {
private static final MockServletWebServerFactory webServerFactory = new MockServletWebServerFactory();
private final ContextLoader<AnnotationConfigWebApplicationContext> contextLoader = ContextLoader
.servletWeb()
private final ServletWebContextLoader contextLoader = ContextLoader.servletWeb()
.autoConfig(WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class)
......@@ -236,7 +235,6 @@ public class WebMvcAutoConfigurationTests {
assertThat(resolver.getStrategyMap().get("/**/*.js"))
.isInstanceOf(FixedVersionStrategy.class);
});
;
}
@Test
......@@ -575,7 +573,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage() {
this.contextLoader
.env("spring.resources.static-locations:classpath:/no-welcome-page/");
this.contextLoader.load(context -> {
this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvcBuilders.webAppContextSetup(context).build()
......@@ -600,7 +598,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() {
this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> {
this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
......@@ -615,7 +613,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingDoesNotHandleRequestsThatDoNotAcceptTextHtml() {
this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> {
this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
......@@ -628,7 +626,7 @@ public class WebMvcAutoConfigurationTests {
public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() {
this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> {
this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
......@@ -642,7 +640,7 @@ public class WebMvcAutoConfigurationTests {
throws Exception {
this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page/");
this.contextLoader.load(context -> {
this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
......@@ -656,7 +654,7 @@ public class WebMvcAutoConfigurationTests {
throws Exception {
this.contextLoader
.env("spring.resources.static-locations:classpath:/welcome-page");
this.contextLoader.load(context -> {
this.contextLoader.loadWeb(context -> {
assertThat(context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
......
......@@ -22,9 +22,9 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ServletWebContextLoader;
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;
......@@ -37,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class WebServicesAutoConfigurationTests {
private final ContextLoader<AnnotationConfigWebApplicationContext> contextLoader = ContextLoader
.servletWeb().autoConfig(WebServicesAutoConfiguration.class);
private final ServletWebContextLoader contextLoader = ContextLoader.servletWeb()
.autoConfig(WebServicesAutoConfiguration.class);
@Rule
public ExpectedException thrown = ExpectedException.none();
......
......@@ -21,7 +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.boot.test.context.StandardContextLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -38,8 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class TestDatabaseAutoConfigurationTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard().autoConfig(TestDatabaseAutoConfiguration.class);
private final StandardContextLoader contextLoader = ContextLoader.standard()
.autoConfig(TestDatabaseAutoConfiguration.class);
@Test
public void replaceWithNoDataSourceAvailable() {
......
......@@ -24,9 +24,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
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.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -44,8 +44,8 @@ import static org.mockito.Mockito.mock;
@ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" })
public class TestDatabaseAutoConfigurationNoEmbeddedTests {
private final ContextLoader<AnnotationConfigApplicationContext> contextLoader = ContextLoader
.standard().config(ExistingDataSourceConfiguration.class)
private final StandardContextLoader contextLoader = ContextLoader.standard()
.config(ExistingDataSourceConfiguration.class)
.autoConfig(TestDatabaseAutoConfiguration.class);
@Test
......
......@@ -25,16 +25,15 @@ import org.springframework.context.ConfigurableApplicationContext;
* @author Stephane Nicoll
* @author Andy Wilkinson
* @since 2.0.0
* @param <T> the type of the context that can be consumed
*/
@FunctionalInterface
public interface ContextConsumer<T extends ConfigurableApplicationContext> {
public interface ContextConsumer {
/**
* 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(T context) throws Throwable;
void accept(ConfigurableApplicationContext context) throws Throwable;
}
......@@ -20,7 +20,6 @@ import java.util.function.Consumer;
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.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
......@@ -32,7 +31,7 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* <pre class="code">
* public class FooAutoConfigurationTests {
*
* private final ContextLoader contextLoader = new ContextLoader()
* private final ContextLoader contextLoader = ContextLoader.standard()
* .autoConfig(FooAutoConfiguration.class).env("spring.foo=bar");
*
* }</pre>
......@@ -67,6 +66,10 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* automatically closed.
*
* <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
* instead: it expects the startup of the context to fail and call the {@link Consumer}
* with the exception for further assertions.
......@@ -74,9 +77,8 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
* @author Stephane Nicoll
* @author Andy Wilkinson
* @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
......@@ -84,9 +86,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
*
* @return the context loader
*/
static ContextLoader<AnnotationConfigApplicationContext> standard() {
return new StandardContextLoader<>(
() -> new AnnotationConfigApplicationContext());
static StandardContextLoader standard() {
return new StandardContextLoader(AnnotationConfigApplicationContext::new);
}
/**
......@@ -95,8 +96,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
*
* @return the context loader
*/
static ContextLoader<AnnotationConfigWebApplicationContext> servletWeb() {
return new StandardContextLoader<>(() -> {
static ServletWebContextLoader servletWeb() {
return new ServletWebContextLoader(() -> {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
return context;
......@@ -109,9 +110,8 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
*
* @return the context loader
*/
static ContextLoader<GenericReactiveWebApplicationContext> reactiveWeb() {
return new StandardContextLoader<>(
() -> new GenericReactiveWebApplicationContext());
static ReactiveWebContextLoader reactiveWeb() {
return new ReactiveWebContextLoader(GenericReactiveWebApplicationContext::new);
}
/**
......@@ -122,7 +122,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @param value the value (can be null to remove any existing customization)
* @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 (":")
......@@ -132,21 +132,21 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* environment
* @return this instance
*/
public ContextLoader<T> env(String... pairs);
ContextLoader env(String... pairs);
/**
* Add the specified user configuration classes.
* @param configs the user configuration classes to add
* @return this instance
*/
public ContextLoader<T> config(Class<?>... configs);
ContextLoader config(Class<?>... configs);
/**
* Add the specified auto-configuration classes.
* @param autoConfigurations the auto-configuration classes to add
* @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
......@@ -156,7 +156,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @param autoConfigurations the auto-configuration to add
* @return this instance
*/
public ContextLoader<T> autoConfigFirst(Class<?>... autoConfigurations);
ContextLoader autoConfigFirst(Class<?>... autoConfigurations);
/**
* Customize the {@link ClassLoader} that the {@link ApplicationContext} should use.
......@@ -166,15 +166,15 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @return this instance
* @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
* 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.
* @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
......@@ -183,7 +183,7 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* specified {@link Consumer} with no expectation on the type of the exception.
* @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
......@@ -195,7 +195,6 @@ public interface ContextLoader<T extends ConfigurableApplicationContext> {
* @param consumer the consumer of the failure
* @param <E> the expected type of the failure
*/
public <E extends Throwable> void loadAndFail(Class<E> exceptionType,
Consumer<E> consumer);
<E extends Throwable> void loadAndFail(Class<E> exceptionType, 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 @@
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;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Standard implementation of {@link ContextLoader}.
* A {@link ContextLoader} that simulates a standard, non web environment.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
* @param <T> the type of the context to be loaded
* @since 2.0.0
*/
final class StandardContextLoader<T extends ConfigurableApplicationContext & AnnotationConfigRegistry>
implements ContextLoader<T> {
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 class StandardContextLoader
extends AbstractContextLoader<AnnotationConfigApplicationContext, StandardContextLoader> {
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
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 AbstractContextLoader}.
* Tests for {@link StandardContextLoader}.
*
* @author Stephane Nicoll
*/
......@@ -44,8 +43,8 @@ public class StandardContextLoaderTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final StandardContextLoader<AnnotationConfigApplicationContext> contextLoader = new StandardContextLoader<>(
() -> new AnnotationConfigApplicationContext());
private final StandardContextLoader contextLoader = new StandardContextLoader(
AnnotationConfigApplicationContext::new);
@Test
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