我正在创建一个使用 dbunit xml 在多个数据库上运行的测试套件。不幸的是,昨天我发现我们架构中的某些表名超过 30 个字符,并且针对 Oracle 被截断。例如,名为 unusually_long_table_name_error
的表在mysql中名为unusually_long_table_name_erro
在甲骨文中。这意味着我的 dbunit 文件包含类似 <unusually_long_table_name_error col1="value1" col2="value2 />
的行。这些行抛出 NoSuchTableException
在 Oracle 中运行测试时。
是否有解决此问题的编程方法?我真的很想避免为 Oracle 生成特殊的 xml 文件。我调查了一个自定义MetadataHandler
但它返回很多 java.sql
我不知道如何拦截/欺骗的数据类型。我可以自己读取 xml,将每个表名截断为 30 个字符,将其写入临时文件或 StringBufferInputStream
然后将其用作我的 DataSetBuilder 的输入,但这似乎需要很多步骤才能完成很少的任务。也许有一些带有同义词、存储过程或天知道还有什么的忍者 Oracle 技巧可以帮助我。这些想法中的一个明显比其他想法更好吗?还有其他方法以其简单和优雅让我震惊吗?谢谢!
最佳答案
鉴于缺乏答案,我最终采用了自己建议的方法,
- 读取 .xml 文件
- 正则表达式给出表名
- 如果表名称超过 30 个字符,则将其截断
- 将(可能修改的)行附加到 StringBuilder
- 将 StringBuilder 馈入 ByteArrayInputStream,适合传递到 DataSetBuilder
public InputStream oracleWorkaroundStream(String fileName) throws IOException
{
String ls = System.getProperty("line.separator");
// This pattern isolates the table name from the rest of the line
Pattern pattern = Pattern.compile("(\\s*<)(\\w+)(.*/>)");
FileInputStream fis = new FileInputStream(fileName);
// Use a StringBuidler for better performance over repeated concatenation
StringBuilder sb = new StringBuilder(fis.available()*2);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader buff = new BufferedReader(isr);
while (buff.ready())
{
// Read a line from the source xml file
String line = buff.readLine();
Matcher matcher = pattern.matcher(line);
// See if the line contains a table name
if (matcher.matches())
{
String tableName = matcher.group(2);
if (tableName.length() > 30)
{
tableName = tableName.substring(0, 30);
}
// Append the (potentially modified) line
sb.append(matcher.group(1));
sb.append(tableName);
sb.append(matcher.group(3));
}
else
{
// Some lines don't have tables names (<dataset>, <?xml?>, etc.)
sb.append(line);
}
sb.append(ls);
}
return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
}
编辑:从重复的字符串连接切换到 StringBuilder,这提供了巨大的性能提升
关于java - DbUnit NoSuchTableException - Oracle 中长表名的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11417213/