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