KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > AbstractDataSetTest


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.dataset;
23
24 import org.dbunit.database.AmbiguousTableNameException;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * @author Manuel Laflamme
32  * @version $Revision: 1.22 $
33  * @since Feb 22, 2002
34  */

35 public abstract class AbstractDataSetTest extends AbstractTest
36 {
37     public AbstractDataSetTest(String JavaDoc s)
38     {
39         super(s);
40     }
41
42     protected int[] getExpectedDuplicateRows()
43     {
44         return new int[] {1, 0, 2};
45     }
46
47     /**
48      * This method exclude BLOB_TABLE and CLOB_TABLE from the specified dataset
49      * because BLOB and CLOB are not supported by all database vendor. It also excludes
50      * tables with Identity columns (MSSQL) becasuse they are specific to MSSQL.
51      * TODO : should be refactored into thee various DatabaseEnvironments!
52      */

53     public static IDataSet removeExtraTestTables(IDataSet dataSet) throws Exception JavaDoc
54     {
55         String JavaDoc[] names = dataSet.getTableNames();
56
57         // exclude BLOB_TABLE and CLOB_TABLE from test since not supported by
58
// all database vendor
59
List JavaDoc nameList = new ArrayList JavaDoc(Arrays.asList(names));
60         nameList.remove("BLOB_TABLE");
61         nameList.remove("CLOB_TABLE");
62         nameList.remove("DBUNIT.BLOB_TABLE");
63         nameList.remove("DBUNIT.CLOB_TABLE");
64         /*
65         this table shows up on MSSQLServer. It is a user table for storing diagram information
66         that really should be considered a system table.
67         */

68         nameList.remove("DBUNIT.dtproperties");
69         nameList.remove("dtproperties");
70         /*
71         These tables are created specifically for testing identity columns on MSSQL server.
72         They should be ignored on other platforms.
73         */

74         nameList.remove("DBUNIT.IDENTITY_TABLE");
75         nameList.remove("IDENTITY_TABLE");
76         nameList.remove("DBUNIT.TEST_IDENTITY_NOT_PK");
77         nameList.remove("TEST_IDENTITY_NOT_PK");
78
79         names = (String JavaDoc[])nameList.toArray(new String JavaDoc[0]);
80
81         return new FilteredDataSet(names, dataSet);
82     }
83
84     /**
85      * Create a dataset with duplicate tables having different char case in name
86      * @return
87      */

88     protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception JavaDoc
89     {
90         IDataSet dataSet = createDuplicateDataSet();
91         ITable[] tables = DataSetUtils.getTables(dataSet.iterator());
92         ITable lowerTable = tables[0];
93         dataSet = new DefaultDataSet(new ITable[]{
94             new CompositeTable(getDuplicateTableName().toLowerCase(), lowerTable),
95             tables[1],
96             tables[2],
97         });
98         return dataSet;
99     }
100
101     protected abstract IDataSet createDataSet() throws Exception JavaDoc;
102
103     protected abstract IDataSet createDuplicateDataSet() throws Exception JavaDoc;
104
105     protected void assertEqualsTableName(String JavaDoc mesage, String JavaDoc expected,
106             String JavaDoc actual)
107     {
108         assertEquals(mesage, expected, actual);
109     }
110
111     public void testGetTableNames() throws Exception JavaDoc
112     {
113         String JavaDoc[] expected = getExpectedNames();
114         assertContainsIgnoreCase("minimal names subset",
115                 super.getExpectedNames(), expected);
116
117         IDataSet dataSet = createDataSet();
118         String JavaDoc[] names = dataSet.getTableNames();
119
120         assertEquals("table count", expected.length, names.length);
121         for (int i = 0; i < expected.length; i++)
122         {
123             assertEqualsTableName("name " + i, expected[i], names[i]);
124         }
125     }
126
127     public void testGetTableNamesDefensiveCopy() throws Exception JavaDoc
128     {
129         IDataSet dataSet = createDataSet();
130         assertTrue("Should not be same intance",
131                 dataSet.getTableNames() != dataSet.getTableNames());
132     }
133
134     public void testGetTable() throws Exception JavaDoc
135     {
136         String JavaDoc[] expected = getExpectedNames();
137
138         IDataSet dataSet = createDataSet();
139         for (int i = 0; i < expected.length; i++)
140         {
141             ITable table = dataSet.getTable(expected[i]);
142             assertEqualsTableName("name " + i, expected[i], table.getTableMetaData().getTableName());
143         }
144     }
145
146     public void testGetUnknownTable() throws Exception JavaDoc
147     {
148         IDataSet dataSet = createDataSet();
149         try
150         {
151             dataSet.getTable("UNKNOWN_TABLE");
152             fail("Should throw a NoSuchTableException");
153         }
154         catch (NoSuchTableException e)
155         {
156         }
157     }
158
159     public void testGetTableMetaData() throws Exception JavaDoc
160     {
161         String JavaDoc[] expected = getExpectedNames();
162
163         IDataSet dataSet = createDataSet();
164         for (int i = 0; i < expected.length; i++)
165         {
166             ITableMetaData metaData = dataSet.getTableMetaData(expected[i]);
167             assertEqualsTableName("name " + i, expected[i], metaData.getTableName());
168         }
169     }
170
171     public void testGetUnknownTableMetaData() throws Exception JavaDoc
172     {
173         IDataSet dataSet = createDataSet();
174         try
175         {
176             dataSet.getTableMetaData("UNKNOWN_TABLE");
177             fail("Should throw a NoSuchTableException");
178         }
179         catch (NoSuchTableException e)
180         {
181         }
182     }
183
184     public void testGetTables() throws Exception JavaDoc
185     {
186         String JavaDoc[] expected = getExpectedNames();
187         assertContainsIgnoreCase("minimal names subset",
188                 super.getExpectedNames(), expected);
189
190         IDataSet dataSet = createDataSet();
191         ITable[] tables = dataSet.getTables();
192
193         assertEquals("table count", expected.length, tables.length);
194         for (int i = 0; i < expected.length; i++)
195         {
196             assertEqualsTableName("name " + i, expected[i],
197                     tables[i].getTableMetaData().getTableName());
198         }
199     }
200
201     public void testGetTablesDefensiveCopy() throws Exception JavaDoc
202     {
203         IDataSet dataSet = createDataSet();
204         assertTrue("Should not be same intance",
205                 dataSet.getTables() != dataSet.getTables());
206     }
207
208     public void testGetDuplicateTables() throws Exception JavaDoc
209     {
210         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
211         int[] expectedRows = getExpectedDuplicateRows();
212         assertEquals(expectedNames.length, expectedRows.length);
213
214         IDataSet dataSet = createDuplicateDataSet();
215         ITable[] tables = dataSet.getTables();
216
217         assertEquals("table count", expectedNames.length, tables.length);
218         for (int i = 0; i < expectedNames.length; i++)
219         {
220             ITable table = tables[i];
221             String JavaDoc name = table.getTableMetaData().getTableName();
222             assertEqualsTableName("name " + i, expectedNames[i], name);
223             assertEquals("row count", expectedRows[i], table.getRowCount());
224         }
225     }
226
227     public void testGetDuplicateTableNames() throws Exception JavaDoc
228     {
229         String JavaDoc[] expected = getExpectedDuplicateNames();
230
231         IDataSet dataSet = createDuplicateDataSet();
232         String JavaDoc[] names = dataSet.getTableNames();
233
234         assertEquals("table count", expected.length, names.length);
235         for (int i = 0; i < expected.length; i++)
236         {
237             assertEqualsTableName("name " + i, expected[i], names[i]);
238         }
239     }
240
241     public void testGetDuplicateTable() throws Exception JavaDoc
242     {
243         IDataSet dataSet = createDuplicateDataSet();
244         try
245         {
246             dataSet.getTable(getDuplicateTableName());
247             fail("Should throw AmbiguousTableNameException");
248         }
249         catch (AmbiguousTableNameException e)
250         {
251         }
252     }
253
254     public void testGetDuplicateTableMetaData() throws Exception JavaDoc
255     {
256         IDataSet dataSet = createDuplicateDataSet();
257         try
258         {
259             dataSet.getTableMetaData(getDuplicateTableName());
260             fail("Should throw AmbiguousTableNameException");
261         }
262         catch (AmbiguousTableNameException e)
263         {
264         }
265     }
266
267     public void testGetCaseInsensitiveTable() throws Exception JavaDoc
268     {
269         String JavaDoc[] expectedNames = getExpectedLowerNames();
270
271         IDataSet dataSet = createDataSet();
272         for (int i = 0; i < expectedNames.length; i++)
273         {
274             String JavaDoc expected = expectedNames[i];
275             ITable table = dataSet.getTable(expected);
276             String JavaDoc actual = table.getTableMetaData().getTableName();
277
278             if (!expected.equalsIgnoreCase(actual))
279             {
280                 assertEquals("name " + i, expected, actual);
281             }
282         }
283     }
284
285     public void testGetCaseInsensitiveTableMetaData() throws Exception JavaDoc
286     {
287         String JavaDoc[] expectedNames = getExpectedLowerNames();
288         IDataSet dataSet = createDataSet();
289
290         for (int i = 0; i < expectedNames.length; i++)
291         {
292             String JavaDoc expected = expectedNames[i];
293             ITableMetaData metaData = dataSet.getTableMetaData(expected);
294             String JavaDoc actual = metaData.getTableName();
295
296             if (!expected.equalsIgnoreCase(actual))
297             {
298                 assertEquals("name " + i, expected, actual);
299             }
300         }
301     }
302
303     public void testGetCaseInsensitiveDuplicateTable() throws Exception JavaDoc
304     {
305         IDataSet dataSet = createMultipleCaseDuplicateDataSet();
306
307         try
308         {
309             dataSet.getTable(getDuplicateTableName().toLowerCase());
310             fail("Should throw AmbiguousTableNameException");
311         }
312         catch (AmbiguousTableNameException e)
313         {
314         }
315     }
316
317     public void testGetCaseInsensitiveDuplicateTableMetaData() throws Exception JavaDoc
318     {
319         IDataSet dataSet = createMultipleCaseDuplicateDataSet();
320         try
321         {
322             dataSet.getTableMetaData(getDuplicateTableName().toLowerCase());
323             fail("Should throw AmbiguousTableNameException");
324         }
325         catch (AmbiguousTableNameException e)
326         {
327         }
328     }
329
330     public void testIterator() throws Exception JavaDoc
331     {
332         String JavaDoc[] expected = getExpectedNames();
333         assertContainsIgnoreCase("minimal names subset",
334                 super.getExpectedNames(), expected);
335
336         int i = 0;
337         ITableIterator iterator = createDataSet().iterator();
338         while(iterator.next())
339         {
340             assertEqualsTableName("name " + i, expected[i],
341                     iterator.getTableMetaData().getTableName());
342             i++;
343         }
344
345         assertEquals("table count", expected.length, i);
346     }
347
348     public void testIteratorAndDuplicateTable() throws Exception JavaDoc
349     {
350         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
351 // int[] expectedRows = getExpectedDuplicateRows();
352

353         int i = 0;
354         ITableIterator iterator = createDuplicateDataSet().iterator();
355         while(iterator.next())
356         {
357             ITable table = iterator.getTable();
358             String JavaDoc name = table.getTableMetaData().getTableName();
359             assertEqualsTableName("name " + i, expectedNames[i], name);
360 // assertEquals("row count", expectedRows[i], table.getRowCount());
361
i++;
362         }
363
364         assertEquals("table count", expectedNames.length, i);
365     }
366
367     public void testReverseIterator() throws Exception JavaDoc
368     {
369         String JavaDoc[] expected = DataSetUtils.reverseStringArray(getExpectedNames());
370         assertContainsIgnoreCase("minimal names subset",
371                 super.getExpectedNames(), expected);
372
373         int i = 0;
374         ITableIterator iterator = createDataSet().reverseIterator();
375         while(iterator.next())
376         {
377             assertEqualsTableName("name " + i, expected[i],
378                     iterator.getTableMetaData().getTableName());
379             i++;
380         }
381
382         assertEquals("table count", expected.length, i);
383     }
384
385     public void testReverseIteratorAndDuplicateTable() throws Exception JavaDoc
386     {
387         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
388         int[] expectedRows = getExpectedDuplicateRows();
389
390         int i = expectedRows.length - 1;
391         int count = 0;
392         ITableIterator iterator = createDuplicateDataSet().reverseIterator();
393         while(iterator.next())
394         {
395             ITable table = iterator.getTable();
396             String JavaDoc name = table.getTableMetaData().getTableName();
397             assertEqualsTableName("name " + i, expectedNames[i], name);
398             assertEquals("row count", expectedRows[i], table.getRowCount());
399             i--;
400             count++;
401         }
402
403         assertEquals("table count", expectedNames.length, count);
404     }
405 }
406
407
408
409
410
411
412
413
414
415
416
Popular Tags