Merge branch '2.2.x' into 2.3.x

Closes gh-22234
pull/22996/head
Andy Wilkinson 4 years ago
commit 2007490ce7

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,12 +16,14 @@
package org.springframework.boot.autoconfigure.context;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@ -33,43 +35,75 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link PropertyPlaceholderAutoConfiguration}.
*
* @author Dave Syer
* @author Andy Wilkinson
*/
class PropertyPlaceholderAutoConfigurationTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@AfterEach
void close() {
if (this.context != null) {
this.context.close();
}
@Test
void whenTheAutoConfigurationIsNotUsedThenBeanDefinitionPlaceholdersAreNotResolved() {
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("${fruit:apple}"));
}
@Test
void whenTheAutoConfigurationIsUsedThenBeanDefinitionPlaceholdersAreResolved() {
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("banana"));
}
@Test
void propertyPlaceholders() {
this.context.register(PropertyPlaceholderAutoConfiguration.class, PlaceholderConfig.class);
TestPropertyValues.of("foo:two").applyTo(this.context);
this.context.refresh();
assertThat(this.context.getBean(PlaceholderConfig.class).getFoo()).isEqualTo("two");
void whenTheAutoConfigurationIsNotUsedThenValuePlaceholdersAreResolved() {
this.contextRunner.withPropertyValues("fruit:banana").withUserConfiguration(PlaceholderConfig.class)
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("banana"));
}
@Test
void propertyPlaceholdersOverride() {
this.context.register(PropertyPlaceholderAutoConfiguration.class, PlaceholderConfig.class,
PlaceholdersOverride.class);
TestPropertyValues.of("foo:two").applyTo(this.context);
this.context.refresh();
assertThat(this.context.getBean(PlaceholderConfig.class).getFoo()).isEqualTo("spam");
void whenTheAutoConfigurationIsUsedThenValuePlaceholdersAreResolved() {
this.contextRunner.withPropertyValues("fruit:banana")
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
.withUserConfiguration(PlaceholderConfig.class)
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("banana"));
}
@Test
void whenThereIsAUserDefinedPropertySourcesPlaceholderConfigurerThenItIsUsedForBeanDefinitionPlaceholderResolution() {
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
.withUserConfiguration(PlaceholdersOverride.class)
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("orange"));
}
@Test
void whenThereIsAUserDefinedPropertySourcesPlaceholderConfigurerThenItIsUsedForValuePlaceholderResolution() {
this.contextRunner.withPropertyValues("fruit:banana")
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
.withUserConfiguration(PlaceholderConfig.class, PlaceholdersOverride.class)
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("orange"));
}
private void definePlaceholderBean(ConfigurableApplicationContext context) {
((BeanDefinitionRegistry) context.getBeanFactory()).registerBeanDefinition("placeholderBean",
BeanDefinitionBuilder.genericBeanDefinition(PlaceholderBean.class)
.addConstructorArgValue("${fruit:apple}").getBeanDefinition());
}
@Configuration(proxyBeanMethods = false)
static class PlaceholderConfig {
@Value("${foo:bar}")
private String foo;
@Value("${fruit:apple}")
private String fruit;
}
static class PlaceholderBean {
private final String fruit;
String getFoo() {
return this.foo;
PlaceholderBean(String fruit) {
this.fruit = fruit;
}
}
@ -80,7 +114,8 @@ class PropertyPlaceholderAutoConfigurationTests {
@Bean
static PropertySourcesPlaceholderConfigurer morePlaceholders() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "foo=spam" }, "="));
configurer
.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "fruit=orange" }, "="));
configurer.setLocalOverride(true);
configurer.setOrder(0);
return configurer;

Loading…
Cancel
Save