1 package org.dbunit.assertion;
2
3 import java.util.Map;
4
5 import org.dbunit.DatabaseUnitException;
6 import org.dbunit.assertion.comparer.value.ValueComparer;
7 import org.dbunit.dataset.Column;
8 import org.dbunit.dataset.IDataSet;
9 import org.dbunit.dataset.ITable;
10
11 /**
12 * DbUnit assertions using {@link ValueComparer}s for the column comparisons.
13 *
14 * @author Jeff Jensen
15 * @since 2.6.0
16 */
17 public class DbUnitValueComparerAssert extends DbUnitAssertBase
18 {
19 /**
20 * Asserts the two specified {@link IDataSet}s comparing their columns using
21 * the default {@link ValueComparer} and handles failures using the default
22 * {@link FailureHandler}. This method ignores the table names, the columns
23 * order, the columns data type, and which columns are composing the primary
24 * keys.
25 *
26 * @param expectedDataSet
27 * {@link IDataSet} containing all expected results.
28 * @param actualDataSet
29 * {@link IDataSet} containing all actual results.
30 * @throws DatabaseUnitException
31 */
32 public void assertWithValueComparer(final IDataSet expectedDataSet,
33 final IDataSet actualDataSet) throws DatabaseUnitException
34 {
35 final ValueComparer defaultValueComparer =
36 valueComparerDefaults.getDefaultValueComparer();
37 assertWithValueComparer(expectedDataSet, actualDataSet,
38 defaultValueComparer);
39 }
40
41 /**
42 * Asserts the two specified {@link IDataSet}s comparing their columns using
43 * the specified defaultValueComparer and handles failures using the default
44 * {@link FailureHandler}. This method ignores the table names, the columns
45 * order, the columns data type, and which columns are composing the primary
46 * keys.
47 *
48 * @param expectedDataSet
49 * {@link IDataSet} containing all expected results.
50 * @param actualDataSet
51 * {@link IDataSet} containing all actual results.
52 * @param defaultValueComparer
53 * {@link ValueComparer} to use with all column value
54 * comparisons. Can be <code>null</code> and will default to
55 * {@link #getDefaultValueComparer()}.
56 * @throws DatabaseUnitException
57 */
58 public void assertWithValueComparer(final IDataSet expectedDataSet,
59 final IDataSet actualDataSet,
60 final ValueComparer defaultValueComparer)
61 throws DatabaseUnitException
62 {
63 final Map<String, Map<String, ValueComparer>> tableColumnValueComparers =
64 valueComparerDefaults.getDefaultTableColumnValueComparerMap();
65 assertWithValueComparer(expectedDataSet, actualDataSet,
66 defaultValueComparer, tableColumnValueComparers);
67 }
68
69 /**
70 * Asserts the two specified {@link IDataSet}s comparing their columns using
71 * the specified columnValueComparers or defaultValueComparer and handles
72 * failures using the default {@link FailureHandler}. This method ignores
73 * the table names, the columns order, the columns data type, and which
74 * columns are composing the primary keys.
75 *
76 * @param expectedDataSet
77 * {@link IDataSet} containing all expected results.
78 * @param actualDataSet
79 * {@link IDataSet} containing all actual results.
80 * @param defaultValueComparer
81 * {@link ValueComparer} to use with column value comparisons
82 * when the column name for the table is not in the
83 * tableColumnValueComparers {@link Map}. Can be
84 * <code>null</code> and will default to
85 * {@link #getDefaultValueComparer()}.
86 * @param tableColumnValueComparers
87 * {@link Map} of {@link ValueComparer}s to use for specific
88 * tables and columns. Key is table name, value is {@link Map} of
89 * column name in the table to {@link ValueComparer}s. Can be
90 * <code>null</code> and will default to using
91 * {@link #getDefaultColumnValueComparerMapForTable(String)} or,
92 * if that is empty, defaultValueComparer for all columns in all
93 * tables.
94 * @throws DatabaseUnitException
95 */
96 public void assertWithValueComparer(final IDataSet expectedDataSet,
97 final IDataSet actualDataSet,
98 final ValueComparer defaultValueComparer,
99 final Map<String, Map<String, ValueComparer>> tableColumnValueComparers)
100 throws DatabaseUnitException
101 {
102 final FailureHandler failureHandler = getDefaultFailureHandler();
103 assertWithValueComparer(expectedDataSet, actualDataSet, failureHandler,
104 defaultValueComparer, tableColumnValueComparers);
105 }
106
107 /**
108 * Asserts the two specified {@link ITable}s comparing their columns using
109 * the default {@link ValueComparer} and handles failures using the default
110 * {@link FailureHandler}. This method ignores the table names, the columns
111 * order, the columns data type, and which columns are composing the primary
112 * keys.
113 *
114 * @param expectedTable
115 * {@link ITable} containing all expected results.
116 * @param actualTable
117 * {@link ITable} containing all actual results.
118 * @throws DatabaseUnitException
119 */
120 public void assertWithValueComparer(final ITable expectedTable,
121 final ITable actualTable) throws DatabaseUnitException
122 {
123 final ValueComparer defaultValueComparer =
124 valueComparerDefaults.getDefaultValueComparer();
125
126 assertWithValueComparer(expectedTable, actualTable,
127 defaultValueComparer);
128 }
129
130 /**
131 * Asserts the two specified {@link ITable}s comparing their columns using
132 * the specified defaultValueComparer and handles failures using the default
133 * {@link FailureHandler}. This method ignores the table names, the columns
134 * order, the columns data type, and which columns are composing the primary
135 * keys.
136 *
137 * @param expectedTable
138 * {@link ITable} containing all expected results.
139 * @param actualTable
140 * {@link ITable} containing all actual results.
141 * @param defaultValueComparer
142 * {@link ValueComparer} to use with all column value
143 * comparisons. Can be <code>null</code> and will default to
144 * {@link #getDefaultValueComparer()}.
145 * @throws DatabaseUnitException
146 */
147 public void assertWithValueComparer(final ITable expectedTable,
148 final ITable actualTable, final ValueComparer defaultValueComparer)
149 throws DatabaseUnitException
150 {
151 final String tableName =
152 expectedTable.getTableMetaData().getTableName();
153 final Map<String, ValueComparer> columnValueComparers =
154 valueComparerDefaults
155 .getDefaultColumnValueComparerMapForTable(tableName);
156
157 assertWithValueComparer(expectedTable, actualTable,
158 defaultValueComparer, columnValueComparers);
159 }
160
161 /**
162 * Asserts the two specified {@link ITable}s comparing their columns using
163 * the specified columnValueComparers or defaultValueComparer and handles
164 * failures using the default {@link FailureHandler}. This method ignores
165 * the table names, the columns order, the columns data type, and which
166 * columns are composing the primary keys.
167 *
168 * @param expectedTable
169 * {@link ITable} containing all expected results.
170 * @param actualTable
171 * {@link ITable} containing all actual results.
172 * @param defaultValueComparer
173 * {@link ValueComparer} to use with column value comparisons
174 * when the column name for the table is not in the
175 * columnValueComparers {@link Map}. Can be <code>null</code> and
176 * will default to {@link #getDefaultValueComparer()}.
177 * @param columnValueComparers
178 * {@link Map} of {@link ValueComparer}s to use for specific
179 * columns. Key is column name in the table, value is
180 * {@link ValueComparer} to use in comparing expected to actual
181 * column values. Can be <code>null</code> and will default to
182 * using
183 * {@link #getDefaultColumnValueComparerMapForTable(String)} or,
184 * if that is empty, defaultValueComparer for all columns in the
185 * table.
186 * @throws DatabaseUnitException
187 */
188 public void assertWithValueComparer(final ITable expectedTable,
189 final ITable actualTable, final ValueComparer defaultValueComparer,
190 final Map<String, ValueComparer> columnValueComparers)
191 throws DatabaseUnitException
192 {
193 final FailureHandler failureHandler = getDefaultFailureHandler();
194 assertWithValueComparer(expectedTable, actualTable, failureHandler,
195 defaultValueComparer, columnValueComparers);
196 }
197
198 /**
199 * Asserts the two specified {@link ITable}s comparing their columns using
200 * the specified columnValueComparers or defaultValueComparer and handles
201 * failures using the default {@link FailureHandler}, using
202 * additionalColumnInfo, if specified. This method ignores the table names,
203 * the columns order, the columns data type, and which columns are composing
204 * the primary keys.
205 *
206 * @param expectedTable
207 * {@link ITable} containing all expected results.
208 * @param actualTable
209 * {@link ITable} containing all actual results.
210 * @param additionalColumnInfo
211 * The columns to be printed out if the assert fails because of a
212 * data mismatch. Provides some additional column values that may
213 * be useful to quickly identify the columns for which the
214 * mismatch occurred (for example a primary key column). Can be
215 * <code>null</code>
216 * @param defaultValueComparer
217 * {@link ValueComparer} to use with column value comparisons
218 * when the column name for the table is not in the
219 * columnValueComparers {@link Map}. Can be <code>null</code> and
220 * will default to {@link #getDefaultValueComparer()}.
221 * @param columnValueComparers
222 * {@link Map} of {@link ValueComparer}s to use for specific
223 * columns. Key is column name in the table, value is
224 * {@link ValueComparer} to use in comparing expected to actual
225 * column values. Can be <code>null</code> and will default to
226 * using
227 * {@link #getDefaultColumnValueComparerMapForTable(String)} or,
228 * if that is empty, defaultValueComparer for all columns in the
229 * table.
230 * @throws DatabaseUnitException
231 */
232 public void assertWithValueComparer(final ITable expectedTable,
233 final ITable actualTable, final Column[] additionalColumnInfo,
234 final ValueComparer defaultValueComparer,
235 final Map<String, ValueComparer> columnValueComparers)
236 throws DatabaseUnitException
237 {
238 final FailureHandler failureHandler =
239 getDefaultFailureHandler(additionalColumnInfo);
240
241 assertWithValueComparer(expectedTable, actualTable, failureHandler,
242 defaultValueComparer, columnValueComparers);
243 }
244 }