Class RowCountChecker
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcapture(IDatabaseConnection connection) Captures the baseline using the given connection, resolving aRowCountCheckfrom itsDatabaseConfigfirst if none has been resolved or set yet.voidClears the enabled flag and excluded table patternssetEnabledOverride(boolean, String[])set, returning to resolving aRowCountCheckfrom a connection's ownDatabaseConfig.voidDiscards the captured baseline, so a laterverify(IDatabaseConnection)call skips silently instead of comparing against it.Returns theRowCountCheckin use.booleanReturns whether a baseline is currently held, so a caller can tell there is nothing to verify - e.g. to skip acquiring a connection forverify(IDatabaseConnection)entirely - without needing one just to ask.voidsetEnabledOverride(boolean enabled, String[] exclude) Sets the enabled flag and excluded table patterns to resolve aRowCountCheckfrom, instead of a connection's ownDatabaseConfig- the values an annotation such as@DbUnitRowCountCheckdeclares, 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 itsRowCounter.voidsetRowCountCheck(RowCountCheck rowCountCheck) Sets theRowCountCheckto use, overriding the one otherwise lazily built from a connection's DatabaseConfig on first use.voidverify(IDatabaseConnection connection) Verifies the captured baseline against the given connection's current row counts.
-
Constructor Details
-
RowCountChecker
public RowCountChecker()
-
-
Method Details
-
capture
Captures the baseline using the given connection, resolving aRowCountCheckfrom itsDatabaseConfigfirst if none has been resolved or set yet. UnlesssetRowCountCheck(RowCountCheck)pinned one in place explicitly, any previously resolvedRowCountCheckis discarded first, so this capture re-resolves from the currentsetEnabledOverride(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
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, ordiscardBaseline()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 laterverify(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 forverify(IDatabaseConnection)entirely - without needing one just to ask.- Returns:
truewhen a baseline was captured and neither consumed bydiscardBaseline()nor left uncaptured because the check was disabled.
-
getRowCountCheck
Returns theRowCountCheckin use.- Returns:
- the row count check, or
nullif none has been resolved or set yet.
-
setRowCountCheck
Sets theRowCountCheckto use, overriding the one otherwise lazily built from a connection's DatabaseConfig on first use.- Parameters:
rowCountCheck- the row count check to use.
-
setEnabledOverride
Sets the enabled flag and excluded table patterns to resolve aRowCountCheckfrom, instead of a connection's ownDatabaseConfig- the values an annotation such as@DbUnitRowCountCheckdeclares, 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 itsRowCounter.- Parameters:
enabled- Whether the check is enabled.exclude- The excluded table patterns;nullis treated as empty (excludes none).- Since:
- 3.6.0
-
clearEnabledOverride
public void clearEnabledOverride()Clears the enabled flag and excluded table patternssetEnabledOverride(boolean, String[])set, returning to resolving aRowCountCheckfrom a connection's ownDatabaseConfig.A caller sharing one
RowCountCheckerinstance across several tests - e.g. aDefaultPrepAndExpectedTestCaseheld by a@DbUnitTestCasestatic 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
-