Introduce AbstractEnvironment#validateProfile

Consolidates validation for profiles and provides a mechanism for
AbstractEnvironment subclasses to customize validation logic if
desired.
This commit is contained in:
Chris Beams
2011-08-20 03:02:12 +00:00
parent e19a2e7370
commit c0eeb8bacd
4 changed files with 111 additions and 17 deletions

View File

@@ -72,14 +72,54 @@ public class EnvironmentTests {
}
@Test
public void activeProfiles() {
public void activeProfilesIsEmptyByDefault() {
assertThat(environment.getActiveProfiles().length, is(0));
}
@Test
public void defaultProfilesContainsDefaultProfileByDefault() {
assertThat(environment.getDefaultProfiles().length, is(1));
assertThat(environment.getDefaultProfiles()[0], equalTo("default"));
}
@Test
public void setActiveProfiles() {
environment.setActiveProfiles("local", "embedded");
String[] activeProfiles = environment.getActiveProfiles();
assertThat(Arrays.asList(activeProfiles), hasItems("local", "embedded"));
assertThat(activeProfiles.length, is(2));
}
@Test(expected=IllegalArgumentException.class)
public void setActiveProfiles_withNullProfileArray() {
environment.setActiveProfiles((String[])null);
}
@Test(expected=IllegalArgumentException.class)
public void setActiveProfiles_withNullProfile() {
environment.setActiveProfiles((String)null);
}
@Test(expected=IllegalArgumentException.class)
public void setActiveProfiles_withEmptyProfile() {
environment.setActiveProfiles("");
}
@Test(expected=IllegalArgumentException.class)
public void setDefaultProfiles_withNullProfileArray() {
environment.setDefaultProfiles((String[])null);
}
@Test(expected=IllegalArgumentException.class)
public void setDefaultProfiles_withNullProfile() {
environment.setDefaultProfiles((String)null);
}
@Test(expected=IllegalArgumentException.class)
public void setDefaultProfiles_withEmptyProfile() {
environment.setDefaultProfiles("");
}
@Test
public void reservedDefaultProfile() {
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));
@@ -100,7 +140,6 @@ public class EnvironmentTests {
@Test
public void getActiveProfiles_fromSystemProperties() {
assertThat(environment.getActiveProfiles().length, is(0));
System.setProperty(ACTIVE_PROFILES_PROPERTY_NAME, "foo");
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItem("foo"));
System.getProperties().remove(ACTIVE_PROFILES_PROPERTY_NAME);
@@ -108,7 +147,6 @@ public class EnvironmentTests {
@Test
public void getActiveProfiles_fromSystemProperties_withMultipleProfiles() {
assertThat(environment.getActiveProfiles().length, is(0));
System.setProperty(ACTIVE_PROFILES_PROPERTY_NAME, "foo,bar");
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("foo", "bar"));
System.getProperties().remove(ACTIVE_PROFILES_PROPERTY_NAME);
@@ -116,7 +154,6 @@ public class EnvironmentTests {
@Test
public void getActiveProfiles_fromSystemProperties_withMulitpleProfiles_withWhitespace() {
assertThat(environment.getActiveProfiles().length, is(0));
System.setProperty(ACTIVE_PROFILES_PROPERTY_NAME, " bar , baz "); // notice whitespace
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("bar", "baz"));
System.getProperties().remove(ACTIVE_PROFILES_PROPERTY_NAME);
@@ -142,10 +179,26 @@ public class EnvironmentTests {
}
@Test(expected=IllegalArgumentException.class)
public void acceptsProfiles_mustSpecifyAtLeastOne() {
public void acceptsProfiles_withEmptyArgumentList() {
environment.acceptsProfiles();
}
@Test(expected=IllegalArgumentException.class)
public void acceptsProfiles_withNullArgumentList() {
environment.acceptsProfiles((String[])null);
}
@Test(expected=IllegalArgumentException.class)
public void acceptsProfiles_withNullArgument() {
environment.acceptsProfiles((String)null);
}
@Test(expected=IllegalArgumentException.class)
public void acceptsProfiles_withEmptyArgument() {
environment.acceptsProfiles("");
}
@Test
public void acceptsProfiles_activeProfileSetProgrammatically() {
assertThat(environment.acceptsProfiles("p1", "p2"), is(false));
@@ -174,6 +227,30 @@ public class EnvironmentTests {
assertThat(environment.acceptsProfiles("p1"), is(true));
}
@Test
public void environmentSubclass_withCustomProfileValidation() {
ConfigurableEnvironment env = new AbstractEnvironment() {
@Override
protected void validateProfile(String profile) {
super.validateProfile(profile);
if (profile.contains("-")) {
throw new IllegalArgumentException(
"Invalid profile [" + profile + "]: must not contain dash character");
}
}
};
env.addActiveProfile("validProfile"); // succeeds
try {
env.addActiveProfile("invalid-profile");
fail("expected validation exception");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(),
equalTo("Invalid profile [invalid-profile]: must not contain dash character"));
}
}
@Test
public void getSystemProperties_withAndWithoutSecurityManager() {
System.setProperty(ALLOWED_PROPERTY_NAME, ALLOWED_PROPERTY_VALUE);