@Test public void catMoreTextThanFitsInSingleBufferReceivedOnStdout() throws Exception { ProcessExecutorParams params; if (Platform.detect() == Platform.WINDOWS) { params = ProcessExecutorParams.ofCommand( "python", "-c", "import sys, shutil; shutil.copyfileobj(sys.stdin, sys.stdout)"); } else { params = ProcessExecutorParams.ofCommand("cat"); } ListeningProcessExecutor executor = new ListeningProcessExecutor(); StringBuilder sb = new StringBuilder(); // Use a 3 byte Unicode sequence to ensure writes go across byte buffer // boundaries, and append it as many times as needed to ensure it doesn't // fit in a single I/O buffer. String threeByteUTF8 = "\u2764"; for (int i = 0; i < ListeningProcessExecutor.LaunchedProcess.BUFFER_CAPACITY + 1; i++) { sb.append(threeByteUTF8); } sb.append(String.format("%n")); String longString = sb.toString(); StdinWritingListener listener = new StdinWritingListener(longString); ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener); process.wantWrite(); int returnCode = executor.waitForProcess(process); assertThat(returnCode, equalTo(0)); assertThat(listener.capturedStdout.toString("UTF-8"), equalTo(longString)); assertThat(listener.capturedStderr.toString("UTF-8"), is(emptyString())); }
@Test public void catTextSentToStdinReceivedOnStdout() throws Exception { ProcessExecutorParams params; if (Platform.detect() == Platform.WINDOWS) { params = ProcessExecutorParams.ofCommand( "python", "-c", "import sys, shutil; shutil.copyfileobj(sys.stdin, sys.stdout)"); } else { params = ProcessExecutorParams.ofCommand("cat"); } ListeningProcessExecutor executor = new ListeningProcessExecutor(); StdinWritingListener listener = new StdinWritingListener(String.format("Meow%n")); ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener); process.wantWrite(); int returnCode = executor.waitForProcess(process); assertThat(returnCode, equalTo(0)); assertThat(listener.capturedStdout.toString("UTF-8"), equalTo(String.format("Meow%n"))); assertThat(listener.capturedStderr.toString("UTF-8"), is(emptyString())); }