使用 dbunit 从 ant 目标清理数据库中的插入

发布于 2024-11-19 16:34:19 字数 2382 浏览 2 评论 0 原文

我使用 dbunit 在我的 web 应用程序中测试数据库操作。最近,我使用 dbunits ant 任务 org.dbunit.ant.DbUnitTask 在我的数据库中创建项目的 xml 表示形式。我得到了这个

<?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>
...

,我想清理插入到数据库中的值这个xml文件。在测试用例中,你可以通过

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();
        }
    }

我想使用ant目标做同样的事情来做到这一点。所以我写了

<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"
    />

从属性文件中获取driver.username、password等的位置

但是,我收到此错误

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]

有人能理解此错误吗?传递给方法 DatabaseOperation.CLEAN_INSERT.execute(connection,DbUnitUtils.createDataSet("initialdata.xml"))

成功 时,相同的 xml 文件会成功干净地插入数据。

任何帮助欢迎

感谢

标记

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2024-11-26 16:34:19

我不确定 DbUnitUtils.createDataSet() 方法的作用是什么,但看起来您可能需要将 XML 的格式指定为 xml - 即符合 XMLDataSet 的固定 DTD。如果您未指定格式,Ant 任务将采用 flat 格式,并且 平面格式 与您的示例 XML 不同。

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

请参阅指定为嵌套元素的参数进行操作

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文