Browse Source

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)
pull/355/merge
Dave Syer 12 years ago
parent
commit
842e037b72
  1. 5
      spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java
  2. 10
      spring-boot/src/main/java/org/springframework/boot/env/PropertySourcesLoader.java
  3. 42
      spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java
  4. 12
      spring-boot/src/test/resources/threeprofiles-with-override.yml
  5. 11
      spring-boot/src/test/resources/threeprofiles.yml

5
spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

@ -305,11 +305,8 @@ public class ConfigFileApplicationListener implements @@ -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) {

10
spring-boot/src/main/java/org/springframework/boot/env/PropertySourcesLoader.java vendored

@ -27,6 +27,7 @@ import org.springframework.core.env.PropertySource; @@ -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 { @@ -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 { @@ -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) {

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

@ -17,7 +17,10 @@ @@ -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; @@ -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 { @@ -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 { @@ -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,

12
spring-boot/src/test/resources/threeprofiles-with-override.yml

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

11
spring-boot/src/test/resources/threeprofiles.yml

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
---
---
spring.profiles: A
version: A
---
spring.profiles: B
version: B
---
spring.profiles: C
version: C
---
Loading…
Cancel
Save