ValueComparerBase.java

  1. package org.dbunit.assertion.comparer.value;

  2. import org.dbunit.DatabaseUnitException;
  3. import org.dbunit.dataset.ITable;
  4. import org.dbunit.dataset.datatype.DataType;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;

  7. /**
  8.  * Base class for {@link ValueComparer}s providing a template method and common
  9.  * elements, mainly consistent log message and toString.
  10.  *
  11.  * @author Jeff Jensen
  12.  * @since 2.6.0
  13.  */
  14. public abstract class ValueComparerBase implements ValueComparer
  15. {
  16.     private final Logger log = LoggerFactory.getLogger(getClass());

  17.     /**
  18.      * Format String for consistent fail message; substitution strings are:
  19.      * actual, fail phrase, expected.
  20.      */
  21.     public static final String BASE_FAIL_MSG =
  22.             "Actual value='%s' is %s expected value='%s'";

  23.     /**
  24.      * {@inheritDoc}
  25.      *
  26.      * This implementation calls
  27.      * {@link #doCompare(ITable, ITable, int, String, DataType, Object, Object)}.
  28.      */
  29.     public String compare(final ITable expectedTable, final ITable actualTable,
  30.             final int rowNum, final String columnName, final DataType dataType,
  31.             final Object expectedValue, final Object actualValue)
  32.             throws DatabaseUnitException
  33.     {
  34.         final String failMessage;

  35.         failMessage = doCompare(expectedTable, actualTable, rowNum, columnName,
  36.                 dataType, expectedValue, actualValue);

  37.         log.debug(
  38.                 "compare: rowNum={}, columnName={}, expectedValue={},"
  39.                         + " actualValue={}, failMessage={}",
  40.                 rowNum, columnName, expectedValue, actualValue, failMessage);

  41.         return failMessage;
  42.     }

  43.     /**
  44.      * Do the comparison and return a fail message or null if comparison passes.
  45.      *
  46.      * @see ValueComparer#compare(ITable, ITable, int, String, DataType, Object,
  47.      *      Object)
  48.      */
  49.     protected abstract String doCompare(final ITable expectedTable,
  50.             final ITable actualTable, final int rowNum, final String columnName,
  51.             final DataType dataType, final Object expectedValue,
  52.             final Object actualValue) throws DatabaseUnitException;

  53.     @Override
  54.     public String toString()
  55.     {
  56.         return getClass().getName();
  57.     }
  58. }