To compare multiline text results in JUnit Tests I often need to go from a text representation
to Java code that initialize a string with the multiline text.
E.g. if the test should check for an xml string containing:
<?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>
I’d like to use a tool/generator that takes the above input and get the following result:
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";
and/or
// 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 );
}
What would be a development tool / eclipse utility or plugin to achieve this?
- take a multiline input text from a file (in the IDE or on command line)
- escape quotes and backslashes
- convert to Java code that initializes a string literal and/or will do the file creation in the java code without the need of an extra file resource
- output result to new files and/or console or directly into editor to be used at compile time
The output file (if any) should not be shipped with the compile result. In the file mode an equivalent for the input file should be recreated from the string literals in the java code.
Assuming you are copying the multi-line text from another source, and you are using Eclipse, it can automatically convert your text into a multiline String literal.
In my version, enable it under Windows -> Preferences -> Java -> Editor -> Typing -> Escape text when pasting into a String literal
Then if you type
and copy some text like
and then paste your string, Eclipse creates:
Personally I think it would be nice if Java had a multi-line equivalent to Perl’s HERE documents.