Support text blocks for inlined properties in @TestPropertySource
Prior to this commit, inlined properties could only be supplied as an
array of Strings as follows.
@TestPropertySource(properties = {
"key1 = value1",
"key2 = value2"
})
Although a user could supply a text block, it was previously rejected
due to a "single key-value pair per string" check in
TestPropertySourceUtils.convertInlinedPropertiesToMap(String...).
This commit removes that restriction and allows the above example to be
refactored to use a text block as follows.
@TestPropertySource(properties = """
key1 = value1
key2 = value2
"""
)
Closes gh-31053
This commit is contained in:
@@ -211,7 +211,8 @@ public @interface TestPropertySource {
|
||||
* {@link org.springframework.core.env.Environment Environment} before the
|
||||
* {@code ApplicationContext} is loaded for the test. All key-value pairs
|
||||
* will be added to the enclosing {@code Environment} as a single test
|
||||
* {@code PropertySource} with the highest precedence.
|
||||
* {@code PropertySource} with the highest precedence. As of Spring Framework
|
||||
* 6.1, multiple key-value pairs may be specified via a single <em>text block</em>.
|
||||
* <h4>Supported Syntax</h4>
|
||||
* <p>The supported syntax for key-value pairs is the same as the
|
||||
* syntax defined for entries in a Java
|
||||
@@ -221,6 +222,28 @@ public @interface TestPropertySource {
|
||||
* <li>{@code "key:value"}</li>
|
||||
* <li>{@code "key value"}</li>
|
||||
* </ul>
|
||||
* <h4>Examples</h4>
|
||||
* <pre class="code">
|
||||
* // Using an array of strings
|
||||
* @TestPropertySource(properties = {
|
||||
* "key1 = value1",
|
||||
* "key2 = value2"
|
||||
* })
|
||||
* @ContextConfiguration
|
||||
* class MyTests {
|
||||
* // ...
|
||||
* }</pre>
|
||||
* <pre class="code">
|
||||
* // Using a single text block
|
||||
* @TestPropertySource(properties = """
|
||||
* key1 = value1
|
||||
* key2 = value2
|
||||
* """
|
||||
* )
|
||||
* @ContextConfiguration
|
||||
* class MyTests {
|
||||
* // ...
|
||||
* }</pre>
|
||||
* <h4>Precedence</h4>
|
||||
* <p>Properties declared via this attribute have higher precedence than
|
||||
* properties loaded from resource {@link #locations}.
|
||||
|
||||
@@ -367,18 +367,22 @@ public abstract class TestPropertySourceUtils {
|
||||
|
||||
/**
|
||||
* Convert the supplied <em>inlined properties</em> (in the form of <em>key-value</em>
|
||||
* pairs) into a map keyed by property name, preserving the ordering of property names
|
||||
* in the returned map.
|
||||
* <p>Parsing of the key-value pairs is achieved by converting all pairs
|
||||
* into <em>virtual</em> properties files in memory and delegating to
|
||||
* pairs) into a map keyed by property name.
|
||||
* <p>Parsing of the key-value pairs is achieved by converting all supplied
|
||||
* strings into <em>virtual</em> properties files in memory and delegating to
|
||||
* {@link Properties#load(java.io.Reader)} to parse each virtual file.
|
||||
* <p>Generally speaking, the ordering of property names will be preserved in
|
||||
* the returned map, analogous to the order in which the key-value pairs are
|
||||
* supplied to this method. However, if a single string contains multiple
|
||||
* key-value pairs separated by newlines — for example, when supplied by
|
||||
* a user via a <em>text block</em> — the ordering of property names for
|
||||
* those particular key-value pairs cannot be guaranteed in the returned map.
|
||||
* <p>For a full discussion of <em>inlined properties</em>, consult the Javadoc
|
||||
* for {@link TestPropertySource#properties}.
|
||||
* @param inlinedProperties the inlined properties to convert; potentially empty
|
||||
* but never {@code null}
|
||||
* @return a new, ordered map containing the converted properties
|
||||
* @throws IllegalStateException if a given key-value pair cannot be parsed, or if
|
||||
* a given inlined property contains multiple key-value pairs
|
||||
* @throws IllegalStateException if a given key-value pair cannot be parsed
|
||||
* @since 4.1.5
|
||||
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
|
||||
*/
|
||||
@@ -395,9 +399,8 @@ public abstract class TestPropertySourceUtils {
|
||||
props.load(new StringReader(pair));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Failed to load test environment property from [" + pair + "]", ex);
|
||||
throw new IllegalStateException("Failed to load test environment properties from [" + pair + "]", ex);
|
||||
}
|
||||
Assert.state(props.size() == 1, () -> "Failed to load exactly one test environment property from [" + pair + "]");
|
||||
for (String name : props.stringPropertyNames()) {
|
||||
map.put(name, props.getProperty(name));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user