Browse Source

Remove internal EnvironmentTestUtils

Replace the internal EnvironmentTestUtils using in `spring-boot` tests
with Spring's TestPropertySourceUtils.

Fixes gh-5492
pull/5497/head
Phillip Webb 10 years ago
parent
commit
f96cd144dc
  1. 9
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  2. 5
      spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryPerformanceTests.java
  3. 12
      spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBinderTests.java
  4. 25
      spring-boot/src/test/java/org/springframework/boot/cloud/cloudfoundry/CloudFoundryVcapEnvironmentPostProcessorTests.java
  5. 23
      spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java
  6. 8
      spring-boot/src/test/java/org/springframework/boot/context/FileEncodingApplicationListenerTests.java
  7. 72
      spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java
  8. 22
      spring-boot/src/test/java/org/springframework/boot/context/config/DelegatingApplicationContextInitializerTests.java
  9. 10
      spring-boot/src/test/java/org/springframework/boot/context/config/DelegatingApplicationListenerTests.java
  10. 5
      spring-boot/src/test/java/org/springframework/boot/context/embedded/config/AnsiOutputApplicationListenerTests.java
  11. 67
      spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java
  12. 71
      spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java
  13. 18
      spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java
  14. 66
      spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java
  15. 8
      spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java
  16. 74
      spring-boot/src/test/java/org/springframework/boot/testutil/EnvironmentTestUtils.java

9
spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -43,7 +43,6 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.boot.testutil.OutputCapture; import org.springframework.boot.testutil.OutputCapture;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@ -68,6 +67,7 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.support.StandardServletEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
@ -737,8 +737,8 @@ public class SpringApplicationTests {
ApplicationEnvironmentPreparedEvent event) { ApplicationEnvironmentPreparedEvent event) {
assertThat(event.getEnvironment()) assertThat(event.getEnvironment())
.isInstanceOf(StandardServletEnvironment.class); .isInstanceOf(StandardServletEnvironment.class);
EnvironmentTestUtils.addEnvironment(event.getEnvironment(), TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
"foo=bar"); event.getEnvironment(), "foo=bar");
event.getSpringApplication().setWebEnvironment(false); event.getSpringApplication().setWebEnvironment(false);
} }
@ -748,7 +748,8 @@ public class SpringApplicationTests {
.isNotInstanceOf(StandardServletEnvironment.class); .isNotInstanceOf(StandardServletEnvironment.class);
assertThat(this.context.getEnvironment().getProperty("foo")); assertThat(this.context.getEnvironment().getProperty("foo"));
assertThat(this.context.getEnvironment().getPropertySources().iterator().next() assertThat(this.context.getEnvironment().getPropertySources().iterator().next()
.getName()).isEqualTo("test"); .getName()).isEqualTo(
TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
} }
@Test @Test

5
spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryPerformanceTests.java

