Class DbUnitAssertBase

java.lang.Object
org.dbunit.assertion.DbUnitAssertBase
Direct Known Subclasses:
DbUnitAssert, DbUnitValueComparerAssert

public class DbUnitAssertBase extends Object
Base class for DbUnit assert classes containing common methods.
Since:
2.6.0
Author:
Jeff Jensen
  • Field Details

    • valueComparerDefaults

      protected ValueComparerDefaults valueComparerDefaults
      The value comparer defaults used when no comparer is explicitly configured for a table/column.
  • Constructor Details

    • DbUnitAssertBase

      public DbUnitAssertBase()
  • Method Details

    • getDefaultFailureHandler

      protected FailureHandler getDefaultFailureHandler()
      Returns the default failure handler, without additional column info.
      Returns:
      The default failure handler
      Since:
      2.4
    • getDefaultFailureHandler

      protected FailureHandler getDefaultFailureHandler(Column[] additionalColumnInfo)
      Returns the default failure handler, reporting the given additional columns on failure.
      Parameters:
      additionalColumnInfo - the additional columns to report on failure, may be null.
      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 - The FailureHandler to 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

      protected boolean skipCompare(String columnName, Object expectedValue, Object actualValue)
      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 compared
      expectedValue - The expected value to be compared
      actualValue - The actual value to be compared
      Returns:
      false always so that the comparison is never skipped
      Since:
      2.4
    • determineFailureHandler

      protected FailureHandler determineFailureHandler(FailureHandler failureHandler)
      Returns the given failure handler, or the default one if the given handler is null.
      Parameters:
      failureHandler - the failure handler to validate, may be null.
      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:
      true if 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

      protected String[] getSortedTableNames(IDataSet dataSet) throws DataSetException
      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,ValueComparer>> tableColumnValueComparers) throws DatabaseUnitException
      Asserts the two specified IDataSets 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 - IDataSet containing all expected results.
      actualDataSet - IDataSet containing 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 be null.
      defaultValueComparer - ValueComparer to use with column value comparisons when the column name for the table is not in the tableColumnValueComparers Map. Can be null and will default to ValueComparerDefaults.getDefaultValueComparer().
      tableColumnValueComparers - Map of ValueComparers to use for specific tables and columns. Key is table name, value is Map of column name in the table to ValueComparers. Can be null and will default to using ValueComparerDefaults.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,ValueComparer>> tableColumnValueComparers) throws DatabaseUnitException
      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 DatabaseUnitException
      Asserts the two specified ITables 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 - ITable containing all expected results.
      actualTable - ITable containing 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 be null.
      defaultValueComparer - ValueComparer to use with column value comparisons when the column name for the table is not in the columnValueComparers Map. Can be null and will default to ValueComparerDefaults.getDefaultValueComparer().
      columnValueComparers - Map of ValueComparers to use for specific columns. Key is column name in the table, value is ValueComparer to use in comparing expected to actual column values. Can be null and will default to using ValueComparerDefaults.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 correct DataTypes for comparison
      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). Must not be null at 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 DatabaseUnitException
      Compares the data of the two given tables using the given value comparers.
      Parameters:
      expectedTable - ITable containing all expected results.
      actualTable - ITable containing all actual results.
      comparisonCols - The columns to be compared, also including the correct DataTypes for comparison
      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). Must not be null at this stage.
      defaultValueComparer - ValueComparer to use with column value comparisons when the column name for the table is not in the columnValueComparers Map. Can be null and will default to ValueComparerDefaults.getDefaultValueComparer().
      columnValueComparers - Map of ValueComparers to use for specific columns. Key is column name in the table, value is ValueComparer to use in comparing expected to actual column values. Can be null and will default to using ValueComparerDefaults.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 DatabaseUnitException
      Compares a single expected and actual cell value at the given row and column, failing via the given failure handler on mismatch.
      Parameters:
      expectedTable - ITable containing all expected results.
      actualTable - ITable containing all actual results.
      comparisonCols - the columns to be compared, also including the correct DataTypes.
      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 not null.
      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, or null if 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

      protected ValueComparer determineValidDefaultValueComparer(ValueComparer defaultValueComparer)
      Returns the given default value comparer, or ValueComparerDefaults.getDefaultValueComparer() if the given comparer is null.
      Parameters:
      defaultValueComparer - the value comparer to validate, may be null.
      Returns:
      the given value comparer, or the framework default if null.
    • determineValidTableColumnValueComparers

      protected Map<String,Map<String,ValueComparer>> determineValidTableColumnValueComparers(Map<String,Map<String,ValueComparer>> tableColumnValueComparers)
      Returns the given per-table, per-column value comparer map, or ValueComparerDefaults.getDefaultTableColumnValueComparerMap() if the given map is null.
      Parameters:
      tableColumnValueComparers - the map to validate, may be null.
      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, or ValueComparerDefaults.getDefaultColumnValueComparerMapForTable(String) for the given table if the given map is null.
      Parameters:
      columnValueComparers - the map to validate, may be null.
      tableName - the table name to look up the default map for, if needed.
      Returns:
      the given map, or the framework default if null.
    • setValueComparerDefaults

      public void setValueComparerDefaults(ValueComparerDefaults valueComparerDefaults)
      Sets the value comparer defaults used when no comparer is explicitly configured for a table/column.
      Parameters:
      valueComparerDefaults - the value comparer defaults to use.