DBUnit 和 Oracle JDBC (ClassCastException)

发布于 2024-10-14 06:58:37 字数 1150 浏览 10 评论 0原文

我想使用 dbunit 将测试数据库转储到原始 xml 文件中,但出现了 ClassCastException。在代码下方:

new FlatXmlWriter(new FileOutputStream("expected_ds.xml")).
        write(getDbunitConnection().createDataSet(new String[]{"TAB1","TAB2"}));

结果:

    java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingResultSet cannot be cast to oracle.jdbc.OracleResultSet

我正在使用 ojdbc14-10.2.0.3.0.jar、commons-dbcp-1.2.2.jar 和 dbunit-2.4.7.jar。

这是 oracle jdbc 中的错误吗? 在 ojdbc 驱动程序中,我发现了这样的内容:

/**
 * 
 * TODO UnitTests are completely missing
 * @author Phil Barr
 * @author Last changed by: $Author: jbhurst $
 * @version $Revision: 1072 $ $Date: 2009-10-12 19:46:45 +0200 (lun, 12 ott 2009) $
 * @since 2.4.0
 */
public class OracleXMLTypeDataType extends BlobDataType
{

    public Object getSqlValue(int column, ResultSet resultSet) throws SQLException,     TypeCastException
    {
        byte[] data = new byte[0];
        OracleResultSet oracleResultSet = (OracleResultSet) resultSet;
        ... some other stuf ...
    }
...
}

它看起来像 oracle 问题,并且从 javadoc 看来它根本没有经过测试。 有人遇到过类似的问题吗?

I want to dump my test db into the raw xml file using dbunit and I'm getting ClassCastException. Below the code:

new FlatXmlWriter(new FileOutputStream("expected_ds.xml")).
        write(getDbunitConnection().createDataSet(new String[]{"TAB1","TAB2"}));

and as a result :

    java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingResultSet cannot be cast to oracle.jdbc.OracleResultSet

I'm using ojdbc14-10.2.0.3.0.jar, commons-dbcp-1.2.2.jar and dbunit-2.4.7.jar.

Is that a bug in oracle jdbc ?
In the ojdbc driver I have found sth like that:

/**
 * 
 * TODO UnitTests are completely missing
 * @author Phil Barr
 * @author Last changed by: $Author: jbhurst $
 * @version $Revision: 1072 $ $Date: 2009-10-12 19:46:45 +0200 (lun, 12 ott 2009) $
 * @since 2.4.0
 */
public class OracleXMLTypeDataType extends BlobDataType
{

    public Object getSqlValue(int column, ResultSet resultSet) throws SQLException,     TypeCastException
    {
        byte[] data = new byte[0];
        OracleResultSet oracleResultSet = (OracleResultSet) resultSet;
        ... some other stuf ...
    }
...
}

It looks like oracle issue and from the javadoc it seems that it was not tested at all.
Has anyone had similar problem?

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

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

发布评论

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

评论(2

空袭的梦i 2024-10-21 06:58:37

Oracle JDBC 驱动程序在这里没有错误。

看起来 DBUnit 假设它可以将 ResultSet 转换为 Oracle 特定的类型。这首先是一个坏主意(但在某些情况下无法避免)。

由于您使用的是连接池,因此 DbUnit 实际上无法访问 Oracle 特定的对象,而是访问连接池提供的包装器。

要么停止使用池进行测试,要么从池连接中获取底层连接并将其传递给 DBUnit(这有 DBUnit 关闭物理连接的风险,这是池试图通过仅提供包装器来避免的情况)。

The Oracle JDBC driver is not at fault here.

It looks like DBUnit assumes it can cast the ResultSet to the Oracle-specific type. That's a bad idea in the first place (but can't be avoided in some cases).

Since you're using a connection pool, DbUnit doesn't actually get access to the Oracle-specific object, but to a wrapper provided by the pool instead.

Either stop using a pool for the tests or get the underlying connection from the pooled connection and pass that to DBUnit (this risks that DBUnit closes the physical connection, which is what the pool tries to avoid by only providing the wrapper).

北笙凉宸 2024-10-21 06:58:37

另一个选项是“不使用 Apache DBCP”。使用 Oracle 数据源代替 Apache

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="URL" value="${test.db.url}" />
        <property name="user" value="${test.db.username}"/>
        <property name="password" value="${test.db.password}"/>
        <property name="connectionCachingEnabled" value="true"/>
</bean>

Another option is "Do Not Use Apache DBCP". Instead of Apache use Oracle Datasource

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="URL" value="${test.db.url}" />
        <property name="user" value="${test.db.username}"/>
        <property name="password" value="${test.db.password}"/>
        <property name="connectionCachingEnabled" value="true"/>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文