Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:12:31+00:00 2026-06-13T00:12:31+00:00

To compare multiline text results in JUnit Tests I often need to go from

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:12:32+00:00Added an answer on June 13, 2026 at 12:12 am

    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

    String expected = "
    

    and copy some text like

    blah
    blah
    blah
    blah
    blah
    

    and then paste your string, Eclipse creates:

     String expected = "blah\n" + 
     "blah\n" + 
     "blah\n" + 
     "blah\n" + 
     "blah"
    

    Personally I think it would be nice if Java had a multi-line equivalent to Perl’s HERE documents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare values from two tables that are identical but contain some
I want to compare two text files that i have generated from one of
I need to compare the current url with a url obtained from createURL .
Is there a cross-platform library function that would collapse a multiline string into a
I am trying to compare a date against the first of next month from
Possible Duplicate: How to compare Dates in C# This code of mine: public static
I'm trying to compare a character string with the argv argument. I have this
I need to compare two row with each other and then write the fields
I would like to compare the current date with another date, and if that
I need to compare an int in a switch statement, and I'm unsure of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.