@@ -60,6 +60,13 @@ public abstract class TestPropertySourceUtils {
60
60
61
61
private static final Log logger = LogFactory .getLog (TestPropertySourceUtils .class );
62
62
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
+
63
70
64
71
private TestPropertySourceUtils () {
65
72
/* no-op */
@@ -151,19 +158,25 @@ private static String[] mergeProperties(List<TestPropertySourceAttributes> attri
151
158
/**
152
159
* Add the {@link Properties} files from the given resource {@code locations}
153
160
* 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}
155
165
* that will be added to the {@link PropertySources} of the environment with
156
166
* 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}
160
171
* @since 4.1.5
161
172
* @see ResourcePropertySource
162
173
* @see TestPropertySource#locations
163
174
* @throws IllegalStateException if an error occurs while processing a properties file
164
175
*/
165
176
public static void addPropertiesFilesToEnvironment (ConfigurableApplicationContext context ,
166
177
String [] locations ) {
178
+ Assert .notNull (context , "context must not be null" );
179
+ Assert .notNull (locations , "locations must not be null" );
167
180
try {
168
181
ConfigurableEnvironment environment = context .getEnvironment ();
169
182
for (String location : locations ) {
@@ -178,18 +191,22 @@ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContex
178
191
}
179
192
180
193
/**
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}.
183
196
* <p>This method simply delegates to
184
197
* {@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}
187
202
* @since 4.1.5
188
203
* @see TestPropertySource#properties
189
204
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
190
205
*/
191
206
public static void addInlinedPropertiesToEnvironment (ConfigurableApplicationContext context ,
192
207
String [] inlinedProperties ) {
208
+ Assert .notNull (context , "context must not be null" );
209
+ Assert .notNull (inlinedProperties , "inlinedProperties must not be null" );
193
210
addInlinedPropertiesToEnvironment (context .getEnvironment (), inlinedProperties );
194
211
}
195
212
@@ -200,17 +217,25 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationCont
200
217
* single {@link MapPropertySource} with the highest precedence.
201
218
* <p>For details on the parsing of <em>inlined properties</em>, consult the
202
219
* 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}
205
223
* @since 4.1.5
206
224
* @see MapPropertySource
225
+ * @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
207
226
* @see TestPropertySource#properties
208
227
* @see #convertInlinedPropertiesToMap
209
228
*/
210
229
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" );
211
232
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 ));
214
239
environment .getPropertySources ().addFirst (ps );
215
240
}
216
241
}
@@ -224,11 +249,16 @@ public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment env
224
249
* {@link Properties#load(java.io.Reader)} to parse each virtual file.
225
250
* <p>For a full discussion of <em>inlined properties</em>, consult the Javadoc
226
251
* 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
227
255
* @since 4.1.5
228
256
* @throws IllegalStateException if a given key-value pair cannot be parsed, or if
229
257
* a given inlined property contains multiple key-value pairs
258
+ * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
230
259
*/
231
260
public static Map <String , Object > convertInlinedPropertiesToMap (String [] inlinedProperties ) {
261
+ Assert .notNull (inlinedProperties , "inlinedProperties must not be null" );
232
262
Map <String , Object > map = new LinkedHashMap <String , Object >();
233
263
234
264
Properties props = new Properties ();
0 commit comments