Add some tests for profile behaviour

Ordering: profiles are applied in order (from the active profiles
list in the Environment), with the last one winning as far as
property values goes. This *does* mean that a profile activated
inside application.yml is applied last and hence takes precedence.
It's debatable whether that is the right semantics, but that's
what it is for now.

Re gh-342: a profile added via SpringApplication also takes
precedence over one added on the command line. Also debatable
but at least it's predictable.

Naming: a profile adds "#<profile>" to the end of a property source
name (no more, no less)
This commit is contained in:
Dave Syer
2014-02-17 10:44:30 +00:00
parent 703d7d3fd9
commit 842e037b72
5 changed files with 75 additions and 5 deletions

View File

@@ -305,11 +305,8 @@ public class ConfigFileApplicationListener implements
private PropertySource<?> load(String resourceLocation, String profile)
throws IOException {
Resource resource = this.resourceLoader.getResource(resourceLocation);
if (resource != null && resource.exists()) {
if (resource != null) {
String name = "applicationConfig: " + resource.getDescription();
if (StringUtils.hasLength(profile)) {
name += " " + profile;
}
PropertySource<?> propertySource = this.propertiesLoader.load(resource,
name, profile);
if (propertySource != null) {

View File

@@ -27,6 +27,7 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Utiltiy that can be used to {@link MutablePropertySources} using
@@ -70,7 +71,7 @@ public class PropertySourcesLoader {
*/
public PropertySource<?> load(Resource resource, String name, String profile)
throws IOException {
if (resource != null && resource.exists()) {
if (isFile(resource)) {
name = generatePropertySourceName(resource, name, profile);
for (PropertySourceLoader loader : this.loaders) {
if (canLoadFileExtension(loader, resource)) {
@@ -83,6 +84,13 @@ public class PropertySourcesLoader {
return null;
}
private boolean isFile(Resource resource) {
return resource != null
&& resource.exists()
&& StringUtils.hasText(StringUtils.getFilenameExtension(resource
.getFilename()));
}
private String generatePropertySourceName(Resource resource, String name,
String profile) {
if (name == null) {

View File

@@ -17,7 +17,10 @@
package org.springframework.boot.context.config;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
@@ -45,6 +48,7 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
@@ -178,6 +182,20 @@ public class ConfigFileApplicationListenerTests {
String property = this.environment.getProperty("my.property");
assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev"));
assertThat(property, equalTo("fromdevprofile"));
ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment
.getPropertySources().get("applicationConfigurationProperties");
Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource
.getSource();
assertEquals(2, sources.size());
List<String> names = new ArrayList<String>();
for (org.springframework.core.env.PropertySource<?> source : sources) {
names.add(source.getName());
}
assertThat(
names,
contains(
"applicationConfig: class path resource [testsetprofiles.yml]#dev",
"applicationConfig: class path resource [testsetprofiles.yml]"));
}
@Test
@@ -189,6 +207,30 @@ public class ConfigFileApplicationListenerTests {
assertThat(this.environment.getActiveProfiles(), equalTo(new String[] { "prod" }));
}
@Test
public void yamlProfileOrdering() throws Exception {
this.initializer.setSearchNames("threeprofiles");
this.environment.setActiveProfiles("A", "C");
this.initializer.onApplicationEvent(this.event);
assertThat(this.environment.getProperty("version"), equalTo("C"));
}
@Test
public void yamlProfileOrderingReverse() throws Exception {
this.initializer.setSearchNames("threeprofiles");
this.environment.setActiveProfiles("C", "A");
this.initializer.onApplicationEvent(this.event);
assertThat(this.environment.getProperty("version"), equalTo("A"));
}
@Test
public void yamlProfileOrderingOverride() throws Exception {
this.initializer.setSearchNames("threeprofiles-with-override");
this.environment.setActiveProfiles("C", "A");
this.initializer.onApplicationEvent(this.event);
assertThat(this.environment.getProperty("version"), equalTo("B"));
}
@Test
public void specificNameAndProfileFromExistingSource() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment,

View File

@@ -0,0 +1,12 @@
---
spring.profiles.active: B
---
spring.profiles: A
version: A
---
spring.profiles: B
version: B
---
spring.profiles: C
version: C
---

View File

@@ -0,0 +1,11 @@
---
---
spring.profiles: A
version: A
---
spring.profiles: B
version: B
---
spring.profiles: C
version: C
---