Search code examples
databaseantinsertdbunit

using dbunit to clean insert in a db from ant target


I use dbunit to test db operations in my webapp.Recently ,I used dbunits ant task org.dbunit.ant.DbUnitTask to create an xml representation of items in my database.I got this

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <table name="AUTHOR">
    <column>AUTHOR_ID</column>
    <column>AUTHOR_NAME</column>
    <column>AUTHOR_ADDRESS</column>
    <row>
      <value>0</value>
      <value>j.k.rowling</value>
      <value>london</value>
    </row>
    <row>
      <value>1</value>
      <value><![CDATA[stephen king]]></value>
      <value><![CDATA[castle rock,maine]]></value>
    </row>
  </table>
...

I wanted to clean insert into the db ,values from this xml file.In a testcase's you do this by

public void init() throws FileNotFoundException, IOException, ClassNotFoundException, SQLException, DatabaseUnitException {
        connection = DbUnitUtils.createConnection();
        try {
            DatabaseOperation.CLEAN_INSERT.execute(connection,DbUnitUtils.createDataSet("initialdata.xml"));
        }finally {
            connection.close();
        }
    }

I wanted to do the same using an ant target.So I wrote

<target name="insertdata" depends="startdb">
        <dbunit driver="${db.driver}"
            url="${db.url}"
            userid="${db.username}"
            password="${db.password}">
            <operation type="CLEAN_INSERT" src="data/dbunit/initialdata.xml"/>
        </dbunit>

    </target>

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

where the driver.username,password etc are taken from a properties file

However,I get this error

insertdata:
   [dbunit] Executing operation: CLEAN_INSERT
   [dbunit]           on   file: C:\code\jee\myapp\data\dbunit\initialdata.xml
   [dbunit]           with format: null
   [dbunit] 550 [main] ERROR org.dbunit.database.DatabaseDataSet - Table 'value' not found in tableMap=org.dbunit.dataset.OrderedTableNameMa
p[_tableNames=[AUTHOR], _tableMap={AUTHOR=null}, _caseSensitiveTableNames=false]

Can anyone make sense of this error?The same xml file when passed to the method DatabaseOperation.CLEAN_INSERT.execute(connection,DbUnitUtils.createDataSet("initialdata.xml"))

succeeds in cleanly inserting the data.

Any help welcome

thanks

mark


Solution

  • I'm not sure what the DbUnitUtils.createDataSet() method is up to, but it looks like you may need to specify the format for the XML as xml - i.e. conforming to the fixed DTD of an XMLDataSet. The Ant task assumes flat format if you don't specify one, and flat format doesn't look like your example XML.

    <operation type="CLEAN_INSERT" src="data/dbunit/initialdata.xml" format="xml" />
    

    See Parameters specified as nested elements for operation.