Support not (!) operator for profile selection

The following syntax is now supported

  <beans profile="p1,!p2">

  @Profile("p1", "!p2")

indicating that the <beans> element or annotated component should
be processed only if profile 'p1' is active or profile 'p2' is not
active.

Issue: SPR-8728
This commit is contained in:
Chris Beams
2012-05-27 08:10:40 +03:00
parent e72c49f4cf
commit bcd44f3798
6 changed files with 115 additions and 47 deletions

View File

@@ -17,8 +17,10 @@
package org.springframework.core.env;
import java.lang.reflect.Field;
import java.security.AccessControlException;
import java.security.Permission;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
@@ -147,6 +149,11 @@ public class StandardEnvironmentTests {
environment.setActiveProfiles("");
}
@Test(expected=IllegalArgumentException.class)
public void setActiveProfiles_withNotOperator() {
environment.setActiveProfiles("p1", "!p2");
}
@Test(expected=IllegalArgumentException.class)
public void setDefaultProfiles_withNullProfileArray() {
environment.setDefaultProfiles((String[])null);
@@ -162,6 +169,11 @@ public class StandardEnvironmentTests {
environment.setDefaultProfiles("");
}
@Test(expected=IllegalArgumentException.class)
public void setDefaultProfiles_withNotOperator() {
environment.setDefaultProfiles("d1", "!d2");
}
@Test
public void addActiveProfile() {
assertThat(environment.getActiveProfiles().length, is(0));
@@ -284,6 +296,20 @@ public class StandardEnvironmentTests {
assertThat(environment.acceptsProfiles("p1"), is(true));
}
@Test
public void acceptsProfiles_withNotOperator() {
assertThat(environment.acceptsProfiles("p1"), is(false));
assertThat(environment.acceptsProfiles("!p1"), is(true));
environment.addActiveProfile("p1");
assertThat(environment.acceptsProfiles("p1"), is(true));
assertThat(environment.acceptsProfiles("!p1"), is(false));
}
@Test(expected=IllegalArgumentException.class)
public void acceptsProfiles_withInvalidNotOperator() {
environment.acceptsProfiles("p1", "!");
}
@Test
public void environmentSubclass_withCustomProfileValidation() {
ConfigurableEnvironment env = new AbstractEnvironment() {