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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
43
44
45
46
47
48
49
50
51
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 }