Make TestPropertySourceUtils more robust

- Added assertions for pre-conditions on method arguments for all
   public utility methods.

 - Introduced additional tests in TestPropertySourceUtilsTests to verify
   the new pre-conditions.

 - Introduced INLINED_PROPERTIES_PROPERTY_SOURCE_NAME constant for the
   name of the MapPropertySource created from inlined properties; the
   name therefore no longer contains the inlined properties, but the
   original values of the inlined properties can now be logged at debug
   level.

 - Simplified tests in InlinedPropertiesTestPropertySourceTests.

Issue: SPR-12721
This commit is contained in:
Sam Brannen
2015-02-17 19:42:52 +01:00
parent 0267715c9d
commit 42af33034d
3 changed files with 122 additions and 52 deletions

View File

@@ -23,13 +23,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.context.support.TestPropertySourceUtils.*;
/**
* Integration tests for {@link TestPropertySource @TestPropertySource} support with
@@ -45,45 +45,36 @@ import static org.junit.Assert.*;
public class InlinedPropertiesTestPropertySourceTests {
@Autowired
private Environment env;
private ConfigurableEnvironment env;
private String property(String key) {
return env.getProperty(key);
}
@Test
public void propertiesAreAvailableInEnvironment() {
// Simple key/value pairs
assertEquals("bar", env.getProperty("foo"));
assertEquals("quux", env.getProperty("baz"));
assertEquals(42, env.getProperty("enigma", Integer.class).intValue());
assertThat(property("foo"), is("bar"));
assertThat(property("baz"), is("quux"));
assertThat(property("enigma"), is("42"));
// Values containing key/value delimiters (":", "=", " ")
assertEquals("a=b=c", env.getProperty("x.y.z"));
assertEquals("http://example.com", env.getProperty("server.url"));
assertEquals("key=value", env.getProperty("key.value.1"));
assertEquals("key=value", env.getProperty("key.value.2"));
assertEquals("key:value", env.getProperty("key.value.3"));
assertThat(property("x.y.z"), is("a=b=c"));
assertThat(property("server.url"), is("http://example.com"));
assertThat(property("key.value.1"), is("key=value"));
assertThat(property("key.value.2"), is("key=value"));
assertThat(property("key.value.3"), is("key:value"));
}
@Test
@SuppressWarnings("rawtypes")
public void propertyNameOrderingIsPreservedInEnvironment() {
String[] propertyNames = null;
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) env;
for (PropertySource<?> propertySource : configurableEnvironment.getPropertySources()) {
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource eps = (EnumerablePropertySource) propertySource;
if (eps.getName().startsWith("test properties")) {
propertyNames = eps.getPropertyNames();
break;
}
}
}
final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url",
"key.value.1", "key.value.2", "key.value.3" };
assertArrayEquals(expectedPropertyNames, propertyNames);
EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get(
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
assertArrayEquals(expectedPropertyNames, eps.getPropertyNames());
}

View File

@@ -22,6 +22,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.mock.env.MockEnvironment;
@@ -30,6 +31,7 @@ import org.springframework.test.context.TestPropertySource;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.springframework.test.context.support.TestPropertySourceUtils.*;
/**
@@ -41,6 +43,7 @@ import static org.springframework.test.context.support.TestPropertySourceUtils.*
public class TestPropertySourceUtilsTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final String[] KEY_VALUE_PAIR = new String[] { "key = value" };
@Rule
public ExpectedException expectedException = ExpectedException.none();
@@ -110,35 +113,60 @@ public class TestPropertySourceUtilsTests {
@Test
public void overriddenProperties() {
assertMergedTestPropertySources(OverriddenPropertiesPropertySources.class, new String[] {
"classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties" }, new String[] { "key = value" });
"classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties" }, KEY_VALUE_PAIR);
}
@Test
public void overriddenLocationsAndProperties() {
assertMergedTestPropertySources(OverriddenLocationsAndPropertiesPropertySources.class,
new String[] { "classpath:/baz.properties" }, new String[] { "key = value" });
new String[] { "classpath:/baz.properties" }, KEY_VALUE_PAIR);
}
/**
* @since 4.1.5
*/
@Test
@SuppressWarnings("rawtypes")
public void emptyInlinedProperty() {
ConfigurableEnvironment environment = new MockEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
assertEquals(0, propertySources.size());
addInlinedPropertiesToEnvironment(environment, new String[] { " " });
assertEquals(1, propertySources.size());
assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
public void addInlinedPropertiesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("context");
addInlinedPropertiesToEnvironment((ConfigurableApplicationContext) null, KEY_VALUE_PAIR);
}
/**
* @since 4.1.5
*/
@Test
public void inlinedPropertyWithMalformedUnicodeInValue() {
public void addInlinedPropertiesToEnvironmentWithContextAndNullInlinedProperties() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("inlined");
addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), null);
}
/**
* @since 4.1.5
*/
@Test
public void addInlinedPropertiesToEnvironmentWithNullEnvironment() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("environment");
addInlinedPropertiesToEnvironment((ConfigurableEnvironment) null, KEY_VALUE_PAIR);
}
/**
* @since 4.1.5
*/
@Test
public void addInlinedPropertiesToEnvironmentWithEnvironmentAndNullInlinedProperties() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("inlined");
addInlinedPropertiesToEnvironment(new MockEnvironment(), null);
}
/**
* @since 4.1.5
*/
@Test
public void addInlinedPropertiesToEnvironmentWithMalformedUnicodeInValue() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Failed to load test environment property");
addInlinedPropertiesToEnvironment(new MockEnvironment(), new String[] { "key = \\uZZZZ" });
@@ -148,12 +176,33 @@ public class TestPropertySourceUtilsTests {
* @since 4.1.5
*/
@Test
public void inlinedPropertyWithMultipleKeyValuePairs() {
public void addInlinedPropertiesToEnvironmentWithMultipleKeyValuePairsInSingleInlinedProperty() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Failed to load exactly one test environment property");
addInlinedPropertiesToEnvironment(new MockEnvironment(), new String[] { "a=b\nx=y" });
}
/**
* @since 4.1.5
*/
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
ConfigurableEnvironment environment = new MockEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
assertEquals(0, propertySources.size());
addInlinedPropertiesToEnvironment(environment, new String[] { " " });
assertEquals(1, propertySources.size());
assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
@Test
public void convertInlinedPropertiesToMapWithNullInlinedProperties() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("inlined");
convertInlinedPropertiesToMap(null);
}
// -------------------------------------------------------------------