1

我正在尝试使用 DBUnit 从我们的 AS400 导出数据并将其导入 HSQLDB 数据库以进行 JUnit 测试,但是,似乎没有导出/导入数据类型信息。一切似乎都作为 CDATA 导出(因此将被导入)。有人能告诉我在哪里可以找到答案吗?

我已经成功构建并运行了其中一个示例:

public static void main(String[] args) throws Exception
{
    // database connection
    Class driverClass = Class.forName("com.ibm.as400.access.AS400JDBCDriver");
    Connection jdbcConnection = DriverManager.getConnection("jdbc:as400://myserver;libraries=MYLIB", "user", "password");
    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

    // partial database export
    QueryDataSet partialDataSet = new QueryDataSet(connection);
    partialDataSet.addTable("TABLE1");
    partialDataSet.addTable("TABLE2");
    FlatXmlDataSet.write(partialDataSet, new FileOutputStream("flatpartial_as400.xml"));
    FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream("flatdtd_as400.dtd"));
    XmlDataSet.write(partialDataSet, new FileOutputStream("partial_as400.xml"));

例如,这给了我以下输出:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <TABLE1/>
  <TABLE2 ADDRESS_ID="1" ADDRESS_TYPE="1" ADDRESS_DESCRIPTION="Billing Address          "/>
  <TABLE2 ADDRESS_ID="2" ADDRESS_TYPE="2" ADDRESS_DESCRIPTION="Delivery Address         "/>
  <TABLE2 ADDRESS_ID="3" ADDRESS_TYPE="3" ADDRESS_DESCRIPTION="Owner Address            "/>
  <TABLE2 ADDRESS_ID="4" ADDRESS_TYPE="4" ADDRESS_DESCRIPTION="Owner Previous Address   "/>
  <TABLE2 ADDRESS_ID="5" ADDRESS_TYPE="5" ADDRESS_DESCRIPTION="Store Address            "/>
</dataset>

这是生成的 DTD 片段:

<!ELEMENT TABLE2 EMPTY>
<!ATTLIST TABLE2
    ADDRESS_ID CDATA #REQUIRED
    ADDRESS_TYPE CDATA #REQUIRED
    ADDRES_DESCRIPTION CDATA #REQUIRED
>

第一个字段应该是 DECIMAL(10,0) 但它是 CDATA。显然,从数据完整性的角度来看,这将是一个问题。在调试器中,我可以看到类型信息正在进入 DBUnit 包(代码中的 ADDRESS_ID 字段是 DECIMAL。)但是该信息没有进入文件的 XML DTD。

任何帮助表示赞赏...

4

1 回答 1

2

也许它会有所帮助:Generete dtd file for tables you want to import

private static void generateDTD(String fileName,IDatabaseConnection connection,
        String[] tablesToExport) throws IOException, DataSetException,
        SQLException, FileNotFoundException {
    FlatDtdDataSet.write(connection.createDataSet(tablesToExport), new FileOutputStream(fileName));
}

然后在builder中设置

FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
        builder.setMetaDataSetFromDtd(new FileInputStream(schemaFile));
于 2013-07-31T14:49:01.123 回答