1 21 22 package org.dbunit.ant; 23 24 import org.apache.tools.ant.AntClassLoader; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.Task; 28 import org.apache.tools.ant.types.Path; 29 import org.apache.tools.ant.types.Reference; 30 import org.dbunit.DatabaseUnitException; 31 import org.dbunit.database.DatabaseConfig; 32 import org.dbunit.database.DatabaseConnection; 33 import org.dbunit.database.ForwardOnlyResultSetTableFactory; 34 import org.dbunit.database.IDatabaseConnection; 35 import org.dbunit.dataset.datatype.IDataTypeFactory; 36 37 import java.sql.Connection ; 38 import java.sql.Driver ; 39 import java.sql.SQLException ; 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Properties ; 44 45 57 public class DbUnitTask extends Task 58 { 59 60 63 private Connection conn = null; 64 65 68 private String driver = null; 69 70 73 private String url = null; 74 75 78 private String userId = null; 79 80 83 private String password = null; 84 85 88 private String schema = null; 89 90 93 private List steps = new ArrayList (); 94 95 private Path classpath; 96 97 private AntClassLoader loader; 98 99 102 private boolean useQualifiedTableNames = false; 103 104 107 private boolean supportBatchStatement = false; 108 109 112 private boolean datatypeWarning = true; 113 114 private String escapePattern = null; 115 116 private String dataTypeFactory = "org.dbunit.dataset.datatype.DefaultDataTypeFactory"; 117 118 121 public void setDriver(String driver) 122 { 123 this.driver = driver; 124 } 125 126 129 public void setUrl(String url) 130 { 131 this.url = url; 132 } 133 134 137 public void setUserid(String userId) 138 { 139 this.userId = userId; 140 } 141 142 145 public void setPassword(String password) 146 { 147 this.password = password; 148 } 149 150 153 public void setSchema(String schema) 154 { 155 this.schema = schema; 156 } 157 158 161 public void setUseQualifiedTableNames(boolean useQualifiedTableNames) 162 { 163 this.useQualifiedTableNames = useQualifiedTableNames; 164 } 165 166 171 public void setSupportBatchStatement(boolean supportBatchStatement) 172 { 173 this.supportBatchStatement = supportBatchStatement; 174 } 175 176 public void setDatatypeWarning(boolean datatypeWarning) 177 { 178 this.datatypeWarning = datatypeWarning; 179 } 180 181 public void setDatatypeFactory(String datatypeFactory) 182 { 183 this.dataTypeFactory = datatypeFactory; 184 } 185 186 public void setEscapePattern(String escapePattern) 187 { 188 this.escapePattern = escapePattern; 189 } 190 191 194 public void setClasspath(Path classpath) 195 { 196 if (this.classpath == null) 197 { 198 this.classpath = classpath; 199 } 200 else 201 { 202 this.classpath.append(classpath); 203 } 204 } 205 206 209 public Path createClasspath() 210 { 211 if (this.classpath == null) 212 { 213 this.classpath = new Path(project); 214 } 215 return this.classpath.createPath(); 216 } 217 218 221 public void setClasspathRef(Reference r) 222 { 223 createClasspath().setRefid(r); 224 } 225 226 229 public List getSteps() 230 { 231 return steps; 232 } 233 234 237 public void addOperation(Operation operation) 238 { 239 steps.add(operation); 240 } 241 242 245 public void addCompare(Compare compare) 246 { 247 steps.add(compare); 248 } 249 250 253 public void addExport(Export export) 254 { 255 steps.add(export); 256 } 257 258 261 public void execute() throws BuildException 262 { 263 try 264 { 265 IDatabaseConnection connection = createConnection(); 266 267 Iterator stepIter = steps.listIterator(); 268 while (stepIter.hasNext()) 269 { 270 DbUnitTaskStep step = (DbUnitTaskStep)stepIter.next(); 271 log(step.getLogMessage(), Project.MSG_INFO); 272 step.execute(connection); 273 } 274 } 275 catch (DatabaseUnitException e) 276 { 277 throw new BuildException(e, location); 278 } 279 catch (SQLException e) 280 { 281 throw new BuildException(e, location); 282 } 283 finally 284 { 285 try 286 { 287 if (conn != null) 288 { 289 conn.close(); 290 } 291 } 292 catch (SQLException e) 293 { 294 } 295 } 296 } 297 298 IDatabaseConnection createConnection() throws SQLException 299 { 300 if (driver == null) 301 { 302 throw new BuildException("Driver attribute must be set!", location); 303 } 304 if (userId == null) 305 { 306 throw new BuildException("User Id attribute must be set!", location); 307 } 308 if (password == null) 309 { 310 throw new BuildException("Password attribute must be set!", location); 311 } 312 if (url == null) 313 { 314 throw new BuildException("Url attribute must be set!", location); 315 } 316 if (steps.size() == 0) 317 { 318 throw new BuildException("Must declare at least one step in a <dbunit> task!"); 319 } 320 321 Driver driverInstance = null; 323 try 324 { 325 Class dc; 326 if (classpath != null) 327 { 328 log("Loading " + driver + " using AntClassLoader with classpath " + classpath, 329 Project.MSG_VERBOSE); 330 331 loader = new AntClassLoader(project, classpath); 332 dc = loader.loadClass(driver); 333 } 334 else 335 { 336 log("Loading " + driver + " using system loader.", Project.MSG_VERBOSE); 337 dc = Class.forName(driver); 338 } 339 driverInstance = (Driver )dc.newInstance(); 340 } 341 catch (ClassNotFoundException e) 342 { 343 throw new BuildException("Class Not Found: JDBC driver " 344 + driver + " could not be loaded", e, location); 345 } 346 catch (IllegalAccessException e) 347 { 348 throw new BuildException("Illegal Access: JDBC driver " 349 + driver + " could not be loaded", e, location); 350 } 351 catch (InstantiationException e) 352 { 353 throw new BuildException("Instantiation Exception: JDBC driver " 354 + driver + " could not be loaded", e, location); 355 } 356 357 log("connecting to " + url, Project.MSG_VERBOSE); 358 Properties info = new Properties (); 359 info.put("user", userId); 360 info.put("password", password); 361 conn = driverInstance.connect(url, info); 362 363 if (conn == null) 364 { 365 throw new SQLException ("No suitable Driver for " + url); 367 } 368 conn.setAutoCommit(true); 369 370 IDatabaseConnection connection = new DatabaseConnection(conn, schema); 371 DatabaseConfig config = connection.getConfig(); 372 config.setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, supportBatchStatement); 373 config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, useQualifiedTableNames); 374 config.setFeature(DatabaseConfig.FEATURE_DATATYPE_WARNING, datatypeWarning); 375 config.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern); 376 config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, 377 new ForwardOnlyResultSetTableFactory()); 378 379 try 381 { 382 IDataTypeFactory dataTypeFactory = (IDataTypeFactory)Class.forName( 383 this.dataTypeFactory).newInstance(); 384 config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory); 385 } 386 catch (ClassNotFoundException e) 387 { 388 throw new BuildException("Class Not Found: DataType factory " 389 + driver + " could not be loaded", e, location); 390 } 391 catch (IllegalAccessException e) 392 { 393 throw new BuildException("Illegal Access: DataType factory " 394 + driver + " could not be loaded", e, location); 395 } 396 catch (InstantiationException e) 397 { 398 throw new BuildException("Instantiation Exception: DataType factory " 399 + driver + " could not be loaded", e, location); 400 } 401 402 return connection; 403 } 404 } 405 406 | Popular Tags |