Skip to content

Add regular-expression ValueComparer #973

Description

@jeffjensen

Background

org.dbunit.assertion.comparer.value has ValueComparer implementations for equality, ordering, numeric/timestamp tolerance, null checks, and literal substring containment (IsActualContainingExpectedStringValueComparer), but none for matching a column value against a regular expression.

Regex matching is the natural fit for verifying values that are correct in shape but not in exact content:

  • database-generated surrogate keys ([0-9]+)
  • UUID / GUID columns stored as text
  • formatted identifiers (order numbers, SKUs, account numbers)
  • timestamps or dates rendered into a text column
  • any value where only the format, not the exact content, is under the test's control

Today each project has to write its own ValueComparer for this.

Proposed design

Add org.dbunit.assertion.comparer.value.RegularExpressionValueComparer:

  • The expected value (from the expected dataset) is the regular expression; the actual value from the database, converted to a String, is tested against it. Same "expected value carries the comparison criterion" model as IsActualContainingExpectedStringValueComparer.
  • Matches when the pattern matches the entire actual value (java.util.regex.Matcher.matches()), consistent with String.matches(), AssertJ .matches(), and Hamcrest matchesPattern(). A caller wanting a partial match wraps the pattern in .*.
  • Null handling mirrors the sibling comparers: both null match; exactly one null does not.
  • An invalid regex in the expected value throws DatabaseUnitException (wrapping PatternSyntaxException) with row/column context, mirroring how IsActualEqualToExpectedJsonValueComparer reports an unparseable expected value.
  • No new dependency (java.util.regex is part of the JDK), so it is also exposed as a ValueComparers constant, unlike the Jackson-backed JSON comparer.

Docs

Update datacomparisons/valuecomparer.adoc and add a changes.xml entry.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: assertionDbUnitAssert, ValueComparer, Difference, DifferenceListener

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions