Skip to content

Commit d98f626

Browse files
committed
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 (cherry picked from commit 42af330)
1 parent b8dbf23 commit d98f626

File tree

3 files changed

+122
-52
lines changed

3 files changed

+122
-52
lines changed

spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ public abstract class TestPropertySourceUtils {
6060

6161
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
6262

63+
/**
64+
* The name of the {@link MapPropertySource} created from <em>inlined properties</em>.
65+
* @since 4.1.5
66+
* @see {@link #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])}
67+
*/
68+
public static final String INLINED_PROPERTIES_PROPERTY_SOURCE_NAME = "Inlined Test Properties";
69+
6370

6471
private TestPropertySourceUtils() {
6572
/* no-op */
@@ -151,19 +158,25 @@ private static String[] mergeProperties(List<TestPropertySourceAttributes> attri
151158
/**
152159
* Add the {@link Properties} files from the given resource {@code locations}
153160
* to the {@link Environment} of the supplied {@code context}.
154-
* <p>Each properties file will be converted to a {@code ResourcePropertySource}
161+
* <p>Property placeholders in resource locations (i.e., <code>${...}</code>)
162+
* will be {@linkplain Environment#resolveRequiredPlaceholders(String) resolved}
163+
* against the {@code Environment}.
164+
* <p>Each properties file will be converted to a {@link ResourcePropertySource}
155165
* that will be added to the {@link PropertySources} of the environment with
156166
* highest precedence.
157-
* @param context the application context whose environment should be updated
158-
* @param locations the resource locations of {@link Properties} files to add
159-
* to the environment
167+
* @param context the application context whose environment should be updated;
168+
* never {@code null}
169+
* @param locations the resource locations of {@code Properties} files to add
170+
* to the environment; potentially empty but never {@code null}
160171
* @since 4.1.5
161172
* @see ResourcePropertySource
162173
* @see TestPropertySource#locations
163174
* @throws IllegalStateException if an error occurs while processing a properties file
164175
*/
165176
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context,
166177
String[] locations) {
178+
Assert.notNull(context, "context must not be null");
179+
Assert.notNull(locations, "locations must not be null");
167180
try {
168181
ConfigurableEnvironment environment = context.getEnvironment();
169182
for (String location : locations) {
@@ -178,18 +191,22 @@ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContex
178191
}
179192

180193
/**
181-
* Add the given <em>inlined properties</em> (in the form of <em>key-value</em>
182-
* pairs) to the {@link Environment} of the supplied {@code context}.
194+
* Add the given <em>inlined properties</em> to the {@link Environment} of the
195+
* supplied {@code context}.
183196
* <p>This method simply delegates to
184197
* {@link #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])}.
185-
* @param context the application context whose environment should be updated
186-
* @param inlinedProperties the inlined properties to add to the environment
198+
* @param context the application context whose environment should be updated;
199+
* never {@code null}
200+
* @param inlinedProperties the inlined properties to add to the environment;
201+
* potentially empty but never {@code null}
187202
* @since 4.1.5
188203
* @see TestPropertySource#properties
189204
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
190205
*/
191206
public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context,
192207
String[] inlinedProperties) {
208+
Assert.notNull(context, "context must not be null");
209+
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
193210
addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties);
194211
}
195212

@@ -200,17 +217,25 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationCont
200217
* single {@link MapPropertySource} with the highest precedence.
201218
* <p>For details on the parsing of <em>inlined properties</em>, consult the
202219
* Javadoc for {@link #convertInlinedPropertiesToMap}.
203-
* @param environment the environment to update
204-
* @param inlinedProperties the inlined properties to add to the environment
220+
* @param environment the environment to update; never {@code null}
221+
* @param inlinedProperties the inlined properties to add to the environment;
222+
* potentially empty but never {@code null}
205223
* @since 4.1.5
206224
* @see MapPropertySource
225+
* @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
207226
* @see TestPropertySource#properties
208227
* @see #convertInlinedPropertiesToMap
209228
*/
210229
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String[] inlinedProperties) {
230+
Assert.notNull(environment, "environment must not be null");
231+
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
211232
if (!ObjectUtils.isEmpty(inlinedProperties)) {
212-
String name = "test properties " + ObjectUtils.nullSafeToString(inlinedProperties);
213-
MapPropertySource ps = new MapPropertySource(name, convertInlinedPropertiesToMap(inlinedProperties));
233+
if (logger.isDebugEnabled()) {
234+
logger.debug("Adding inlined properties to environment: "
235+
+ ObjectUtils.nullSafeToString(inlinedProperties));
236+
}
237+
MapPropertySource ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME,
238+
convertInlinedPropertiesToMap(inlinedProperties));
214239
environment.getPropertySources().addFirst(ps);
215240
}
216241
}
@@ -224,11 +249,16 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env
224249
* {@link Properties#load(java.io.Reader)} to parse each virtual file.
225250
* <p>For a full discussion of <em>inlined properties</em>, consult the Javadoc
226251
* for {@link TestPropertySource#properties}.
252+
* @param inlinedProperties the inlined properties to convert; potentially empty
253+
* but never {@code null}
254+
* @return a new, ordered map containing the converted properties
227255
* @since 4.1.5
228256
* @throws IllegalStateException if a given key-value pair cannot be parsed, or if
229257
* a given inlined property contains multiple key-value pairs
258+
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
230259
*/
231260
public static Map<String, Object> convertInlinedPropertiesToMap(String[] inlinedProperties) {
261+
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
232262
Map<String, Object> map = new LinkedHashMap<String, Object>();
233263

234264
Properties props = new Properties();

spring-test/src/test/java/org/springframework/test/context/env/InlinedPropertiesTestPropertySourceTests.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import org.springframework.context.annotation.Configuration;
2424
import org.springframework.core.env.ConfigurableEnvironment;
2525
import org.springframework.core.env.EnumerablePropertySource;
26-
import org.springframework.core.env.Environment;
27-
import org.springframework.core.env.PropertySource;
2826
import org.springframework.test.context.ContextConfiguration;
2927
import org.springframework.test.context.TestPropertySource;
3028
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3129

30+
import static org.hamcrest.CoreMatchers.*;
3231
import static org.junit.Assert.*;
32+
import static org.springframework.test.context.support.TestPropertySourceUtils.*;
3333

3434
/**
3535
* Integration tests for {@link TestPropertySource @TestPropertySource} support with
@@ -45,45 +45,36 @@
4545
public class InlinedPropertiesTestPropertySourceTests {
4646

4747
@Autowired
48-
private Environment env;
48+
private ConfigurableEnvironment env;
4949

5050

51+
private String property(String key) {
52+
return env.getProperty(key);
53+
}
54+
5155
@Test
5256
public void propertiesAreAvailableInEnvironment() {
53-
5457
// Simple key/value pairs
55-
assertEquals("bar", env.getProperty("foo"));
56-
assertEquals("quux", env.getProperty("baz"));
57-
assertEquals(42, env.getProperty("enigma", Integer.class).intValue());
58+
assertThat(property("foo"), is("bar"));
59+
assertThat(property("baz"), is("quux"));
60+
assertThat(property("enigma"), is("42"));
5861

5962
// Values containing key/value delimiters (":", "=", " ")
60-
assertEquals("a=b=c", env.getProperty("x.y.z"));
61-
assertEquals("http://example.com", env.getProperty("server.url"));
62-
assertEquals("key=value", env.getProperty("key.value.1"));
63-
assertEquals("key=value", env.getProperty("key.value.2"));
64-
assertEquals("key:value", env.getProperty("key.value.3"));
63+
assertThat(property("x.y.z"), is("a=b=c"));
64+
assertThat(property("server.url"), is("http://example.com"));
65+
assertThat(property("key.value.1"), is("key=value"));
66+
assertThat(property("key.value.2"), is("key=value"));
67+
assertThat(property("key.value.3"), is("key:value"));
6568
}
6669

6770
@Test
6871
@SuppressWarnings("rawtypes")
6972
public void propertyNameOrderingIsPreservedInEnvironment() {
70-
String[] propertyNames = null;
71-
72-
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) env;
73-
for (PropertySource<?> propertySource : configurableEnvironment.getPropertySources()) {
74-
if (propertySource instanceof EnumerablePropertySource) {
75-
EnumerablePropertySource eps = (EnumerablePropertySource) propertySource;
76-
if (eps.getName().startsWith("test properties")) {
77-
propertyNames = eps.getPropertyNames();
78-
break;
79-
}
80-
}
81-
}
82-
8373
final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url",
8474
"key.value.1", "key.value.2", "key.value.3" };
85-
86-
assertArrayEquals(expectedPropertyNames, propertyNames);
75+
EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get(
76+
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
77+
assertArrayEquals(expectedPropertyNames, eps.getPropertyNames());
8778
}
8879

8980

spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323
import org.junit.rules.ExpectedException;
2424

25+
import org.springframework.context.ConfigurableApplicationContext;
2526
import org.springframework.core.env.ConfigurableEnvironment;
2627
import org.springframework.core.env.MutablePropertySources;
2728
import org.springframework.mock.env.MockEnvironment;
@@ -30,6 +31,7 @@
3031

3132
import static org.hamcrest.CoreMatchers.*;
3233
import static org.junit.Assert.*;
34+
import static org.mockito.Mockito.mock;
3335
import static org.springframework.test.context.support.TestPropertySourceUtils.*;
3436

3537
/**
@@ -41,6 +43,7 @@
4143
public class TestPropertySourceUtilsTests {
4244

4345
private static final String[] EMPTY_STRING_ARRAY = new String[0];
46+
private static final String[] KEY_VALUE_PAIR = new String[] { "key = value" };
4447

4548
@Rule
4649
public ExpectedException expectedException = ExpectedException.none();
@@ -110,35 +113,60 @@ public void overriddenLocations() {
110113
@Test
111114
public void overriddenProperties() {
112115
assertMergedTestPropertySources(OverriddenPropertiesPropertySources.class, new String[] {
113-
"classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties" }, new String[] { "key = value" });
116+
"classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties" }, KEY_VALUE_PAIR);
114117
}
115118

116119
@Test
117120
public void overriddenLocationsAndProperties() {
118121
assertMergedTestPropertySources(OverriddenLocationsAndPropertiesPropertySources.class,
119-
new String[] { "classpath:/baz.properties" }, new String[] { "key = value" });
122+
new String[] { "classpath:/baz.properties" }, KEY_VALUE_PAIR);
120123
}
121124

122125
/**
123126
* @since 4.1.5
124127
*/
125128
@Test
126-
@SuppressWarnings("rawtypes")
127-
public void emptyInlinedProperty() {
128-
ConfigurableEnvironment environment = new MockEnvironment();
129-
MutablePropertySources propertySources = environment.getPropertySources();
130-
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
131-
assertEquals(0, propertySources.size());
132-
addInlinedPropertiesToEnvironment(environment, new String[] { " " });
133-
assertEquals(1, propertySources.size());
134-
assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
129+
public void addInlinedPropertiesToEnvironmentWithNullContext() {
130+
expectedException.expect(IllegalArgumentException.class);
131+
expectedException.expectMessage("context");
132+
addInlinedPropertiesToEnvironment((ConfigurableApplicationContext) null, KEY_VALUE_PAIR);
133+
}
134+
135+
/**
136+
* @since 4.1.5
137+
*/
138+
@Test
139+
public void addInlinedPropertiesToEnvironmentWithContextAndNullInlinedProperties() {
140+
expectedException.expect(IllegalArgumentException.class);
141+
expectedException.expectMessage("inlined");
142+
addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), null);
143+
}
144+
145+
/**
146+
* @since 4.1.5
147+
*/
148+
@Test
149+
public void addInlinedPropertiesToEnvironmentWithNullEnvironment() {
150+
expectedException.expect(IllegalArgumentException.class);
151+
expectedException.expectMessage("environment");
152+
addInlinedPropertiesToEnvironment((ConfigurableEnvironment) null, KEY_VALUE_PAIR);
153+
}
154+
155+
/**
156+
* @since 4.1.5
157+
*/
158+
@Test
159+
public void addInlinedPropertiesToEnvironmentWithEnvironmentAndNullInlinedProperties() {
160+
expectedException.expect(IllegalArgumentException.class);
161+
expectedException.expectMessage("inlined");
162+
addInlinedPropertiesToEnvironment(new MockEnvironment(), null);
135163
}
136164

137165
/**
138166
* @since 4.1.5
139167
*/
140168
@Test
141-
public void inlinedPropertyWithMalformedUnicodeInValue() {
169+
public void addInlinedPropertiesToEnvironmentWithMalformedUnicodeInValue() {
142170
expectedException.expect(IllegalStateException.class);
143171
expectedException.expectMessage("Failed to load test environment property");
144172
addInlinedPropertiesToEnvironment(new MockEnvironment(), new String[] { "key = \\uZZZZ" });
@@ -148,12 +176,33 @@ public void inlinedPropertyWithMalformedUnicodeInValue() {
148176
* @since 4.1.5
149177
*/
150178
@Test
151-
public void inlinedPropertyWithMultipleKeyValuePairs() {
179+
public void addInlinedPropertiesToEnvironmentWithMultipleKeyValuePairsInSingleInlinedProperty() {
152180
expectedException.expect(IllegalStateException.class);
153181
expectedException.expectMessage("Failed to load exactly one test environment property");
154182
addInlinedPropertiesToEnvironment(new MockEnvironment(), new String[] { "a=b\nx=y" });
155183
}
156184

185+
/**
186+
* @since 4.1.5
187+
*/
188+
@Test
189+
@SuppressWarnings("rawtypes")
190+
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
191+
ConfigurableEnvironment environment = new MockEnvironment();
192+
MutablePropertySources propertySources = environment.getPropertySources();
193+
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
194+
assertEquals(0, propertySources.size());
195+
addInlinedPropertiesToEnvironment(environment, new String[] { " " });
196+
assertEquals(1, propertySources.size());
197+
assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
198+
}
199+
200+
@Test
201+
public void convertInlinedPropertiesToMapWithNullInlinedProperties() {
202+
expectedException.expect(IllegalArgumentException.class);
203+
expectedException.expectMessage("inlined");
204+
convertInlinedPropertiesToMap(null);
205+
}
157206

158207
// -------------------------------------------------------------------
159208

0 commit comments

Comments
 (0)