Class IsActualEqualToExpectedJsonValueComparer
- All Implemented Interfaces:
ValueComparer
ValueComparer implementation that verifies the actual value is
semantically equal to the expected value by parsing both as JSON and
comparing the resulting document trees, instead of comparing their raw
text.
Databases that store native JSON (for example MySQL JSON,
PostgreSQL json/jsonb, or H2 JSON) commonly
reformat the text on storage: insignificant whitespace is stripped and
object keys may be reordered. A plain string or
DataType.compare(Object, Object) comparison then fails even when
the expected and actual documents are equivalent. This comparer instead
treats JSON object member order as insignificant while still treating JSON
array element order as significant, matching JSON's own equality
semantics. Special case: if both values are null, they match.
Requires the optional jackson-databind dependency (the same one
used by JsonDataSet) on the classpath.
Deliberately not exposed as a constant on ValueComparers, because
that class eagerly instantiates every constant it declares; doing so here
would force the optional dependency onto every consumer of
ValueComparers, not only those comparing JSON columns. Construct
this comparer directly instead.
- Since:
- 3.5.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 booleanisJsonEqual(int rowNum, String columnName, Object expectedValue, Object actualValue) Returns whether the expected and actual values parse as structurally equal JSON documents.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
-
IsActualEqualToExpectedJsonValueComparer
public IsActualEqualToExpectedJsonValueComparer()
-
-
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.
-
isJsonEqual
protected boolean isJsonEqual(int rowNum, String columnName, Object expectedValue, Object actualValue) throws DatabaseUnitException Returns whether the expected and actual values parse as structurally equal JSON documents.- Parameters:
rowNum- the current row number comparing, used only to identify a parse failure.columnName- the name of the current column comparing, used only to identify a parse failure.expectedValue- the expected value.actualValue- the actual value.- Returns:
trueif both values parse as JSON and their document trees are equal.- Throws:
DatabaseUnitException- if either value cannot be converted to a string or parsed as JSON.
-
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.
-