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