Commit 16d4214f authored by Dave Syer's avatar Dave Syer

Add a couple more tests on profile ordering

parent b8d85dec
...@@ -388,8 +388,8 @@ public class SpringApplication { ...@@ -388,8 +388,8 @@ public class SpringApplication {
* the above for fine-grained control over property sources or profiles, respectively. * the above for fine-grained control over property sources or profiles, respectively.
* @param environment this application's environment * @param environment this application's environment
* @param args arguments passed to the {@code run} method * @param args arguments passed to the {@code run} method
* @see #configurePropertySources(ConfigurableEnvironment, String[])
* @see #configureProfiles(ConfigurableEnvironment, String[]) * @see #configureProfiles(ConfigurableEnvironment, String[])
* @see #configurePropertySources(ConfigurableEnvironment, String[])
*/ */
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
configurePropertySources(environment, args); configurePropertySources(environment, args);
......
...@@ -285,6 +285,9 @@ public class ConfigFileApplicationListener implements ...@@ -285,6 +285,9 @@ public class ConfigFileApplicationListener implements
this.profiles.addAll(Arrays.asList(this.environment.getActiveProfiles())); this.profiles.addAll(Arrays.asList(this.environment.getActiveProfiles()));
} }
// The default profile for these purposes is represented as null. We add it
// last so that it is first out (active profiles will then override any
// settings in the defaults when the list is reversed later).
this.profiles.add(null); this.profiles.add(null);
while (!this.profiles.isEmpty()) { while (!this.profiles.isEmpty()) {
......
...@@ -296,9 +296,22 @@ public class SpringApplicationTests { ...@@ -296,9 +296,22 @@ public class SpringApplicationTests {
application.setAdditionalProfiles("foo"); application.setAdditionalProfiles("foo");
ConfigurableEnvironment environment = new StandardEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment); application.setEnvironment(environment);
application.run("--spring.profiles.active=bar"); application.run("--spring.profiles.active=bar,spam");
// Command line should always come last // Command line should always come last
assertArrayEquals(new String[] { "foo", "bar" }, environment.getActiveProfiles()); assertArrayEquals(new String[] { "foo", "bar", "spam" },
environment.getActiveProfiles());
}
@Test
public void addProfilesOrderWithProperties() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
application.setAdditionalProfiles("other");
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
application.run();
// Active profile should win over default
assertEquals("fromotherpropertiesfile", environment.getProperty("my.property"));
} }
@Test @Test
......
...@@ -189,7 +189,20 @@ public class ConfigFileApplicationListenerTests { ...@@ -189,7 +189,20 @@ public class ConfigFileApplicationListenerTests {
} }
@Test @Test
public void loadPropertiesThenProfileProperties() throws Exception { public void loadPropertiesThenProfilePropertiesActivatedInSpringApplication()
throws Exception {
// This should be the effect of calling
// SpringApplication.setAdditionalProfiles("other")
this.environment.setActiveProfiles("other");
this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property");
// The "other" profile is activated in SpringApplication so it should take
// precedence over the default profile
assertThat(property, equalTo("fromotherpropertiesfile"));
}
@Test
public void loadPropertiesThenProfilePropertiesActivatedInFirst() throws Exception {
this.initializer.setSearchNames("enableprofile"); this.initializer.setSearchNames("enableprofile");
this.initializer.onApplicationEvent(this.event); this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment