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