Ant Integration

Installation

1. Add the DbUnit jar to Ant's classpath.

2. Add a < taskdef > element to your build script as follows:

<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask"/>

3. Use the task in the rest of the buildfile.

Usage

Executes either a single transaction, or a group of transactions, under the DbUnit database testing framework.

Parameters

Attribute Description Required
driver Class name of the jdbc driver Yes
url Database connection url Yes
userid Database username Yes
password Database password Yes
schema Database schema No
classpath Classpath used to load driver No (use system classpath)
useQualifiedTableNames Set the qualified table names feature. Defaults to false No
supportBatchStatement Set the batched statement feature. Defaults to false No
datatypeWarning Set the data type warning feature. Defaults to true No
escapePattern Set the escape pattern property. No
datatypeFactory Set the datatype factory property. No

Parameters specified as nested elements

classpath

DbUnit's classpath attribute is a PATH like structure and can also be set via a nested classpath element. It is used to load the JDBC classes.

operation

Use nested < operation > elements to specify which DbUnit operation to perform on the particular file.

Attribute Description Required
type Type of Database operation to perform. Supported types are UPDATE, INSERT, DELETE, DELETE_ALL, REFRESH, CLEAN_INSERT, MSSQL_INSERT, MSSQL_REFRESH, MSSQL_CLEAN_INSERT. Yes
src The xml source upon which the operation is to be performed Yes
format For mat type of supplied source file. Possible values are "flat" or "xml". Defaults to "flat"

No

export

Use nested < export > operation elements to export the database to the supplied filename. The default operation is to < export > the entire database to the destination filename with the supplied dataset type. You can specify individual tables or queries to < export > by nesting them under the < export > step.

Attribute Description Required
dest The xml destination filename Yes
format Format type of supplied destination file. Possible values are "flat", "xml" or "dtd". Defaults to "flat" No
doctype If set and format is "flat", add DOCTYPE declaration referencing specified DTD to exported dataset. The DTD path can be absolute or relative. No

Parameters specified as nested elements

table

Use nested < table > elements to export specific tables.

Attribute Description Required
name Name of the database table to export. Yes
query

Use nested < query > elements to export data according to a sql statement.

Attribute Description Required
name Name to reference the sql statement. Yes
sql The sql to execute. You can use either SELECT * from Mytable or SELECT col1, col4 from MyTable Yes
compare

Use nested < compare > elements to validate the content of the database against the specified dataset file.

Attribute Description Required
src The xml source upon which the comparison is to be performed Yes
format Format type of supplied source file. Possible values are "flat" or "xml". Defaults to "flat" No
sort Sorts tables prior comparison. Defaults to "false". No

Parameters specified as nested elements

table

Use nested < table > elements to compare specific tables.

Attribute Description Required
name Name of the database table to compare. Yes
query

Use nested < query > elements to compare data according to a sql statement.

Attribute Description Required
name Name of the database table to compare. Yes
sql The sql to execute. You can use either SELECT * from Mytable or SELECT col1, col4 from MyTable Yes

Examples

Update operation with specified JDBC driver jar

<dbunit driver="com.vendor.jdbc.Driver"          
	url="jdbc:vendor:mydatabase"          
	userid="user"          
	password="password">
    <classpath>    	
        <pathelement location="/some/jdbc.jar"/>    
    </classpath>    
    <operation type="UPDATE" src="updateFile.xml"/>  
</dbunit>

Insert and update operations

<dbunit driver="com.vendor.jdbc.Driver"          
        url="jdbc:vendor:mydatabase"          
        userid="user"          
        password="password">    
    <operation type="INSERT" src="insertFile.xml"/>    
    <operation type="UPDATE" src="updateFile.xml"/>  
</dbunit>

Database data export to XML

<dbunit driver="com.vendor.jdbc.Driver"          
        url="jdbc:vendor:mydatabase"          
        userid="user"          
        password="password">    
    <export dest="export.xml"/> 
</dbunit>

Database structure export to DTD

<dbunit driver="com.vendor.jdbc.Driver"          
        url="jdbc:vendor:mydatabase"          
        userid="user"          
        password="password">    
    <export dest="export.dtd" format="dtd"/>
</dbunit>

Partial database data export

Export two tables: FOO, resulting from specified query and BAR entire content

<dbunit driver="com.vendor.jdbc.Driver"          
        url="jdbc:vendor:mydatabase"          
        userid="user"          
        password="password">    
    <export dest="partial.xml">      
        <query name="FOO" sql="SELECT COL1, COL2 FROM FOO WHERE COL1=4"/>
        <table name="BAR"/>      
    </export>
</dbunit>

Database data comparison

<dbunit driver="com.vendor.jdbc.Driver"          
        url="jdbc:vendor:mydatabase"          
        userid="user"          
        password="password">    
    <compare src="expectedData.xml"/>    
</dbunit>

Partial database data comparison

<dbunit driver="com.vendor.jdbc.Driver"          
        url="jdbc:vendor:mydatabase"          
        userid="user"          
        password="password">    
    <compare src="expectedData.xml">      
        <query name="FOO" sql="SELECT COL1, COL2 FROM FOO WHERE COL1=4"/>
        <table name="BAR"/>      
    </compare>
</dbunit>