Add a couple more tests on profile ordering

This commit is contained in:
Dave Syer 2014-03-17 09:36:47 +00:00
parent b8d85decad
commit 16d4214f56
4 changed files with 33 additions and 4 deletions

View File

@ -388,8 +388,8 @@ public class SpringApplication {
* the above for fine-grained control over property sources or profiles, respectively.
* @param environment this application's environment
* @param args arguments passed to the {@code run} method
* @see #configurePropertySources(ConfigurableEnvironment, String[])
* @see #configureProfiles(ConfigurableEnvironment, String[])
* @see #configurePropertySources(ConfigurableEnvironment, String[])
*/
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
configurePropertySources(environment, args);

View File

@ -285,6 +285,9 @@ public class ConfigFileApplicationListener implements
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);
while (!this.profiles.isEmpty()) {

View File

@ -296,9 +296,22 @@ public class SpringApplicationTests {
application.setAdditionalProfiles("foo");
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
application.run("--spring.profiles.active=bar");
application.run("--spring.profiles.active=bar,spam");
// 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

View File

@ -189,7 +189,20 @@ public class ConfigFileApplicationListenerTests {
}
@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.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property");