DBUnit 和 Oracle JDBC (ClassCastException)
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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).
另一个选项是“不使用 Apache DBCP”。使用 Oracle 数据源代替 Apache
Another option is "Do Not Use Apache DBCP". Instead of Apache use Oracle Datasource