Skip to content

Commit 2b135f0

Browse files
committed
Open up TestPropertySourceUtils for public consumption
Spring Framework 4.1 introduced support for @TestPropertySource; however, the utilities used to parse inlined properties and add test property sources to the environment are currently private which prevents reuse by third-party frameworks like Spring Boot. This commit addresses this issue by making such utilities public. - TestPropertySourceUtils is now a public class. - Various utility methods in TestPropertySourceUtils have been made public. - addResourcePropertySourcesToEnvironment() has been renamed to addPropertiesFilesToEnvironment(). - extractEnvironmentProperties() has been renamed to convertInlinedPropertiesToMap(). - All public methods in TestPropertySourceUtils are now fully documented. Issue: SPR-12721 (cherry picked from commit 75e0bc9)
1 parent e5d41d9 commit 2b135f0

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
119119
* @param context the newly created application context
120120
* @param mergedConfig the merged context configuration
121121
* @since 3.2
122+
* @see TestPropertySourceUtils#addPropertiesFilesToEnvironment
123+
* @see TestPropertySourceUtils#addInlinedPropertiesToEnvironment
122124
* @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
123125
* @see #loadContext(MergedContextConfiguration)
124126
* @see ConfigurableApplicationContext#setId
125127
*/
126128
protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
127129
context.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles());
128-
TestPropertySourceUtils.addResourcePropertySourcesToEnvironment(context, mergedConfig.getPropertySourceLocations());
130+
TestPropertySourceUtils.addPropertiesFilesToEnvironment(context, mergedConfig.getPropertySourceLocations());
129131
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, mergedConfig.getPropertySourceProperties());
130132
invokeApplicationContextInitializers(context, mergedConfig);
131133
}

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

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import org.springframework.context.ConfigurableApplicationContext;
3232
import org.springframework.core.annotation.AnnotationAttributes;
3333
import org.springframework.core.env.ConfigurableEnvironment;
34+
import org.springframework.core.env.Environment;
3435
import org.springframework.core.env.MapPropertySource;
3536
import org.springframework.core.env.PropertySource;
37+
import org.springframework.core.env.PropertySources;
3638
import org.springframework.core.io.Resource;
3739
import org.springframework.core.io.support.ResourcePropertySource;
3840
import org.springframework.test.context.TestPropertySource;
@@ -54,7 +56,7 @@
5456
* @since 4.1
5557
* @see TestPropertySource
5658
*/
57-
abstract class TestPropertySourceUtils {
59+
public abstract class TestPropertySourceUtils {
5860

5961
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
6062

@@ -147,18 +149,27 @@ private static String[] mergeProperties(List<TestPropertySourceAttributes> attri
147149
}
148150

149151
/**
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
150160
* @since 4.1.5
161+
* @see ResourcePropertySource
162+
* @see TestPropertySource#locations
163+
* @throws IllegalStateException if an error occurs while processing a properties file
151164
*/
152-
static void addResourcePropertySourcesToEnvironment(ConfigurableApplicationContext context,
153-
String[] propertySourceLocations) {
165+
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context,
166+
String[] locations) {
154167
try {
155168
ConfigurableEnvironment environment = context.getEnvironment();
156-
String[] locations = propertySourceLocations;
157169
for (String location : locations) {
158170
String resolvedLocation = environment.resolveRequiredPlaceholders(location);
159171
Resource resource = context.getResource(resolvedLocation);
160-
ResourcePropertySource ps = new ResourcePropertySource(resource);
161-
environment.getPropertySources().addFirst(ps);
172+
environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
162173
}
163174
}
164175
catch (IOException e) {
@@ -167,36 +178,61 @@ static void addResourcePropertySourcesToEnvironment(ConfigurableApplicationConte
167178
}
168179

169180
/**
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
170187
* @since 4.1.5
188+
* @see TestPropertySource#properties
189+
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
171190
*/
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);
175194
}
176195

177196
/**
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
178205
* @since 4.1.5
206+
* @see MapPropertySource
207+
* @see TestPropertySource#properties
208+
* @see #convertInlinedPropertiesToMap
179209
*/
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));
184214
environment.getPropertySources().addFirst(ps);
185215
}
186216
}
187217

188218
/**
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
192223
* into <em>virtual</em> properties files in memory and delegating to
193224
* {@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
194230
*/
195-
private static Map<String, Object> extractEnvironmentProperties(String[] keyValuePairs) {
231+
public static Map<String, Object> convertInlinedPropertiesToMap(String[] inlinedProperties) {
196232
Map<String, Object> map = new LinkedHashMap<String, Object>();
197233

198234
Properties props = new Properties();
199-
for (String pair : keyValuePairs) {
235+
for (String pair : inlinedProperties) {
200236
if (!StringUtils.hasText(pair)) {
201237
continue;
202238
}

0 commit comments

Comments
 (0)