Test java programs that read from stdin and write to stdout - java

I am writing some code for a programming contest in java. The input to the program is given using stdin and output is on stdout. How are you folks testing programs that work on stdin/stdout? This is what I am thinking:
Since System.in is of type InputStream and System.out is of type PrintStream, I wrote my code in a func with this prototype:
void printAverage(InputStream in, PrintStream out)
Now, I would like to test this using junit. I would like to fake the System.in using a String and receive the output in a String.
#Test
void testPrintAverage() {
String input="10 20 30";
String expectedOutput="20";
InputStream in = getInputStreamFromString(input);
PrintStream out = getPrintStreamForString();
printAverage(in, out);
assertEquals(expectedOutput, out.toString());
}
What is the 'correct' way to implement getInputStreamFromString() and getPrintStreamForString()?
Am I making this more complicated than it needs to be?

Try the following:
String string = "aaa";
InputStream stringStream = new java.io.ByteArrayInputStream(string.getBytes())
stringStream is a stream that will read chars from the input string.
OutputStream outputStream = new java.io.ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
// .. writes to printWriter and flush() at the end.
String result = outputStream.toString()
printStream is a PrintStream that will write to the outputStream which in turn will be able to return a string.

EDITED: Sorry I misread your question.
Read with scanner or bufferedreader, The latter is much faster than the former.
Scanner jin = new Scanner(System.in);
BufferedReader reader = new BufferedReader(System.in);
Write to stdout with print writer. You can also print directly to Syso but this is slower.
System.out.println("Sample");
System.out.printf("%.2f",5.123);
PrintWriter out = new PrintWriter(System.out);
out.print("Sample");
out.close();

I am writing some code for a programming contest in java. The input to the program is given using stdin and output is on stdout. How are you folks testing programs that work on stdin/stdout?
Another way to send characters to System.in is to use PipedInputStream and PipedOutputStream. Maybe something like the following:
PipedInputStream pipeIn = new PipedInputStream(1024);
System.setIn(pipeIn);
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
// then I can write to the pipe
pipeOut.write(new byte[] { ... });
// if I need a writer I do:
Writer writer = OutputStreamWriter(pipeOut);
writer.write("some string");
// call code that reads from System.in
processInput();
On the flip side, as mentioned by #Mihai Toader, if I need to test System.out then I do something like:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
// call code that prints to System.out
printSomeOutput();
// now interrogate the byte[] inside of baos
byte[] outputBytes = baos.toByteArray();
// if I need it as a string I do
String outputStr = baos.toString();
Assert.assertTrue(outputStr.contains("some important output"));

Related

Testing a method that reads from standard input and outputs to standard output

I have a method which reads lines from the standard input and writes lines to the standard output.
From within a JUnit test, how can I send input to the method, and how can I capture its output so that I can make assertions on it?
You should not have a method which reads from standard input and writes to standard output.
You should have a method which accepts as parameters the InputStream from which it reads, and the PrintStream into which it writes. (This is an application, at the method level, of a principle known as Dependency Injection (Wikipedia) which is generally used at the class level.)
Then, under normal circumstances, you invoke that method passing it System.in and System.out as parameters.
But when you want to test it, you can pass it an InputStream and a PrintStream that you have created for test purposes.
So, you can use something along these lines:
void testMyAwesomeMethod( String testInput, String expectedOutput )
{
byte[] bytes = testInput.getBytes( StandardCharsets.UTF_8 );
InputStream inputStream = new ByteArrayInputStream( bytes );
StringWriter stringWriter = new StringWriter();
try( PrintWriter printWriter = new PrintWriter( stringWriter ) )
{
myAwesomeMethod( inputStream, printWriter );
}
String result = stringWriter.toString();
assert result.equals( expectedOutput );
}

BufferedReader vs Scanner, and FileInputStream vs FileReader?

Can someone explain to me why can I use FileInputStream or FileReader for a BufferedReader? What's the difference? And at the same time what is the advantage of a Scanner over a BufferedReader? I was reading that it helps by tokenizing, but what does that mean?
try {
//Simple reading of bytes
FileInputStream fileInputStream = new FileInputStream("path to file");
byte[] arr = new byte[1024];
int actualBytesRead = fileInputStream.read(arr, 0, arr.length);
//Can read characters and lines now
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String lineRead = bufferedReader.readLine();
char [] charArrr = new char[1024];
int actulCharsRead = bufferedReader.read(charArrr, 0, charArrr.length);
//File reader allows reading of characters from a file
FileReader fileReader = new FileReader("path to file");
actulCharsRead = fileReader.read(charArrr, 0, charArrr.length);
//It is a good idea to wrap a bufferedReader around a fileReader
BufferedReader betterFileReader = new BufferedReader(new FileReader(""));
lineRead = betterFileReader.readLine();
actulCharsRead = betterFileReader.read(charArrr, 0, charArrr.length);
//allows reading int, long, short, byte, line etc. Scanner tends to be very slow
Scanner scanner = new Scanner("path to file");
//can also give inputStream as source
scanner = new Scanner(System.in);
long valueRead = scanner.nextLong();
//might wanna check out javadoc for more info
} catch (IOException e) {
e.printStackTrace();
}
Dexter's answer is already useful, but some extra explanation might still help:
In genereal:
An InputStream only provides access to byte data from a source.
A Reader can be wrapped around a stream and adds proper text encoding, so you can now read chars.
A BufferedReader can be wrapped around a Reader to buffer operations, so instead of 1 byte per call, it reads a bunch at once, thereby reducing system calls and improving performance in most cases.
For files:
A FileInputStream is the most basic way to read data from files.
If you do not want to handle text encoding on your own, you can wrap it into a InputStreamReader, which can be wrapped into a BufferedReader.
Alternatively, you can use a FilerReader, which should basically do the same thing as FileInputStream + InputStreamReader.
Now if you do not want to just read arbitrary text, but specific data types (int, long, double,...) or regular expressions, Scanner is quite useful. But as mentioned, it will add some overhead for building those expressions, so only use it when needed.
Introduced in Java 8 is Files.lines. This supports sufficient simple file manipulation to relieve at least some Perl envy :-)
Files.lines(Paths.get("input.txt"))
.filter(line -> line.startsWith("ERROR:"))
.map(String::toUpperCase).forEach(System.out::println);

