Polish
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
* 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;
|
||||
* @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);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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() {
|
||||
|
||||
Reference in New Issue
Block a user