diff options
Diffstat (limited to 'spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java')
-rw-r--r-- | spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java | 145 |
1 files changed, 93 insertions, 52 deletions
diff --git a/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java b/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java index 8f6c8d7e..b1d7ddfd 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,13 +26,17 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.annotation.AnnotationConfigurationException; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.ResourceLoader; import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockPropertySource; import org.springframework.test.context.TestPropertySource; import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.*; import static org.springframework.test.context.support.TestPropertySourceUtils.*; /** @@ -44,20 +48,16 @@ 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" }; + + private static final String[] KEY_VALUE_PAIR = new String[] {"key = value"}; + + private static final String[] FOO_LOCATIONS = new String[] {"classpath:/foo.properties"}; + @Rule public ExpectedException expectedException = ExpectedException.none(); - private void assertMergedTestPropertySources(Class<?> testClass, String[] expectedLocations, - String[] expectedProperties) { - MergedTestPropertySources mergedPropertySources = buildMergedTestPropertySources(testClass); - assertNotNull(mergedPropertySources); - assertArrayEquals(expectedLocations, mergedPropertySources.getLocations()); - assertArrayEquals(expectedProperties, mergedPropertySources.getProperties()); - } - @Test public void emptyAnnotation() { expectedException.expect(IllegalStateException.class); @@ -76,8 +76,8 @@ public class TestPropertySourceUtilsTests { @Test public void value() { - assertMergedTestPropertySources(ValuePropertySources.class, new String[] { "classpath:/value.xml" }, - EMPTY_STRING_ARRAY); + assertMergedTestPropertySources(ValuePropertySources.class, asArray("classpath:/value.xml"), + EMPTY_STRING_ARRAY); } @Test @@ -88,44 +88,88 @@ public class TestPropertySourceUtilsTests { @Test public void locationsAndProperties() { - assertMergedTestPropertySources(LocationsAndPropertiesPropertySources.class, new String[] { - "classpath:/foo1.xml", "classpath:/foo2.xml" }, new String[] { "k1a=v1a", "k1b: v1b" }); + assertMergedTestPropertySources(LocationsAndPropertiesPropertySources.class, + asArray("classpath:/foo1.xml", "classpath:/foo2.xml"), asArray("k1a=v1a", "k1b: v1b")); } @Test public void inheritedLocationsAndProperties() { - assertMergedTestPropertySources(InheritedPropertySources.class, new String[] { "classpath:/foo1.xml", - "classpath:/foo2.xml" }, new String[] { "k1a=v1a", "k1b: v1b" }); + assertMergedTestPropertySources(InheritedPropertySources.class, + asArray("classpath:/foo1.xml", "classpath:/foo2.xml"), asArray("k1a=v1a", "k1b: v1b")); } @Test public void extendedLocationsAndProperties() { - assertMergedTestPropertySources(ExtendedPropertySources.class, new String[] { "classpath:/foo1.xml", - "classpath:/foo2.xml", "classpath:/bar1.xml", "classpath:/bar2.xml" }, new String[] { "k1a=v1a", - "k1b: v1b", "k2a v2a", "k2b: v2b" }); + assertMergedTestPropertySources(ExtendedPropertySources.class, + asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/bar1.xml", "classpath:/bar2.xml"), + asArray("k1a=v1a", "k1b: v1b", "k2a v2a", "k2b: v2b")); } @Test public void overriddenLocations() { assertMergedTestPropertySources(OverriddenLocationsPropertySources.class, - new String[] { "classpath:/baz.properties" }, new String[] { "k1a=v1a", "k1b: v1b", "key = value" }); + asArray("classpath:/baz.properties"), asArray("k1a=v1a", "k1b: v1b", "key = value")); } @Test public void overriddenProperties() { - assertMergedTestPropertySources(OverriddenPropertiesPropertySources.class, new String[] { - "classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties" }, KEY_VALUE_PAIR); + assertMergedTestPropertySources(OverriddenPropertiesPropertySources.class, + asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties"), KEY_VALUE_PAIR); } @Test public void overriddenLocationsAndProperties() { assertMergedTestPropertySources(OverriddenLocationsAndPropertiesPropertySources.class, - new String[] { "classpath:/baz.properties" }, KEY_VALUE_PAIR); + asArray("classpath:/baz.properties"), KEY_VALUE_PAIR); + } + + + @Test + public void addPropertiesFilesToEnvironmentWithNullContext() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("must not be null"); + addPropertiesFilesToEnvironment((ConfigurableApplicationContext) null, FOO_LOCATIONS); + } + + @Test + public void addPropertiesFilesToEnvironmentWithContextAndNullLocations() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("must not be null"); + addPropertiesFilesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null); + } + + @Test + public void addPropertiesFilesToEnvironmentWithNullEnvironment() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("must not be null"); + addPropertiesFilesToEnvironment((ConfigurableEnvironment) null, mock(ResourceLoader.class), FOO_LOCATIONS); + } + + @Test + public void addPropertiesFilesToEnvironmentWithEnvironmentAndNullLocations() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("must not be null"); + addPropertiesFilesToEnvironment(new MockEnvironment(), mock(ResourceLoader.class), (String[]) null); + } + + @Test + public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() { + ConfigurableEnvironment environment = new MockEnvironment(); + + MutablePropertySources propertySources = environment.getPropertySources(); + propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); + assertEquals(0, propertySources.size()); + + String pair = "key = value"; + ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair); + ResourceLoader resourceLoader = mock(ResourceLoader.class); + when(resourceLoader.getResource(anyString())).thenReturn(resource); + + addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS); + assertEquals(1, propertySources.size()); + assertEquals("value", environment.getProperty("key")); } - /** - * @since 4.1.5 - */ @Test public void addInlinedPropertiesToEnvironmentWithNullContext() { expectedException.expect(IllegalArgumentException.class); @@ -133,19 +177,13 @@ public class TestPropertySourceUtilsTests { addInlinedPropertiesToEnvironment((ConfigurableApplicationContext) null, KEY_VALUE_PAIR); } - /** - * @since 4.1.5 - */ @Test public void addInlinedPropertiesToEnvironmentWithContextAndNullInlinedProperties() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("inlined"); - addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), null); + addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null); } - /** - * @since 4.1.5 - */ @Test public void addInlinedPropertiesToEnvironmentWithNullEnvironment() { expectedException.expect(IllegalArgumentException.class); @@ -153,39 +191,27 @@ public class TestPropertySourceUtilsTests { 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); + addInlinedPropertiesToEnvironment(new MockEnvironment(), (String[]) 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" }); + addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("key = \\uZZZZ")); } - /** - * @since 4.1.5 - */ @Test 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" }); + addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("a=b\nx=y")); } - /** - * @since 4.1.5 - */ @Test @SuppressWarnings("rawtypes") public void addInlinedPropertiesToEnvironmentWithEmptyProperty() { @@ -193,7 +219,7 @@ public class TestPropertySourceUtilsTests { MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); - addInlinedPropertiesToEnvironment(environment, new String[] { " " }); + addInlinedPropertiesToEnvironment(environment, asArray(" ")); assertEquals(1, propertySources.size()); assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size()); } @@ -202,10 +228,25 @@ public class TestPropertySourceUtilsTests { public void convertInlinedPropertiesToMapWithNullInlinedProperties() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("inlined"); - convertInlinedPropertiesToMap(null); + convertInlinedPropertiesToMap((String[]) null); + } + + + private static void assertMergedTestPropertySources(Class<?> testClass, String[] expectedLocations, + String[] expectedProperties) { + + MergedTestPropertySources mergedPropertySources = buildMergedTestPropertySources(testClass); + assertNotNull(mergedPropertySources); + assertArrayEquals(expectedLocations, mergedPropertySources.getLocations()); + assertArrayEquals(expectedProperties, mergedPropertySources.getProperties()); + } + + + @SafeVarargs + private static <T> T[] asArray(T... arr) { + return arr; } - // ------------------------------------------------------------------- @TestPropertySource static class EmptyPropertySources { |