Class TestPropertySourceUtils

java.lang.Object
org.springframework.test.context.support.TestPropertySourceUtils

public abstract class TestPropertySourceUtils extends Object
Utility methods for working with @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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The name of the MapPropertySource created from inlined properties.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addInlinedPropertiesToEnvironment(org.springframework.context.ConfigurableApplicationContext context, String... inlinedProperties)
    Add the given inlined properties to the Environment of the supplied context.
    static void
    addInlinedPropertiesToEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, String... inlinedProperties)
    Add the given inlined properties (in the form of key-value pairs) to the supplied environment.
    static void
    addPropertiesFilesToEnvironment(org.springframework.context.ConfigurableApplicationContext context, String... locations)
    Add the Properties files from the given resource locations to the Environment of the supplied context.
    static void
    addPropertiesFilesToEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, org.springframework.core.io.ResourceLoader resourceLoader, String... locations)
    Add the Properties files from the given resource locations to the supplied environment.
    static void
    addPropertySourcesToEnvironment(org.springframework.context.ConfigurableApplicationContext context, List<org.springframework.core.io.support.PropertySourceDescriptor> descriptors)
    Add property sources for the given descriptors to the Environment of the supplied context.
    static void
    addPropertySourcesToEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, org.springframework.core.io.ResourceLoader resourceLoader, List<org.springframework.core.io.support.PropertySourceDescriptor> descriptors)
    Add property sources for the given descriptors to the supplied environment.
    static Map<String,Object>
    convertInlinedPropertiesToMap(String... inlinedProperties)
    Convert the supplied inlined properties (in the form of key-value pairs) into a map keyed by property name.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TestPropertySourceUtils

      public TestPropertySourceUtils()
  • Method Details

    • addPropertiesFilesToEnvironment

      public static void addPropertiesFilesToEnvironment(org.springframework.context.ConfigurableApplicationContext context, String... locations)
      Add the Properties files from the given resource locations to the Environment of the supplied context.

      This method delegates to addPropertiesFilesToEnvironment(ConfigurableEnvironment, ResourceLoader, String...).

      Parameters:
      context - the application context whose environment should be updated; never null
      locations - the resource locations of Properties files to add to the environment; potentially empty but never null
      Throws:
      IllegalStateException - if an error occurs while processing a properties file
      Since:
      4.1.5
      See Also:
    • addPropertiesFilesToEnvironment

      public static void addPropertiesFilesToEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, org.springframework.core.io.ResourceLoader resourceLoader, String... locations)
      Add the Properties files from the given resource locations to the supplied environment.

      Property placeholders in resource locations (i.e., ${...}) will be resolved against the Environment.

      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 the PropertySources of the environment with the highest precedence.

      Parameters:
      environment - the environment to update; never null
      resourceLoader - the ResourceLoader to use to load each resource; never null
      locations - the resource locations of Properties files to add to the environment; potentially empty but never null
      Throws:
      IllegalStateException - if an error occurs while processing a properties file
      Since:
      4.3
      See Also:
    • addPropertySourcesToEnvironment

      public static void addPropertySourcesToEnvironment(org.springframework.context.ConfigurableApplicationContext context, List<org.springframework.core.io.support.PropertySourceDescriptor> descriptors)
      Add property sources for the given descriptors to the Environment of the supplied context.

      This method delegates to addPropertySourcesToEnvironment(ConfigurableEnvironment, ResourceLoader, List).

      Parameters:
      context - the application context whose environment should be updated; never null
      descriptors - the property source descriptors to process; potentially empty but never null
      Throws:
      IllegalStateException - if an error occurs while processing the descriptors and registering property sources
      Since:
      6.1
      See Also:
    • addPropertySourcesToEnvironment

      public static void addPropertySourcesToEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, org.springframework.core.io.ResourceLoader resourceLoader, List<org.springframework.core.io.support.PropertySourceDescriptor> descriptors)
      Add property sources for the given descriptors to the supplied environment.

      Property placeholders in resource locations (i.e., ${...}) will be resolved against the Environment.

      A ResourcePatternResolver will be used to resolve resource location patterns into multiple resource locations.

      Each PropertySource will be created via the configured PropertySourceFactory (or the DefaultPropertySourceFactory if no factory is configured) and added to the PropertySources of the environment with the highest precedence.

      Parameters:
      environment - the environment to update; never null
      resourceLoader - the ResourceLoader to use to load resources; never null
      descriptors - the property source descriptors to process; potentially empty but never null
      Throws:
      IllegalStateException - if an error occurs while processing the descriptors and registering property sources
      Since:
      6.1
      See Also:
    • addInlinedPropertiesToEnvironment

      public static void addInlinedPropertiesToEnvironment(org.springframework.context.ConfigurableApplicationContext context, String... inlinedProperties)
      Add the given inlined properties to the Environment of the supplied context.

      This method simply delegates to addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]).

      Parameters:
      context - the application context whose environment should be updated; never null
      inlinedProperties - the inlined properties to add to the environment; potentially empty but never null
      Since:
      4.1.5
      See Also:
    • addInlinedPropertiesToEnvironment

      public static void addInlinedPropertiesToEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, String... inlinedProperties)
      Add the given inlined properties (in the form of key-value pairs) to the supplied environment.

      All key-value pairs will be added to the Environment as a single MapPropertySource with the highest precedence.

      For details on the parsing of inlined properties, consult the Javadoc for convertInlinedPropertiesToMap(String...).

      Parameters:
      environment - the environment to update; never null
      inlinedProperties - the inlined properties to add to the environment; potentially empty but never null
      Since:
      4.1.5
      See Also:
    • convertInlinedPropertiesToMap

      public static Map<String,Object> convertInlinedPropertiesToMap(String... inlinedProperties)
      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 never null
      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: