Package org.dbunit.assertion
Class DbUnitAssertBase
java.lang.Object
org.dbunit.assertion.DbUnitAssertBase
- Direct Known Subclasses:
DbUnitAssert,DbUnitValueComparerAssert
Base class for DbUnit assert classes containing common methods.
- Since:
- 2.6.0
- Author:
- Jeff Jensen
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ValueComparerDefaultsThe value comparer defaults used when no comparer is explicitly configured for a table/column. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidassertWithValueComparer(IDataSet expectedDataSet, IDataSet actualDataSet, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, Map<String, ValueComparer>> tableColumnValueComparers) Asserts the two specifiedIDataSets comparing their columns using the specified columnValueComparers or defaultValueComparer and handles failures using the specified failureHandler.voidassertWithValueComparer(ITable expectedTable, ITable actualTable, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) Asserts the two specifiedITables comparing their columns using the specified columnValueComparers or defaultValueComparer and handles failures using the specified failureHandler.protected voidcompareColumns(Column[] expectedColumns, Column[] actualColumns, ITableMetaData expectedMetaData, ITableMetaData actualMetaData, FailureHandler failureHandler) Compares the columns of the two given tables, failing via the given failure handler on mismatch.protected voidcompareData(ITable expectedTable, ITable actualTable, DbUnitAssert.ComparisonColumn[] comparisonCols, FailureHandler failureHandler) Compares the data of the two given tables using the default value comparers.protected voidcompareData(ITable expectedTable, ITable actualTable, DbUnitAssert.ComparisonColumn[] comparisonCols, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) Compares the data of the two given tables using the given value comparers.protected voidcompareData(ITable expectedTable, ITable actualTable, DbUnitAssert.ComparisonColumn[] comparisonCols, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers, int rowNum, int columnNum) Compares a single expected and actual cell value at the given row and column, failing via the given failure handler on mismatch.protected booleancompareRowCounts(ITable expectedTable, ITable actualTable, FailureHandler failureHandler, String expectedTableName) Compares the row counts of the two given tables, failing via the given failure handler on mismatch.protected voidcompareTableCounts(String[] expectedNames, String[] actualNames, FailureHandler failureHandler) Compares the number of expected and actual table names, failing via the given failure handler on mismatch.protected voidcompareTableNames(String[] expectedNames, String[] actualNames, FailureHandler failureHandler) Compares the expected and actual table names, failing via the given failure handler on mismatch.protected voidcompareTables(IDataSet expectedDataSet, IDataSet actualDataSet, String[] expectedNames, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, Map<String, ValueComparer>> tableColumnValueComparers) Asserts each expected table against its corresponding actual table.protected FailureHandlerdetermineFailureHandler(FailureHandler failureHandler) Returns the given failure handler, or the default one if the given handler isnull.protected Map<String, ValueComparer> determineValidColumnValueComparers(Map<String, ValueComparer> columnValueComparers, String tableName) Returns the given per-column value comparer map, orValueComparerDefaults.getDefaultColumnValueComparerMapForTable(String)for the given table if the given map isnull.protected ValueComparerdetermineValidDefaultValueComparer(ValueComparer defaultValueComparer) Returns the given default value comparer, orValueComparerDefaults.getDefaultValueComparer()if the given comparer isnull.protected Map<String, Map<String, ValueComparer>> determineValidTableColumnValueComparers(Map<String, Map<String, ValueComparer>> tableColumnValueComparers) Returns the given per-table, per-column value comparer map, orValueComparerDefaults.getDefaultTableColumnValueComparerMap()if the given map isnull.protected ValueComparerdetermineValueComparer(String columnName, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) Returns the value comparer configured for the given column, or the default value comparer if the column has none configured.protected voidfailIfNecessary(ITable expectedTable, ITable actualTable, FailureHandler failureHandler, int rowNum, String columnName, Object expectedValue, Object actualValue, String failMessage) Reports a failure to the given failure handler if the given fail message is notnull.protected DbUnitAssert.ComparisonColumn[]getComparisonColumns(String expectedTableName, Column[] expectedColumns, Column[] actualColumns, FailureHandler failureHandler) Builds the comparison columns to use for the assertion, pairing each expected column with its actual counterpart and resolving the correct datatype.protected FailureHandlerReturns the default failure handler, without additional column info.protected FailureHandlergetDefaultFailureHandler(Column[] additionalColumnInfo) Returns the default failure handler, reporting the given additional columns on failure.protected String[]getSortedTableNames(IDataSet dataSet) Returns the table names of the given dataset, sorted and upper-cased unless the dataset is case-sensitive.voidsetValueComparerDefaults(ValueComparerDefaults valueComparerDefaults) Sets the value comparer defaults used when no comparer is explicitly configured for a table/column.protected booleanskipCompare(String columnName, Object expectedValue, Object actualValue) Method to last-minute intercept the comparison of a single expected and actual value.
-
Field Details
-
valueComparerDefaults
The value comparer defaults used when no comparer is explicitly configured for a table/column.
-
-
Constructor Details
-
DbUnitAssertBase
public DbUnitAssertBase()
-
-
Method Details
-
getDefaultFailureHandler
Returns the default failure handler, without additional column info.- Returns:
- The default failure handler
- Since:
- 2.4
-
getDefaultFailureHandler
Returns the default failure handler, reporting the given additional columns on failure.- Parameters:
additionalColumnInfo- the additional columns to report on failure, may benull.- Returns:
- The default failure handler
- Since:
- 2.4
-
getComparisonColumns
protected DbUnitAssert.ComparisonColumn[] getComparisonColumns(String expectedTableName, Column[] expectedColumns, Column[] actualColumns, FailureHandler failureHandler) Builds the comparison columns to use for the assertion, pairing each expected column with its actual counterpart and resolving the correct datatype.- Parameters:
expectedTableName- the name of the table being compared, used for failure reporting.expectedColumns- the expected columns, providing the comparison order.actualColumns- the actual columns, matched positionally to expectedColumns.failureHandler- TheFailureHandlerto be used when no datatype can be determined- Returns:
- The columns to be used for the assertion, including the correct datatype
- Since:
- 2.4
-
skipCompare
Method to last-minute intercept the comparison of a single expected and actual value. Designed to be overridden in order to skip cell comparison by specific cell values.- Parameters:
columnName- The column being comparedexpectedValue- The expected value to be comparedactualValue- The actual value to be compared- Returns:
falsealways so that the comparison is never skipped- Since:
- 2.4
-
determineFailureHandler
Returns the given failure handler, or the default one if the given handler isnull.- Parameters:
failureHandler- the failure handler to validate, may benull.- Returns:
- the given failure handler, or the default one if
null.
-
compareRowCounts
protected boolean compareRowCounts(ITable expectedTable, ITable actualTable, FailureHandler failureHandler, String expectedTableName) throws Error Compares the row counts of the two given tables, failing via the given failure handler on mismatch.- Parameters:
expectedTable- the table containing all expected results.actualTable- the table containing all actual results.failureHandler- the failure handler used to report a row count mismatch.expectedTableName- the table name, used for failure reporting.- Returns:
trueif both tables are empty, in which case column comparison can be skipped.- Throws:
Error
-
compareColumns
protected void compareColumns(Column[] expectedColumns, Column[] actualColumns, ITableMetaData expectedMetaData, ITableMetaData actualMetaData, FailureHandler failureHandler) throws DataSetException, Error Compares the columns of the two given tables, failing via the given failure handler on mismatch.- Parameters:
expectedColumns- the expected columns.actualColumns- the actual columns.expectedMetaData- the metadata of the expected table.actualMetaData- the metadata of the actual table.failureHandler- the failure handler used to report a column mismatch.- Throws:
DataSetException- if the column difference cannot be computed.Error
-
compareTableCounts
protected void compareTableCounts(String[] expectedNames, String[] actualNames, FailureHandler failureHandler) throws Error Compares the number of expected and actual table names, failing via the given failure handler on mismatch.- Parameters:
expectedNames- the expected table names.actualNames- the actual table names.failureHandler- the failure handler used to report a table count mismatch.- Throws:
Error
-
compareTableNames
protected void compareTableNames(String[] expectedNames, String[] actualNames, FailureHandler failureHandler) throws Error Compares the expected and actual table names, failing via the given failure handler on mismatch.- Parameters:
expectedNames- the expected table names, sorted.actualNames- the actual table names, sorted.failureHandler- the failure handler used to report a table name mismatch.- Throws:
Error
-
getSortedTableNames
Returns the table names of the given dataset, sorted and upper-cased unless the dataset is case-sensitive.- Parameters:
dataSet- the dataset providing the table names.- Returns:
- the sorted table names.
- Throws:
DataSetException- if the table names cannot be retrieved.
-
assertWithValueComparer
public void assertWithValueComparer(IDataSet expectedDataSet, IDataSet actualDataSet, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, Map<String, throws DatabaseUnitExceptionValueComparer>> tableColumnValueComparers) Asserts the two specifiedIDataSets comparing their columns using the specified columnValueComparers or defaultValueComparer and handles failures using the specified failureHandler. This method ignores the table names, the columns order, the columns data type, and which columns are composing the primary keys.- Parameters:
expectedDataSet-IDataSetcontaining all expected results.actualDataSet-IDataSetcontaining all actual results.failureHandler- The failure handler used if the assert fails because of a data mismatch. Provides some additional information that may be useful to quickly identify the rows for which the mismatch occurred (for example by printing an additional primary key column). Can benull.defaultValueComparer-ValueComparerto use with column value comparisons when the column name for the table is not in the tableColumnValueComparersMap. Can benulland will default toValueComparerDefaults.getDefaultValueComparer().tableColumnValueComparers-MapofValueComparers to use for specific tables and columns. Key is table name, value isMapof column name in the table toValueComparers. Can benulland will default to usingValueComparerDefaults.getDefaultColumnValueComparerMapForTable(String)or, if that is empty, defaultValueComparer for all columns in all tables.- Throws:
DatabaseUnitException- if an expected table is missing from the actual dataset, or a table comparison fails.
-
compareTables
protected void compareTables(IDataSet expectedDataSet, IDataSet actualDataSet, String[] expectedNames, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, Map<String, throws DatabaseUnitExceptionValueComparer>> tableColumnValueComparers) Asserts each expected table against its corresponding actual table.- Parameters:
expectedDataSet- the dataset containing all expected results.actualDataSet- the dataset containing all actual results.expectedNames- the table names to compare, in comparison order.failureHandler- the failure handler used if the assert fails because of a data mismatch.defaultValueComparer- the default value comparer, used when a table/column has none configured.tableColumnValueComparers- the per-table, per-column value comparers to use.- Throws:
DatabaseUnitException- if a table comparison fails.
-
assertWithValueComparer
public void assertWithValueComparer(ITable expectedTable, ITable actualTable, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) throws DatabaseUnitExceptionAsserts the two specifiedITables comparing their columns using the specified columnValueComparers or defaultValueComparer and handles failures using the specified failureHandler. This method ignores the table names, the columns order, the columns data type, and which columns are composing the primary keys.- Parameters:
expectedTable-ITablecontaining all expected results.actualTable-ITablecontaining all actual results.failureHandler- The failure handler used if the assert fails because of a data mismatch. Provides some additional information that may be useful to quickly identify the rows for which the mismatch occurred (for example by printing an additional primary key column). Can benull.defaultValueComparer-ValueComparerto use with column value comparisons when the column name for the table is not in the columnValueComparersMap. Can benulland will default toValueComparerDefaults.getDefaultValueComparer().columnValueComparers-MapofValueComparers to use for specific columns. Key is column name in the table, value isValueComparerto use in comparing expected to actual column values. Can benulland will default to usingValueComparerDefaults.getDefaultColumnValueComparerMapForTable(String)or, if that is empty, defaultValueComparer for all columns in the table.- Throws:
DatabaseUnitException- if the tables' row counts, columns, or data do not match.
-
compareData
protected void compareData(ITable expectedTable, ITable actualTable, DbUnitAssert.ComparisonColumn[] comparisonCols, FailureHandler failureHandler) throws DataSetException Compares the data of the two given tables using the default value comparers.- Parameters:
expectedTable- Table containing all expected results.actualTable- Table containing all actual results.comparisonCols- The columns to be compared, also including the correctDataTypes for comparisonfailureHandler- The failure handler used if the assert fails because of a data mismatch. Provides some additional information that may be useful to quickly identify the rows for which the mismatch occurred (for example by printing an additional primary key column). Must not benullat this stage- Throws:
DataSetException- if a data comparison fails.- Since:
- 2.4
-
compareData
protected void compareData(ITable expectedTable, ITable actualTable, DbUnitAssert.ComparisonColumn[] comparisonCols, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) throws DatabaseUnitExceptionCompares the data of the two given tables using the given value comparers.- Parameters:
expectedTable-ITablecontaining all expected results.actualTable-ITablecontaining all actual results.comparisonCols- The columns to be compared, also including the correctDataTypes for comparisonfailureHandler- The failure handler used if the assert fails because of a data mismatch. Provides some additional information that may be useful to quickly identify the rows for which the mismatch occurred (for example by printing an additional primary key column). Must not benullat this stage.defaultValueComparer-ValueComparerto use with column value comparisons when the column name for the table is not in the columnValueComparersMap. Can benulland will default toValueComparerDefaults.getDefaultValueComparer().columnValueComparers-MapofValueComparers to use for specific columns. Key is column name in the table, value isValueComparerto use in comparing expected to actual column values. Can benulland will default to usingValueComparerDefaults.getDefaultColumnValueComparerMapForTable(String)or, if that is empty, defaultValueComparer for all columns in the table.- Throws:
DatabaseUnitException- if a data comparison fails.- Since:
- 2.4, 2.6.0
-
compareData
protected void compareData(ITable expectedTable, ITable actualTable, DbUnitAssert.ComparisonColumn[] comparisonCols, FailureHandler failureHandler, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers, int rowNum, int columnNum) throws DatabaseUnitExceptionCompares a single expected and actual cell value at the given row and column, failing via the given failure handler on mismatch.- Parameters:
expectedTable-ITablecontaining all expected results.actualTable-ITablecontaining all actual results.comparisonCols- the columns to be compared, also including the correctDataTypes.failureHandler- the failure handler used if the assert fails because of a data mismatch.defaultValueComparer- the default value comparer, used when the column has none configured.columnValueComparers- the per-column value comparers to use.rowNum- the row index of the cell to compare.columnNum- the index, into comparisonCols, of the column to compare.- Throws:
DatabaseUnitException- if the cell comparison fails.
-
failIfNecessary
protected void failIfNecessary(ITable expectedTable, ITable actualTable, FailureHandler failureHandler, int rowNum, String columnName, Object expectedValue, Object actualValue, String failMessage) Reports a failure to the given failure handler if the given fail message is notnull.- Parameters:
expectedTable- the table containing all expected results.actualTable- the table containing all actual results.failureHandler- the failure handler to report the difference to.rowNum- the row index of the compared cell.columnName- the name of the compared column.expectedValue- the expected cell value.actualValue- the actual cell value.failMessage- the comparison failure message, ornullif the values matched.
-
determineValueComparer
protected ValueComparer determineValueComparer(String columnName, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) Returns the value comparer configured for the given column, or the default value comparer if the column has none configured.- Parameters:
columnName- the column name to look up.defaultValueComparer- the value comparer to fall back to.columnValueComparers- the per-column value comparers to look up columnName in.- Returns:
- the value comparer to use for the given column.
-
determineValidDefaultValueComparer
Returns the given default value comparer, orValueComparerDefaults.getDefaultValueComparer()if the given comparer isnull.- Parameters:
defaultValueComparer- the value comparer to validate, may benull.- Returns:
- the given value comparer, or the framework default if
null.
-
determineValidTableColumnValueComparers
protected Map<String,Map<String, determineValidTableColumnValueComparersValueComparer>> (Map<String, Map<String, ValueComparer>> tableColumnValueComparers) Returns the given per-table, per-column value comparer map, orValueComparerDefaults.getDefaultTableColumnValueComparerMap()if the given map isnull.- Parameters:
tableColumnValueComparers- the map to validate, may benull.- Returns:
- the given map, or the framework default if
null.
-
determineValidColumnValueComparers
protected Map<String,ValueComparer> determineValidColumnValueComparers(Map<String, ValueComparer> columnValueComparers, String tableName) Returns the given per-column value comparer map, orValueComparerDefaults.getDefaultColumnValueComparerMapForTable(String)for the given table if the given map isnull.- Parameters:
columnValueComparers- the map to validate, may benull.tableName- the table name to look up the default map for, if needed.- Returns:
- the given map, or the framework default if
null.
-
setValueComparerDefaults
Sets the value comparer defaults used when no comparer is explicitly configured for a table/column.- Parameters:
valueComparerDefaults- the value comparer defaults to use.
-