View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import org.dbunit.DatabaseUnitException;
4   import org.dbunit.dataset.ITable;
5   import org.dbunit.dataset.datatype.DataType;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   /**
10   * Base class for {@link ValueComparer}s providing a template method and common
11   * elements, mainly consistent log message and toString.
12   *
13   * @author jjensen
14   * @since 2.6.0
15   */
16  public abstract class ValueComparerBase implements ValueComparer
17  {
18      private final Logger log = LoggerFactory.getLogger(getClass());
19  
20      /**
21       * Format String for consistent fail message; substitution strings are:
22       * actual, fail phrase, expected.
23       */
24      public static final String BASE_FAIL_MSG =
25              "Actual value='%s' is %s expected value='%s'";
26  
27      /**
28       * {@inheritDoc}
29       *
30       * This implementation calls
31       * {@link #doCompare(ITable, ITable, int, String, DataType, Object, Object)}.
32       */
33      public String compare(final ITable expectedTable, final ITable actualTable,
34              final int rowNum, final String columnName, final DataType dataType,
35              final Object expectedValue, final Object actualValue)
36              throws DatabaseUnitException
37      {
38          final String failMessage;
39  
40          failMessage = doCompare(expectedTable, actualTable, rowNum, columnName,
41                  dataType, expectedValue, actualValue);
42  
43          log.debug(
44                  "compare: rowNum={}, columnName={}, expectedValue={},"
45                          + " actualValue={}, failMessage={}",
46                  rowNum, columnName, expectedValue, actualValue, failMessage);
47  
48          return failMessage;
49      }
50  
51      /**
52       * Do the comparison and return a fail message or null if comparison passes.
53       *
54       * @see ValueComparer#compare(ITable, ITable, int, String, DataType, Object,
55       *      Object)
56       */
57      protected abstract String doCompare(final ITable expectedTable,
58              final ITable actualTable, final int rowNum, final String columnName,
59              final DataType dataType, final Object expectedValue,
60              final Object actualValue) throws DatabaseUnitException;
61  
62      @Override
63      public String toString()
64      {
65          return getClass().getName();
66      }
67  }