31
31
import org .springframework .context .ConfigurableApplicationContext ;
32
32
import org .springframework .core .annotation .AnnotationAttributes ;
33
33
import org .springframework .core .env .ConfigurableEnvironment ;
34
+ import org .springframework .core .env .Environment ;
34
35
import org .springframework .core .env .MapPropertySource ;
35
36
import org .springframework .core .env .PropertySource ;
37
+ import org .springframework .core .env .PropertySources ;
36
38
import org .springframework .core .io .Resource ;
37
39
import org .springframework .core .io .support .ResourcePropertySource ;
38
40
import org .springframework .test .context .TestPropertySource ;
54
56
* @since 4.1
55
57
* @see TestPropertySource
56
58
*/
57
- abstract class TestPropertySourceUtils {
59
+ public abstract class TestPropertySourceUtils {
58
60
59
61
private static final Log logger = LogFactory .getLog (TestPropertySourceUtils .class );
60
62
@@ -147,18 +149,27 @@ private static String[] mergeProperties(List<TestPropertySourceAttributes> attri
147
149
}
148
150
149
151
/**
152
+ * Add the {@link Properties} files from the given resource {@code locations}
153
+ * to the {@link Environment} of the supplied {@code context}.
154
+ * <p>Each properties file will be converted to a {@code ResourcePropertySource}
155
+ * that will be added to the {@link PropertySources} of the environment with
156
+ * 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
150
160
* @since 4.1.5
161
+ * @see ResourcePropertySource
162
+ * @see TestPropertySource#locations
163
+ * @throws IllegalStateException if an error occurs while processing a properties file
151
164
*/
152
- static void addResourcePropertySourcesToEnvironment (ConfigurableApplicationContext context ,
153
- String [] propertySourceLocations ) {
165
+ public static void addPropertiesFilesToEnvironment (ConfigurableApplicationContext context ,
166
+ String [] locations ) {
154
167
try {
155
168
ConfigurableEnvironment environment = context .getEnvironment ();
156
- String [] locations = propertySourceLocations ;
157
169
for (String location : locations ) {
158
170
String resolvedLocation = environment .resolveRequiredPlaceholders (location );
159
171
Resource resource = context .getResource (resolvedLocation );
160
- ResourcePropertySource ps = new ResourcePropertySource (resource );
161
- environment .getPropertySources ().addFirst (ps );
172
+ environment .getPropertySources ().addFirst (new ResourcePropertySource (resource ));
162
173
}
163
174
}
164
175
catch (IOException e ) {
@@ -167,36 +178,61 @@ static void addResourcePropertySourcesToEnvironment(ConfigurableApplicationConte
167
178
}
168
179
169
180
/**
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}.
183
+ * <p>This method simply delegates to
184
+ * {@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
170
187
* @since 4.1.5
188
+ * @see TestPropertySource#properties
189
+ * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
171
190
*/
172
- static void addInlinedPropertiesToEnvironment (ConfigurableApplicationContext context ,
173
- String [] propertySourceProperties ) {
174
- addInlinedPropertiesToEnvironment (context .getEnvironment (), propertySourceProperties );
191
+ public static void addInlinedPropertiesToEnvironment (ConfigurableApplicationContext context ,
192
+ String [] inlinedProperties ) {
193
+ addInlinedPropertiesToEnvironment (context .getEnvironment (), inlinedProperties );
175
194
}
176
195
177
196
/**
197
+ * Add the given <em>inlined properties</em> (in the form of <em>key-value</em>
198
+ * pairs) to the supplied {@link ConfigurableEnvironment environment}.
199
+ * <p>All key-value pairs will be added to the {@code Environment} as a
200
+ * single {@link MapPropertySource} with the highest precedence.
201
+ * <p>For details on the parsing of <em>inlined properties</em>, consult the
202
+ * Javadoc for {@link #convertInlinedPropertiesToMap}.
203
+ * @param environment the environment to update
204
+ * @param inlinedProperties the inlined properties to add to the environment
178
205
* @since 4.1.5
206
+ * @see MapPropertySource
207
+ * @see TestPropertySource#properties
208
+ * @see #convertInlinedPropertiesToMap
179
209
*/
180
- static void addInlinedPropertiesToEnvironment (ConfigurableEnvironment environment , String [] propertySourceProperties ) {
181
- if (!ObjectUtils .isEmpty (propertySourceProperties )) {
182
- String name = "test properties " + ObjectUtils .nullSafeToString (propertySourceProperties );
183
- MapPropertySource ps = new MapPropertySource (name , extractEnvironmentProperties ( propertySourceProperties ));
210
+ public static void addInlinedPropertiesToEnvironment (ConfigurableEnvironment environment , String [] inlinedProperties ) {
211
+ if (!ObjectUtils .isEmpty (inlinedProperties )) {
212
+ String name = "test properties " + ObjectUtils .nullSafeToString (inlinedProperties );
213
+ MapPropertySource ps = new MapPropertySource (name , convertInlinedPropertiesToMap ( inlinedProperties ));
184
214
environment .getPropertySources ().addFirst (ps );
185
215
}
186
216
}
187
217
188
218
/**
189
- * Extract environment properties from the supplied key/value pairs,
190
- * preserving the ordering of property names in the returned map.
191
- * <p>Parsing of the key/value pairs is achieved by converting all pairs
219
+ * Convert the supplied <em>inlined properties</em> (in the form of <em>key-value</em>
220
+ * pairs) into a map keyed by property name, preserving the ordering of property names
221
+ * in the returned map.
222
+ * <p>Parsing of the key-value pairs is achieved by converting all pairs
192
223
* into <em>virtual</em> properties files in memory and delegating to
193
224
* {@link Properties#load(java.io.Reader)} to parse each virtual file.
225
+ * <p>For a full discussion of <em>inlined properties</em>, consult the Javadoc
226
+ * for {@link TestPropertySource#properties}.
227
+ * @since 4.1.5
228
+ * @throws IllegalStateException if a given key-value pair cannot be parsed, or if
229
+ * a given inlined property contains multiple key-value pairs
194
230
*/
195
- private static Map <String , Object > extractEnvironmentProperties (String [] keyValuePairs ) {
231
+ public static Map <String , Object > convertInlinedPropertiesToMap (String [] inlinedProperties ) {
196
232
Map <String , Object > map = new LinkedHashMap <String , Object >();
197
233
198
234
Properties props = new Properties ();
199
- for (String pair : keyValuePairs ) {
235
+ for (String pair : inlinedProperties ) {
200
236
if (!StringUtils .hasText (pair )) {
201
237
continue ;
202
238
}
0 commit comments