Skip to content

Commit 842e037

Browse files
author
Dave Syer
committed
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)
1 parent 703d7d3 commit 842e037

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,8 @@ private void load(String location, String name, String profile)
305305
private PropertySource<?> load(String resourceLocation, String profile)
306306
throws IOException {
307307
Resource resource = this.resourceLoader.getResource(resourceLocation);
308-
if (resource != null && resource.exists()) {
308+
if (resource != null) {
309309
String name = "applicationConfig: " + resource.getDescription();
310-
if (StringUtils.hasLength(profile)) {
311-
name += " " + profile;
312-
}
313310
PropertySource<?> propertySource = this.propertiesLoader.load(resource,
314311
name, profile);
315312
if (propertySource != null) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.core.io.Resource;
2828
import org.springframework.core.io.support.SpringFactoriesLoader;
2929
import org.springframework.util.Assert;
30+
import org.springframework.util.StringUtils;
3031

3132
/**
3233
* Utiltiy that can be used to {@link MutablePropertySources} using
@@ -70,7 +71,7 @@ public PropertySourcesLoader(MutablePropertySources propertySources) {
7071
*/
7172
public PropertySource<?> load(Resource resource, String name, String profile)
7273
throws IOException {
73-
if (resource != null && resource.exists()) {
74+
if (isFile(resource)) {
7475
name = generatePropertySourceName(resource, name, profile);
7576
for (PropertySourceLoader loader : this.loaders) {
7677
if (canLoadFileExtension(loader, resource)) {
@@ -83,6 +84,13 @@ public PropertySource<?> load(Resource resource, String name, String profile)
8384
return null;
8485
}
8586

87+
private boolean isFile(Resource resource) {
88+
return resource != null
89+
&& resource.exists()
90+
&& StringUtils.hasText(StringUtils.getFilenameExtension(resource
91+
.getFilename()));
92+
}
93+
8694
private String generatePropertySourceName(Resource resource, String name,
8795
String profile) {
8896
if (name == null) {

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package org.springframework.boot.context.config;
1818

1919
import java.lang.reflect.Field;
20+
import java.util.ArrayList;
2021
import java.util.Arrays;
22+
import java.util.Collection;
23+
import java.util.List;
2124

2225
import org.hamcrest.Description;
2326
import org.hamcrest.Matcher;
@@ -45,6 +48,7 @@
4548
import static org.hamcrest.Matchers.not;
4649
import static org.hamcrest.Matchers.notNullValue;
4750
import static org.hamcrest.Matchers.nullValue;
51+
import static org.junit.Assert.assertEquals;
4852
import static org.junit.Assert.assertThat;
4953

5054
/**
@@ -178,6 +182,20 @@ public void yamlSetsProfiles() throws Exception {
178182
String property = this.environment.getProperty("my.property");
179183
assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev"));
180184
assertThat(property, equalTo("fromdevprofile"));
185+
ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment
186+
.getPropertySources().get("applicationConfigurationProperties");
187+
Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource
188+
.getSource();
189+
assertEquals(2, sources.size());
190+
List<String> names = new ArrayList<String>();
191+
for (org.springframework.core.env.PropertySource<?> source : sources) {
192+
names.add(source.getName());
193+
}
194+
assertThat(
195+
names,
196+
contains(
197+
"applicationConfig: class path resource [testsetprofiles.yml]#dev",
198+
"applicationConfig: class path resource [testsetprofiles.yml]"));
181199
}
182200

183201
@Test
@@ -189,6 +207,30 @@ public void yamlProfileCanBeChanged() throws Exception {
189207
assertThat(this.environment.getActiveProfiles(), equalTo(new String[] { "prod" }));
190208
}
191209

210+
@Test
211+
public void yamlProfileOrdering() throws Exception {
212+
this.initializer.setSearchNames("threeprofiles");
213+
this.environment.setActiveProfiles("A", "C");
214+
this.initializer.onApplicationEvent(this.event);
215+
assertThat(this.environment.getProperty("version"), equalTo("C"));
216+
}
217+
218+
@Test
219+
public void yamlProfileOrderingReverse() throws Exception {
220+
this.initializer.setSearchNames("threeprofiles");
221+
this.environment.setActiveProfiles("C", "A");
222+
this.initializer.onApplicationEvent(this.event);
223+
assertThat(this.environment.getProperty("version"), equalTo("A"));
224+
}
225+
226+
@Test
227+
public void yamlProfileOrderingOverride() throws Exception {
228+
this.initializer.setSearchNames("threeprofiles-with-override");
229+
this.environment.setActiveProfiles("C", "A");
230+
this.initializer.onApplicationEvent(this.event);
231+
assertThat(this.environment.getProperty("version"), equalTo("B"));
232+
}
233+
192234
@Test
193235
public void specificNameAndProfileFromExistingSource() throws Exception {
194236
EnvironmentTestUtils.addEnvironment(this.environment,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
spring.profiles.active: B
3+
---
4+
spring.profiles: A
5+
version: A
6+
---
7+
spring.profiles: B
8+
version: B
9+
---
10+
spring.profiles: C
11+
version: C
12+
---
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
---
3+
spring.profiles: A
4+
version: A
5+
---
6+
spring.profiles: B
7+
version: B
8+
---
9+
spring.profiles: C
10+
version: C
11+
---

0 commit comments

Comments
 (0)