Add initializer support

Add `ApplicationContextInitializer` support to
`ApplicationContextRunner`.
This commit is contained in:
Phillip Webb 2018-02-12 14:53:01 -08:00
parent 3bec55e16c
commit 7ab587a067
5 changed files with 74 additions and 30 deletions

View File

@ -29,6 +29,7 @@ import org.springframework.boot.test.context.assertj.ApplicationContextAssert;
import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigRegistry;
import org.springframework.core.ResolvableType;
@ -99,6 +100,8 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
private final Supplier<C> contextFactory;
private final List<ApplicationContextInitializer<C>> initializers;
private final TestPropertyValues environmentProperties;
private final TestPropertyValues systemProperties;
@ -114,13 +117,14 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @param contextFactory the factory used to create the actual context
*/
protected AbstractApplicationContextRunner(Supplier<C> contextFactory) {
this(contextFactory, TestPropertyValues.empty(), TestPropertyValues.empty(), null,
null, Collections.emptyList());
this(contextFactory, Collections.emptyList(), 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 initializers the initializers
* @param environmentProperties the environment properties
* @param systemProperties the system properties
* @param classLoader the class loader
@ -128,6 +132,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @param configurations the configuration
*/
protected AbstractApplicationContextRunner(Supplier<C> contextFactory,
List<ApplicationContextInitializer<C>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
@ -135,7 +140,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
Assert.notNull(environmentProperties, "EnvironmentProperties must not be null");
Assert.notNull(systemProperties, "SystemProperties must not be null");
Assert.notNull(configurations, "Configurations must not be null");
Assert.notNull(initializers, "Initializers must not be null");
this.contextFactory = contextFactory;
this.initializers = Collections.unmodifiableList(initializers);
this.environmentProperties = environmentProperties;
this.systemProperties = systemProperties;
this.classLoader = classLoader;
@ -143,6 +150,19 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
this.configurations = Collections.unmodifiableList(configurations);
}
/**
* Add a {@link ApplicationContextInitializer} to be called when the context is
* created.
* @param initializer the initializer to add
* @return a new instance with the updated initializers
*/
public SELF withInitializer(ApplicationContextInitializer<C> initializer) {
Assert.notNull(initializer, "Initializer must not be null");
return newInstance(this.contextFactory, add(this.initializers, initializer),
this.environmentProperties, this.systemProperties, this.classLoader,
this.parent, this.configurations);
}
/**
* Add the specified {@link Environment} property pairs. Key-value pairs can be
* specified with colon (":") or equals ("=") separators. Override matching keys that
@ -154,9 +174,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @see #withSystemProperties(String...)
*/
public SELF withPropertyValues(String... pairs) {
return newInstance(this.contextFactory, this.environmentProperties.and(pairs),
this.systemProperties, this.classLoader, this.parent,
this.configurations);
return newInstance(this.contextFactory, this.initializers,
this.environmentProperties.and(pairs), this.systemProperties,
this.classLoader, this.parent, this.configurations);
}
/**
@ -170,9 +190,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @see #withSystemProperties(String...)
*/
public SELF withSystemProperties(String... pairs) {
return newInstance(this.contextFactory, this.environmentProperties,
this.systemProperties.and(pairs), this.classLoader, this.parent,
this.configurations);
return newInstance(this.contextFactory, this.initializers,
this.environmentProperties, this.systemProperties.and(pairs),
this.classLoader, this.parent, this.configurations);
}
/**
@ -184,8 +204,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @see FilteredClassLoader
*/
public SELF withClassLoader(ClassLoader classLoader) {
return newInstance(this.contextFactory, this.environmentProperties,
this.systemProperties, classLoader, this.parent, this.configurations);
return newInstance(this.contextFactory, this.initializers,
this.environmentProperties, this.systemProperties, classLoader,
this.parent, this.configurations);
}
/**
@ -195,8 +216,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @return a new instance with the updated parent
*/
public SELF withParent(ApplicationContext parent) {
return newInstance(this.contextFactory, this.environmentProperties,
this.systemProperties, this.classLoader, parent, this.configurations);
return newInstance(this.contextFactory, this.initializers,
this.environmentProperties, this.systemProperties, this.classLoader,
parent, this.configurations);
}
/**
@ -216,9 +238,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
*/
public SELF withConfiguration(Configurations configurations) {
Assert.notNull(configurations, "Configurations must not be null");
return newInstance(this.contextFactory, this.environmentProperties,
this.systemProperties, this.classLoader, this.parent,
add(this.configurations, configurations));
return newInstance(this.contextFactory, this.initializers,
this.environmentProperties, this.systemProperties, this.classLoader,
this.parent, add(this.configurations, configurations));
}
/**
@ -238,6 +260,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
}
protected abstract SELF newInstance(Supplier<C> contextFactory,
List<ApplicationContextInitializer<C>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations);
@ -295,6 +318,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
if (classes.length > 0) {
((AnnotationConfigRegistry) context).register(classes);
}
this.initializers.forEach((initializer) -> initializer.initialize(context));
context.refresh();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -23,6 +23,7 @@ import org.springframework.boot.context.annotation.Configurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -60,21 +61,24 @@ public class ApplicationContextRunner extends
private ApplicationContextRunner(
Supplier<ConfigurableApplicationContext> contextFactory,
List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
super(contextFactory, environmentProperties, systemProperties, classLoader,
parent, configurations);
super(contextFactory, initializers, environmentProperties, systemProperties,
classLoader, parent, configurations);
}
@Override
protected ApplicationContextRunner newInstance(
Supplier<ConfigurableApplicationContext> contextFactory,
List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
return new ApplicationContextRunner(contextFactory, environmentProperties,
systemProperties, classLoader, parent, configurations);
return new ApplicationContextRunner(contextFactory, initializers,
environmentProperties, systemProperties, classLoader, parent,
configurations);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -25,6 +25,7 @@ import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
/**
* An {@link AbstractApplicationContextRunner ApplicationContext runner} for a
@ -60,20 +61,22 @@ public final class ReactiveWebApplicationContextRunner extends
private ReactiveWebApplicationContextRunner(
Supplier<ConfigurableReactiveWebApplicationContext> contextFactory,
List<ApplicationContextInitializer<ConfigurableReactiveWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
super(contextFactory, environmentProperties, systemProperties, classLoader,
parent, configurations);
super(contextFactory, initializers, environmentProperties, systemProperties,
classLoader, parent, configurations);
}
@Override
protected ReactiveWebApplicationContextRunner newInstance(
Supplier<ConfigurableReactiveWebApplicationContext> contextFactory,
List<ApplicationContextInitializer<ConfigurableReactiveWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
return new ReactiveWebApplicationContextRunner(contextFactory,
return new ReactiveWebApplicationContextRunner(contextFactory, initializers,
environmentProperties, systemProperties, classLoader, parent,
configurations);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -23,6 +23,7 @@ import org.springframework.boot.context.annotation.Configurations;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
@ -64,21 +65,24 @@ public final class WebApplicationContextRunner extends
private WebApplicationContextRunner(
Supplier<ConfigurableWebApplicationContext> contextFactory,
List<ApplicationContextInitializer<ConfigurableWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
super(contextFactory, environmentProperties, systemProperties, classLoader,
parent, configurations);
super(contextFactory, initializers, environmentProperties, systemProperties,
classLoader, parent, configurations);
}
@Override
protected WebApplicationContextRunner newInstance(
Supplier<ConfigurableWebApplicationContext> contextFactory,
List<ApplicationContextInitializer<ConfigurableWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties,
ClassLoader classLoader, ApplicationContext parent,
List<Configurations> configurations) {
return new WebApplicationContextRunner(contextFactory, environmentProperties,
systemProperties, classLoader, parent, configurations);
return new WebApplicationContextRunner(contextFactory, initializers,
environmentProperties, systemProperties, classLoader, parent,
configurations);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -18,6 +18,7 @@ package org.springframework.boot.test.context.runner;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import com.google.gson.Gson;
import org.junit.Rule;
@ -50,6 +51,14 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void runWithInitializerShouldInitialize() {
AtomicBoolean called = new AtomicBoolean();
get().withInitializer((context) -> called.set(true)).run((context) -> {
});
assertThat(called).isTrue();
}
@Test
public void runWithSystemPropertiesShouldSetAndRemoveProperties() {
String key = "test." + UUID.randomUUID();