KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > Assertion


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit;
23
24 import junit.framework.Assert;
25 import org.dbunit.dataset.*;
26 import org.dbunit.dataset.datatype.DataType;
27 import org.dbunit.dataset.datatype.UnknownDataType;
28
29 import java.util.Arrays JavaDoc;
30 import java.util.Comparator JavaDoc;
31
32 /**
33  * @author Manuel Laflamme
34  * @version $Revision: 1.13 $
35  * @since Mar 22, 2002
36  */

37 public class Assertion
38 {
39     private static final ColumnComparator COLUMN_COMPARATOR = new ColumnComparator();
40
41     private Assertion()
42     {
43     }
44
45     /**
46      * Asserts that the two specified dataset are equals. This method ignore
47      * the tables order.
48      */

49     public static void assertEquals(IDataSet expectedDataSet,
50             IDataSet actualDataSet) throws DatabaseUnitException
51     {
52         // do not continue if same instance
53
if (expectedDataSet == actualDataSet)
54         {
55             return;
56         }
57
58         String JavaDoc[] expectedNames = getSortedUpperTableNames(expectedDataSet);
59         String JavaDoc[] actualNames = getSortedUpperTableNames(actualDataSet);
60
61         // tables count
62
Assert.assertEquals("table count", expectedNames.length, actualNames.length);
63
64
65         // table names in no specific order
66
for (int i = 0; i < expectedNames.length; i++)
67         {
68             if (!actualNames[i].equals(expectedNames[i]))
69             {
70                 Assert.fail("expected tables " + Arrays.asList(expectedNames) +
71                         " but was " + Arrays.asList(actualNames));
72             }
73
74         }
75
76         // tables
77
for (int i = 0; i < expectedNames.length; i++)
78         {
79             String JavaDoc name = expectedNames[i];
80             assertEquals(expectedDataSet.getTable(name),
81                     actualDataSet.getTable(name));
82         }
83
84     }
85
86
87     /**
88      * Asserts that the two specified tables are equals. This method ignore the
89      * table names, the columns order, the columns data type and which columns
90      * are composing the primary keys.
91      */

92     public static void assertEquals(ITable expectedTable, ITable actualTable)
93             throws DatabaseUnitException
94     {
95         // Do not continue if same instance
96
if (expectedTable == actualTable)
97         {
98             return;
99         }
100
101         ITableMetaData expectedMetaData = expectedTable.getTableMetaData();
102         ITableMetaData actualMetaData = actualTable.getTableMetaData();
103         String JavaDoc expectedTableName = expectedMetaData.getTableName();
104
105 // // verify table name
106
// Assert.assertEquals("table name", expectedMetaData.getTableName(),
107
// actualMetaData.getTableName());
108

109         // Verify columns
110
Column[] expectedColumns = getSortedColumns(expectedMetaData);
111         Column[] actualColumns = getSortedColumns(actualMetaData);
112         Assert.assertEquals("column count (table=" + expectedTableName + ")",
113                 expectedColumns.length, actualColumns.length);
114
115         for (int i = 0; i < expectedColumns.length; i++)
116         {
117             String JavaDoc expectedName = expectedColumns[i].getColumnName();
118             String JavaDoc actualName = actualColumns[i].getColumnName();
119             if (!expectedName.equalsIgnoreCase(actualName))
120             {
121                 Assert.fail("expected columns " + getColumnNamesAsString(expectedColumns) +
122                         " but was " + getColumnNamesAsString(actualColumns) +
123                         " (table=" + expectedTableName + ")");
124             }
125         }
126
127         // Verify row count
128
Assert.assertEquals("row count (table=" + expectedTableName + ")",
129                 expectedTable.getRowCount(), actualTable.getRowCount());
130
131         // values as strings
132
for (int i = 0; i < expectedTable.getRowCount(); i++)
133         {
134             for (int j = 0; j < expectedColumns.length; j++)
135             {
136                 Column expectedColumn = expectedColumns[j];
137                 Column actualColumn = actualColumns[j];
138
139                 String JavaDoc columnName = expectedColumn.getColumnName();
140                 Object JavaDoc expectedValue = expectedTable.getValue(i, columnName);
141                 Object JavaDoc actualValue = actualTable.getValue(i, columnName);
142
143                 DataType dataType = getComparisonDataType(
144                         expectedTableName, expectedColumn, actualColumn);
145                 if (dataType.compare(expectedValue, actualValue) != 0)
146                 {
147                     Assert.fail("value (table=" + expectedTableName + ", " +
148                             "row=" + i + ", col=" + columnName + "): expected:<" +
149                             expectedValue + "> but was:<" + actualValue + ">");
150                 }
151             }
152         }
153     }
154
155     static DataType getComparisonDataType(String JavaDoc tableName, Column expectedColumn,
156             Column actualColumn)
157     {
158         DataType expectedDataType = expectedColumn.getDataType();
159         DataType actualDataType = actualColumn.getDataType();
160
161         // The two columns have different data type
162
if (!expectedDataType.getClass().isInstance(actualDataType))
163         {
164             // Expected column data type is unknown, use actual column data type
165
if (expectedDataType instanceof UnknownDataType)
166             {
167                 return actualDataType;
168             }
169
170             // Actual column data type is unknown, use expected column data type
171
if (actualDataType instanceof UnknownDataType)
172             {
173                 return expectedDataType;
174             }
175
176             // Impossible to determine which data type to use
177
Assert.fail("Incompatible data types: " + expectedDataType + ", " +
178                     actualDataType + " (table=" + tableName + ", col=" +
179                     expectedColumn.getColumnName() + ")");
180         }
181 // // Both columns have unknown data type, use string comparison
182
// else if (expectedDataType instanceof UnknownDataType)
183
// {
184
// return DataType.LONGVARCHAR;
185
// }
186

187         // Both columns have same data type, return any one of them
188
return expectedDataType;
189     }
190
191     private static Column[] getSortedColumns(ITableMetaData metaData)
192             throws DataSetException
193     {
194         Column[] columns = metaData.getColumns();
195         Column[] sortColumns = new Column[columns.length];
196         System.arraycopy(columns, 0, sortColumns, 0, columns.length);
197         Arrays.sort(sortColumns, COLUMN_COMPARATOR);
198         return sortColumns;
199     }
200
201     private static String JavaDoc getColumnNamesAsString(Column[] columns)
202     {
203         String JavaDoc[] names = new String JavaDoc[columns.length];
204         for (int i = 0; i < columns.length; i++)
205         {
206             Column column = columns[i];
207             names[i] = column.getColumnName();
208         }
209         return Arrays.asList(names).toString();
210     }
211
212     private static String JavaDoc[] getSortedUpperTableNames(IDataSet dataSet)
213             throws DataSetException
214     {
215         String JavaDoc[] names = dataSet.getTableNames();
216         for (int i = 0; i < names.length; i++)
217         {
218             names[i] = names[i].toUpperCase();
219         }
220         Arrays.sort(names);
221         return names;
222     }
223
224     ////////////////////////////////////////////////////////////////////////////
225
// ColumnComparator class
226

227     private static class ColumnComparator implements Comparator JavaDoc
228     {
229         public int compare(Object JavaDoc o1, Object JavaDoc o2)
230         {
231             Column column1 = (Column)o1;
232             Column column2 = (Column)o2;
233
234             String JavaDoc columnName1 = column1.getColumnName();
235             String JavaDoc columnName2 = column2.getColumnName();
236             return columnName1.compareToIgnoreCase(columnName2);
237         }
238     }
239 }
240
241
242
243
244
Popular Tags