|
|
@ -17,6 +17,7 @@
|
|
|
|
package org.springframework.boot.test.context.runner;
|
|
|
|
package org.springframework.boot.test.context.runner;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
|
|
|
@ -98,25 +99,48 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
|
|
|
|
|
|
|
|
|
|
|
|
private final Supplier<C> contextFactory;
|
|
|
|
private final Supplier<C> contextFactory;
|
|
|
|
|
|
|
|
|
|
|
|
private TestPropertyValues environmentProperties;
|
|
|
|
private final TestPropertyValues environmentProperties;
|
|
|
|
|
|
|
|
|
|
|
|
private TestPropertyValues systemProperties;
|
|
|
|
private final TestPropertyValues systemProperties;
|
|
|
|
|
|
|
|
|
|
|
|
private ClassLoader classLoader;
|
|
|
|
private final ClassLoader classLoader;
|
|
|
|
|
|
|
|
|
|
|
|
private ApplicationContext parent;
|
|
|
|
private final ApplicationContext parent;
|
|
|
|
|
|
|
|
|
|
|
|
private final List<Configurations> configurations = new ArrayList<>();
|
|
|
|
private final List<Configurations> configurations;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Create a new {@link AbstractApplicationContextRunner} instance.
|
|
|
|
* Create a new {@link AbstractApplicationContextRunner} instance.
|
|
|
|
* @param contextFactory the factory used to create the actual context
|
|
|
|
* @param contextFactory the factory used to create the actual context
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected AbstractApplicationContextRunner(Supplier<C> contextFactory) {
|
|
|
|
protected AbstractApplicationContextRunner(Supplier<C> contextFactory) {
|
|
|
|
|
|
|
|
this(contextFactory, TestPropertyValues.empty(), TestPropertyValues.empty(), null,
|
|
|
|
|
|
|
|
null, Collections.emptyList());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Create a new {@link AbstractApplicationContextRunner} instance.
|
|
|
|
|
|
|
|
* @param contextFactory the factory used to create the actual context
|
|
|
|
|
|
|
|
* @param environmentProperties the environment properties
|
|
|
|
|
|
|
|
* @param systemProperties the system properties
|
|
|
|
|
|
|
|
* @param classLoader the class loader
|
|
|
|
|
|
|
|
* @param parent the parent
|
|
|
|
|
|
|
|
* @param configurations the configuration
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
protected AbstractApplicationContextRunner(Supplier<C> contextFactory,
|
|
|
|
|
|
|
|
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
|
|
|
|
|
|
|
|
ClassLoader classLoader, ApplicationContext parent,
|
|
|
|
|
|
|
|
List<Configurations> configurations) {
|
|
|
|
Assert.notNull(contextFactory, "ContextFactory must not be null");
|
|
|
|
Assert.notNull(contextFactory, "ContextFactory must not be null");
|
|
|
|
|
|
|
|
Assert.notNull(environmentProperties, "EnvironmentProperties must not be null");
|
|
|
|
|
|
|
|
Assert.notNull(systemProperties, "SystemProperties must not be null");
|
|
|
|
|
|
|
|
Assert.notNull(configurations, "Configurations must not be null");
|
|
|
|
this.contextFactory = contextFactory;
|
|
|
|
this.contextFactory = contextFactory;
|
|
|
|
this.environmentProperties = TestPropertyValues.empty();
|
|
|
|
this.environmentProperties = environmentProperties;
|
|
|
|
this.systemProperties = TestPropertyValues.empty();
|
|
|
|
this.systemProperties = systemProperties;
|
|
|
|
|
|
|
|
this.classLoader = classLoader;
|
|
|
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
this.configurations = Collections.unmodifiableList(configurations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -125,13 +149,14 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
|
|
|
|
* might have been specified previously.
|
|
|
|
* might have been specified previously.
|
|
|
|
* @param pairs the key-value pairs for properties that need to be added to the
|
|
|
|
* @param pairs the key-value pairs for properties that need to be added to the
|
|
|
|
* environment
|
|
|
|
* environment
|
|
|
|
* @return this instance
|
|
|
|
* @return a new instance with the updated property values
|
|
|
|
* @see TestPropertyValues
|
|
|
|
* @see TestPropertyValues
|
|
|
|
* @see #withSystemProperties(String...)
|
|
|
|
* @see #withSystemProperties(String...)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public SELF withPropertyValues(String... pairs) {
|
|
|
|
public SELF withPropertyValues(String... pairs) {
|
|
|
|
this.environmentProperties = this.environmentProperties.and(pairs);
|
|
|
|
return newInstance(this.contextFactory, this.environmentProperties.and(pairs),
|
|
|
|
return self();
|
|
|
|
this.systemProperties, this.classLoader, this.parent,
|
|
|
|
|
|
|
|
this.configurations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -140,13 +165,14 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
|
|
|
|
* context is {@link #run(ContextConsumer) run} and restored when the context is
|
|
|
|
* context is {@link #run(ContextConsumer) run} and restored when the context is
|
|
|
|
* closed.
|
|
|
|
* closed.
|
|
|
|
* @param pairs the key-value pairs for properties that need to be added to the system
|
|
|
|
* @param pairs the key-value pairs for properties that need to be added to the system
|
|
|
|
* @return this instance
|
|
|
|
* @return a new instance with the updated system properties
|
|
|
|
* @see TestPropertyValues
|
|
|
|
* @see TestPropertyValues
|
|
|
|
* @see #withSystemProperties(String...)
|
|
|
|
* @see #withSystemProperties(String...)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public SELF withSystemProperties(String... pairs) {
|
|
|
|
public SELF withSystemProperties(String... pairs) {
|
|
|
|
this.systemProperties = this.systemProperties.and(pairs);
|
|
|
|
return newInstance(this.contextFactory, this.environmentProperties,
|
|
|
|
return self();
|
|
|
|
this.systemProperties.and(pairs), this.classLoader, this.parent,
|
|
|
|
|
|
|
|
this.configurations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -154,30 +180,30 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
|
|
|
|
* Customizing the {@link ClassLoader} is an effective manner to hide resources from
|
|
|
|
* Customizing the {@link ClassLoader} is an effective manner to hide resources from
|
|
|
|
* the classpath.
|
|
|
|
* the classpath.
|
|
|
|
* @param classLoader the classloader to use (can be null to use the default)
|
|
|
|
* @param classLoader the classloader to use (can be null to use the default)
|
|
|
|
* @return this instance
|
|
|
|
* @return a new instance with the updated class loader
|
|
|
|
* @see HidePackagesClassLoader
|
|
|
|
* @see HidePackagesClassLoader
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public SELF withClassLoader(ClassLoader classLoader) {
|
|
|
|
public SELF withClassLoader(ClassLoader classLoader) {
|
|
|
|
this.classLoader = classLoader;
|
|
|
|
return newInstance(this.contextFactory, this.environmentProperties,
|
|
|
|
return self();
|
|
|
|
this.systemProperties, classLoader, this.parent, this.configurations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Configure the {@link ConfigurableApplicationContext#setParent(ApplicationContext)
|
|
|
|
* Configure the {@link ConfigurableApplicationContext#setParent(ApplicationContext)
|
|
|
|
* parent} of the {@link ApplicationContext}.
|
|
|
|
* parent} of the {@link ApplicationContext}.
|
|
|
|
* @param parent the parent
|
|
|
|
* @param parent the parent
|
|
|
|
* @return this instance
|
|
|
|
* @return a new instance with the updated parent
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public SELF withParent(ApplicationContext parent) {
|
|
|
|
public SELF withParent(ApplicationContext parent) {
|
|
|
|
this.parent = parent;
|
|
|
|
return newInstance(this.contextFactory, this.environmentProperties,
|
|
|
|
return self();
|
|
|
|
this.systemProperties, this.classLoader, parent, this.configurations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Register the specified user configuration classes with the
|
|
|
|
* Register the specified user configuration classes with the
|
|
|
|
* {@link ApplicationContext}.
|
|
|
|
* {@link ApplicationContext}.
|
|
|
|
* @param configurationClasses the user configuration classes to add
|
|
|
|
* @param configurationClasses the user configuration classes to add
|
|
|
|
* @return this instance
|
|
|
|
* @return a new instance with the updated configuration
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public SELF withUserConfiguration(Class<?>... configurationClasses) {
|
|
|
|
public SELF withUserConfiguration(Class<?>... configurationClasses) {
|
|
|
|
return withConfiguration(UserConfigurations.of(configurationClasses));
|
|
|
|
return withConfiguration(UserConfigurations.of(configurationClasses));
|
|
|
@ -186,32 +212,42 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Register the specified configuration classes with the {@link ApplicationContext}.
|
|
|
|
* Register the specified configuration classes with the {@link ApplicationContext}.
|
|
|
|
* @param configurations the configurations to add
|
|
|
|
* @param configurations the configurations to add
|
|
|
|
* @return this instance
|
|
|
|
* @return a new instance with the updated configuration
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public SELF withConfiguration(Configurations configurations) {
|
|
|
|
public SELF withConfiguration(Configurations configurations) {
|
|
|
|
Assert.notNull(configurations, "Configurations must not be null");
|
|
|
|
Assert.notNull(configurations, "Configurations must not be null");
|
|
|
|
this.configurations.add(configurations);
|
|
|
|
return newInstance(this.contextFactory, this.environmentProperties,
|
|
|
|
return self();
|
|
|
|
this.systemProperties, this.classLoader, this.parent,
|
|
|
|
|
|
|
|
add(this.configurations, configurations));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private <T> List<T> add(List<T> list, T element) {
|
|
|
|
protected final SELF self() {
|
|
|
|
List<T> result = new ArrayList<>(list);
|
|
|
|
return (SELF) this;
|
|
|
|
result.add(element);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract SELF newInstance(Supplier<C> contextFactory,
|
|
|
|
|
|
|
|
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
|
|
|
|
|
|
|
|
ClassLoader classLoader, ApplicationContext parent,
|
|
|
|
|
|
|
|
List<Configurations> configurations);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 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 consumer} 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}
|
|
|
|
|
|
|
|
* @return this instance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void run(ContextConsumer<? super A> consumer) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
|
|
public SELF run(ContextConsumer<? super A> consumer) {
|
|
|
|
this.systemProperties.applyToSystemProperties(() -> {
|
|
|
|
this.systemProperties.applyToSystemProperties(() -> {
|
|
|
|
try (A context = createAssertableContext()) {
|
|
|
|
try (A context = createAssertableContext()) {
|
|
|
|
accept(consumer, context);
|
|
|
|
accept(consumer, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return (SELF) this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|