Class RegularExpressionValueComparer
- All Implemented Interfaces:
ValueComparer
ValueComparer implementation that verifies the actual value matches
the regular expression supplied as the expected value.
The expected value, converted to a String, is the
regular expression; the actual value, also
converted to a String, is the input tested against it. This mirrors
how IsActualContainingExpectedStringValueComparer treats the expected
value as the substring to look for.
The comparison succeeds only when the pattern matches the entire
actual value, using Matcher.matches(), consistent with
String.matches(String). To match only part of the actual value, make
the pattern permissive at both ends, for example .*[0-9]{4}.*.
A . does not match a line terminator unless the
DOTALL flag is set, so prefix such a pattern with
(?s) when the actual value may contain a newline, for example
(?s).*[0-9]{4}.*.
Useful for columns whose exact content a test does not control but whose format it does, such as database-generated identifiers, UUID columns, or timestamps rendered into a text column.
Special case: if both values are null, they match; if exactly one is null, they do not.
This comparer adds no dependency beyond java.util.regex, so it is also
available as ValueComparers.regularExpressionValueComparer.
- Since:
- 3.6.0
- Author:
- Jeff Jensen
-
Field Summary
Fields inherited from class org.dbunit.assertion.comparer.value.ValueComparerBase
BASE_FAIL_MSG -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringReturns the text snippet for substitution inValueComparerBase.BASE_FAIL_MSG.protected booleanisExpected(ITable expectedTable, ITable actualTable, int rowNum, String columnName, DataType dataType, Object expectedValue, Object actualValue) Determines whether the actual value compares as expected against the expected value.protected booleanisMatching(int rowNum, String columnName, Object expectedValue, Object actualValue) Returns whether the regular expression held in the expected value matches the whole actual value, both converted to strings.Methods inherited from class org.dbunit.assertion.comparer.value.ValueComparerTemplateBase
doCompare, makeFailMessageMethods inherited from class org.dbunit.assertion.comparer.value.ValueComparerBase
compare, toString
-
Constructor Details
-
RegularExpressionValueComparer
public RegularExpressionValueComparer()
-
-
Method Details
-
isExpected
protected boolean isExpected(ITable expectedTable, ITable actualTable, int rowNum, String columnName, DataType dataType, Object expectedValue, Object actualValue) throws DatabaseUnitException Description copied from class:ValueComparerTemplateBaseDetermines whether the actual value compares as expected against the expected value.- Specified by:
isExpectedin classValueComparerTemplateBase- Parameters:
expectedTable- Table containing all expected results.actualTable- Table containing all actual results.rowNum- The current row number comparing.columnName- The name of the current column comparing.dataType- TheDataTypefor the current column comparing.expectedValue- The current expected value for the column.actualValue- The current actual value for the column.- Returns:
- true if comparing actual to expected is as expected.
- Throws:
DatabaseUnitException- if the comparison cannot be performed.
-
isMatching
protected boolean isMatching(int rowNum, String columnName, Object expectedValue, Object actualValue) throws DatabaseUnitException Returns whether the regular expression held in the expected value matches the whole actual value, both converted to strings.- Parameters:
rowNum- The current row number comparing, used only to identify an invalid pattern.columnName- The name of the current column comparing, used only to identify an invalid pattern.expectedValue- The expected value, holding the regular expression.actualValue- The actual value tested against the regular expression.- Returns:
trueif the regular expression matches the entire actual value string.- Throws:
DatabaseUnitException- If either value cannot be converted to a string, or the expected value is not a valid regular expression.
-
getFailPhrase
Description copied from class:ValueComparerTemplateBaseReturns the text snippet for substitution inValueComparerBase.BASE_FAIL_MSG.- Specified by:
getFailPhrasein classValueComparerTemplateBase- Returns:
- The text snippet for substitution in
ValueComparerBase.BASE_FAIL_MSG.
-