EnvironmentPostProcessorApplicationListenerTests Class — spring-boot Architecture
Architecture documentation for the EnvironmentPostProcessorApplicationListenerTests class in EnvironmentPostProcessorApplicationListenerTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/test/java/org/springframework/boot/support/EnvironmentPostProcessorApplicationListenerTests.java lines 88–392
class EnvironmentPostProcessorApplicationListenerTests {
@Nested
class ListenerTests {
private final DeferredLogs deferredLogs = spy(new DeferredLogs());
private final DefaultBootstrapContext bootstrapContext = spy(new DefaultBootstrapContext());
private final EnvironmentPostProcessorApplicationListener listener = new EnvironmentPostProcessorApplicationListener();
@BeforeEach
void setup() {
ReflectionTestUtils.setField(this.listener, "deferredLogs", this.deferredLogs);
ReflectionTestUtils.setField(this.listener, "postProcessorsFactory",
(Function<ClassLoader, EnvironmentPostProcessorsFactory>) (
classLoader) -> EnvironmentPostProcessorsFactory.of(TestEnvironmentPostProcessor.class));
}
@Test
void createUsesSpringFactories() {
EnvironmentPostProcessorApplicationListener listener = new EnvironmentPostProcessorApplicationListener();
assertThat(listener.getEnvironmentPostProcessors(null, this.bootstrapContext)).hasSizeGreaterThan(1);
}
@Test
void createWhenHasFactoryUsesFactory() {
EnvironmentPostProcessorApplicationListener listener = EnvironmentPostProcessorApplicationListener
.with(EnvironmentPostProcessorsFactory.of(TestEnvironmentPostProcessor.class));
List<EnvironmentPostProcessor> postProcessors = listener.getEnvironmentPostProcessors(null,
this.bootstrapContext);
assertThat(postProcessors).hasSize(1);
assertThat(postProcessors.get(0)).isInstanceOf(TestEnvironmentPostProcessor.class);
}
@Test
void supportsEventTypeWhenApplicationEnvironmentPreparedEventReturnsTrue() {
assertThat(this.listener.supportsEventType(ApplicationEnvironmentPreparedEvent.class)).isTrue();
}
@Test
void supportsEventTypeWhenApplicationPreparedEventReturnsTrue() {
assertThat(this.listener.supportsEventType(ApplicationPreparedEvent.class)).isTrue();
}
@Test
void supportsEventTypeWhenApplicationFailedEventReturnsTrue() {
assertThat(this.listener.supportsEventType(ApplicationFailedEvent.class)).isTrue();
}
@Test
void supportsEventTypeWhenOtherEventReturnsFalse() {
assertThat(this.listener.supportsEventType(ApplicationStartingEvent.class)).isFalse();
}
@Test
void onApplicationEventWhenApplicationEnvironmentPreparedEventCallsPostProcessors() {
SpringApplication application = mock(SpringApplication.class);
MockEnvironment environment = new MockEnvironment();
ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(this.bootstrapContext,
application, new String[0], environment);
this.listener.onApplicationEvent(event);
assertThat(environment.getProperty("processed")).isEqualTo("true");
}
@Test
void onApplicationEventWhenApplicationPreparedEventSwitchesLogs() {
SpringApplication application = mock(SpringApplication.class);
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
ApplicationPreparedEvent event = new ApplicationPreparedEvent(application, new String[0], context);
this.listener.onApplicationEvent(event);
then(this.deferredLogs).should().switchOverAll();
}
@Test
void onApplicationEventWhenApplicationFailedEventSwitchesLogs() {
SpringApplication application = mock(SpringApplication.class);
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
ApplicationFailedEvent event = new ApplicationFailedEvent(application, new String[0], context,
new RuntimeException());
this.listener.onApplicationEvent(event);
then(this.deferredLogs).should().switchOverAll();
}
static class TestEnvironmentPostProcessor implements EnvironmentPostProcessor {
TestEnvironmentPostProcessor(DeferredLogFactory logFactory, BootstrapRegistry bootstrapRegistry) {
assertThat(logFactory).isNotNull();
assertThat(bootstrapRegistry).isNotNull();
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
((MockEnvironment) environment).setProperty("processed", "true");
}
}
}
@Nested
class AotTests {
private static final ClassName TEST_APP = ClassName.get("com.example", "TestApp");
@Test
void aotContributionIsNotNecessaryWithDefaultConfiguration() {
assertThat(getContribution(new StandardEnvironment())).isNull();
}
@Test
void aotContributionIsNotNecessaryWithDefaultProfileActive() {
StandardEnvironment environment = new StandardEnvironment();
environment.setDefaultProfiles("fallback");
environment.setActiveProfiles("fallback");
assertThat(getContribution(environment)).isNull();
}
@Test
void aotContributionRegistersActiveProfiles() {
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("one", "two");
compile(createContext(environment), (compiled) -> {
EnvironmentPostProcessor environmentPostProcessor = compiled.getInstance(EnvironmentPostProcessor.class,
ClassName.get("com.example", "TestApp__EnvironmentPostProcessor").toString());
StandardEnvironment freshEnvironment = new StandardEnvironment();
environmentPostProcessor.postProcessEnvironment(freshEnvironment, new SpringApplication());
assertThat(freshEnvironment.getActiveProfiles()).containsExactly("one", "two");
});
}
@Test
void aotContributionRegistersReflectionHints() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("one", "two");
applicationContext.getBeanFactory().registerSingleton("environment", environment);
BeanFactoryInitializationAotContribution aotContribution = new EnvironmentBeanFactoryInitializationAotProcessor()
.processAheadOfTime(applicationContext.getBeanFactory());
assertThat(aotContribution).isNotNull();
GenerationContext generationContext = new TestGenerationContext();
aotContribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class));
assertThat(RuntimeHintsPredicates.reflection()
.onType(TypeReference.of(TestGenerationContext.TEST_TARGET + "__"
+ EnvironmentPostProcessorApplicationListener.AOT_FEATURE_NAME))
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
.accepts(generationContext.getRuntimeHints());
}
@Test
void shouldUseAotEnvironmentPostProcessor() {
SpringApplication application = new SpringApplication(ExampleAotProcessedApp.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setMainApplicationClass(ExampleAotProcessedApp.class);
System.setProperty(AotDetector.AOT_ENABLED, "true");
try {
ApplicationContext context = application.run();
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("one", "three");
assertThat(context.getBean("test")).isEqualTo("test");
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
}
}
@Test
void aotEnvironmentPostProcessorShouldBeAppliedFirst(@TempDir Path tempDir) {
Properties properties = new Properties();
properties.put(EnvironmentPostProcessor.class.getName(), TestEnvironmentPostProcessor.class.getName());
ClassLoader classLoader = createClassLoaderWithAdditionalSpringFactories(tempDir, properties);
DefaultResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
SpringApplication application = new SpringApplication(ExampleAotProcessedApp.class);
application.setResourceLoader(resourceLoader);
application.setWebApplicationType(WebApplicationType.NONE);
application.setMainApplicationClass(ExampleAotProcessedApp.class);
System.setProperty(AotDetector.AOT_ENABLED, "true");
try {
ApplicationContext context = application.run();
// See TestEnvironmentPostProcessor
assertThat(context.getEnvironment().getProperty("test.activeProfiles")).isEqualTo("one,three");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("one", "three");
assertThat(context.getBean("test")).isEqualTo("test");
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
}
}
@Test
void shouldBeLenientIfAotEnvironmentPostProcessorDoesNotExist() {
SpringApplication application = new SpringApplication(ExampleAotProcessedNoProfileApp.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setMainApplicationClass(ExampleAotProcessedNoProfileApp.class);
System.setProperty(AotDetector.AOT_ENABLED, "true");
try {
ApplicationContext context = application.run();
assertThat(context.getEnvironment().getActiveProfiles()).isEmpty();
assertThat(context.getBean("test")).isEqualTo("test");
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
}
}
private @Nullable BeanFactoryInitializationAotContribution getContribution(
ConfigurableEnvironment environment) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerSingleton(ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME, environment);
return new EnvironmentBeanFactoryInitializationAotProcessor().processAheadOfTime(beanFactory);
}
private GenericApplicationContext createContext(ConfigurableEnvironment environment) {
GenericApplicationContext context = new GenericApplicationContext();
context.setEnvironment(environment);
return context;
}
private void compile(GenericApplicationContext context, Consumer<Compiled> compiled) {
TestGenerationContext generationContext = new TestGenerationContext(TEST_APP);
new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
generationContext.writeGeneratedContent();
TestCompiler.forSystem()
.withCompilerOptions("-Xlint:deprecation,removal", "-Werror")
.with(generationContext)
.compile(compiled);
}
private ClassLoader createClassLoaderWithAdditionalSpringFactories(Path tempDir, Properties properties) {
return new ClassLoader() {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
Enumeration<URL> resources = super.getResources(name);
if (SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION.equals(name)) {
Path springFactories = tempDir.resolve("spring.factories");
try (BufferedWriter writer = Files.newBufferedWriter(springFactories)) {
properties.store(writer, "");
}
List<URL> allResources = new ArrayList<>();
allResources.add(springFactories.toUri().toURL());
allResources.addAll(Collections.list(resources));
return Collections.enumeration(allResources);
}
return resources;
}
};
}
static class TestEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
MockPropertySource propertySource = new MockPropertySource().withProperty("test.activeProfiles",
StringUtils.arrayToCommaDelimitedString(environment.getActiveProfiles()));
environment.getPropertySources().addLast(propertySource);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
static class ExampleAotProcessedApp {
}
static class ExampleAotProcessedApp__ApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getBeanFactory().registerSingleton("test", "test");
}
}
static class ExampleAotProcessedApp__EnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.addActiveProfile("one");
environment.addActiveProfile("three");
}
}
static class ExampleAotProcessedNoProfileApp {
}
static class ExampleAotProcessedNoProfileApp__ApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getBeanFactory().registerSingleton("test", "test");
}
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free