KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > AssertionTest


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 org.dbunit.dataset.Column;
25 import org.dbunit.dataset.CompositeDataSet;
26 import org.dbunit.dataset.CompositeTable;
27 import org.dbunit.dataset.DataSetUtils;
28 import org.dbunit.dataset.DefaultDataSet;
29 import org.dbunit.dataset.DefaultTable;
30 import org.dbunit.dataset.FilteredDataSet;
31 import org.dbunit.dataset.IDataSet;
32 import org.dbunit.dataset.ITable;
33 import org.dbunit.dataset.datatype.DataType;
34 import org.dbunit.dataset.xml.FlatXmlDataSet;
35
36 import junit.framework.AssertionFailedError;
37 import junit.framework.TestCase;
38
39 import java.io.FileReader JavaDoc;
40 import java.math.BigDecimal JavaDoc;
41
42 /**
43  * @author Manuel Laflamme
44  * @version $Revision: 1.12 $
45  * @since Mar 22, 2002
46  */

47 public class AssertionTest extends TestCase
48 {
49     public AssertionTest(String JavaDoc s)
50     {
51         super(s);
52     }
53
54     private IDataSet getDataSet() throws Exception JavaDoc
55     {
56         return new FlatXmlDataSet(new FileReader JavaDoc(
57                 "src/xml/assertionTest.xml"));
58     }
59
60     ////////////////////////////////////////////////////////////////////////////
61
// Test methods
62

63     public void testAssertTablesEquals() throws Exception JavaDoc
64     {
65         IDataSet dataSet = getDataSet();
66         Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
67                 dataSet.getTable("TEST_TABLE_WITH_SAME_VALUE"));
68     }
69
70     public void testAssertTablesEqualsColumnNamesCaseInsensitive() throws Exception JavaDoc
71     {
72         IDataSet dataSet = getDataSet();
73         Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
74                 dataSet.getTable("TEST_TABLE_WITH_LOWER_COLUMN_NAMES"));
75     }
76
77     public void testAssertTablesAndNamesNotEquals() throws Exception JavaDoc
78     {
79         IDataSet dataSet = getDataSet();
80         Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
81                 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_NAME"));
82     }
83
84     public void testAssertTablesAndColumnCountNotEquals() throws Exception JavaDoc
85     {
86         IDataSet dataSet = getDataSet();
87
88         try
89         {
90             Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
91                     dataSet.getTable("TEST_TABLE_WITH_3_COLUMNS"));
92             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
93         }
94         catch (AssertionFailedError e)
95         {
96         }
97     }
98
99     public void testAssertTablesAndColumnSequenceNotEquals() throws Exception JavaDoc
100     {
101         IDataSet dataSet = getDataSet();
102
103         Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
104                 dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_COLUMN_SEQUENCE"));
105     }
106
107     public void testAssertTablesAndColumnNamesNotEquals() throws Exception JavaDoc
108     {
109         IDataSet dataSet = getDataSet();
110
111         try
112         {
113             Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
114                     dataSet.getTable("TEST_TABLE_WITH_DIFFERENT_COLUMN_NAMES"));
115             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
116         }
117         catch (AssertionFailedError e)
118         {
119         }
120     }
121
122     public void testAssertTablesAndRowCountNotEquals() throws Exception JavaDoc
123     {
124         IDataSet dataSet = getDataSet();
125
126         try
127         {
128             Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
129                     dataSet.getTable("TEST_TABLE_WITH_ONE_ROW"));
130             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
131         }
132         catch (AssertionFailedError e)
133         {
134         }
135     }
136
137     public void testAssertTablesAndValuesNotEquals() throws Exception JavaDoc
138     {
139         IDataSet dataSet = getDataSet();
140
141         try
142         {
143             Assertion.assertEquals(dataSet.getTable("TEST_TABLE"),
144                     dataSet.getTable("TEST_TABLE_WITH_WRONG_VALUE"));
145             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
146         }
147         catch (AssertionFailedError e)
148         {
149         }
150     }
151
152     public void testAssertTablesEqualsAndIncompatibleDataType() throws Exception JavaDoc
153     {
154         String JavaDoc tableName = "TABLE_NAME";
155
156         // Setup actual table
157
Column[] actualColumns = new Column[] {
158             new Column("BOOLEAN", DataType.BOOLEAN),
159         };
160         Object JavaDoc[] actualRow = new Object JavaDoc[] {
161             Boolean.TRUE,
162         };
163         DefaultTable actualTable = new DefaultTable(tableName,
164                 actualColumns);
165         actualTable.addRow(actualRow);
166
167         // Setup expected table
168
Column[] expectedColumns = new Column[] {
169             new Column("BOOLEAN", DataType.VARCHAR),
170         };
171         Object JavaDoc[] expectedRow = new Object JavaDoc[] {
172             "1",
173         };
174         DefaultTable expectedTable = new DefaultTable(tableName,
175                 expectedColumns);
176         expectedTable.addRow(expectedRow);
177
178
179         try
180         {
181             Assertion.assertEquals(expectedTable, actualTable);
182         }
183         catch (AssertionFailedError e)
184         {
185
186         }
187     }
188
189     public void testAssertTablesEqualsAndCompatibleDataType() throws Exception JavaDoc
190     {
191         String JavaDoc tableName = "TABLE_NAME";
192         java.sql.Timestamp JavaDoc now = new java.sql.Timestamp JavaDoc(
193                 System.currentTimeMillis());
194
195         // Setup actual table
196
Column[] actualColumns = new Column[] {
197             new Column("BOOLEAN", DataType.BOOLEAN),
198             new Column("TIMESTAMP", DataType.TIMESTAMP),
199             new Column("STRING", DataType.CHAR),
200             new Column("NUMERIC", DataType.NUMERIC),
201         };
202         Object JavaDoc[] actualRow = new Object JavaDoc[] {
203             Boolean.TRUE,
204             now,
205             "0",
206             new BigDecimal JavaDoc("123.4"),
207         };
208         DefaultTable actualTable = new DefaultTable(tableName,
209                 actualColumns);
210         actualTable.addRow(actualRow);
211
212
213         // Setup expected table
214
Column[] expectedColumns = new Column[] {
215             new Column("BOOLEAN", DataType.UNKNOWN),
216             new Column("TIMESTAMP", DataType.UNKNOWN),
217             new Column("STRING", DataType.UNKNOWN),
218             new Column("NUMERIC", DataType.UNKNOWN),
219         };
220         Object JavaDoc[] expectedRow = new Object JavaDoc[] {
221             "1",
222             new Long JavaDoc(now.getTime()),
223             new Integer JavaDoc("0"),
224             "123.4000",
225         };
226         DefaultTable expectedTable = new DefaultTable(tableName,
227                 expectedColumns);
228         expectedTable.addRow(expectedRow);
229
230         Assertion.assertEquals(expectedTable, actualTable);
231     }
232
233     public void testAssertDataSetsEquals() throws Exception JavaDoc
234     {
235         IDataSet dataSet1 = getDataSet();
236
237         // change table names order
238
String JavaDoc[] names = DataSetUtils.getReverseTableNames(dataSet1);
239         IDataSet dataSet2 = new FilteredDataSet(names, dataSet1);
240
241         assertTrue("assert not same", dataSet1 != dataSet2);
242         Assertion.assertEquals(dataSet1, dataSet2);
243     }
244
245     public void testAssertDataSetsEqualsTableNamesCaseInsensitive() throws Exception JavaDoc
246     {
247         IDataSet dataSet1 = getDataSet();
248
249         // change table names case
250
String JavaDoc[] names = dataSet1.getTableNames();
251         for (int i = 0; i < names.length; i++)
252         {
253             names[i] = names[i].toLowerCase();
254         }
255         IDataSet dataSet2 = new FilteredDataSet(names, dataSet1);
256
257         assertTrue("assert not same", dataSet1 != dataSet2);
258         Assertion.assertEquals(dataSet1, dataSet2);
259     }
260
261     public void testAssertDataSetsAndTableCountNotEquals() throws Exception JavaDoc
262     {
263         IDataSet dataSet1 = getDataSet();
264
265         // only one table
266
String JavaDoc[] names = new String JavaDoc[]{dataSet1.getTableNames()[0]};
267         IDataSet dataSet2 = new FilteredDataSet(names, dataSet1);
268
269         assertTrue("assert not same", dataSet1 != dataSet2);
270
271         try
272         {
273             Assertion.assertEquals(dataSet1, dataSet2);
274             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
275         }
276         catch (AssertionFailedError e)
277         {
278         }
279     }
280
281     public void testAssertDataSetsAndTableNamesNotEquals() throws Exception JavaDoc
282     {
283         IDataSet dataSet1 = getDataSet();
284
285         // reverse table names
286
String JavaDoc[] names = dataSet1.getTableNames();
287         ITable[] tables = new ITable[names.length];
288         for (int i = 0; i < names.length; i++)
289         {
290             String JavaDoc reversedName = new StringBuffer JavaDoc(names[i]).reverse().toString();
291             tables[i] = new CompositeTable(reversedName,
292                     dataSet1.getTable(names[i]));
293         }
294         IDataSet dataSet2 = new DefaultDataSet(tables);
295
296         assertTrue("assert not same", dataSet1 != dataSet2);
297         assertEquals("table count", dataSet1.getTableNames().length,
298                 dataSet2.getTableNames().length);
299
300         try
301         {
302             Assertion.assertEquals(dataSet1, dataSet2);
303             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
304         }
305         catch (AssertionFailedError e)
306         {
307         }
308     }
309
310     public void testAssertDataSetsAndTablesNotEquals() throws Exception JavaDoc
311     {
312         IDataSet dataSet1 = getDataSet();
313
314         // different row counts (double)
315
IDataSet dataSet2 = new CompositeDataSet(dataSet1, dataSet1);
316
317         assertTrue("assert not same", dataSet1 != dataSet2);
318         assertEquals("table count", dataSet1.getTableNames().length,
319                 dataSet2.getTableNames().length);
320
321         try
322         {
323             Assertion.assertEquals(dataSet1, dataSet2);
324             throw new IllegalStateException JavaDoc("Should throw an AssertionFailedError");
325         }
326         catch (AssertionFailedError e)
327         {
328         }
329     }
330 }
331
332
333
334
335
336
Popular Tags