DbUnitValueComparerAssert.java

  1. package org.dbunit.assertion;

  2. import java.util.Map;

  3. import org.dbunit.DatabaseUnitException;
  4. import org.dbunit.assertion.comparer.value.ValueComparer;
  5. import org.dbunit.dataset.Column;
  6. import org.dbunit.dataset.IDataSet;
  7. import org.dbunit.dataset.ITable;

  8. /**
  9.  * DbUnit assertions using {@link ValueComparer}s for the column comparisons.
  10.  *
  11.  * @author Jeff Jensen
  12.  * @since 2.6.0
  13.  */
  14. public class DbUnitValueComparerAssert extends DbUnitAssertBase
  15. {
  16.     /**
  17.      * Asserts the two specified {@link IDataSet}s comparing their columns using
  18.      * the default {@link ValueComparer} and handles failures using the default
  19.      * {@link FailureHandler}. This method ignores the table names, the columns
  20.      * order, the columns data type, and which columns are composing the primary
  21.      * keys.
  22.      *
  23.      * @param expectedDataSet
  24.      *            {@link IDataSet} containing all expected results.
  25.      * @param actualDataSet
  26.      *            {@link IDataSet} containing all actual results.
  27.      * @throws DatabaseUnitException
  28.      */
  29.     public void assertWithValueComparer(final IDataSet expectedDataSet,
  30.             final IDataSet actualDataSet) throws DatabaseUnitException
  31.     {
  32.         final ValueComparer defaultValueComparer =
  33.                 valueComparerDefaults.getDefaultValueComparer();
  34.         assertWithValueComparer(expectedDataSet, actualDataSet,
  35.                 defaultValueComparer);
  36.     }

  37.     /**
  38.      * Asserts the two specified {@link IDataSet}s comparing their columns using
  39.      * the specified defaultValueComparer and handles failures using the default
  40.      * {@link FailureHandler}. This method ignores the table names, the columns
  41.      * order, the columns data type, and which columns are composing the primary
  42.      * keys.
  43.      *
  44.      * @param expectedDataSet
  45.      *            {@link IDataSet} containing all expected results.
  46.      * @param actualDataSet
  47.      *            {@link IDataSet} containing all actual results.
  48.      * @param defaultValueComparer
  49.      *            {@link ValueComparer} to use with all column value
  50.      *            comparisons. Can be <code>null</code> and will default to
  51.      *            {@link #getDefaultValueComparer()}.
  52.      * @throws DatabaseUnitException
  53.      */
  54.     public void assertWithValueComparer(final IDataSet expectedDataSet,
  55.             final IDataSet actualDataSet,
  56.             final ValueComparer defaultValueComparer)
  57.             throws DatabaseUnitException
  58.     {
  59.         final Map<String, Map<String, ValueComparer>> tableColumnValueComparers =
  60.                 valueComparerDefaults.getDefaultTableColumnValueComparerMap();
  61.         assertWithValueComparer(expectedDataSet, actualDataSet,
  62.                 defaultValueComparer, tableColumnValueComparers);
  63.     }

  64.     /**
  65.      * Asserts the two specified {@link IDataSet}s comparing their columns using
  66.      * the specified columnValueComparers or defaultValueComparer and handles
  67.      * failures using the default {@link FailureHandler}. This method ignores
  68.      * the table names, the columns order, the columns data type, and which
  69.      * columns are composing the primary keys.
  70.      *
  71.      * @param expectedDataSet
  72.      *            {@link IDataSet} containing all expected results.
  73.      * @param actualDataSet
  74.      *            {@link IDataSet} containing all actual results.
  75.      * @param defaultValueComparer
  76.      *            {@link ValueComparer} to use with column value comparisons
  77.      *            when the column name for the table is not in the
  78.      *            tableColumnValueComparers {@link Map}. Can be
  79.      *            <code>null</code> and will default to
  80.      *            {@link #getDefaultValueComparer()}.
  81.      * @param tableColumnValueComparers
  82.      *            {@link Map} of {@link ValueComparer}s to use for specific
  83.      *            tables and columns. Key is table name, value is {@link Map} of
  84.      *            column name in the table to {@link ValueComparer}s. Can be
  85.      *            <code>null</code> and will default to using
  86.      *            {@link #getDefaultColumnValueComparerMapForTable(String)} or,
  87.      *            if that is empty, defaultValueComparer for all columns in all
  88.      *            tables.
  89.      * @throws DatabaseUnitException
  90.      */
  91.     public void assertWithValueComparer(final IDataSet expectedDataSet,
  92.             final IDataSet actualDataSet,
  93.             final ValueComparer defaultValueComparer,
  94.             final Map<String, Map<String, ValueComparer>> tableColumnValueComparers)
  95.             throws DatabaseUnitException
  96.     {
  97.         final FailureHandler failureHandler = getDefaultFailureHandler();
  98.         assertWithValueComparer(expectedDataSet, actualDataSet, failureHandler,
  99.                 defaultValueComparer, tableColumnValueComparers);
  100.     }

  101.     /**
  102.      * Asserts the two specified {@link ITable}s comparing their columns using
  103.      * the default {@link ValueComparer} and handles failures using the default
  104.      * {@link FailureHandler}. This method ignores the table names, the columns
  105.      * order, the columns data type, and which columns are composing the primary
  106.      * keys.
  107.      *
  108.      * @param expectedTable
  109.      *            {@link ITable} containing all expected results.
  110.      * @param actualTable
  111.      *            {@link ITable} containing all actual results.
  112.      * @throws DatabaseUnitException
  113.      */
  114.     public void assertWithValueComparer(final ITable expectedTable,
  115.             final ITable actualTable) throws DatabaseUnitException
  116.     {
  117.         final ValueComparer defaultValueComparer =
  118.                 valueComparerDefaults.getDefaultValueComparer();

  119.         assertWithValueComparer(expectedTable, actualTable,
  120.                 defaultValueComparer);
  121.     }

  122.     /**
  123.      * Asserts the two specified {@link ITable}s comparing their columns using
  124.      * the specified defaultValueComparer and handles failures using the default
  125.      * {@link FailureHandler}. This method ignores the table names, the columns
  126.      * order, the columns data type, and which columns are composing the primary
  127.      * keys.
  128.      *
  129.      * @param expectedTable
  130.      *            {@link ITable} containing all expected results.
  131.      * @param actualTable
  132.      *            {@link ITable} containing all actual results.
  133.      * @param defaultValueComparer
  134.      *            {@link ValueComparer} to use with all column value
  135.      *            comparisons. Can be <code>null</code> and will default to
  136.      *            {@link #getDefaultValueComparer()}.
  137.      * @throws DatabaseUnitException
  138.      */
  139.     public void assertWithValueComparer(final ITable expectedTable,
  140.             final ITable actualTable, final ValueComparer defaultValueComparer)
  141.             throws DatabaseUnitException
  142.     {
  143.         final String tableName =
  144.                 expectedTable.getTableMetaData().getTableName();
  145.         final Map<String, ValueComparer> columnValueComparers =
  146.                 valueComparerDefaults
  147.                         .getDefaultColumnValueComparerMapForTable(tableName);

  148.         assertWithValueComparer(expectedTable, actualTable,
  149.                 defaultValueComparer, columnValueComparers);
  150.     }

  151.     /**
  152.      * Asserts the two specified {@link ITable}s comparing their columns using
  153.      * the specified columnValueComparers or defaultValueComparer and handles
  154.      * failures using the default {@link FailureHandler}. This method ignores
  155.      * the table names, the columns order, the columns data type, and which
  156.      * columns are composing the primary keys.
  157.      *
  158.      * @param expectedTable
  159.      *            {@link ITable} containing all expected results.
  160.      * @param actualTable
  161.      *            {@link ITable} containing all actual results.
  162.      * @param defaultValueComparer
  163.      *            {@link ValueComparer} to use with column value comparisons
  164.      *            when the column name for the table is not in the
  165.      *            columnValueComparers {@link Map}. Can be <code>null</code> and
  166.      *            will default to {@link #getDefaultValueComparer()}.
  167.      * @param columnValueComparers
  168.      *            {@link Map} of {@link ValueComparer}s to use for specific
  169.      *            columns. Key is column name in the table, value is
  170.      *            {@link ValueComparer} to use in comparing expected to actual
  171.      *            column values. Can be <code>null</code> and will default to
  172.      *            using
  173.      *            {@link #getDefaultColumnValueComparerMapForTable(String)} or,
  174.      *            if that is empty, defaultValueComparer for all columns in the
  175.      *            table.
  176.      * @throws DatabaseUnitException
  177.      */
  178.     public void assertWithValueComparer(final ITable expectedTable,
  179.             final ITable actualTable, final ValueComparer defaultValueComparer,
  180.             final Map<String, ValueComparer> columnValueComparers)
  181.             throws DatabaseUnitException
  182.     {
  183.         final FailureHandler failureHandler = getDefaultFailureHandler();
  184.         assertWithValueComparer(expectedTable, actualTable, failureHandler,
  185.                 defaultValueComparer, columnValueComparers);
  186.     }

  187.     /**
  188.      * Asserts the two specified {@link ITable}s comparing their columns using
  189.      * the specified columnValueComparers or defaultValueComparer and handles
  190.      * failures using the default {@link FailureHandler}, using
  191.      * additionalColumnInfo, if specified. This method ignores the table names,
  192.      * the columns order, the columns data type, and which columns are composing
  193.      * the primary keys.
  194.      *
  195.      * @param expectedTable
  196.      *            {@link ITable} containing all expected results.
  197.      * @param actualTable
  198.      *            {@link ITable} containing all actual results.
  199.      * @param additionalColumnInfo
  200.      *            The columns to be printed out if the assert fails because of a
  201.      *            data mismatch. Provides some additional column values that may
  202.      *            be useful to quickly identify the columns for which the
  203.      *            mismatch occurred (for example a primary key column). Can be
  204.      *            <code>null</code>
  205.      * @param defaultValueComparer
  206.      *            {@link ValueComparer} to use with column value comparisons
  207.      *            when the column name for the table is not in the
  208.      *            columnValueComparers {@link Map}. Can be <code>null</code> and
  209.      *            will default to {@link #getDefaultValueComparer()}.
  210.      * @param columnValueComparers
  211.      *            {@link Map} of {@link ValueComparer}s to use for specific
  212.      *            columns. Key is column name in the table, value is
  213.      *            {@link ValueComparer} to use in comparing expected to actual
  214.      *            column values. Can be <code>null</code> and will default to
  215.      *            using
  216.      *            {@link #getDefaultColumnValueComparerMapForTable(String)} or,
  217.      *            if that is empty, defaultValueComparer for all columns in the
  218.      *            table.
  219.      * @throws DatabaseUnitException
  220.      */
  221.     public void assertWithValueComparer(final ITable expectedTable,
  222.             final ITable actualTable, final Column[] additionalColumnInfo,
  223.             final ValueComparer defaultValueComparer,
  224.             final Map<String, ValueComparer> columnValueComparers)
  225.             throws DatabaseUnitException
  226.     {
  227.         final FailureHandler failureHandler =
  228.                 getDefaultFailureHandler(additionalColumnInfo);

  229.         assertWithValueComparer(expectedTable, actualTable, failureHandler,
  230.                 defaultValueComparer, columnValueComparers);
  231.     }
  232. }