@ -26,9 +26,9 @@ import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory; import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.support.StaticMessageSource; import org.springframework.context.support.StaticMessageSource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -56,7 +56,8 @@ public class PropertiesConfigurationFactoryPerformanceTests {
@BeforeClass @BeforeClass
public static void init() { public static void init() {
EnvironmentTestUtils.addEnvironment(environment, "name: blah", "bar: blah"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment,
"name=blah", "bar=blah");
} }
@Theory @Theory

12
spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBinderTests.java

@ -20,8 +20,8 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -36,7 +36,8 @@ public class PropertySourcesBinderTests {
@Test @Test
public void extractAllWithPrefix() { public void extractAllWithPrefix() {
EnvironmentTestUtils.addEnvironment(this.env, "foo.first=1", "foo.second=2"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.env, "foo.first=1",
"foo.second=2");
Map<String, Object> content = new PropertySourcesBinder(this.env) Map<String, Object> content = new PropertySourcesBinder(this.env)
.extractAll("foo"); .extractAll("foo");
assertThat(content.get("first")).isEqualTo("1"); assertThat(content.get("first")).isEqualTo("1");
@ -47,8 +48,8 @@ public class PropertySourcesBinderTests {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void extractNoPrefix() { public void extractNoPrefix() {
EnvironmentTestUtils.addEnvironment(this.env, "foo.ctx.first=1", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.env,
"foo.ctx.second=2"); "foo.ctx.first=1", "foo.ctx.second=2");
Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll(""); Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll("");
assertThat(content.get("foo")).isInstanceOf(Map.class); assertThat(content.get("foo")).isInstanceOf(Map.class);
Map<String, Object> foo = (Map<String, Object>) content.get("foo"); Map<String, Object> foo = (Map<String, Object>) content.get("foo");
@ -62,7 +63,8 @@ public class PropertySourcesBinderTests {
@Test @Test
public void bindToSimplePojo() { public void bindToSimplePojo() {
EnvironmentTestUtils.addEnvironment(this.env, "test.name=foo", "test.counter=42"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.env,
"test.name=foo", "test.counter=42");
TestBean bean = new TestBean(); TestBean bean = new TestBean();
new PropertySourcesBinder(this.env).bindTo("test", bean); new PropertySourcesBinder(this.env).bindTo("test", bean);
assertThat(bean.getName()).isEqualTo("foo"); assertThat(bean.getName()).isEqualTo("foo");

25
spring-boot/src/test/java/org/springframework/boot/cloud/cloudfoundry/CloudFoundryVcapEnvironmentPostProcessorTests.java

@ -19,9 +19,9 @@ package org.springframework.boot.cloud.cloudfoundry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor; import org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -39,8 +39,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test @Test
public void testApplicationProperties() { public void testApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION:{\"application_users\":[]," "VCAP_APPLICATION={\"application_users\":[],"
+ "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\"," + "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\","
+ "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\"," + "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\","
+ "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"]," + "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"],"
@ -60,8 +60,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test @Test
public void testApplicationUris() { public void testApplicationUris() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION:{\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}"); "VCAP_APPLICATION={\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}");
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertThat(this.context.getEnvironment().getProperty("vcap.application.uris[0]")) assertThat(this.context.getEnvironment().getProperty("vcap.application.uris[0]"))
.isEqualTo("foo.cfapps.io"); .isEqualTo("foo.cfapps.io");
@ -69,15 +69,16 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test @Test
public void testUnparseableApplicationProperties() { public void testUnparseableApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context, "VCAP_APPLICATION:"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION:");
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertThat(getProperty("vcap")).isNull(); assertThat(getProperty("vcap")).isNull();
} }
@Test @Test
public void testNullApplicationProperties() { public void testNullApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION:{\"application_users\":null," "VCAP_APPLICATION={\"application_users\":null,"
+ "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\"," + "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\","
+ "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\"," + "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\","
+ "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"]," + "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"],"
@ -95,8 +96,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test @Test
public void testServiceProperties() { public void testServiceProperties() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES:{\"rds-mysql-n/a\":[{" "VCAP_SERVICES={\"rds-mysql-n/a\":[{"
+ "\"name\":\"mysql\",\"label\":\"rds-mysql-n/a\"," + "\"name\":\"mysql\",\"label\":\"rds-mysql-n/a\","
+ "\"plan\":\"10mb\",\"credentials\":{" + "\"plan\":\"10mb\",\"credentials\":{"
+ "\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\"," + "\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\","
@ -114,8 +115,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test @Test
public void testServicePropertiesWithoutNA() { public void testServicePropertiesWithoutNA() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES:{\"rds-mysql\":[{" "VCAP_SERVICES={\"rds-mysql\":[{"
+ "\"name\":\"mysql\",\"label\":\"rds-mysql\",\"plan\":\"10mb\"," + "\"name\":\"mysql\",\"label\":\"rds-mysql\",\"plan\":\"10mb\","
+ "\"credentials\":{\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\"," + "\"credentials\":{\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\","
+ "\"hostname\":\"mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\"," + "\"hostname\":\"mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\","

23
spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java

@ -18,9 +18,9 @@ package org.springframework.boot.context;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -43,8 +43,8 @@ public class ContextIdApplicationContextInitializerTests {
@Test @Test
public void testNameAndPort() { public void testNameAndPort() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:foo", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"PORT:8080"); "spring.application.name=foo", "PORT=8080");
this.initializer.initialize(context); this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("foo:8080"); assertThat(context.getId()).isEqualTo("foo:8080");
} }
@ -52,8 +52,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test @Test
public void testNameAndProfiles() { public void testNameAndProfiles() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:foo", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"spring.profiles.active: spam,bar", "spring.application.index:12"); "spring.application.name=foo", "spring.profiles.active=spam,bar",
"spring.application.index=12");
this.initializer.initialize(context); this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("foo:spam,bar:12"); assertThat(context.getId()).isEqualTo("foo:spam,bar:12");
} }
@ -61,9 +62,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test @Test
public void testCloudFoundry() { public void testCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.config.name:foo", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"PORT:8080", "vcap.application.name:bar", "spring.config.name=foo", "PORT=8080", "vcap.application.name=bar",
"vcap.application.instance_index:2"); "vcap.application.instance_index=2");
this.initializer.initialize(context); this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("bar:2"); assertThat(context.getId()).isEqualTo("bar:2");
} }
@ -71,9 +72,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test @Test
public void testExplicitNameIsChosenInFavorOfCloudFoundry() { public void testExplicitNameIsChosenInFavorOfCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:spam", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"spring.config.name:foo", "PORT:8080", "vcap.application.name:bar", "spring.application.name=spam", "spring.config.name=foo", "PORT=8080",
"vcap.application.instance_index:2"); "vcap.application.name=bar", "vcap.application.instance_index=2");
this.initializer.initialize(context); this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("spam:2"); assertThat(context.getId()).isEqualTo("spam:2");
} }

8
spring-boot/src/test/java/org/springframework/boot/context/FileEncodingApplicationListenerTests.java

@ -21,9 +21,9 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
/** /**
* Tests for {@link FileEncodingApplicationListener}. * Tests for {@link FileEncodingApplicationListener}.
@ -39,8 +39,8 @@ public class FileEncodingApplicationListenerTests {
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void testIllegalState() { public void testIllegalState() {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.mandatory_file_encoding:FOO"); "spring.mandatory_file_encoding=FOO");
this.initializer.onApplicationEvent(this.event); this.initializer.onApplicationEvent(this.event);
} }
@ -52,7 +52,7 @@ public class FileEncodingApplicationListenerTests {
@Test @Test
public void testSunnyDayMandated() { public void testSunnyDayMandated() {
Assume.assumeNotNull(System.getProperty("file.encoding")); Assume.assumeNotNull(System.getProperty("file.encoding"));
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.mandatory_file_encoding:" + System.getProperty("file.encoding")); "spring.mandatory_file_encoding:" + System.getProperty("file.encoding"));
this.initializer.onApplicationEvent(this.event); this.initializer.onApplicationEvent(this.event);
} }

72
spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

@ -46,7 +46,6 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnumerableCompositePropertySource; import org.springframework.boot.env.EnumerableCompositePropertySource;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.boot.testutil.OutputCapture; import org.springframework.boot.testutil.OutputCapture;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -63,6 +62,7 @@ import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -156,8 +156,9 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadTwoPropertiesFile() throws Exception { public void loadTwoPropertiesFile() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
+ "classpath:application.properties,classpath:testproperties.properties"); "spring.config.location="
+ "classpath:application.properties,classpath:testproperties.properties");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile"); assertThat(property).isEqualTo("frompropertiesfile");
@ -165,8 +166,9 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadTwoPropertiesFilesWithProfiles() throws Exception { public void loadTwoPropertiesFilesWithProfiles() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
+ "classpath:enableprofile.properties,classpath:enableother.properties"); "spring.config.location="
+ "classpath:enableprofile.properties,classpath:enableother.properties");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("other"); assertThat(this.environment.getActiveProfiles()).containsExactly("other");
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
@ -175,8 +177,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadTwoPropertiesFilesWithProfilesAndSwitchOneOff() throws Exception { public void loadTwoPropertiesFilesWithProfilesAndSwitchOneOff() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location:" + "classpath:enabletwoprofiles.properties," "spring.config.location=classpath:enabletwoprofiles.properties,"
+ "classpath:enableprofile.properties"); + "classpath:enableprofile.properties");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("myprofile"); assertThat(this.environment.getActiveProfiles()).containsExactly("myprofile");
@ -189,9 +191,9 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadTwoPropertiesFilesWithProfilesAndSwitchOneOffFromSpecificLocation() public void loadTwoPropertiesFilesWithProfilesAndSwitchOneOffFromSpecificLocation()
throws Exception { throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.name:enabletwoprofiles", "spring.config.name=enabletwoprofiles",
"spring.config.location:classpath:enableprofile.properties"); "spring.config.location=classpath:enableprofile.properties");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("myprofile"); assertThat(this.environment.getActiveProfiles()).containsExactly("myprofile");
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
@ -225,8 +227,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void moreSpecificLocationTakesPrecedenceOverRoot() throws Exception { public void moreSpecificLocationTakesPrecedenceOverRoot() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.name:specific"); "spring.config.name=specific");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property).isEqualTo("specific"); assertThat(property).isEqualTo("specific");
@ -234,8 +236,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadTwoOfThreePropertiesFile() throws Exception { public void loadTwoOfThreePropertiesFile() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location:" + "classpath:application.properties," "spring.config.location=classpath:application.properties,"
+ "classpath:testproperties.properties," + "classpath:testproperties.properties,"
+ "classpath:nonexistent.properties"); + "classpath:nonexistent.properties");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
@ -373,7 +375,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadPropertiesThenProfilePropertiesWithOverride() throws Exception { public void loadPropertiesThenProfilePropertiesWithOverride() throws Exception {
this.environment.setActiveProfiles("other"); this.environment.setActiveProfiles("other");
// EnvironmentTestUtils.addEnvironment(this.environment, // TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
// "spring.profiles.active:other"); // "spring.profiles.active:other");
this.initializer.setSearchNames("enableprofile"); this.initializer.setSearchNames("enableprofile");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
@ -398,8 +400,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void profilesAddedToEnvironmentAndViaProperty() throws Exception { public void profilesAddedToEnvironmentAndViaProperty() throws Exception {
// External profile takes precedence over profile added via the environment // External profile takes precedence over profile added via the environment
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active:other"); "spring.profiles.active=other");
this.environment.addActiveProfile("dev"); this.environment.addActiveProfile("dev");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other"); assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
@ -410,8 +412,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void profilesAddedToEnvironmentAndViaPropertyDuplicate() throws Exception { public void profilesAddedToEnvironmentAndViaPropertyDuplicate() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active:dev,other"); "spring.profiles.active=dev,other");
this.environment.addActiveProfile("dev"); this.environment.addActiveProfile("dev");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other"); assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
@ -423,8 +425,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void profilesAddedToEnvironmentAndViaPropertyDuplicateEnvironmentWins() public void profilesAddedToEnvironmentAndViaPropertyDuplicateEnvironmentWins()
throws Exception { throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active:other,dev"); "spring.profiles.active=other,dev");
this.environment.addActiveProfile("other"); this.environment.addActiveProfile("other");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other"); assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
@ -529,8 +531,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void yamlProfileCanBeChanged() throws Exception { public void yamlProfileCanBeChanged() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active:prod"); "spring.profiles.active=prod");
this.initializer.setSearchNames("testsetprofiles"); this.initializer.setSearchNames("testsetprofiles");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("prod"); assertThat(this.environment.getActiveProfiles()).containsExactly("prod");
@ -538,7 +540,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void specificNameAndProfileFromExistingSource() throws Exception { public void specificNameAndProfileFromExistingSource() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=specificprofile", "spring.profiles.active=specificprofile",
"spring.config.name=specificfile"); "spring.config.name=specificfile");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
@ -549,8 +551,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void specificResource() throws Exception { public void specificResource() throws Exception {
String location = "classpath:specificlocation.properties"; String location = "classpath:specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location:" + location); "spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property).isEqualTo("fromspecificlocation"); assertThat(property).isEqualTo("fromspecificlocation");
@ -565,8 +567,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void specificResourceAsFile() throws Exception { public void specificResourceAsFile() throws Exception {
String location = "file:src/test/resources/specificlocation.properties"; String location = "file:src/test/resources/specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location:" + location); "spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment) assertThat(this.environment)
.has(matchingPropertySource("applicationConfig: [" + location + "]")); .has(matchingPropertySource("applicationConfig: [" + location + "]"));
@ -575,8 +577,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void specificResourceDefaultsToFile() throws Exception { public void specificResourceDefaultsToFile() throws Exception {
String location = "src/test/resources/specificlocation.properties"; String location = "src/test/resources/specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location:" + location); "spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment).has( assertThat(this.environment).has(
matchingPropertySource("applicationConfig: [file:" + location + "]")); matchingPropertySource("applicationConfig: [file:" + location + "]"));
@ -586,8 +588,8 @@ public class ConfigFileApplicationListenerTests {
public void absoluteResourceDefaultsToFile() throws Exception { public void absoluteResourceDefaultsToFile() throws Exception {
String location = new File("src/test/resources/specificlocation.properties") String location = new File("src/test/resources/specificlocation.properties")
.getAbsolutePath(); .getAbsolutePath();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location:" + location); "spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment) assertThat(this.environment)
.has(matchingPropertySource("applicationConfig: [file:" .has(matchingPropertySource("applicationConfig: [file:"
@ -610,8 +612,8 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void propertySourceAnnotationWithPlaceholder() throws Exception { public void propertySourceAnnotationWithPlaceholder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"source.location:specificlocation"); "source.location=specificlocation");
SpringApplication application = new SpringApplication( SpringApplication application = new SpringApplication(
WithPropertySourcePlaceholders.class); WithPropertySourcePlaceholders.class);
application.setEnvironment(this.environment); application.setEnvironment(this.environment);
@ -756,7 +758,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void disableIgnoreBeanInfoProperty() throws Exception { public void disableIgnoreBeanInfoProperty() throws Exception {
this.initializer.setSearchNames("testproperties"); this.initializer.setSearchNames("testproperties");
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.beaninfo.ignore=false"); "spring.beaninfo.ignore=false");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = System String property = System

22
spring-boot/src/test/java/org/springframework/boot/context/config/DelegatingApplicationContextInitializerTests.java

@ -20,13 +20,13 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.ConfigurableWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -46,8 +46,9 @@ public class DelegatingApplicationContextInitializerTests {
@Test @Test
public void orderedInitialize() throws Exception { public void orderedInitialize() throws Exception {
StaticApplicationContext context = new StaticApplicationContext(); StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "context.initializer.classes:" TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
+ MockInitB.class.getName() + "," + MockInitA.class.getName()); "context.initializer.classes=" + MockInitB.class.getName() + ","
+ MockInitA.class.getName());
this.initializer.initialize(context); this.initializer.initialize(context);
assertThat(context.getBeanFactory().getSingleton("a")).isEqualTo("a"); assertThat(context.getBeanFactory().getSingleton("a")).isEqualTo("a");
assertThat(context.getBeanFactory().getSingleton("b")).isEqualTo("b"); assertThat(context.getBeanFactory().getSingleton("b")).isEqualTo("b");
@ -62,15 +63,16 @@ public class DelegatingApplicationContextInitializerTests {
@Test @Test
public void emptyInitializers() throws Exception { public void emptyInitializers() throws Exception {
StaticApplicationContext context = new StaticApplicationContext(); StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "context.initializer.classes:"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes:");
this.initializer.initialize(context); this.initializer.initialize(context);
} }
@Test @Test
public void noSuchInitializerClass() throws Exception { public void noSuchInitializerClass() throws Exception {
StaticApplicationContext context = new StaticApplicationContext(); StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes:missing.madeup.class"); "context.initializer.classes=missing.madeup.class");
this.thrown.expect(ApplicationContextException.class); this.thrown.expect(ApplicationContextException.class);
this.initializer.initialize(context); this.initializer.initialize(context);
} }
@ -78,8 +80,8 @@ public class DelegatingApplicationContextInitializerTests {
@Test @Test
public void notAnInitializerClass() throws Exception { public void notAnInitializerClass() throws Exception {
StaticApplicationContext context = new StaticApplicationContext(); StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes:" + Object.class.getName()); "context.initializer.classes=" + Object.class.getName());
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.initializer.initialize(context); this.initializer.initialize(context);
} }
@ -87,8 +89,8 @@ public class DelegatingApplicationContextInitializerTests {
@Test @Test
public void genericNotSuitable() throws Exception { public void genericNotSuitable() throws Exception {
StaticApplicationContext context = new StaticApplicationContext(); StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes:" + NotSuitableInit.class.getName()); "context.initializer.classes=" + NotSuitableInit.class.getName());
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("generic parameter"); this.thrown.expectMessage("generic parameter");
this.initializer.initialize(context); this.initializer.initialize(context);

10
spring-boot/src/test/java/org/springframework/boot/context/config/DelegatingApplicationListenerTests.java

@ -23,13 +23,13 @@ import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -56,8 +56,9 @@ public class DelegatingApplicationListenerTests {
@Test @Test
public void orderedInitialize() throws Exception { public void orderedInitialize() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:" TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
+ MockInitB.class.getName() + "," + MockInitA.class.getName()); "context.listener.classes=" + MockInitB.class.getName() + ","
+ MockInitA.class.getName());
this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent( this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), new String[0], this.context.getEnvironment())); new SpringApplication(), new String[0], this.context.getEnvironment()));
this.context.getBeanFactory().registerSingleton("testListener", this.listener); this.context.getBeanFactory().registerSingleton("testListener", this.listener);
@ -74,7 +75,8 @@ public class DelegatingApplicationListenerTests {
@Test @Test
public void emptyInitializers() throws Exception { public void emptyInitializers() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"context.listener.classes:");
this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent( this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), new String[0], this.context.getEnvironment())); new SpringApplication(), new String[0], this.context.getEnvironment()));
} }

5
spring-boot/src/test/java/org/springframework/boot/context/embedded/config/AnsiOutputApplicationListenerTests.java

@ -28,11 +28,11 @@ import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled; import org.springframework.boot.ansi.AnsiOutput.Enabled;
import org.springframework.boot.ansi.AnsiOutputEnabledValue; import org.springframework.boot.ansi.AnsiOutputEnabledValue;
import org.springframework.boot.context.config.AnsiOutputApplicationListener; import org.springframework.boot.context.config.AnsiOutputApplicationListener;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -83,7 +83,8 @@ public class AnsiOutputApplicationListenerTests {
@Test @Test
public void disabledViaApplicationProperties() throws Exception { public void disabledViaApplicationProperties() throws Exception {
ConfigurableEnvironment environment = new StandardEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
EnvironmentTestUtils.addEnvironment(environment, "spring.config.name:ansi"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment,
"spring.config.name=ansi");
SpringApplication application = new SpringApplication(Config.class); SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false); application.setWebEnvironment(false);
application.setEnvironment(environment); application.setEnvironment(environment);

67
spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java

@ -34,12 +34,12 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException; import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
@ -73,7 +73,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void testValidationWithSetter() { public void testValidationWithSetter() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.foo:spam"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"test.foo=spam");
this.context.register(TestConfigurationWithValidatingSetter.class); this.context.register(TestConfigurationWithValidatingSetter.class);
assertBindingFailure(1); assertBindingFailure(1);
} }
@ -81,7 +82,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void unknownFieldFailureMessageContainsDetailsOfPropertyOrigin() { public void unknownFieldFailureMessageContainsDetailsOfPropertyOrigin() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "com.example.baz:spam"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"com.example.baz=spam");
this.context.register(TestConfiguration.class); this.context.register(TestConfiguration.class);
try { try {
this.context.refresh(); this.context.refresh();
@ -91,8 +93,10 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
RelaxedBindingNotWritablePropertyException bex = (RelaxedBindingNotWritablePropertyException) ex RelaxedBindingNotWritablePropertyException bex = (RelaxedBindingNotWritablePropertyException) ex
.getRootCause(); .getRootCause();
assertThat(bex.getMessage()) assertThat(bex.getMessage())
.startsWith("Failed to bind 'com.example.baz' from 'test' to 'baz' " .startsWith("Failed to bind 'com.example.baz' from '"
+ "property on '" + TestConfiguration.class.getName()); + TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
+ "' to 'baz' " + "property on '"
+ TestConfiguration.class.getName());
} }
} }
@ -161,20 +165,20 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void testPropertyWithEnum() throws Exception { public void testPropertyWithEnum() throws Exception {
doEnumTest("test.theValue:foo"); doEnumTest("test.theValue=foo");
} }
@Test @Test
public void testRelaxedPropertyWithEnum() throws Exception { public void testRelaxedPropertyWithEnum() throws Exception {
doEnumTest("test.the-value:FoO"); doEnumTest("test.the-value=FoO");
doEnumTest("TEST_THE_VALUE:FoO"); doEnumTest("TEST_THE_VALUE=FoO");
doEnumTest("test.THE_VALUE:FoO"); doEnumTest("test.THE_VALUE=FoO");
doEnumTest("test_the_value:FoO"); doEnumTest("test_the_value=FoO");
} }
private void doEnumTest(String property) { private void doEnumTest(String property) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, property); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, property);
this.context.register(PropertyWithEnum.class); this.context.register(PropertyWithEnum.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(PropertyWithEnum.class).getTheValue()) assertThat(this.context.getBean(PropertyWithEnum.class).getTheValue())
@ -184,15 +188,15 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void testRelaxedPropertyWithSetOfEnum() { public void testRelaxedPropertyWithSetOfEnum() {
doEnumSetTest("test.the-values:foo,bar", FooEnum.FOO, FooEnum.BAR); doEnumSetTest("test.the-values=foo,bar", FooEnum.FOO, FooEnum.BAR);
doEnumSetTest("test.the-values:foo", FooEnum.FOO); doEnumSetTest("test.the-values=foo", FooEnum.FOO);
doEnumSetTest("TEST_THE_VALUES:FoO", FooEnum.FOO); doEnumSetTest("TEST_THE_VALUES=FoO", FooEnum.FOO);
doEnumSetTest("test_the_values:BaR,FoO", FooEnum.BAR, FooEnum.FOO); doEnumSetTest("test_the_values=BaR,FoO", FooEnum.BAR, FooEnum.FOO);
} }
private void doEnumSetTest(String property, FooEnum... expected) { private void doEnumSetTest(String property, FooEnum... expected) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, property); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, property);
this.context.register(PropertyWithEnum.class); this.context.register(PropertyWithEnum.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(PropertyWithEnum.class).getTheValues()) assertThat(this.context.getBean(PropertyWithEnum.class).getTheValues())
@ -203,7 +207,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void testValueBindingForDefaults() throws Exception { public void testValueBindingForDefaults() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "default.value:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"default.value=foo");
this.context.register(PropertyWithValue.class); this.context.register(PropertyWithValue.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(PropertyWithValue.class).getValue()) assertThat(this.context.getBean(PropertyWithValue.class).getValue())
@ -213,7 +218,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void placeholderResolutionWithCustomLocation() throws Exception { public void placeholderResolutionWithCustomLocation() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "fooValue:bar"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"fooValue=bar");
this.context.register(CustomConfigurationLocation.class); this.context.register(CustomConfigurationLocation.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(CustomConfigurationLocation.class).getFoo()) assertThat(this.context.getBean(CustomConfigurationLocation.class).getFoo())
@ -223,7 +229,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void placeholderResolutionWithUnmergedCustomLocation() throws Exception { public void placeholderResolutionWithUnmergedCustomLocation() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "fooValue:bar"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"fooValue:bar");
this.context.register(UnmergedCustomConfigurationLocation.class); this.context.register(UnmergedCustomConfigurationLocation.class);
this.context.refresh(); this.context.refresh();
assertThat( assertThat(
@ -255,7 +262,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void configurationPropertiesWithCharArray() throws Exception { public void configurationPropertiesWithCharArray() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.chars:word"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"test.chars=word");
this.context.register(PropertyWithCharArray.class); this.context.register(PropertyWithCharArray.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(PropertyWithCharArray.class).getChars()) assertThat(this.context.getBean(PropertyWithCharArray.class).getChars())
@ -265,7 +273,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void configurationPropertiesWithArrayExpansion() throws Exception { public void configurationPropertiesWithArrayExpansion() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.chars[4]:s"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"test.chars[4]=s");
this.context.register(PropertyWithCharArrayExpansion.class); this.context.register(PropertyWithCharArrayExpansion.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(PropertyWithCharArrayExpansion.class).getChars()) assertThat(this.context.getBean(PropertyWithCharArrayExpansion.class).getChars())
@ -275,7 +284,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void notWritablePropertyException() throws Exception { public void notWritablePropertyException() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.madeup:word"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"test.madeup:word");
this.context.register(PropertyWithCharArray.class); this.context.register(PropertyWithCharArray.class);
this.thrown.expect(BeanCreationException.class); this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("test"); this.thrown.expectMessage("test");
@ -284,17 +294,18 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void relaxedPropertyNamesSame() throws Exception { public void relaxedPropertyNamesSame() throws Exception {
testRelaxedPropertyNames("test.FOO_BAR:test1", "test.FOO_BAR:test2"); testRelaxedPropertyNames("test.FOO_BAR=test1", "test.FOO_BAR=test2");
} }
@Test @Test
public void relaxedPropertyNamesMixed() throws Exception { public void relaxedPropertyNamesMixed() throws Exception {
testRelaxedPropertyNames("test.foo-bar:test1", "test.FOO_BAR:test2"); testRelaxedPropertyNames("test.FOO_BAR=test2", "test.foo-bar=test1");
} }
private void testRelaxedPropertyNames(String... environment) { private void testRelaxedPropertyNames(String... environment) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
environment);
this.context.register(RelaxedPropertyNames.class); this.context.register(RelaxedPropertyNames.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(RelaxedPropertyNames.class).getFooBar()) assertThat(this.context.getBean(RelaxedPropertyNames.class).getFooBar())
@ -305,7 +316,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
public void nestedProperties() throws Exception { public void nestedProperties() throws Exception {
// gh-3539 // gh-3539
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "TEST_NESTED_VALUE:test1"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"TEST_NESTED_VALUE=test1");
this.context.register(PropertyWithNestedValue.class); this.context.register(PropertyWithNestedValue.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(PropertyWithNestedValue.class).getNested() assertThat(this.context.getBean(PropertyWithNestedValue.class).getNested()
@ -315,7 +327,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void bindWithoutConfigurationPropertiesAnnotation() { public void bindWithoutConfigurationPropertiesAnnotation() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name:foo");
this.context.register(ConfigurationPropertiesWithoutAnnotation.class); this.context.register(ConfigurationPropertiesWithoutAnnotation.class);
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);

71
spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java

@ -30,13 +30,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -64,7 +64,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testBasicPropertiesBinding() { public void testBasicPropertiesBinding() {
this.context.register(TestConfiguration.class); this.context.register(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1);
assertThat(this.context.containsBean(TestProperties.class.getName())).isTrue(); assertThat(this.context.containsBean(TestProperties.class.getName())).isTrue();
@ -106,7 +107,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testNestedOsEnvironmentVariableWithUnderscore() { public void testNestedOsEnvironmentVariableWithUnderscore() {
EnvironmentTestUtils.addEnvironment(this.context, "NAME:foo", "NESTED_NAME:bar"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"NAME=foo", "NESTED_NAME=bar");
this.context.register(NestedConfiguration.class); this.context.register(NestedConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1);
@ -119,7 +121,8 @@ public class EnableConfigurationPropertiesTests {
public void testStrictPropertiesBinding() { public void testStrictPropertiesBinding() {
removeSystemProperties(); removeSystemProperties();
this.context.register(StrictTestConfiguration.class); this.context.register(StrictTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(StrictTestProperties.class)) assertThat(this.context.getBeanNamesForType(StrictTestProperties.class))
.hasSize(1); .hasSize(1);
@ -129,7 +132,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testPropertiesEmbeddedBinding() { public void testPropertiesEmbeddedBinding() {
this.context.register(EmbeddedTestConfiguration.class); this.context.register(EmbeddedTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring_foo_name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"spring_foo_name=foo");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(EmbeddedTestProperties.class)) assertThat(this.context.getBeanNamesForType(EmbeddedTestProperties.class))
.hasSize(1); .hasSize(1);
@ -138,7 +142,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testOsEnvironmentVariableEmbeddedBinding() { public void testOsEnvironmentVariableEmbeddedBinding() {
EnvironmentTestUtils.addEnvironment(this.context, "SPRING_FOO_NAME:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"SPRING_FOO_NAME=foo");
this.context.register(EmbeddedTestConfiguration.class); this.context.register(EmbeddedTestConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(EmbeddedTestProperties.class)) assertThat(this.context.getBeanNamesForType(EmbeddedTestProperties.class))
@ -150,7 +155,8 @@ public class EnableConfigurationPropertiesTests {
public void testIgnoreNestedPropertiesBinding() { public void testIgnoreNestedPropertiesBinding() {
removeSystemProperties(); removeSystemProperties();
this.context.register(IgnoreNestedTestConfiguration.class); this.context.register(IgnoreNestedTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "nested.name:bar"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo", "nested.name=bar");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(IgnoreNestedTestProperties.class)) assertThat(this.context.getBeanNamesForType(IgnoreNestedTestProperties.class))
.hasSize(1); .hasSize(1);
@ -160,7 +166,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testExceptionOnValidation() { public void testExceptionOnValidation() {
this.context.register(ExceptionIfInvalidTestConfiguration.class); this.context.register(ExceptionIfInvalidTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name:foo");
this.thrown.expectCause(Matchers.<Throwable>instanceOf(BindException.class)); this.thrown.expectCause(Matchers.<Throwable>instanceOf(BindException.class));
this.context.refresh(); this.context.refresh();
} }
@ -168,7 +175,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testNoExceptionOnValidation() { public void testNoExceptionOnValidation() {
this.context.register(NoExceptionIfInvalidTestConfiguration.class); this.context.register(NoExceptionIfInvalidTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh(); this.context.refresh();
assertThat(this.context assertThat(this.context
.getBeanNamesForType(NoExceptionIfInvalidTestProperties.class)) .getBeanNamesForType(NoExceptionIfInvalidTestProperties.class))
@ -179,7 +187,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testNestedPropertiesBinding() { public void testNestedPropertiesBinding() {
this.context.register(NestedConfiguration.class); this.context.register(NestedConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "nested.name:bar"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo", "nested.name=bar");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1);
assertThat(this.context.getBean(NestedProperties.class).name).isEqualTo("foo"); assertThat(this.context.getBean(NestedProperties.class).name).isEqualTo("foo");
@ -190,7 +199,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testBasicPropertiesBindingWithAnnotationOnBaseClass() { public void testBasicPropertiesBindingWithAnnotationOnBaseClass() {
this.context.register(DerivedConfiguration.class); this.context.register(DerivedConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(DerivedProperties.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(DerivedProperties.class)).hasSize(1);
assertThat(this.context.getBean(BaseProperties.class).name).isEqualTo("foo"); assertThat(this.context.getBean(BaseProperties.class).name).isEqualTo("foo");
@ -199,7 +209,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testArrayPropertiesBinding() { public void testArrayPropertiesBinding() {
this.context.register(TestConfiguration.class); this.context.register(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "array:1,2,3"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo", "array=1,2,3");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1);
assertThat(this.context.getBean(TestProperties.class).getArray()).hasSize(3); assertThat(this.context.getBean(TestProperties.class).getArray()).hasSize(3);
@ -208,8 +219,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testCollectionPropertiesBindingFromYamlArray() { public void testCollectionPropertiesBindingFromYamlArray() {
this.context.register(TestConfiguration.class); this.context.register(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "list[0]:1", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"list[1]:2"); "name=foo", "list[0]=1", "list[1]=2");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(TestProperties.class).getList()).hasSize(2); assertThat(this.context.getBean(TestProperties.class).getList()).hasSize(2);
} }
@ -217,7 +228,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testPropertiesBindingWithoutAnnotation() { public void testPropertiesBindingWithoutAnnotation() {
this.context.register(InvalidConfiguration.class); this.context.register(InvalidConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name:foo");
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("No ConfigurationProperties annotation found"); this.thrown.expectMessage("No ConfigurationProperties annotation found");
@ -227,7 +239,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testPropertiesBindingWithoutAnnotationValue() { public void testPropertiesBindingWithoutAnnotationValue() {
this.context.register(MoreConfiguration.class); this.context.register(MoreConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(MoreProperties.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(MoreProperties.class)).hasSize(1);
assertThat(this.context.getBean(MoreProperties.class).name).isEqualTo("foo"); assertThat(this.context.getBean(MoreProperties.class).name).isEqualTo("foo");
@ -263,8 +276,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testBindingDirectlyToFileResolvedFromEnvironment() { public void testBindingDirectlyToFileResolvedFromEnvironment() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"binding.location:classpath:other.yml"); "binding.location=classpath:other.yml");
this.context.register(ResourceBindingProperties.class, TestConfiguration.class); this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class)) assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
@ -321,7 +334,8 @@ public class EnableConfigurationPropertiesTests {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
parent.register(TestConfiguration.class); parent.register(TestConfiguration.class);
parent.refresh(); parent.refresh();
EnvironmentTestUtils.addEnvironment(this.context, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.setParent(parent); this.context.setParent(parent);
this.context.register(TestConfiguration.class, TestConsumer.class); this.context.register(TestConfiguration.class, TestConsumer.class);
this.context.refresh(); this.context.refresh();
@ -334,7 +348,7 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testBindingOnlyParentContext() { public void testBindingOnlyParentContext() {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(parent, "name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(parent, "name=foo");
parent.register(TestConfiguration.class); parent.register(TestConfiguration.class);
parent.refresh(); parent.refresh();
this.context.setParent(parent); this.context.setParent(parent);
@ -348,7 +362,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testUnderscoresInPrefix() throws Exception { public void testUnderscoresInPrefix() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring_test_external_val:baz"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"spring_test_external_val=baz");
this.context.register(SystemExampleConfig.class); this.context.register(SystemExampleConfig.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(SystemEnvVar.class).getVal()).isEqualTo("baz"); assertThat(this.context.getBean(SystemEnvVar.class).getVal()).isEqualTo("baz");
@ -356,7 +371,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testSimpleAutoConfig() throws Exception { public void testSimpleAutoConfig() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"external.name=foo");
this.context.register(ExampleConfig.class); this.context.register(ExampleConfig.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo"); assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo");
@ -364,7 +380,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testExplicitType() throws Exception { public void testExplicitType() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"external.name=foo");
this.context.register(AnotherExampleConfig.class); this.context.register(AnotherExampleConfig.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.containsBean("external-" + External.class.getName())) assertThat(this.context.containsBean("external-" + External.class.getName()))
@ -374,8 +391,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testMultipleExplicitTypes() throws Exception { public void testMultipleExplicitTypes() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"another.name:bar"); "external.name=foo", "another.name=bar");
this.context.register(FurtherExampleConfig.class); this.context.register(FurtherExampleConfig.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo"); assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo");
@ -397,8 +414,8 @@ public class EnableConfigurationPropertiesTests {
@Test @Test
public void testAnnotatedBean() { public void testAnnotatedBean() {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:bar", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"spam.name:foo"); "external.name=bar", "spam.name=foo");
this.context.register(TestConfigurationWithAnnotatedBean.class); this.context.register(TestConfigurationWithAnnotatedBean.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo"); assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo");

18
spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java vendored

@ -18,9 +18,9 @@ package org.springframework.boot.env;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -38,7 +38,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void error() { public void error() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json=foo:bar"); "spring.application.json=foo:bar");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
@ -54,7 +54,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void empty() { public void empty() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json={}"); "spring.application.json={}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
@ -63,7 +63,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void periodSeparated() { public void periodSeparated() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json={\"foo\":\"bar\"}"); "spring.application.json={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar"); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
@ -72,7 +72,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void envVar() { public void envVar() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}"); "SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar"); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
@ -81,7 +81,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void nested() { public void nested() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":{\"bar\":\"spam\",\"rab\":\"maps\"}}"); "SPRING_APPLICATION_JSON={\"foo\":{\"bar\":\"spam\",\"rab\":\"maps\"}}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam"); assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam");
@ -91,7 +91,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void prefixed() { public void prefixed() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo.bar\":\"spam\"}"); "SPRING_APPLICATION_JSON={\"foo.bar\":\"spam\"}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam"); assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam");
@ -100,7 +100,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void list() { public void list() {
assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[\"bar\",\"spam\"]}"); "SPRING_APPLICATION_JSON={\"foo\":[\"bar\",\"spam\"]}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEqualTo("spam"); assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEqualTo("spam");
@ -109,7 +109,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test @Test
public void listOfObject() { public void listOfObject() {
assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[{\"bar\":\"spam\"}]}"); "SPRING_APPLICATION_JSON={\"foo\":[{\"bar\":\"spam\"}]}");
this.processor.postProcessEnvironment(this.environment, null); this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}")) assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}"))

66
spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java

@ -39,10 +39,10 @@ import org.springframework.boot.ApplicationPid;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.logging.java.JavaLoggingSystem;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.boot.testutil.OutputCapture; import org.springframework.boot.testutil.OutputCapture;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -128,8 +128,8 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void overrideConfigLocation() { public void overrideConfigLocation() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config: classpath:logback-nondefault.xml"); "logging.config=classpath:logback-nondefault.xml");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.info("Hello world"); this.logger.info("Hello world");
@ -140,8 +140,8 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void overrideConfigDoesNotExist() throws Exception { public void overrideConfigDoesNotExist() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config: doesnotexist.xml"); "logging.config=doesnotexist.xml");
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.outputCapture.expect(containsString( this.outputCapture.expect(containsString(
"Logging system failed to initialize using configuration from 'doesnotexist.xml'")); "Logging system failed to initialize using configuration from 'doesnotexist.xml'"));
@ -151,7 +151,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void azureDefaultLoggingConfigDoesNotCauseAFailure() throws Exception { public void azureDefaultLoggingConfigDoesNotCauseAFailure() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config: -Djava.util.logging.config.file=\"d:\\home\\site\\wwwroot\\bin\\apache-tomcat-7.0.52\\conf\\logging.properties\""); "logging.config: -Djava.util.logging.config.file=\"d:\\home\\site\\wwwroot\\bin\\apache-tomcat-7.0.52\\conf\\logging.properties\"");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
@ -163,8 +163,8 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void overrideConfigBroken() throws Exception { public void overrideConfigBroken() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config: classpath:logback-broken.xml"); "logging.config=classpath:logback-broken.xml");
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
this.outputCapture.expect(containsString( this.outputCapture.expect(containsString(
"Logging system failed to initialize using configuration from 'classpath:logback-broken.xml'")); "Logging system failed to initialize using configuration from 'classpath:logback-broken.xml'"));
@ -175,9 +175,9 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void addLogFileProperty() { public void addLogFileProperty() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config: classpath:logback-nondefault.xml", "logging.config=classpath:logback-nondefault.xml",
"logging.file: target/foo.log"); "logging.file=target/foo.log");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
@ -189,7 +189,8 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void addLogFilePropertyWithDefault() { public void addLogFilePropertyWithDefault() {
assertThat(new File("target/foo.log").exists()).isFalse(); assertThat(new File("target/foo.log").exists()).isFalse();
EnvironmentTestUtils.addEnvironment(this.context, "logging.file: target/foo.log"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.file=target/foo.log");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
@ -199,9 +200,9 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void addLogPathProperty() { public void addLogPathProperty() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config: classpath:logback-nondefault.xml", "logging.config=classpath:logback-nondefault.xml",
"logging.path: target/foo/"); "logging.path=target/foo/");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class); Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
@ -212,7 +213,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseDebugArg() throws Exception { public void parseDebugArg() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "debug"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.debug("testatdebug"); this.logger.debug("testatdebug");
@ -223,7 +224,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseTraceArg() throws Exception { public void parseTraceArg() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "trace"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "trace");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.debug("testatdebug"); this.logger.debug("testatdebug");
@ -243,7 +244,8 @@ public class LoggingApplicationListenerTests {
} }
private void disableDebugTraceArg(String... environment) { private void disableDebugTraceArg(String... environment) {
EnvironmentTestUtils.addEnvironment(this.context, environment); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
environment);
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.debug("testatdebug"); this.logger.debug("testatdebug");
@ -254,7 +256,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseLevels() throws Exception { public void parseLevels() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=TRACE"); "logging.level.org.springframework.boot=TRACE");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
@ -266,7 +268,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseLevelsCaseInsensitive() throws Exception { public void parseLevelsCaseInsensitive() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=TrAcE"); "logging.level.org.springframework.boot=TrAcE");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
@ -278,8 +280,8 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseLevelsWithPlaceholder() throws Exception { public void parseLevelsWithPlaceholder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "foo=TRACE", TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=${foo}"); "foo=TRACE", "logging.level.org.springframework.boot=${foo}");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.debug("testatdebug"); this.logger.debug("testatdebug");
@ -290,7 +292,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseLevelsFails() throws Exception { public void parseLevelsFails() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=GARBAGE"); "logging.level.org.springframework.boot=GARBAGE");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
@ -301,7 +303,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseLevelsNone() throws Exception { public void parseLevelsNone() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=OFF"); "logging.level.org.springframework.boot=OFF");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
@ -313,7 +315,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseLevelsMapsFalseToOff() throws Exception { public void parseLevelsMapsFalseToOff() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=false"); "logging.level.org.springframework.boot=false");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
@ -326,7 +328,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void parseArgsDisabled() throws Exception { public void parseArgsDisabled() throws Exception {
this.initializer.setParseArgs(false); this.initializer.setParseArgs(false);
EnvironmentTestUtils.addEnvironment(this.context, "debug"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.logger.debug("testatdebug"); this.logger.debug("testatdebug");
@ -365,8 +367,8 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void overrideExceptionConversionWord() throws Exception { public void overrideExceptionConversionWord() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.exceptionConversionWord:%rEx"); "logging.exceptionConversionWord=%rEx");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());
this.outputCapture.expect(containsString("Hello world")); this.outputCapture.expect(containsString("Hello world"));
@ -392,8 +394,8 @@ public class LoggingApplicationListenerTests {
TestLoggingApplicationListener listener = new TestLoggingApplicationListener(); TestLoggingApplicationListener listener = new TestLoggingApplicationListener();
System.setProperty(LoggingSystem.class.getName(), System.setProperty(LoggingSystem.class.getName(),
TestShutdownHandlerLoggingSystem.class.getName()); TestShutdownHandlerLoggingSystem.class.getName());
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.register_shutdown_hook:true"); "logging.register_shutdown_hook=true");
listener.onApplicationEvent( listener.onApplicationEvent(
new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); new ApplicationStartedEvent(new SpringApplication(), NO_ARGS));
listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()); listener.initialize(this.context.getEnvironment(), this.context.getClassLoader());
@ -436,7 +438,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void systemPropertiesAreSetForLoggingConfiguration() { public void systemPropertiesAreSetForLoggingConfiguration() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.exception-conversion-word=conversion", "logging.file=target/log", "logging.exception-conversion-word=conversion", "logging.file=target/log",
"logging.path=path", "logging.pattern.console=console", "logging.path=path", "logging.pattern.console=console",
"logging.pattern.file=file", "logging.pattern.level=level"); "logging.pattern.file=file", "logging.pattern.level=level");
@ -454,7 +456,7 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void logFilePropertiesCanReferenceSystemProperties() { public void logFilePropertiesCanReferenceSystemProperties() {
EnvironmentTestUtils.addEnvironment(this.context, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.file=target/${PID}.log"); "logging.file=target/${PID}.log");
this.initializer.initialize(this.context.getEnvironment(), this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader()); this.context.getClassLoader());

8
spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java

@ -28,9 +28,9 @@ import org.slf4j.Logger;
import org.slf4j.impl.StaticLoggerBinder; import org.slf4j.impl.StaticLoggerBinder;
import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.boot.testutil.OutputCapture; import org.springframework.boot.testutil.OutputCapture;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
@ -126,14 +126,16 @@ public class SpringBootJoranConfiguratorTests {
@Test @Test
public void springProperty() throws Exception { public void springProperty() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "my.example-property:test"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"my.example-property=test");
initialize("property.xml"); initialize("property.xml");
assertThat(this.context.getProperty("MINE")).isEqualTo("test"); assertThat(this.context.getProperty("MINE")).isEqualTo("test");
} }
@Test @Test
public void relaxedSpringProperty() throws Exception { public void relaxedSpringProperty() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "my.EXAMPLE_PROPERTY:test"); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"my.EXAMPLE_PROPERTY=test");
initialize("property.xml"); initialize("property.xml");
assertThat(this.context.getProperty("MINE")).isEqualTo("test"); assertThat(this.context.getProperty("MINE")).isEqualTo("test");
} }

74
spring-boot/src/test/java/org/springframework/boot/testutil/EnvironmentTestUtils.java

@ -1,74 +0,0 @@
/*
* Copyright 2012-2016 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.testutil;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
/**
* Internal test utilities for setting environment values.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public abstract class EnvironmentTestUtils {
public static void addEnvironment(ConfigurableApplicationContext context,
String... pairs) {
addEnvironment(context.getEnvironment(), pairs);
}
public static void addEnvironment(ConfigurableEnvironment environment,
String... pairs) {
MutablePropertySources sources = environment.getPropertySources();
Map<String, Object> map = getOrAdd(sources);
for (String pair : pairs) {
int index = getSeparatorIndex(pair);
String key = pair.substring(0, index > 0 ? index : pair.length());
String value = index > 0 ? pair.substring(index + 1) : "";
map.put(key.trim(), value.trim());
}
}
@SuppressWarnings("unchecked")
private static Map<String, Object> getOrAdd(MutablePropertySources sources) {
if (sources.contains("test")) {
return (Map<String, Object>) sources.get("test").getSource();
}
Map<String, Object> map = new HashMap<String, Object>();
sources.addFirst(new MapPropertySource("test", map));
return map;
}
private static int getSeparatorIndex(String pair) {
int colonIndex = pair.indexOf(":");
int equalIndex = pair.indexOf("=");
if (colonIndex == -1) {
return equalIndex;
}
if (equalIndex == -1) {
return colonIndex;
}
return Math.min(colonIndex, equalIndex);
}
}
Loading…
Cancel
Save