为了比较 JUnit 测试中的多行文本结果,我经常需要从文本表示转到使用多行文本初始化字符串的 Java 代码。
例如,如果测试应该检查包含以下内容的 xml 字符串:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer>
<id>100</id>
<name>John Doe</name>
<orders>
<Order>
<address>100 main street, smalltown, pa</address>
<orderid>1100</orderid>
</Order>
<Order>
<address>5 broadway, ny, ny</address>
<orderid>1200</orderid>
</Order>
</orders>
</Customer>
我想使用接受上述输入的工具/生成器并获得以下结果:
String expected ="";
expected+="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
expected+="<Customer>\n";
expected+=" <id>100</id>\n";
expected+=" <name>John Doe</name>\n";
expected+=" <orders>\n";
expected+=" <Order>\n";
expected+=" <address>100 main street, smalltown, pa</address>\n";
expected+=" <orderid>1100</orderid>\n";
expected+=" </Order>\n";
expected+=" <Order>\n";
expected+=" <address>5 broadway, ny, ny</address>\n";
expected+=" <orderid>1200</orderid>\n";
expected+=" </Order>\n";
expected+=" </orders>\n";
expected+="</Customer>\n";
和/或
// Create test file
java.io.PrintWriter srcWriter = new java.io.PrintWriter(new java.io.FileOutputStream(testFile));
srcWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
srcWriter.println("<Customer>\n");
srcWriter.println(" <id>100</id>\n");
srcWriter.println(" <name>John Doe</name>\n");
srcWriter.println(" <orders>\n");
srcWriter.println(" <Order>\n");
srcWriter.println(" <address>100 main street, smalltown, pa</address>\n");
srcWriter.println(" <orderid>1100</orderid>\n");
srcWriter.println(" </Order>\n");
srcWriter.println(" <Order>\n");
srcWriter.println(" <address>5 broadway, ny, ny</address>\n");
srcWriter.println(" <orderid>1200</orderid>\n");
srcWriter.println(" </Order>\n");
srcWriter.println(" </orders>\n");
srcWriter.println("</Customer>\n");
srcWriter.close();
// PrintWriter never throws Exceptions, one must check the error state manually
//
if (srcWriter.checkError())
{
throw new IOException( "can not write " + testFile );
}
什么是开发工具/eclipse实用程序或插件来实现这一点?
- 从文件中获取多行输入文本(在 IDE 或命令行中)
- 转义引号和反斜杠
- 转换为初始化字符串文字和/或将在 Java 代码中创建文件而不需要额外的文件资源的 Java 代码
- 将结果输出到新文件和/或控制台或直接输出到编辑器以在编译时使用
输出文件(如果有)不应与编译结果一起提供。在文件模式下,输入文件的等效项应该从 java 代码中的字符串文字重新创建。