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   
7   /**
8    * Base class for {@link ValueComparer}s, providing template methods and common
9    * elements.
10   *
11   * @author jjensen
12   * @since 2.6.0
13   */
14  public abstract class ValueComparerTemplateBase extends ValueComparerBase
15  {
16      /**
17       * {@inheritDoc}
18       *
19       * This implementation calls
20       * {@link #isExpected(ITable, ITable, int, String, DataType, Object, Object)}.
21       *
22       * @see ValueComparer#compare(ITable, ITable, int, String, DataType, Object,
23       *      Object)
24       */
25      @Override
26      protected String doCompare(final ITable expectedTable,
27              final ITable actualTable, final int rowNum, final String columnName,
28              final DataType dataType, final Object expectedValue,
29              final Object actualValue) throws DatabaseUnitException
30      {
31          final String failMessage;
32  
33          final boolean isExpected = isExpected(expectedTable, actualTable,
34                  rowNum, columnName, dataType, expectedValue, actualValue);
35          if (isExpected)
36          {
37              failMessage = null;
38          } else
39          {
40              failMessage = makeFailMessage(expectedValue, actualValue);
41          }
42  
43          return failMessage;
44      }
45  
46      /**
47       * Makes the fail message using {@link #getFailPhrase()}.
48       *
49       * @return the formatted fail message with the fail phrase.
50       */
51      protected String makeFailMessage(final Object expectedValue,
52              final Object actualValue)
53      {
54          final String failPhrase = getFailPhrase();
55          return String.format(BASE_FAIL_MSG, actualValue, failPhrase,
56                  expectedValue);
57      }
58  
59      /** @return true if comparing actual to expected is as expected. */
60      protected abstract boolean isExpected(final ITable expectedTable,
61              final ITable actualTable, final int rowNum, final String columnName,
62              final DataType dataType, final Object expectedValue,
63              final Object actualValue) throws DatabaseUnitException;
64  
65      /** @return The text snippet for substitution in {@link #BASE_FAIL_MSG}. */
66      protected abstract String getFailPhrase();
67  }