Results 1 to 7 of 7
  1. #1
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default Is there a better way to read from stdout?

    Hi,

    I've written a test to make sure that my program writes out only certain information to stdout. I had a tough time figuring out how to read the stdout from my program, so I replaced the System stdout with a ByteArrayOutputStream from which I can then stringify the output. Of course I make sure I replace the System stdout with the original.

    I would just like to know if there is a better way of achieving the same thing? Thanks!

    Java Code:
     
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream myBuffer = new PrintStream(out);
    System.setOut(myBuffer);
    String clearText = "mypassword";
    encryptor.run(new String[]{"-encrypt", clearText});
    String output = out.toString();
    
    String ENCRYPTED_OUTPUT_PATTERN = "^(\\S*)\\s*";
    Pattern p = Pattern.compile(ENCRYPTED_OUTPUT_PATTERN); 
    Matcher m = p.matcher(output);
    	if (!m.matches()) {
    		fail("The output of encrypt does not match the pattern " + ENCRYPTED_OUTPUT_PATTERN + ". The output was: " + output);
    	}

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I'm confused about the request "I've written a test to make sure that my program writes out only certain information to stdout". So, you want to write a program to make sure you have the correct output?? Do you mean unit testing?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Yes, exactly. I have a program that writes a response to stdout. I've written a unit test to verify that the expected response is the only thing written out because we had a bug report that there was other information being written out at the same time. So I have to make the call to the class which writes to stdout, then I have to read what it wrote to verify.

    The only way I could see to do this was to hijack System's stdout and replace it with my own. I just wanted to know if there was a more elegant way of doing this (basically redirecting stdout to a string).

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Yeah, that's the only way I could think to do it.

    Without modifying your code, if it outputs to stdout, then you need to redirect stdout to another buffer (i.e. an OutputStream, in this case). ByteArrayOutputStream is a generic buffer, and since you want a String (not a file, for example). Then, like you did, when done, retrieve the String value. From there, you run your validation.

    What were you hoping for, if not this method? Even if you output to a different buffer, you have the same steps

    1) Direct output to a buffer
    2) Retrieve the output from the buffer (as a String)
    3) Validate the output (e.g. using a Regular Expression)
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    What were you hoping for, if not this method? Even if you output to a different buffer, you have the same steps
    Just curious I guess. I do often come up with expedient solutions, but I don't know if they are the best. I've seen lots of awful code and I'm sure it's because people found a solution that works, but then didn't look for something better. Just because something works, doesn't mean that it's good.

    Also, I found that after working with streams, I was left a bit dazed and confused.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by iceagecoming View Post
    Also, I found that after working with streams, I was left a bit dazed and confused.
    In what way, I might be able to help.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Streams aren't that bad. Once you get it set up (which only take a few lines) it's pretty simple to write to the stream.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

Similar Threads

  1. Replies: 1
    Last Post: 10-24-2009, 02:18 AM
  2. Read Xls
    By Deepa in forum New To Java
    Replies: 2
    Last Post: 01-16-2009, 12:46 PM
  3. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  4. Please Read!!!
    By jeffranc in forum New To Java
    Replies: 0
    Last Post: 08-21-2008, 08:47 PM
  5. How to read the following
    By rrp in forum New To Java
    Replies: 0
    Last Post: 12-03-2007, 06:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •