View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import java.util.Set;
4   
5   import org.dbunit.DatabaseUnitException;
6   import org.dbunit.dataset.DataSetException;
7   import org.dbunit.dataset.ITable;
8   import org.dbunit.dataset.datatype.DataType;
9   
10  /**
11   * Use one of two {@link ValueComparer}s based on a value present or not in a
12   * set of values.
13   *
14   * When the value returned by the
15   * {@link ConditionalSetBiValueComparer#actualValueFactory} is in
16   * {@link ConditionalSetBiValueComparer#values}, use the
17   * {@link ConditionalSetBiValueComparer#inValuesValueComparer} on the row;
18   * otherwise, use the
19   * {@link ConditionalSetBiValueComparer#notInValuesValueComparer} on the row.
20   *
21   * @author jjensen
22   * @since 2.6.0
23   */
24  public class ConditionalSetBiValueComparer<T> extends ValueComparerBase
25  {
26      private final ValueFactory<T> actualValueFactory;
27      private final Set<Object> values;
28      private final ValueComparer inValuesValueComparer;
29      private final ValueComparer notInValuesValueComparer;
30  
31      /**
32       * @param actualValueFactory
33       *            Factory to make the value to lookup in the values map.
34       * @param values
35       *            Map of values that mean to use the inValuesValueComparer.
36       * @param inValuesValueComparer
37       *            The {@link ValueComparer} used when the value from the
38       *            actualValueFactory is in the values map.
39       * @param notInValuesValueComparer
40       *            The {@link ValueComparer} used when the value from the
41       *            actualValueFactory is not in the values map.
42       */
43      public ConditionalSetBiValueComparer(
44              final ValueFactory<T> actualValueFactory, final Set<Object> values,
45              final ValueComparer inValuesValueComparer,
46              final ValueComparer notInValuesValueComparer)
47      {
48          this.actualValueFactory = actualValueFactory;
49          this.values = values;
50          this.inValuesValueComparer = inValuesValueComparer;
51          this.notInValuesValueComparer = notInValuesValueComparer;
52      }
53  
54      @Override
55      public String doCompare(final ITable expectedTable,
56              final ITable actualTable, final int rowNum, final String columnName,
57              final DataType dataType, final Object expectedValue,
58              final Object actualValue) throws DatabaseUnitException
59      {
60          final String failMessage;
61  
62          final boolean isInValues = isActualValueInValues(actualTable, rowNum);
63  
64          if (isInValues)
65          {
66              failMessage = inValuesValueComparer.compare(expectedTable,
67                      actualTable, rowNum, columnName, dataType, expectedValue,
68                      actualValue);
69          } else
70          {
71              failMessage = notInValuesValueComparer.compare(expectedTable,
72                      actualTable, rowNum, columnName, dataType, expectedValue,
73                      actualValue);
74          }
75  
76          return failMessage;
77      }
78  
79      protected boolean isActualValueInValues(final ITable actualTable,
80              final int rowNum) throws DataSetException
81      {
82          final Object value = actualValueFactory.make(actualTable, rowNum);
83          return values.contains(value);
84      }
85  
86      @Override
87      public String toString()
88      {
89          final StringBuilder sb = new StringBuilder(400);
90          sb.append(super.toString());
91          sb.append(": [values=");
92          sb.append(values);
93          sb.append(", inValuesValueComparer=");
94          sb.append(inValuesValueComparer);
95          sb.append(", notInValuesValueComparer=");
96          sb.append(notInValuesValueComparer);
97          sb.append("]");
98  
99          return sb.toString();
100     }
101 }