Skip to content

Commit

Permalink
Respect spring.profiles.active in #addActiveProfile
Browse files Browse the repository at this point in the history
Prior to this commit, calls to ConfigurableEnvironment#addActiveProfile
would cause any active profile values provided via the
"spring.profiles.active" property to be ignored.

Now these two mechanisms can be used in conjunction and work as
expected.

Issue: SPR-9944
  • Loading branch information
cbeams committed Nov 2, 2012
1 parent 74e86fe commit c94bc2e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.context.support;

import org.junit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand All @@ -34,7 +33,6 @@
*/
public class EnvironmentIntegrationTests {

@SuppressWarnings("unchecked")
@Test
public void repro() {
ConfigurableApplicationContext parent = new GenericApplicationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ public void setActiveProfiles(String... profiles) {
Assert.notNull(profiles, "Profile array must not be null");
this.activeProfiles.clear();
for (String profile : profiles) {
addActiveProfile(profile);
validateProfile(profile);
this.activeProfiles.add(profile);
}
}

Expand All @@ -249,9 +250,11 @@ public void addActiveProfile(String profile) {
this.logger.debug(format("Activating profile '%s'", profile));
}
validateProfile(profile);
doGetActiveProfiles();
this.activeProfiles.add(profile);
}


public String[] getDefaultProfiles() {
return StringUtils.toStringArray(doGetDefaultProfiles());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ public void addActiveProfile() {
assertThat(environment.getActiveProfiles().length, is(5));
}

@Test
public void addActiveProfile_whenActiveProfilesPropertyIsAlreadySet() {
ConfigurableEnvironment env = new StandardEnvironment();
assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), nullValue());
env.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1"));
assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), equalTo("p1"));
env.addActiveProfile("p2");
assertThat(env.getActiveProfiles(), arrayContaining("p1", "p2"));
}

@Test
public void reservedDefaultProfile() {
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));
Expand Down

0 comments on commit c94bc2e

Please sign in to comment.