1 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 ; 39 import java.util.Hashtable ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 43 52 public class DbUnitTaskTest extends TaskdefsTest 53 { 54 static protected Class classUnderTest = DbUnitTaskTest.class; 55 56 public DbUnitTaskTest(String name) 57 { 58 super(name); 59 } 60 61 public void setUp() throws Exception 62 { 63 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 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 )); 104 } 105 } 106 107 public void testInvalidOperationType() 108 { 109 Throwable 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 )); 123 } 124 } 125 126 public void testSetFlatFalse() 127 { 128 String 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 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 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 targetName = "test-export-partial"; 188 Export export = (Export)getFirstStepFromTarget(targetName); 189 List 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 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 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 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 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 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 targetName = "test-export-query"; 251 Export export = (Export)getFirstStepFromTarget(targetName); 252 assertEquals("format", "flat", export.getFormat()); 253 254 List 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 targetName = "test-export-query-mixed"; 269 Export export = (Export)getFirstStepFromTarget(targetName); 270 assertEquals("format", "flat", export.getFormat()); 271 272 List 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 285 { 286 String 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 expectedClass = OracleDataTypeFactory.class; 294 assertEquals("factory", expectedClass, factory.getClass()); 295 } 296 297 public void testEscapePattern() throws Exception 298 { 299 String targetName = "test-escapepattern"; 300 DbUnitTask task = getFirstTargetTask(targetName); 301 302 IDatabaseConnection connection = task.createConnection(); 303 String actualPattern = (String )connection.getConfig().getProperty( 304 DatabaseConfig.PROPERTY_ESCAPE_PATTERN); 305 306 String expectedPattern = "[?]"; 307 assertEquals("factory", expectedPattern, actualPattern); 308 } 309 310 public void testClasspath() throws Exception 311 { 312 String 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 assertEquals("nested exception type", SQLException .class, e.getException().getClass()); 323 } 324 325 } 326 327 public void testDriverNotInClasspath() throws Exception 328 { 329 String targetName = "test-drivernotinclasspath"; 330 331 try 332 { 333 executeTarget(targetName); 334 fail("Should not have found driver!"); 335 } 336 catch (BuildException e) 337 { 338 assertEquals("nested exception type", ClassNotFoundException .class, e.getException().getClass()); 340 } 341 } 342 343 344 protected void assertOperationType(String failMessage, String 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 tables) 352 { 353 int count = 0; 354 for (Iterator 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 tables) 366 { 367 int count = 0; 368 for (Iterator 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 targetName) 380 { 381 DbUnitTaskStep result = null; 382 DbUnitTask task = getFirstTargetTask(targetName); 383 List 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 targetName) 396 { 397 Hashtable 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 args[]) 411 { 412 if (args.length > 0 && args[0].equals("-gui")) 413 { 414 String [] 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 |