From 4353963df6d6b7af1c8f6a8620370b3fed23dfb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Mon, 4 Apr 2016 15:17:09 +1000 Subject: [PATCH] Fix spring.profiles.active value handling Previously, `spring.profiles.active` did not trim the individual comma separate values. As a result, such profile wasn't enabled as expected. This commit makes sure to trim whitespaces. Closes gh-5442 --- .../config/ConfigFileApplicationListener.java | 5 +-- .../ConfigFileApplicationListenerTests.java | 34 +++++++++++++++++++ .../test/resources/testsetmultiprofiles.yml | 17 ++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/test/resources/testsetmultiprofiles.yml diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 93040195e5..49974ddfe6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -95,6 +95,7 @@ import org.springframework.validation.BindException; * @author Phillip Webb * @author Stephane Nicoll * @author Andy Wilkinson + * @author Eddú Meléndez */ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, ApplicationListener, Ordered { @@ -582,8 +583,8 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, private Set asResolvedSet(String value, String fallback) { List list = Arrays - .asList(StringUtils.commaDelimitedListToStringArray(value != null - ? this.environment.resolvePlaceholders(value) : fallback)); + .asList(StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(value != null + ? this.environment.resolvePlaceholders(value) : fallback))); Collections.reverse(list); return new LinkedHashSet(list); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index bfbddf3161..a8b7db2906 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -73,6 +73,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb * @author Dave Syer + * @author Eddú Meléndez */ public class ConfigFileApplicationListenerTests { @@ -529,6 +530,39 @@ public class ConfigFileApplicationListenerTests { "applicationConfig: [classpath:/testsetprofiles.yml]"); } + @Test + public void yamlSetsMultiProfiles() throws Exception { + this.initializer.setSearchNames("testsetmultiprofiles"); + this.initializer.postProcessEnvironment(this.environment, this.application); + assertThat(this.environment.getActiveProfiles()).containsExactly("dev", "healthcheck"); + String property = this.environment.getProperty("my.property"); + String property2 = this.environment.getProperty("my.property2"); + assertThat(property).isEqualTo("fromdevprofile"); + assertThat(property2).isEqualTo("fromhealthcheckprofile"); + ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment + .getPropertySources() + .get(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME); + Collection> sources = propertySource + .getSource(); + assertThat(sources).hasSize(3); + List names = new ArrayList(); + for (org.springframework.core.env.PropertySource source : sources) { + if (source instanceof EnumerableCompositePropertySource) { + for (org.springframework.core.env.PropertySource nested : ((EnumerableCompositePropertySource) source) + .getSource()) { + names.add(nested.getName()); + } + } + else { + names.add(source.getName()); + } + } + assertThat(names).contains( + "applicationConfig: [classpath:/testsetmultiprofiles.yml]#healthcheck", + "applicationConfig: [classpath:/testsetmultiprofiles.yml]#dev", + "applicationConfig: [classpath:/testsetmultiprofiles.yml]"); + } + @Test public void yamlProfileCanBeChanged() throws Exception { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, diff --git a/spring-boot/src/test/resources/testsetmultiprofiles.yml b/spring-boot/src/test/resources/testsetmultiprofiles.yml new file mode 100644 index 0000000000..42645fb518 --- /dev/null +++ b/spring-boot/src/test/resources/testsetmultiprofiles.yml @@ -0,0 +1,17 @@ +--- +spring: + profiles: + active: dev, healthcheck +my: + property: fromyamlfile + property2: fromyamlfile +--- +spring: + profiles: dev +my: + property: fromdevprofile +--- +spring: + profiles: healthcheck +my: + property2: fromhealthcheckprofile \ No newline at end of file