System.out in DataOutputStream is not printing on the console

I am trying the following code using the DataOutputStream. The OutputStream passed to the DataOutputStream is not printing anything. Please see my below code and pllease tell me anything wrong in this code.
public class DataStreamsExample {
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Enter the First Number");
int i = dis.readInt();
System.out.println("Enter the Second Numebr");
int j= dis.readInt();
int total = i+j;
DataOutputStream dos = new DataOutputStream(System.out);
dos.writeInt(total);
}
}
Why are you using data output streams? Can't you use a Scanner for reading input?
Calling dos.flush() will print out your result though.
OutputStream is fundamentally a binary construct. If you want to write text data (e.g. from the console) you should use a Writer of some description. To convert an OutputStream into a Writer, use OutputStreamWriter. Then create a PrintWriter around the Writer, and you can read a line using PrintWriter.println().
You can replace follow line
DataOutputStream out = new DataOutputStream(out);
into this
PrintWriter out = new PrintWriter(new OutputStreamWriter(out));
The character encoding can then be explicitly specified in the constructor of OutputStreamWriter.

BufferedWriter is not writing to ByteArrayOutputStream

I want to convert an input stream to byte array. I know I can use IOUtils from commons-io. But I am practicing some basics in java io. I read an xml file using BufferedReader and tried writing it to a ByteArrayOutputStream using BufferedWriter. But its not working.
When I write directly to the ByteArrayOutputStream its working. Whats wrong in my code?
try (InputStream inputStream = getClass().getResourceAsStream(
"/productInventory.xml");
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(arrayOutputStream));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));) {
String line = "";
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
}
System.out.println(arrayOutputStream.size());
} catch (IOException e) {
e.printStackTrace();
}
When I include below line in the loop its working
arrayOutputStream.write(line.getBytes(), 0, line.getBytes().length);
What is wrong while using BufferedWriter?
Nothing's wrong - it's just buffering! :D
The BufferedWriter works by filtering everything you send into it into a buffer - when the buffer is full, or when the writer is closed, or flushed, (It's a Closeable, so you should absolutely close it), it sends along those buffered characters to the underlying writer.
If you want to see the underlying writer update you have to either:
1) Fill up the buffer (default size is 8k in Java)
2) Call .flush()
3) Call .close()
4) As mentioned in comments, you can do a try-with-resources to make the close implicit:
try (BufferedWriter writer = new BufferedWriter(underlyingWriter)) {
// doStuff
}

Convert InputStreamReader to InputStream

How can I convert InputStreamReader to InputStream? I have an InputStream which contains some string and byte data and I want to parse it. So I wrap my InputStream to BufferedReader. Then I read 3 lines from it. After that I want to get the rest of data(bytes) as is. But if I try to get it nothing happens.
Code snippet:
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String endOfData = br.readLine();
String contentDisposition = br.readLine();
String contentType = br.readLine();
file = new File(filename);
if(file.exists()) file.delete();
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] data = new byte[8192];
int len = 0;
while (-1 != (len = is.read(data)) )
{
fos.write(data, 0, len);
Log.e("len", len+"");
}
fos.flush();
fos.close();
is.close();
The file is empty. If I don't wrap InputStream it works fine, but I need to read 3 lines and remove it.
Thanks.
If you want to mix text and byte data together, you should use OutputStream.writeUTF to write out those 3 lines, this way one single InputStream will be able to retrieve all the data that you need.
Take a look at commons-io's ReaderInputStream: it is a little heavy handed, but you can wrap the BufferedReader with that and read it as an input stream again.
It's pretty hard to mix byte and character input correctly, especially once you start throwing buffered readers / streams into the mix. I'd suggest that you either pick one and stick with it (converting your bytes to strings as necessary; care with the encoding!) or wrap the entire thing in a ZipOutputStream so you can have multiple logical "files" with different contents.

Categories