KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ant > DbUnitTaskTest


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.ant;
23
24 import org.dbunit.DatabaseEnvironment;
25 import org.dbunit.database.DatabaseConfig;
26 import org.dbunit.database.IDatabaseConnection;
27 import org.dbunit.dataset.datatype.IDataTypeFactory;
28 import org.dbunit.ext.mssql.InsertIdentityOperation;
29 import org.dbunit.ext.oracle.OracleDataTypeFactory;
30 import org.dbunit.operation.DatabaseOperation;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Target;
36 import org.apache.tools.ant.taskdefs.TaskdefsTest;
37
38 import java.sql.SQLException JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * Ant-based test class for the Dbunit ant task definition.
45  *
46  * @author Timothy Ruppert
47  * @author Ben Cox
48  * @version $Revision: 1.17 $
49  * @since Jun 10, 2002
50  * @see org.dbunit.ant.AntTest
51  */

52 public class DbUnitTaskTest extends TaskdefsTest
53 {
54     static protected Class JavaDoc classUnderTest = DbUnitTaskTest.class;
55
56     public DbUnitTaskTest(String JavaDoc name)
57     {
58         super(name);
59     }
60
61     public void setUp() throws Exception JavaDoc
62     {
63         // This line ensure test database is initialized
64
DatabaseEnvironment.getInstance();
65
66         configureProject("src/xml/antTestBuildFile.xml");
67     }
68
69     public void testNoDriver()
70     {
71         expectBuildException("no-driver", "Should have required a driver attribute.");
72     }
73
74     public void testNoDbUrl()
75     {
76         expectBuildException("no-db-url", "Should have required a url attribute.");
77     }
78
79     public void testNoUserid()
80     {
81         expectBuildException("no-userid", "Should have required a userid attribute.");
82     }
83
84     public void testNoPassword()
85     {
86         expectBuildException("no-password", "Should have required a password attribute.");
87     }
88
89     public void testInvalidDatabaseInformation()
90     {
91         Throwable JavaDoc sql = null;
92         try
93         {
94             executeTarget("invalid-db-info");
95         }
96         catch (BuildException e)
97         {
98             sql = e.getException();
99         }
100         finally
101         {
102             assertNotNull("Should have thrown a SQLException.", sql);
103             assertTrue("Should have thrown a SQLException.", (sql instanceof SQLException JavaDoc));
104         }
105     }
106
107     public void testInvalidOperationType()
108     {
109         Throwable JavaDoc iae = null;
110         try
111         {
112             executeTarget("invalid-type");
113         }
114         catch (BuildException e)
115         {
116             iae = e.getException();
117         }
118         finally
119         {
120             assertNotNull("Should have thrown an IllegalArgumentException.", iae);
121             assertTrue("Should have thrown an IllegalArgumentException.",
122                     (iae instanceof IllegalArgumentException JavaDoc));
123         }
124     }
125
126     public void testSetFlatFalse()
127     {
128         String JavaDoc targetName = "set-format-xml";
129         Operation operation = (Operation)getFirstStepFromTarget(targetName);
130         assertTrue("Operation attribute format should have been 'xml', but was: "
131                 + operation.getFormat(), operation.getFormat().equalsIgnoreCase("xml"));
132     }
133
134     public void testResolveOperationTypes()
135     {
136         assertOperationType("Should have been an DELETE_ALL operation",
137                 "test-type-none", DatabaseOperation.NONE);
138         assertOperationType("Should have been an DELETE_ALL operation",
139                 "test-type-delete-all", DatabaseOperation.DELETE_ALL);
140         assertOperationType("Should have been an INSERT operation",
141                 "test-type-insert", DatabaseOperation.INSERT);
142         assertOperationType("Should have been an UPDATE operation",
143                 "test-type-update", DatabaseOperation.UPDATE);
144         assertOperationType("Should have been an REFRESH operation",
145                 "test-type-refresh", DatabaseOperation.REFRESH);
146         assertOperationType("Should have been an CLEAN_INSERT operation",
147                 "test-type-clean-insert", DatabaseOperation.CLEAN_INSERT);
148         assertOperationType("Should have been an DELETE operation",
149                 "test-type-delete", DatabaseOperation.DELETE);
150         assertOperationType("Should have been an MSSQL_INSERT operation",
151                 "test-type-mssql-insert", InsertIdentityOperation.INSERT);
152         assertOperationType("Should have been an MSSQL_REFRESH operation",
153                 "test-type-mssql-refresh", InsertIdentityOperation.REFRESH);
154         assertOperationType("Should have been an MSSQL_CLEAN_INSERT operation",
155                 "test-type-mssql-clean-insert", InsertIdentityOperation.CLEAN_INSERT);
156     }
157
158     public void testInvalidCompositeOperationSrc()
159     {
160         expectBuildException("invalid-composite-operation-src",
161                 "Should have objected to nested operation src attribute "
162                 + "being set.");
163     }
164
165     public void testInvalidCompositeOperationFlat()
166     {
167         expectBuildException("invalid-composite-operation-format-flat",
168                 "Should have objected to nested operation format attribute "
169                 + "being set.");
170     }
171
172     public void testExportFull()
173     {
174         String JavaDoc targetName = "test-export-full";
175         Export export = (Export)getFirstStepFromTarget(targetName);
176         assertTrue("Should have been a flat format, "
177                 + "but was: " + export.getFormat(),
178                 export.getFormat().equalsIgnoreCase("flat"));
179         List JavaDoc tables = export.getTables();
180         assertTrue("Should have been an empty table list "
181                 + "(indicating a full dataset), but was: "
182                 + tables, tables.size() == 0);
183     }
184
185     public void testExportPartial()
186     {
187         String JavaDoc targetName = "test-export-partial";
188         Export export = (Export)getFirstStepFromTarget(targetName);
189         List JavaDoc tables = export.getTables();
190         assertEquals("table count", 2, tables.size());
191         Table testTable = (Table)tables.get(0);
192         Table pkTable = (Table)tables.get(1);
193         assertTrue("Should have been been TABLE TEST_TABLE, but was: "
194                 + testTable.getName(), testTable.getName().equals("TEST_TABLE"));
195         assertTrue("Should have been been TABLE PK_TABLE, but was: "
196                 + pkTable.getName(), pkTable.getName().equals("PK_TABLE"));
197     }
198
199     public void testExportFlat()
200     {
201         String JavaDoc targetName = "test-export-format-flat";
202         Export export = (Export)getFirstStepFromTarget(targetName);
203         assertEquals("format", "flat", export.getFormat());
204     }
205
206     public void testExportFlatWithDocytpe()
207     {
208         String JavaDoc targetName = "test-export-format-flat-with-doctype";
209         Export export = (Export)getFirstStepFromTarget(targetName);
210         assertEquals("format", "flat", export.getFormat());
211         assertEquals("doctype", "dataset.dtd", export.getDoctype());
212     }
213
214     public void testExportXml()
215     {
216         String JavaDoc targetName = "test-export-format-xml";
217         Export export = (Export)getFirstStepFromTarget(targetName);
218         assertTrue("Should have been an xml format, "
219                 + "but was: " + export.getFormat(),
220                 export.getFormat().equalsIgnoreCase("xml"));
221     }
222
223     public void testExportDtd()
224     {
225         String JavaDoc targetName = "test-export-format-dtd";
226         Export export = (Export)getFirstStepFromTarget(targetName);
227         assertTrue("Should have been a dtd format, "
228                 + "but was: " + export.getFormat(),
229                 export.getFormat().equalsIgnoreCase("dtd"));
230     }
231
232 /*
233     public void testExportCsv () {
234         String targetName = "test-export-format-csv";
235         Export export = (Export)getFirstStepFromTarget(targetName);
236         assertTrue("Should have been a csv format, "
237                 + "but was: " + export.getFormat(),
238                 export.getFormat().equalsIgnoreCase("csv"));
239     }
240 */

241
242     public void testInvalidExportFormat()
243     {
244         expectBuildException("invalid-export-format",
245                 "Should have objected to invalid format attribute.");
246     }
247
248     public void testExportQuery()
249     {
250         String JavaDoc targetName = "test-export-query";
251         Export export = (Export)getFirstStepFromTarget(targetName);
252         assertEquals("format", "flat", export.getFormat());
253
254         List JavaDoc queries = export.getTables();
255         assertEquals("query count", 2, getQueryCount(queries));
256
257         Query testTable = (Query)queries.get(0);
258         assertEquals("name", "TEST_TABLE", testTable.getName());
259         assertEquals("sql", "SELECT * FROM test_table ORDER BY column0 DESC", testTable.getSql());
260
261         Query pkTable = (Query)queries.get(1);
262         assertEquals("name", "PK_TABLE", pkTable.getName());
263         assertEquals("sql", "SELECT * FROM pk_table", pkTable.getSql());
264     }
265
266     public void testExportQueryMixed()
267     {
268         String JavaDoc targetName = "test-export-query-mixed";
269         Export export = (Export)getFirstStepFromTarget(targetName);
270         assertEquals("format", "flat", export.getFormat());
271
272         List JavaDoc tables = export.getTables();
273         assertEquals("total count", 2, tables.size());
274         assertEquals("table count", 1, getTableCount(tables));
275         assertEquals("query count", 1, getQueryCount(tables));
276
277         Table testTable = (Table)tables.get(0);
278         assertEquals("name", "TEST_TABLE", testTable.getName());
279
280         Query pkTable = (Query)tables.get(1);
281         assertEquals("name", "PK_TABLE", pkTable.getName());
282     }
283
284     public void testDataTypeFactory() throws Exception JavaDoc
285     {
286         String JavaDoc targetName = "test-datatypefactory";
287         DbUnitTask task = getFirstTargetTask(targetName);
288
289         IDatabaseConnection connection = task.createConnection();
290         IDataTypeFactory factory = (IDataTypeFactory)connection.getConfig().getProperty(
291                         DatabaseConfig.PROPERTY_DATATYPE_FACTORY);
292
293         Class JavaDoc expectedClass = OracleDataTypeFactory.class;
294         assertEquals("factory", expectedClass, factory.getClass());
295     }
296
297     public void testEscapePattern() throws Exception JavaDoc
298     {
299         String JavaDoc targetName = "test-escapepattern";
300         DbUnitTask task = getFirstTargetTask(targetName);
301
302         IDatabaseConnection connection = task.createConnection();
303         String JavaDoc actualPattern = (String JavaDoc)connection.getConfig().getProperty(
304                         DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
305
306         String JavaDoc expectedPattern = "[?]";
307         assertEquals("factory", expectedPattern, actualPattern);
308     }
309
310     public void testClasspath() throws Exception JavaDoc
311     {
312         String JavaDoc targetName = "test-classpath";
313
314         try
315         {
316             executeTarget(targetName);
317             fail("Should not be able to connect with invalid url!");
318         }
319         catch (BuildException e)
320         {
321             // Verify exception type
322
assertEquals("nested exception type", SQLException JavaDoc.class, e.getException().getClass());
323         }
324
325     }
326
327     public void testDriverNotInClasspath() throws Exception JavaDoc
328     {
329         String JavaDoc targetName = "test-drivernotinclasspath";
330
331         try
332         {
333             executeTarget(targetName);
334             fail("Should not have found driver!");
335         }
336         catch (BuildException e)
337         {
338             // Verify exception type
339
assertEquals("nested exception type", ClassNotFoundException JavaDoc.class, e.getException().getClass());
340         }
341     }
342
343
344     protected void assertOperationType(String JavaDoc failMessage, String JavaDoc targetName, DatabaseOperation expected)
345     {
346         Operation oper = (Operation)getFirstStepFromTarget(targetName);
347         DatabaseOperation dbOper = oper.getDbOperation();
348         assertTrue(failMessage + ", but was: " + dbOper, expected.equals(dbOper));
349     }
350
351     protected int getQueryCount(List JavaDoc tables)
352     {
353         int count = 0;
354         for (Iterator JavaDoc it = tables.iterator(); it.hasNext();)
355         {
356             if (it.next() instanceof Query)
357             {
358                 count++;
359             }
360         }
361
362         return count;
363     }
364
365     protected int getTableCount(List JavaDoc tables)
366     {
367         int count = 0;
368         for (Iterator JavaDoc it = tables.iterator(); it.hasNext();)
369         {
370             if (it.next() instanceof Table)
371             {
372                 count++;
373             }
374         }
375
376         return count;
377     }
378
379     protected DbUnitTaskStep getFirstStepFromTarget(String JavaDoc targetName)
380     {
381         DbUnitTaskStep result = null;
382         DbUnitTask task = getFirstTargetTask(targetName);
383         List JavaDoc steps = task.getSteps();
384         if (steps != null && steps.size() > 0)
385         {
386             result = (DbUnitTaskStep)steps.get(0);
387         }
388         else
389         {
390             fail("Can't get a dbunit <step> from the target: " + targetName);
391         }
392         return result;
393     }
394
395     private DbUnitTask getFirstTargetTask(String JavaDoc targetName)
396     {
397         Hashtable JavaDoc targets = project.getTargets();
398         executeTarget(targetName);
399         Target target = (Target)targets.get(targetName);
400         DbUnitTask task = (DbUnitTask)target.getTasks()[0];
401         return task;
402     }
403
404     public static Test suite()
405     {
406         TestSuite suite = new TestSuite(classUnderTest);
407         return suite;
408     }
409
410     public static void main(String JavaDoc args[])
411     {
412         if (args.length > 0 && args[0].equals("-gui"))
413         {
414             String JavaDoc[] testCaseName = {classUnderTest.getName()};
415             junit.swingui.TestRunner.main(testCaseName);
416         }
417         else
418         {
419             junit.textui.TestRunner.run(suite());
420         }
421     }
422 }
423
Popular Tags