Skip to content

Commit

Permalink
Fix spring.profiles.active value handling
Browse files Browse the repository at this point in the history
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 spring-projectsgh-5442
  • Loading branch information
eddumelendez authored and snicoll committed Apr 5, 2016
1 parent d27e280 commit 4353963
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* @author Phillip Webb
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Eddú Meléndez
*/
public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
ApplicationListener<ApplicationEvent>, Ordered {
Expand Down Expand Up @@ -582,8 +583,8 @@ private Set<String> getSearchNames() {

private Set<String> asResolvedSet(String value, String fallback) {
List<String> 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<String>(list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
*
* @author Phillip Webb
* @author Dave Syer
* @author Eddú Meléndez
*/
public class ConfigFileApplicationListenerTests {

Expand Down Expand Up @@ -529,6 +530,39 @@ public void yamlSetsProfiles() throws Exception {
"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<org.springframework.core.env.PropertySource<?>> sources = propertySource
.getSource();
assertThat(sources).hasSize(3);
List<String> names = new ArrayList<String>();
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,
Expand Down
17 changes: 17 additions & 0 deletions spring-boot/src/test/resources/testsetmultiprofiles.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4353963

Please sign in to comment.