View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import java.util.Map;
4   
5   import org.dbunit.DatabaseUnitException;
6   import org.dbunit.dataset.ITable;
7   import org.dbunit.dataset.datatype.DataType;
8   
9   /**
10   * Use a {@link ValueComparerSelector} to select a {@link ValueComparer} for the
11   * column from a {@link Map} of them.
12   *
13   * @author jjensen
14   * @since 2.6.0
15   */
16  public class ConditionalSelectorMultiValueComparer extends ValueComparerBase
17  {
18      private final ValueComparerSelector valueComparerSelector;
19      private final Map<Object, ValueComparer> valueComparers;
20  
21      public ConditionalSelectorMultiValueComparer(
22              final ValueComparerSelector valueComparerSelector,
23              final Map<Object, ValueComparer> valueComparers)
24      {
25          this.valueComparerSelector = valueComparerSelector;
26          this.valueComparers = valueComparers;
27      }
28  
29      @Override
30      public String doCompare(final ITable expectedTable,
31              final ITable actualTable, final int rowNum, final String columnName,
32              final DataType dataType, final Object expectedValue,
33              final Object actualValue) throws DatabaseUnitException
34      {
35          final ValueComparer valueComparer = valueComparerSelector.select(
36                  expectedTable, actualTable, rowNum, columnName, dataType,
37                  expectedValue, actualValue, valueComparers);
38          if (valueComparer == null)
39          {
40              final String msg =
41                      "No ValueComparer found by valueComparerSelector="
42                              + valueComparerSelector + " in map="
43                              + valueComparers;
44              throw new IllegalStateException(msg);
45          }
46  
47          return valueComparer.compare(expectedTable, actualTable, rowNum,
48                  columnName, dataType, expectedValue, actualValue);
49      }
50  
51      @Override
52      public String toString()
53      {
54          final StringBuilder sb = new StringBuilder(400);
55          sb.append(super.toString());
56          sb.append(": [valueComparerSelector=");
57          sb.append(valueComparerSelector.getClass().getName());
58          sb.append(", inValuesValueComparer=");
59          sb.append(valueComparers);
60          sb.append("]");
61  
62          return sb.toString();
63      }
64  }