View Javadoc
1   package org.dbunit.assertion.comparer.value;
2   
3   import static org.junit.jupiter.api.Assertions.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 Jeff Jensen
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(final ValueFactory<T> actualValueFactory, final Set<T> values,
54              final ValueComparer inValuesValueComparer, final ValueComparer notInValuesValueComparer) {
55          assertNotNull(actualValueFactory, "actualValueFactory is null.");
56          assertNotNull(values, "values is null.");
57          assertNotNull(inValuesValueComparer, "inValuesValueComparer is null.");
58          assertNotNull(notInValuesValueComparer, "notInValuesValueComparer is null.");
59  
60          this.actualValueFactory = actualValueFactory;
61          this.values = values;
62          this.inValuesValueComparer = inValuesValueComparer;
63          this.notInValuesValueComparer = notInValuesValueComparer;
64      }
65  
66      @Override
67      public String doCompare(final ITable expectedTable,
68              final ITable actualTable, final int rowNum, final String columnName,
69              final DataType dataType, final Object expectedValue,
70              final Object actualValue) throws DatabaseUnitException
71      {
72          final String failMessage;
73  
74          final boolean isInValues = isActualValueInValues(actualTable, rowNum);
75  
76          if (isInValues)
77          {
78              failMessage = inValuesValueComparer.compare(expectedTable,
79                      actualTable, rowNum, columnName, dataType, expectedValue,
80                      actualValue);
81          } else
82          {
83              failMessage = notInValuesValueComparer.compare(expectedTable,
84                      actualTable, rowNum, columnName, dataType, expectedValue,
85                      actualValue);
86          }
87  
88          return failMessage;
89      }
90  
91      protected boolean isActualValueInValues(final ITable actualTable,
92              final int rowNum) throws DataSetException
93      {
94          final T actualValue = actualValueFactory.make(actualTable, rowNum);
95          final boolean isListContains = values.contains(actualValue);
96  
97          log.debug("isActualValueInValues: actualValue={}, isListContains={}",
98                  actualValue, isListContains);
99  
100         return isListContains;
101     }
102 
103     @Override
104     public String toString()
105     {
106         final StringBuilder sb = new StringBuilder(400);
107         sb.append(super.toString());
108         sb.append(": [values=");
109         sb.append(values);
110         sb.append(", inValuesValueComparer=");
111         sb.append(inValuesValueComparer);
112         sb.append(", notInValuesValueComparer=");
113         sb.append(notInValuesValueComparer);
114         sb.append("]");
115 
116         return sb.toString();
117     }
118 }