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;
27  import java.util.Arrays;
28  import java.util.List;
29  
30  /**
31   * @author Manuel Laflamme
32   * @version $Revision: 1077 $
33   * @since Feb 22, 2002
34   */
35  public abstract class AbstractDataSetTest extends AbstractTest
36  {
37      public AbstractDataSetTest(String 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) because they are specific to MSSQL.
51       * TODO : should be refactored into the various DatabaseEnvironments!
52       */
53      public static IDataSet removeExtraTestTables(IDataSet dataSet) throws Exception
54      {
55          String[] names = dataSet.getTableNames();
56  
57          // exclude BLOB_TABLE and CLOB_TABLE from test since not supported by
58          // all database vendor
59          List nameList = new ArrayList(Arrays.asList(names));
60          nameList.remove("BLOB_TABLE");
61          nameList.remove("CLOB_TABLE");
62          nameList.remove("SDO_GEOMETRY_TABLE");
63          nameList.remove("DBUNIT.BLOB_TABLE");
64          nameList.remove("DBUNIT.CLOB_TABLE");
65          nameList.remove("DBUNIT.SDO_GEOMETRY");
66          /*
67          this table shows up on MSSQLServer.  It is a user table for storing diagram information
68          that really should be considered a system table.
69          */
70          nameList.remove("DBUNIT.dtproperties");
71          nameList.remove("dtproperties");
72          /*
73          These tables are created specifically for testing identity columns on MSSQL server.
74          They should be ignored on other platforms.
75          */
76          nameList.remove("DBUNIT.IDENTITY_TABLE");
77          nameList.remove("IDENTITY_TABLE");
78          nameList.remove("DBUNIT.TEST_IDENTITY_NOT_PK");
79          nameList.remove("TEST_IDENTITY_NOT_PK");
80  
81          names = (String[])nameList.toArray(new String[0]);
82  
83          return new FilteredDataSet(names, dataSet);
84      }
85  
86      protected abstract IDataSet createDataSet() throws Exception;
87  
88      protected abstract IDataSet createDuplicateDataSet() throws Exception;
89  
90      /**
91       * Create a dataset with duplicate tables having different char case in name
92       * @return
93       */
94      protected abstract IDataSet createMultipleCaseDuplicateDataSet() throws Exception;
95  
96      protected void assertEqualsTableName(String mesage, String expected,
97              String actual)
98      {
99          assertEquals(mesage, expected, actual);
100     }
101 
102     public void testGetTableNames() throws Exception
103     {
104         String[] expected = getExpectedNames();
105         assertContainsIgnoreCase("minimal names subset",
106                 super.getExpectedNames(), expected);
107 
108         IDataSet dataSet = createDataSet();
109         String[] names = dataSet.getTableNames();
110 
111         assertEquals("table count", expected.length, names.length);
112         for (int i = 0; i < expected.length; i++)
113         {
114             assertEqualsTableName("name " + i, expected[i], names[i]);
115         }
116     }
117 
118     public void testGetTableNamesDefensiveCopy() throws Exception
119     {
120         IDataSet dataSet = createDataSet();
121         assertTrue("Should not be same intance",
122                 dataSet.getTableNames() != dataSet.getTableNames());
123     }
124 
125     public void testGetTable() throws Exception
126     {
127         String[] expected = getExpectedNames();
128 
129         IDataSet dataSet = createDataSet();
130         for (int i = 0; i < expected.length; i++)
131         {
132             ITable table = dataSet.getTable(expected[i]);
133             assertEqualsTableName("name " + i, expected[i], table.getTableMetaData().getTableName());
134         }
135     }
136 
137     public void testGetUnknownTable() throws Exception
138     {
139         IDataSet dataSet = createDataSet();
140         try
141         {
142             dataSet.getTable("UNKNOWN_TABLE");
143             fail("Should throw a NoSuchTableException");
144         }
145         catch (NoSuchTableException e)
146         {
147         }
148     }
149 
150     public void testGetTableMetaData() throws Exception
151     {
152         String[] expected = getExpectedNames();
153 
154         IDataSet dataSet = createDataSet();
155         for (int i = 0; i < expected.length; i++)
156         {
157             ITableMetaData metaData = dataSet.getTableMetaData(expected[i]);
158             assertEqualsTableName("name " + i, expected[i], metaData.getTableName());
159         }
160     }
161 
162     public void testGetUnknownTableMetaData() throws Exception
163     {
164         IDataSet dataSet = createDataSet();
165         try
166         {
167             dataSet.getTableMetaData("UNKNOWN_TABLE");
168             fail("Should throw a NoSuchTableException");
169         }
170         catch (NoSuchTableException e)
171         {
172         }
173     }
174 
175     public void testGetTables() throws Exception
176     {
177         String[] expected = getExpectedNames();
178         assertContainsIgnoreCase("minimal names subset",
179                 super.getExpectedNames(), expected);
180 
181         IDataSet dataSet = createDataSet();
182         ITable[] tables = dataSet.getTables();
183 
184         assertEquals("table count", expected.length, tables.length);
185         for (int i = 0; i < expected.length; i++)
186         {
187             assertEqualsTableName("name " + i, expected[i],
188                     tables[i].getTableMetaData().getTableName());
189         }
190     }
191 
192     public void testGetTablesDefensiveCopy() throws Exception
193     {
194         IDataSet dataSet = createDataSet();
195         assertTrue("Should not be same instance",
196                 dataSet.getTables() != dataSet.getTables());
197     }
198 
199     public void testCreateDuplicateDataSet() throws Exception
200     {
201         try
202         {
203             /*IDataSet dataSet = */createDuplicateDataSet();
204             fail("Should throw AmbiguousTableNameException in creation phase");
205         }
206         catch (AmbiguousTableNameException expected)
207         {
208             assertEquals("DUPLICATE_TABLE", expected.getMessage());
209         }
210     }
211 
212     public void testCreateMultipleCaseDuplicateDataSet() throws Exception
213     {
214         try
215         {
216             /*IDataSet dataSet = */createMultipleCaseDuplicateDataSet();
217             fail("Should throw AmbiguousTableNameException in creation phase");
218         }
219         catch (AmbiguousTableNameException expected)
220         {
221             assertEquals("DUPLICATE_TABLE", expected.getMessage());
222         }
223     }
224 
225 
226     public void testGetCaseInsensitiveTable() throws Exception
227     {
228         String[] expectedNames = getExpectedLowerNames();
229 
230         IDataSet dataSet = createDataSet();
231         for (int i = 0; i < expectedNames.length; i++)
232         {
233             String expected = expectedNames[i];
234             ITable table = dataSet.getTable(expected);
235             String actual = table.getTableMetaData().getTableName();
236 
237             if (!expected.equalsIgnoreCase(actual))
238             {
239                 assertEquals("name " + i, expected, actual);
240             }
241         }
242     }
243 
244     public void testGetCaseInsensitiveTableMetaData() throws Exception
245     {
246         String[] expectedNames = getExpectedLowerNames();
247         IDataSet dataSet = createDataSet();
248 
249         for (int i = 0; i < expectedNames.length; i++)
250         {
251             String expected = expectedNames[i];
252             ITableMetaData metaData = dataSet.getTableMetaData(expected);
253             String actual = metaData.getTableName();
254 
255             if (!expected.equalsIgnoreCase(actual))
256             {
257                 assertEquals("name " + i, expected, actual);
258             }
259         }
260     }
261 
262     public void testIterator() throws Exception
263     {
264         String[] expected = getExpectedNames();
265         assertContainsIgnoreCase("minimal names subset",
266                 super.getExpectedNames(), expected);
267 
268         int i = 0;
269         ITableIterator iterator = createDataSet().iterator();
270         while(iterator.next())
271         {
272             assertEqualsTableName("name " + i, expected[i],
273                     iterator.getTableMetaData().getTableName());
274             i++;
275         }
276 
277         assertEquals("table count", expected.length, i);
278     }
279 
280     public void testReverseIterator() throws Exception
281     {
282         String[] expected = DataSetUtils.reverseStringArray(getExpectedNames());
283         assertContainsIgnoreCase("minimal names subset",
284                 super.getExpectedNames(), expected);
285 
286         int i = 0;
287         ITableIterator iterator = createDataSet().reverseIterator();
288         while(iterator.next())
289         {
290             assertEqualsTableName("name " + i, expected[i],
291                     iterator.getTableMetaData().getTableName());
292             i++;
293         }
294 
295         assertEquals("table count", expected.length, i);
296     }
297 
298     protected ITable[] createDuplicateTables(boolean multipleCase) throws AmbiguousTableNameException 
299     {
300         ITable table1 = new DefaultTable("DUPLICATE_TABLE");
301         ITable table2 = new DefaultTable("EMPTY_TABLE");
302         ITable table3;
303         if(!multipleCase){
304             table3 = new DefaultTable("DUPLICATE_TABLE");
305         }
306         else {
307             table3 = new DefaultTable("duplicate_TABLE");
308         }
309         ITable[] tables = new ITable[]{table1, table2, table3};
310         return tables;
311     }
312 }
313 
314 
315 
316 
317 
318 
319 
320 
321 
322