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