Class DatabaseConfigOverrides

java.lang.Object
org.dbunit.database.DatabaseConfigOverrides

public class DatabaseConfigOverrides extends Object
Property values, given as strings, to apply to DatabaseConfig instances for the length of one test, and the means to put back what they replaced.

A connection that outlives a test - a fixed connection handed to a tester, one cached by a CachingConnectionProvider, or one a test case keeps with closeConnectionAfterTest off - carries its DatabaseConfig into whatever uses it next. Applying a test's values with DatabaseConfig.setPropertiesByString(Properties) alone leaves them there; applyTo(DatabaseConfig) remembers the value each one replaced, and restore() puts those back.

Only the first value seen for a property on a given DatabaseConfig is remembered, so applying the same values to a connection several times in one test, as the lifecycle does each time it retrieves the connection, still restores what was there before the first.

Not thread-safe; a test's steps run sequentially on one thread.

Since:
3.6.0
Author:
Jeff Jensen
  • Constructor Details

    • DatabaseConfigOverrides

      public DatabaseConfigOverrides(Properties properties)
      Creates overrides for the given values.
      Parameters:
      properties - The property name and value pairs to apply, with names in the long or short forms DatabaseConfig.setPropertiesByString(Properties) accepts; copied, so later changes to it have no effect.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Returns whether there are no values to apply.
      Returns:
      true if applyTo(DatabaseConfig) would change nothing.
    • hasAppliedTo

      public boolean hasAppliedTo(DatabaseConfig config)
      Returns whether applyTo(DatabaseConfig) has changed the given configuration since the last restore().
      Parameters:
      config - The configuration to ask about.
      Returns:
      true if its values are in force and restore() would put the replaced ones back.
    • applyTo

      public void applyTo(DatabaseConfig config) throws DatabaseUnitException
      Applies the values to the given configuration, remembering each one's previous value for restore(). Does nothing when there are no values. A failure part way leaves the values already applied in force, and restore() still puts them back.
      Parameters:
      config - The configuration to apply the values to.
      Throws:
      DatabaseUnitException - If a value cannot be converted to its property's type.
    • restore

      public void restore()
      Puts back, on every configuration applyTo(DatabaseConfig) changed, the values it replaced. Does nothing when nothing was applied or it was already restored.