Class RowCountChecker

java.lang.Object
org.dbunit.database.rowcount.RowCountChecker

public class RowCountChecker extends Object
Manages a RowCountCheck baseline across one caller's test lifecycle: lazily resolves a RowCountCheck on first use - from a connection's DatabaseConfig by default, layering an enabled flag and excluded table patterns onto it instead when setEnabledOverride(boolean, String[]) was called, or bypassing resolution entirely when setRowCountCheck(RowCountCheck) was - captures a baseline, verifies it later, and lets the baseline be discarded when a verify would be noise - e.g. the caller's own test steps already failed, so the database is in an unknown state and a count difference is not a finding worth its own report.

Holds no connection of its own; every method takes the connection to use, leaving acquisition and closing entirely to the caller.

Safe to reuse across more than one test's lifecycle - e.g. one instance held by a DefaultPrepAndExpectedTestCase shared through a @DbUnitTestCase static field: each capture(IDatabaseConnection) call re-resolves the RowCountCheck from the current setEnabledOverride(boolean, String[])/ connection-config state rather than reusing whatever an earlier test resolved, unless setRowCountCheck(RowCountCheck) pinned one in place explicitly - that one is used as-is for every later test too, until changed. A caller sharing one instance across tests must call clearEnabledOverride() for a test that declares no override of its own, so an earlier test's override does not silently carry over.

Since:
3.6.0
Author:
Jeff Jensen
  • Constructor Details

    • RowCountChecker

      public RowCountChecker()
  • Method Details

    • capture

      public void capture(IDatabaseConnection connection) throws DatabaseUnitException, SQLException
      Captures the baseline using the given connection, resolving a RowCountCheck from its DatabaseConfig first if none has been resolved or set yet. Unless setRowCountCheck(RowCountCheck) pinned one in place explicitly, any previously resolved RowCountCheck is discarded first, so this capture re-resolves from the current setEnabledOverride(boolean, String[])/ connection-config state instead of reusing a stale resolution left over from an earlier test that reused this same instance.
      Parameters:
      connection - the connection to capture the baseline from.
      Throws:
      DatabaseUnitException - if enumerating or filtering the tables fails.
      SQLException - if counting a table's rows fails.
    • verify

      public void verify(IDatabaseConnection connection) throws DatabaseUnitException, SQLException
      Verifies the captured baseline against the given connection's current row counts. A no-op that never queries the connection when no baseline was captured - the check is disabled, capture(IDatabaseConnection) was never called, or discardBaseline() was.
      Parameters:
      connection - the connection to read the current row counts from.
      Throws:
      DatabaseUnitException - if enumerating or filtering the tables fails, or if any table's row count no longer matches the baseline (UnexpectedRowCountException).
      SQLException - if counting a table's rows fails.
    • discardBaseline

      public void discardBaseline()
      Discards the captured baseline, so a later verify(IDatabaseConnection) call skips silently instead of comparing against it.
    • hasBaseline

      public boolean hasBaseline()
      Returns whether a baseline is currently held, so a caller can tell there is nothing to verify - e.g. to skip acquiring a connection for verify(IDatabaseConnection) entirely - without needing one just to ask.
      Returns:
      true when a baseline was captured and neither consumed by discardBaseline() nor left uncaptured because the check was disabled.
    • getRowCountCheck

      public RowCountCheck getRowCountCheck()
      Returns the RowCountCheck in use.
      Returns:
      the row count check, or null if none has been resolved or set yet.
    • setRowCountCheck

      public void setRowCountCheck(RowCountCheck rowCountCheck)
      Sets the RowCountCheck to use, overriding the one otherwise lazily built from a connection's DatabaseConfig on first use.
      Parameters:
      rowCountCheck - the row count check to use.
    • setEnabledOverride

      public void setEnabledOverride(boolean enabled, String[] exclude)
      Sets the enabled flag and excluded table patterns to resolve a RowCountCheck from, instead of a connection's own DatabaseConfig - the values an annotation such as @DbUnitRowCountCheck declares, carried over onto whatever connection this checker ends up resolving one from, rather than a caller having to resolve a connection of its own just to read its RowCounter.
      Parameters:
      enabled - Whether the check is enabled.
      exclude - The excluded table patterns; null is treated as empty (excludes none).
      Since:
      3.6.0
    • clearEnabledOverride

      public void clearEnabledOverride()
      Clears the enabled flag and excluded table patterns setEnabledOverride(boolean, String[]) set, returning to resolving a RowCountCheck from a connection's own DatabaseConfig.

      A caller sharing one RowCountChecker instance across several tests - e.g. a DefaultPrepAndExpectedTestCase held by a @DbUnitTestCase static field - must call this for a test that declares no override of its own, so an earlier test's override does not silently carry over onto this one.

      Since:
      3.6.0