Class TestPropertySourceUtils
@TestPropertySource
and adding test PropertySources
to the Environment
.
Primarily intended for use within the framework.
- Since:
- 4.1
- Author:
- Sam Brannen, Anatoliy Korovin, Phillip Webb
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The name of theMapPropertySource
created from inlined properties. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addInlinedPropertiesToEnvironment
(ConfigurableApplicationContext context, String... inlinedProperties) Add the given inlined properties to theEnvironment
of the suppliedcontext
.static void
addInlinedPropertiesToEnvironment
(ConfigurableEnvironment environment, String... inlinedProperties) Add the given inlined properties (in the form of key-value pairs) to the suppliedenvironment
.static void
addPropertiesFilesToEnvironment
(ConfigurableApplicationContext context, String... locations) Add theProperties
files from the given resourcelocations
to theEnvironment
of the suppliedcontext
.static void
addPropertiesFilesToEnvironment
(ConfigurableEnvironment environment, ResourceLoader resourceLoader, String... locations) static void
addPropertySourcesToEnvironment
(ConfigurableApplicationContext context, List<PropertySourceDescriptor> descriptors) static void
addPropertySourcesToEnvironment
(ConfigurableEnvironment environment, ResourceLoader resourceLoader, List<PropertySourceDescriptor> descriptors) Add property sources for the givendescriptors
to the suppliedenvironment
.convertInlinedPropertiesToMap
(String... inlinedProperties) Convert the supplied inlined properties (in the form of key-value pairs) into a map keyed by property name.
-
Field Details
-
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
The name of theMapPropertySource
created from inlined properties.
-
-
Constructor Details
-
TestPropertySourceUtils
public TestPropertySourceUtils()
-
-
Method Details
-
addPropertiesFilesToEnvironment
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, String... locations) Add theProperties
files from the given resourcelocations
to theEnvironment
of the suppliedcontext
.This method delegates to
addPropertiesFilesToEnvironment(ConfigurableEnvironment, ResourceLoader, String...)
.- Parameters:
context
- the application context whose environment should be updated; nevernull
locations
- the resource locations ofProperties
files to add to the environment; potentially empty but nevernull
- Throws:
IllegalStateException
- if an error occurs while processing a properties file- Since:
- 4.1.5
- See Also:
-
addPropertiesFilesToEnvironment
public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, String... locations) Add theProperties
files from the given resourcelocations
to the suppliedenvironment
.Property placeholders in resource locations (i.e.,
${...}
) will be resolved against theEnvironment
.A
ResourcePatternResolver
will be used to resolve resource location patterns into multiple resource locations.Each properties file will be converted to a
ResourcePropertySource
that will be added to thePropertySources
of the environment with the highest precedence.- Parameters:
environment
- the environment to update; nevernull
resourceLoader
- theResourceLoader
to use to load each resource; nevernull
locations
- the resource locations ofProperties
files to add to the environment; potentially empty but nevernull
- Throws:
IllegalStateException
- if an error occurs while processing a properties file- Since:
- 4.3
- See Also:
-
addPropertySourcesToEnvironment
public static void addPropertySourcesToEnvironment(ConfigurableApplicationContext context, List<PropertySourceDescriptor> descriptors) Add property sources for the givendescriptors
to theEnvironment
of the suppliedcontext
.This method delegates to
addPropertySourcesToEnvironment(ConfigurableEnvironment, ResourceLoader, List)
.- Parameters:
context
- the application context whose environment should be updated; nevernull
descriptors
- the property source descriptors to process; potentially empty but nevernull
- Throws:
IllegalStateException
- if an error occurs while processing the descriptors and registering property sources- Since:
- 6.1
- See Also:
-
addPropertySourcesToEnvironment
public static void addPropertySourcesToEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, List<PropertySourceDescriptor> descriptors) Add property sources for the givendescriptors
to the suppliedenvironment
.Property placeholders in resource locations (i.e.,
${...}
) will be resolved against theEnvironment
.A
ResourcePatternResolver
will be used to resolve resource location patterns into multiple resource locations.Each
PropertySource
will be created via the configuredPropertySourceFactory
(or theDefaultPropertySourceFactory
if no factory is configured) and added to thePropertySources
of the environment with the highest precedence.- Parameters:
environment
- the environment to update; nevernull
resourceLoader
- theResourceLoader
to use to load resources; nevernull
descriptors
- the property source descriptors to process; potentially empty but nevernull
- Throws:
IllegalStateException
- if an error occurs while processing the descriptors and registering property sources- Since:
- 6.1
- See Also:
-
addInlinedPropertiesToEnvironment
public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, String... inlinedProperties) Add the given inlined properties to theEnvironment
of the suppliedcontext
.This method simply delegates to
addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
.- Parameters:
context
- the application context whose environment should be updated; nevernull
inlinedProperties
- the inlined properties to add to the environment; potentially empty but nevernull
- Since:
- 4.1.5
- See Also:
-
addInlinedPropertiesToEnvironment
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String... inlinedProperties) Add the given inlined properties (in the form of key-value pairs) to the suppliedenvironment
.All key-value pairs will be added to the
Environment
as a singleMapPropertySource
with the highest precedence.For details on the parsing of inlined properties, consult the Javadoc for
convertInlinedPropertiesToMap(java.lang.String...)
.- Parameters:
environment
- the environment to update; nevernull
inlinedProperties
- the inlined properties to add to the environment; potentially empty but nevernull
- Since:
- 4.1.5
- See Also:
-
convertInlinedPropertiesToMap
Convert the supplied inlined properties (in the form of key-value pairs) into a map keyed by property name.Parsing of the key-value pairs is achieved by converting all supplied strings into virtual properties files in memory and delegating to
Properties.load(java.io.Reader)
to parse each virtual file.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. This also applies if a single string contains multiple key-value pairs separated by newlines — for example, when supplied by a user via a text block.
For a full discussion of inlined properties, consult the Javadoc for
TestPropertySource.properties()
.- Parameters:
inlinedProperties
- the inlined properties to convert; potentially empty but nevernull
- Returns:
- a new, ordered map containing the converted properties
- Throws:
IllegalStateException
- if a given key-value pair cannot be parsed- Since:
- 4.1.5
- See Also:
-