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

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

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

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

@ -20,8 +20,8 @@ import java.util.Map; @@ -20,8 +20,8 @@ import java.util.Map;
import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,7 +36,8 @@ public class PropertySourcesBinderTests { @@ -36,7 +36,8 @@ public class PropertySourcesBinderTests {
@Test
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)
.extractAll("foo");
assertThat(content.get("first")).isEqualTo("1");
@ -47,8 +48,8 @@ public class PropertySourcesBinderTests { @@ -47,8 +48,8 @@ public class PropertySourcesBinderTests {
@Test
@SuppressWarnings("unchecked")
public void extractNoPrefix() {
EnvironmentTestUtils.addEnvironment(this.env, "foo.ctx.first=1",
"foo.ctx.second=2");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.env,
"foo.ctx.first=1", "foo.ctx.second=2");
Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll("");
assertThat(content.get("foo")).isInstanceOf(Map.class);
Map<String, Object> foo = (Map<String, Object>) content.get("foo");
@ -62,7 +63,8 @@ public class PropertySourcesBinderTests { @@ -62,7 +63,8 @@ public class PropertySourcesBinderTests {
@Test
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();
new PropertySourcesBinder(this.env).bindTo("test", bean);
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; @@ -19,9 +19,9 @@ package org.springframework.boot.cloud.cloudfoundry;
import org.junit.Test;
import org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -39,8 +39,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests { @@ -39,8 +39,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test
public void testApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context,
"VCAP_APPLICATION:{\"application_users\":[],"
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION={\"application_users\":[],"
+ "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\","
+ "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\","
+ "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"],"
@ -60,8 +60,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests { @@ -60,8 +60,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test
public void testApplicationUris() {
EnvironmentTestUtils.addEnvironment(this.context,
"VCAP_APPLICATION:{\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION={\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}");
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertThat(this.context.getEnvironment().getProperty("vcap.application.uris[0]"))
.isEqualTo("foo.cfapps.io");
@ -69,15 +69,16 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests { @@ -69,15 +69,16 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test
public void testUnparseableApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context, "VCAP_APPLICATION:");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION:");
this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertThat(getProperty("vcap")).isNull();
}
@Test
public void testNullApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context,
"VCAP_APPLICATION:{\"application_users\":null,"
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_APPLICATION={\"application_users\":null,"
+ "\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\","
+ "\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\","
+ "\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"],"
@ -95,8 +96,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests { @@ -95,8 +96,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test
public void testServiceProperties() {
EnvironmentTestUtils.addEnvironment(this.context,
"VCAP_SERVICES:{\"rds-mysql-n/a\":[{"
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES={\"rds-mysql-n/a\":[{"
+ "\"name\":\"mysql\",\"label\":\"rds-mysql-n/a\","
+ "\"plan\":\"10mb\",\"credentials\":{"
+ "\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\","
@ -114,8 +115,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests { @@ -114,8 +115,8 @@ public class CloudFoundryVcapEnvironmentPostProcessorTests {
@Test
public void testServicePropertiesWithoutNA() {
EnvironmentTestUtils.addEnvironment(this.context,
"VCAP_SERVICES:{\"rds-mysql\":[{"
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"VCAP_SERVICES={\"rds-mysql\":[{"
+ "\"name\":\"mysql\",\"label\":\"rds-mysql\",\"plan\":\"10mb\","
+ "\"credentials\":{\"name\":\"d04fb13d27d964c62b267bbba1cffb9da\","
+ "\"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; @@ -18,9 +18,9 @@ package org.springframework.boot.context;
import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -43,8 +43,8 @@ public class ContextIdApplicationContextInitializerTests { @@ -43,8 +43,8 @@ public class ContextIdApplicationContextInitializerTests {
@Test
public void testNameAndPort() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:foo",
"PORT:8080");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"spring.application.name=foo", "PORT=8080");
this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("foo:8080");
}
@ -52,8 +52,9 @@ public class ContextIdApplicationContextInitializerTests { @@ -52,8 +52,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test
public void testNameAndProfiles() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:foo",
"spring.profiles.active: spam,bar", "spring.application.index:12");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"spring.application.name=foo", "spring.profiles.active=spam,bar",
"spring.application.index=12");
this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("foo:spam,bar:12");
}
@ -61,9 +62,9 @@ public class ContextIdApplicationContextInitializerTests { @@ -61,9 +62,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test
public void testCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.config.name:foo",
"PORT:8080", "vcap.application.name:bar",
"vcap.application.instance_index:2");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"spring.config.name=foo", "PORT=8080", "vcap.application.name=bar",
"vcap.application.instance_index=2");
this.initializer.initialize(context);
assertThat(context.getId()).isEqualTo("bar:2");
}
@ -71,9 +72,9 @@ public class ContextIdApplicationContextInitializerTests { @@ -71,9 +72,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test
public void testExplicitNameIsChosenInFavorOfCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:spam",
"spring.config.name:foo", "PORT:8080", "vcap.application.name:bar",
"vcap.application.instance_index:2");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"spring.application.name=spam", "spring.config.name=foo", "PORT=8080",
"vcap.application.name=bar", "vcap.application.instance_index=2");
this.initializer.initialize(context);
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; @@ -21,9 +21,9 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
/**
* Tests for {@link FileEncodingApplicationListener}.
@ -39,8 +39,8 @@ public class FileEncodingApplicationListenerTests { @@ -39,8 +39,8 @@ public class FileEncodingApplicationListenerTests {
@Test(expected = IllegalStateException.class)
public void testIllegalState() {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.mandatory_file_encoding:FOO");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.mandatory_file_encoding=FOO");
this.initializer.onApplicationEvent(this.event);
}
@ -52,7 +52,7 @@ public class FileEncodingApplicationListenerTests { @@ -52,7 +52,7 @@ public class FileEncodingApplicationListenerTests {
@Test
public void testSunnyDayMandated() {
Assume.assumeNotNull(System.getProperty("file.encoding"));
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.mandatory_file_encoding:" + System.getProperty("file.encoding"));
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 @@ -46,7 +46,6 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnumerableCompositePropertySource;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.boot.testutil.OutputCapture;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -63,6 +62,7 @@ import org.springframework.core.env.StandardEnvironment; @@ -63,6 +62,7 @@ import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@ -156,8 +156,9 @@ public class ConfigFileApplicationListenerTests { @@ -156,8 +156,9 @@ public class ConfigFileApplicationListenerTests {
@Test
public void loadTwoPropertiesFile() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ "classpath:application.properties,classpath:testproperties.properties");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location="
+ "classpath:application.properties,classpath:testproperties.properties");
this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile");
@ -165,8 +166,9 @@ public class ConfigFileApplicationListenerTests { @@ -165,8 +166,9 @@ public class ConfigFileApplicationListenerTests {
@Test
public void loadTwoPropertiesFilesWithProfiles() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ "classpath:enableprofile.properties,classpath:enableother.properties");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location="
+ "classpath:enableprofile.properties,classpath:enableother.properties");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("other");
String property = this.environment.getProperty("my.property");
@ -175,8 +177,8 @@ public class ConfigFileApplicationListenerTests { @@ -175,8 +177,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void loadTwoPropertiesFilesWithProfilesAndSwitchOneOff() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.location:" + "classpath:enabletwoprofiles.properties,"
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=classpath:enabletwoprofiles.properties,"
+ "classpath:enableprofile.properties");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("myprofile");
@ -189,9 +191,9 @@ public class ConfigFileApplicationListenerTests { @@ -189,9 +191,9 @@ public class ConfigFileApplicationListenerTests {
@Test
public void loadTwoPropertiesFilesWithProfilesAndSwitchOneOffFromSpecificLocation()
throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.name:enabletwoprofiles",
"spring.config.location:classpath:enableprofile.properties");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.name=enabletwoprofiles",
"spring.config.location=classpath:enableprofile.properties");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("myprofile");
String property = this.environment.getProperty("the.property");
@ -225,8 +227,8 @@ public class ConfigFileApplicationListenerTests { @@ -225,8 +227,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void moreSpecificLocationTakesPrecedenceOverRoot() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.name:specific");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.name=specific");
this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property");
assertThat(property).isEqualTo("specific");
@ -234,8 +236,8 @@ public class ConfigFileApplicationListenerTests { @@ -234,8 +236,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void loadTwoOfThreePropertiesFile() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.location:" + "classpath:application.properties,"
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=classpath:application.properties,"
+ "classpath:testproperties.properties,"
+ "classpath:nonexistent.properties");
this.initializer.postProcessEnvironment(this.environment, this.application);
@ -373,7 +375,7 @@ public class ConfigFileApplicationListenerTests { @@ -373,7 +375,7 @@ public class ConfigFileApplicationListenerTests {
@Test
public void loadPropertiesThenProfilePropertiesWithOverride() throws Exception {
this.environment.setActiveProfiles("other");
// EnvironmentTestUtils.addEnvironment(this.environment,
// TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
// "spring.profiles.active:other");
this.initializer.setSearchNames("enableprofile");
this.initializer.postProcessEnvironment(this.environment, this.application);
@ -398,8 +400,8 @@ public class ConfigFileApplicationListenerTests { @@ -398,8 +400,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void profilesAddedToEnvironmentAndViaProperty() throws Exception {
// External profile takes precedence over profile added via the environment
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active:other");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=other");
this.environment.addActiveProfile("dev");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
@ -410,8 +412,8 @@ public class ConfigFileApplicationListenerTests { @@ -410,8 +412,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void profilesAddedToEnvironmentAndViaPropertyDuplicate() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active:dev,other");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=dev,other");
this.environment.addActiveProfile("dev");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
@ -423,8 +425,8 @@ public class ConfigFileApplicationListenerTests { @@ -423,8 +425,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void profilesAddedToEnvironmentAndViaPropertyDuplicateEnvironmentWins()
throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active:other,dev");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=other,dev");
this.environment.addActiveProfile("other");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
@ -529,8 +531,8 @@ public class ConfigFileApplicationListenerTests { @@ -529,8 +531,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void yamlProfileCanBeChanged() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active:prod");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=prod");
this.initializer.setSearchNames("testsetprofiles");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).containsExactly("prod");
@ -538,7 +540,7 @@ public class ConfigFileApplicationListenerTests { @@ -538,7 +540,7 @@ public class ConfigFileApplicationListenerTests {
@Test
public void specificNameAndProfileFromExistingSource() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=specificprofile",
"spring.config.name=specificfile");
this.initializer.postProcessEnvironment(this.environment, this.application);
@ -549,8 +551,8 @@ public class ConfigFileApplicationListenerTests { @@ -549,8 +551,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void specificResource() throws Exception {
String location = "classpath:specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.location:" + location);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property");
assertThat(property).isEqualTo("fromspecificlocation");
@ -565,8 +567,8 @@ public class ConfigFileApplicationListenerTests { @@ -565,8 +567,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void specificResourceAsFile() throws Exception {
String location = "file:src/test/resources/specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.location:" + location);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment)
.has(matchingPropertySource("applicationConfig: [" + location + "]"));
@ -575,8 +577,8 @@ public class ConfigFileApplicationListenerTests { @@ -575,8 +577,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void specificResourceDefaultsToFile() throws Exception {
String location = "src/test/resources/specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.location:" + location);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment).has(
matchingPropertySource("applicationConfig: [file:" + location + "]"));
@ -586,8 +588,8 @@ public class ConfigFileApplicationListenerTests { @@ -586,8 +588,8 @@ public class ConfigFileApplicationListenerTests {
public void absoluteResourceDefaultsToFile() throws Exception {
String location = new File("src/test/resources/specificlocation.properties")
.getAbsolutePath();
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.location:" + location);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.config.location=" + location);
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment)
.has(matchingPropertySource("applicationConfig: [file:"
@ -610,8 +612,8 @@ public class ConfigFileApplicationListenerTests { @@ -610,8 +612,8 @@ public class ConfigFileApplicationListenerTests {
@Test
public void propertySourceAnnotationWithPlaceholder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,
"source.location:specificlocation");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"source.location=specificlocation");
SpringApplication application = new SpringApplication(
WithPropertySourcePlaceholders.class);
application.setEnvironment(this.environment);
@ -756,7 +758,7 @@ public class ConfigFileApplicationListenerTests { @@ -756,7 +758,7 @@ public class ConfigFileApplicationListenerTests {
@Test
public void disableIgnoreBeanInfoProperty() throws Exception {
this.initializer.setSearchNames("testproperties");
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.beaninfo.ignore=false");
this.initializer.postProcessEnvironment(this.environment, this.application);
String property = System

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

@ -20,13 +20,13 @@ import org.junit.Rule; @@ -20,13 +20,13 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@ -46,8 +46,9 @@ public class DelegatingApplicationContextInitializerTests { @@ -46,8 +46,9 @@ public class DelegatingApplicationContextInitializerTests {
@Test
public void orderedInitialize() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "context.initializer.classes:"
+ MockInitB.class.getName() + "," + MockInitA.class.getName());
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes=" + MockInitB.class.getName() + ","
+ MockInitA.class.getName());
this.initializer.initialize(context);
assertThat(context.getBeanFactory().getSingleton("a")).isEqualTo("a");
assertThat(context.getBeanFactory().getSingleton("b")).isEqualTo("b");
@ -62,15 +63,16 @@ public class DelegatingApplicationContextInitializerTests { @@ -62,15 +63,16 @@ public class DelegatingApplicationContextInitializerTests {
@Test
public void emptyInitializers() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "context.initializer.classes:");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes:");
this.initializer.initialize(context);
}
@Test
public void noSuchInitializerClass() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context,
"context.initializer.classes:missing.madeup.class");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes=missing.madeup.class");
this.thrown.expect(ApplicationContextException.class);
this.initializer.initialize(context);
}
@ -78,8 +80,8 @@ public class DelegatingApplicationContextInitializerTests { @@ -78,8 +80,8 @@ public class DelegatingApplicationContextInitializerTests {
@Test
public void notAnInitializerClass() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context,
"context.initializer.classes:" + Object.class.getName());
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes=" + Object.class.getName());
this.thrown.expect(IllegalArgumentException.class);
this.initializer.initialize(context);
}
@ -87,8 +89,8 @@ public class DelegatingApplicationContextInitializerTests { @@ -87,8 +89,8 @@ public class DelegatingApplicationContextInitializerTests {
@Test
public void genericNotSuitable() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context,
"context.initializer.classes:" + NotSuitableInit.class.getName());
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context,
"context.initializer.classes=" + NotSuitableInit.class.getName());
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("generic parameter");
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; @@ -23,13 +23,13 @@ import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -56,8 +56,9 @@ public class DelegatingApplicationListenerTests { @@ -56,8 +56,9 @@ public class DelegatingApplicationListenerTests {
@Test
public void orderedInitialize() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:"
+ MockInitB.class.getName() + "," + MockInitA.class.getName());
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"context.listener.classes=" + MockInitB.class.getName() + ","
+ MockInitA.class.getName());
this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), new String[0], this.context.getEnvironment()));
this.context.getBeanFactory().registerSingleton("testListener", this.listener);
@ -74,7 +75,8 @@ public class DelegatingApplicationListenerTests { @@ -74,7 +75,8 @@ public class DelegatingApplicationListenerTests {
@Test
public void emptyInitializers() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "context.listener.classes:");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"context.listener.classes:");
this.listener.onApplicationEvent(new ApplicationEnvironmentPreparedEvent(
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; @@ -28,11 +28,11 @@ import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled;
import org.springframework.boot.ansi.AnsiOutputEnabledValue;
import org.springframework.boot.context.config.AnsiOutputApplicationListener;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -83,7 +83,8 @@ public class AnsiOutputApplicationListenerTests { @@ -83,7 +83,8 @@ public class AnsiOutputApplicationListenerTests {
@Test
public void disabledViaApplicationProperties() throws Exception {
ConfigurableEnvironment environment = new StandardEnvironment();
EnvironmentTestUtils.addEnvironment(environment, "spring.config.name:ansi");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment,
"spring.config.name=ansi");
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
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; @@ -34,12 +34,12 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
@ -73,7 +73,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -73,7 +73,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void testValidationWithSetter() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.foo:spam");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"test.foo=spam");
this.context.register(TestConfigurationWithValidatingSetter.class);
assertBindingFailure(1);
}
@ -81,7 +82,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -81,7 +82,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void unknownFieldFailureMessageContainsDetailsOfPropertyOrigin() {
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);
try {
this.context.refresh();
@ -91,8 +93,10 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -91,8 +93,10 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
RelaxedBindingNotWritablePropertyException bex = (RelaxedBindingNotWritablePropertyException) ex
.getRootCause();
assertThat(bex.getMessage())
.startsWith("Failed to bind 'com.example.baz' from 'test' to 'baz' "
+ "property on '" + TestConfiguration.class.getName());
.startsWith("Failed to bind 'com.example.baz' from '"
+ TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
+ "' to 'baz' " + "property on '"
+ TestConfiguration.class.getName());
}
}
@ -161,20 +165,20 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -161,20 +165,20 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void testPropertyWithEnum() throws Exception {
doEnumTest("test.theValue:foo");
doEnumTest("test.theValue=foo");
}
@Test
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) {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, property);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, property);
this.context.register(PropertyWithEnum.class);
this.context.refresh();
assertThat(this.context.getBean(PropertyWithEnum.class).getTheValue())
@ -184,15 +188,15 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -184,15 +188,15 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void testRelaxedPropertyWithSetOfEnum() {
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:BaR,FoO", FooEnum.BAR, FooEnum.FOO);
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=BaR,FoO", FooEnum.BAR, FooEnum.FOO);
}
private void doEnumSetTest(String property, FooEnum... expected) {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, property);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, property);
this.context.register(PropertyWithEnum.class);
this.context.refresh();
assertThat(this.context.getBean(PropertyWithEnum.class).getTheValues())
@ -203,7 +207,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -203,7 +207,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void testValueBindingForDefaults() throws Exception {
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.refresh();
assertThat(this.context.getBean(PropertyWithValue.class).getValue())
@ -213,7 +218,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -213,7 +218,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void placeholderResolutionWithCustomLocation() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "fooValue:bar");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"fooValue=bar");
this.context.register(CustomConfigurationLocation.class);
this.context.refresh();
assertThat(this.context.getBean(CustomConfigurationLocation.class).getFoo())
@ -223,7 +229,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -223,7 +229,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void placeholderResolutionWithUnmergedCustomLocation() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "fooValue:bar");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"fooValue:bar");
this.context.register(UnmergedCustomConfigurationLocation.class);
this.context.refresh();
assertThat(
@ -255,7 +262,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -255,7 +262,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void configurationPropertiesWithCharArray() throws Exception {
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.refresh();
assertThat(this.context.getBean(PropertyWithCharArray.class).getChars())
@ -265,7 +273,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -265,7 +273,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void configurationPropertiesWithArrayExpansion() throws Exception {
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.refresh();
assertThat(this.context.getBean(PropertyWithCharArrayExpansion.class).getChars())
@ -275,7 +284,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -275,7 +284,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void notWritablePropertyException() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.madeup:word");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"test.madeup:word");
this.context.register(PropertyWithCharArray.class);
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("test");
@ -284,17 +294,18 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -284,17 +294,18 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void relaxedPropertyNamesSame() throws Exception {
testRelaxedPropertyNames("test.FOO_BAR:test1", "test.FOO_BAR:test2");
testRelaxedPropertyNames("test.FOO_BAR=test1", "test.FOO_BAR=test2");
}
@Test
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) {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
environment);
this.context.register(RelaxedPropertyNames.class);
this.context.refresh();
assertThat(this.context.getBean(RelaxedPropertyNames.class).getFooBar())
@ -305,7 +316,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -305,7 +316,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
public void nestedProperties() throws Exception {
// gh-3539
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.refresh();
assertThat(this.context.getBean(PropertyWithNestedValue.class).getNested()
@ -315,7 +327,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @@ -315,7 +327,8 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test
public void bindWithoutConfigurationPropertiesAnnotation() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name:foo");
this.context.register(ConfigurationPropertiesWithoutAnnotation.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; @@ -30,13 +30,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.stereotype.Component;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.validation.BindException;
import static org.assertj.core.api.Assertions.assertThat;
@ -64,7 +64,8 @@ public class EnableConfigurationPropertiesTests { @@ -64,7 +64,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testBasicPropertiesBinding() {
this.context.register(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh();
assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1);
assertThat(this.context.containsBean(TestProperties.class.getName())).isTrue();
@ -106,7 +107,8 @@ public class EnableConfigurationPropertiesTests { @@ -106,7 +107,8 @@ public class EnableConfigurationPropertiesTests {
@Test
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.refresh();
assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1);
@ -119,7 +121,8 @@ public class EnableConfigurationPropertiesTests { @@ -119,7 +121,8 @@ public class EnableConfigurationPropertiesTests {
public void testStrictPropertiesBinding() {
removeSystemProperties();
this.context.register(StrictTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh();
assertThat(this.context.getBeanNamesForType(StrictTestProperties.class))
.hasSize(1);
@ -129,7 +132,8 @@ public class EnableConfigurationPropertiesTests { @@ -129,7 +132,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testPropertiesEmbeddedBinding() {
this.context.register(EmbeddedTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring_foo_name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"spring_foo_name=foo");
this.context.refresh();
assertThat(this.context.getBeanNamesForType(EmbeddedTestProperties.class))
.hasSize(1);
@ -138,7 +142,8 @@ public class EnableConfigurationPropertiesTests { @@ -138,7 +142,8 @@ public class EnableConfigurationPropertiesTests {
@Test
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.refresh();
assertThat(this.context.getBeanNamesForType(EmbeddedTestProperties.class))
@ -150,7 +155,8 @@ public class EnableConfigurationPropertiesTests { @@ -150,7 +155,8 @@ public class EnableConfigurationPropertiesTests {
public void testIgnoreNestedPropertiesBinding() {
removeSystemProperties();
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();
assertThat(this.context.getBeanNamesForType(IgnoreNestedTestProperties.class))
.hasSize(1);
@ -160,7 +166,8 @@ public class EnableConfigurationPropertiesTests { @@ -160,7 +166,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testExceptionOnValidation() {
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.context.refresh();
}
@ -168,7 +175,8 @@ public class EnableConfigurationPropertiesTests { @@ -168,7 +175,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testNoExceptionOnValidation() {
this.context.register(NoExceptionIfInvalidTestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh();
assertThat(this.context
.getBeanNamesForType(NoExceptionIfInvalidTestProperties.class))
@ -179,7 +187,8 @@ public class EnableConfigurationPropertiesTests { @@ -179,7 +187,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testNestedPropertiesBinding() {
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();
assertThat(this.context.getBeanNamesForType(NestedProperties.class)).hasSize(1);
assertThat(this.context.getBean(NestedProperties.class).name).isEqualTo("foo");
@ -190,7 +199,8 @@ public class EnableConfigurationPropertiesTests { @@ -190,7 +199,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testBasicPropertiesBindingWithAnnotationOnBaseClass() {
this.context.register(DerivedConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh();
assertThat(this.context.getBeanNamesForType(DerivedProperties.class)).hasSize(1);
assertThat(this.context.getBean(BaseProperties.class).name).isEqualTo("foo");
@ -199,7 +209,8 @@ public class EnableConfigurationPropertiesTests { @@ -199,7 +209,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testArrayPropertiesBinding() {
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();
assertThat(this.context.getBeanNamesForType(TestProperties.class)).hasSize(1);
assertThat(this.context.getBean(TestProperties.class).getArray()).hasSize(3);
@ -208,8 +219,8 @@ public class EnableConfigurationPropertiesTests { @@ -208,8 +219,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testCollectionPropertiesBindingFromYamlArray() {
this.context.register(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "list[0]:1",
"list[1]:2");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo", "list[0]=1", "list[1]=2");
this.context.refresh();
assertThat(this.context.getBean(TestProperties.class).getList()).hasSize(2);
}
@ -217,7 +228,8 @@ public class EnableConfigurationPropertiesTests { @@ -217,7 +228,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testPropertiesBindingWithoutAnnotation() {
this.context.register(InvalidConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name:foo");
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("No ConfigurationProperties annotation found");
@ -227,7 +239,8 @@ public class EnableConfigurationPropertiesTests { @@ -227,7 +239,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testPropertiesBindingWithoutAnnotationValue() {
this.context.register(MoreConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.refresh();
assertThat(this.context.getBeanNamesForType(MoreProperties.class)).hasSize(1);
assertThat(this.context.getBean(MoreProperties.class).name).isEqualTo("foo");
@ -263,8 +276,8 @@ public class EnableConfigurationPropertiesTests { @@ -263,8 +276,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testBindingDirectlyToFileResolvedFromEnvironment() {
EnvironmentTestUtils.addEnvironment(this.context,
"binding.location:classpath:other.yml");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"binding.location=classpath:other.yml");
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
@ -321,7 +334,8 @@ public class EnableConfigurationPropertiesTests { @@ -321,7 +334,8 @@ public class EnableConfigurationPropertiesTests {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
parent.register(TestConfiguration.class);
parent.refresh();
EnvironmentTestUtils.addEnvironment(this.context, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"name=foo");
this.context.setParent(parent);
this.context.register(TestConfiguration.class, TestConsumer.class);
this.context.refresh();
@ -334,7 +348,7 @@ public class EnableConfigurationPropertiesTests { @@ -334,7 +348,7 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testBindingOnlyParentContext() {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(parent, "name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(parent, "name=foo");
parent.register(TestConfiguration.class);
parent.refresh();
this.context.setParent(parent);
@ -348,7 +362,8 @@ public class EnableConfigurationPropertiesTests { @@ -348,7 +362,8 @@ public class EnableConfigurationPropertiesTests {
@Test
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.refresh();
assertThat(this.context.getBean(SystemEnvVar.class).getVal()).isEqualTo("baz");
@ -356,7 +371,8 @@ public class EnableConfigurationPropertiesTests { @@ -356,7 +371,8 @@ public class EnableConfigurationPropertiesTests {
@Test
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.refresh();
assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo");
@ -364,7 +380,8 @@ public class EnableConfigurationPropertiesTests { @@ -364,7 +380,8 @@ public class EnableConfigurationPropertiesTests {
@Test
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.refresh();
assertThat(this.context.containsBean("external-" + External.class.getName()))
@ -374,8 +391,8 @@ public class EnableConfigurationPropertiesTests { @@ -374,8 +391,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testMultipleExplicitTypes() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo",
"another.name:bar");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"external.name=foo", "another.name=bar");
this.context.register(FurtherExampleConfig.class);
this.context.refresh();
assertThat(this.context.getBean(External.class).getName()).isEqualTo("foo");
@ -397,8 +414,8 @@ public class EnableConfigurationPropertiesTests { @@ -397,8 +414,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testAnnotatedBean() {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:bar",
"spam.name:foo");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"external.name=bar", "spam.name=foo");
this.context.register(TestConfigurationWithAnnotatedBean.class);
this.context.refresh();
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; @@ -18,9 +18,9 @@ package org.springframework.boot.env;
import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -38,7 +38,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -38,7 +38,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void error() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json=foo:bar");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
@ -54,7 +54,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -54,7 +54,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void empty() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json={}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
@ -63,7 +63,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -63,7 +63,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void periodSeparated() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
@ -72,7 +72,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -72,7 +72,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void envVar() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
@ -81,7 +81,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -81,7 +81,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void nested() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":{\"bar\":\"spam\",\"rab\":\"maps\"}}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam");
@ -91,7 +91,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -91,7 +91,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void prefixed() {
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo.bar\":\"spam\"}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo.bar:}")).isEqualTo("spam");
@ -100,7 +100,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -100,7 +100,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void list() {
assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[\"bar\",\"spam\"]}");
this.processor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.resolvePlaceholders("${foo[1]:}")).isEqualTo("spam");
@ -109,7 +109,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests { @@ -109,7 +109,7 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Test
public void listOfObject() {
assertThat(this.environment.resolvePlaceholders("${foo[0].bar:}")).isEmpty();
EnvironmentTestUtils.addEnvironment(this.environment,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[{\"bar\":\"spam\"}]}");
this.processor.postProcessEnvironment(this.environment, null);
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; @@ -39,10 +39,10 @@ import org.springframework.boot.ApplicationPid;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.logging.java.JavaLoggingSystem;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.boot.testutil.OutputCapture;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -128,8 +128,8 @@ public class LoggingApplicationListenerTests { @@ -128,8 +128,8 @@ public class LoggingApplicationListenerTests {
@Test
public void overrideConfigLocation() {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.config: classpath:logback-nondefault.xml");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config=classpath:logback-nondefault.xml");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.info("Hello world");
@ -140,8 +140,8 @@ public class LoggingApplicationListenerTests { @@ -140,8 +140,8 @@ public class LoggingApplicationListenerTests {
@Test
public void overrideConfigDoesNotExist() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.config: doesnotexist.xml");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config=doesnotexist.xml");
this.thrown.expect(IllegalStateException.class);
this.outputCapture.expect(containsString(
"Logging system failed to initialize using configuration from 'doesnotexist.xml'"));
@ -151,7 +151,7 @@ public class LoggingApplicationListenerTests { @@ -151,7 +151,7 @@ public class LoggingApplicationListenerTests {
@Test
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\"");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
@ -163,8 +163,8 @@ public class LoggingApplicationListenerTests { @@ -163,8 +163,8 @@ public class LoggingApplicationListenerTests {
@Test
public void overrideConfigBroken() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.config: classpath:logback-broken.xml");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config=classpath:logback-broken.xml");
this.thrown.expect(IllegalStateException.class);
this.outputCapture.expect(containsString(
"Logging system failed to initialize using configuration from 'classpath:logback-broken.xml'"));
@ -175,9 +175,9 @@ public class LoggingApplicationListenerTests { @@ -175,9 +175,9 @@ public class LoggingApplicationListenerTests {
@Test
public void addLogFileProperty() {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.config: classpath:logback-nondefault.xml",
"logging.file: target/foo.log");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config=classpath:logback-nondefault.xml",
"logging.file=target/foo.log");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
@ -189,7 +189,8 @@ public class LoggingApplicationListenerTests { @@ -189,7 +189,8 @@ public class LoggingApplicationListenerTests {
@Test
public void addLogFilePropertyWithDefault() {
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.context.getClassLoader());
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
@ -199,9 +200,9 @@ public class LoggingApplicationListenerTests { @@ -199,9 +200,9 @@ public class LoggingApplicationListenerTests {
@Test
public void addLogPathProperty() {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.config: classpath:logback-nondefault.xml",
"logging.path: target/foo/");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.config=classpath:logback-nondefault.xml",
"logging.path=target/foo/");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
Log logger = LogFactory.getLog(LoggingApplicationListenerTests.class);
@ -212,7 +213,7 @@ public class LoggingApplicationListenerTests { @@ -212,7 +213,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseDebugArg() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "debug");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
@ -223,7 +224,7 @@ public class LoggingApplicationListenerTests { @@ -223,7 +224,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseTraceArg() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "trace");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "trace");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
@ -243,7 +244,8 @@ public class LoggingApplicationListenerTests { @@ -243,7 +244,8 @@ public class LoggingApplicationListenerTests {
}
private void disableDebugTraceArg(String... environment) {
EnvironmentTestUtils.addEnvironment(this.context, environment);
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
environment);
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
@ -254,7 +256,7 @@ public class LoggingApplicationListenerTests { @@ -254,7 +256,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseLevels() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=TRACE");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
@ -266,7 +268,7 @@ public class LoggingApplicationListenerTests { @@ -266,7 +268,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseLevelsCaseInsensitive() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=TrAcE");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
@ -278,8 +280,8 @@ public class LoggingApplicationListenerTests { @@ -278,8 +280,8 @@ public class LoggingApplicationListenerTests {
@Test
public void parseLevelsWithPlaceholder() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "foo=TRACE",
"logging.level.org.springframework.boot=${foo}");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"foo=TRACE", "logging.level.org.springframework.boot=${foo}");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
@ -290,7 +292,7 @@ public class LoggingApplicationListenerTests { @@ -290,7 +292,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseLevelsFails() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=GARBAGE");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
@ -301,7 +303,7 @@ public class LoggingApplicationListenerTests { @@ -301,7 +303,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseLevelsNone() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=OFF");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
@ -313,7 +315,7 @@ public class LoggingApplicationListenerTests { @@ -313,7 +315,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseLevelsMapsFalseToOff() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.level.org.springframework.boot=false");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
@ -326,7 +328,7 @@ public class LoggingApplicationListenerTests { @@ -326,7 +328,7 @@ public class LoggingApplicationListenerTests {
@Test
public void parseArgsDisabled() throws Exception {
this.initializer.setParseArgs(false);
EnvironmentTestUtils.addEnvironment(this.context, "debug");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "debug");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
@ -365,8 +367,8 @@ public class LoggingApplicationListenerTests { @@ -365,8 +367,8 @@ public class LoggingApplicationListenerTests {
@Test
public void overrideExceptionConversionWord() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.exceptionConversionWord:%rEx");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.exceptionConversionWord=%rEx");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.outputCapture.expect(containsString("Hello world"));
@ -392,8 +394,8 @@ public class LoggingApplicationListenerTests { @@ -392,8 +394,8 @@ public class LoggingApplicationListenerTests {
TestLoggingApplicationListener listener = new TestLoggingApplicationListener();
System.setProperty(LoggingSystem.class.getName(),
TestShutdownHandlerLoggingSystem.class.getName());
EnvironmentTestUtils.addEnvironment(this.context,
"logging.register_shutdown_hook:true");
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.register_shutdown_hook=true");
listener.onApplicationEvent(
new ApplicationStartedEvent(new SpringApplication(), NO_ARGS));
listener.initialize(this.context.getEnvironment(), this.context.getClassLoader());
@ -436,7 +438,7 @@ public class LoggingApplicationListenerTests { @@ -436,7 +438,7 @@ public class LoggingApplicationListenerTests {
@Test
public void systemPropertiesAreSetForLoggingConfiguration() {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.exception-conversion-word=conversion", "logging.file=target/log",
"logging.path=path", "logging.pattern.console=console",
"logging.pattern.file=file", "logging.pattern.level=level");
@ -454,7 +456,7 @@ public class LoggingApplicationListenerTests { @@ -454,7 +456,7 @@ public class LoggingApplicationListenerTests {
@Test
public void logFilePropertiesCanReferenceSystemProperties() {
EnvironmentTestUtils.addEnvironment(this.context,
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
"logging.file=target/${PID}.log");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());

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

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

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

@ -1,74 +0,0 @@ @@ -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