/** installs a thread local print stream to System.out if one is not already set; * caller may then #capture and #captureTee on it. * @return the ThreadLocalPrintStream which System.out is using */ public synchronized static ThreadLocalPrintStream stdout() { PrintStream oldOut = System.out; if (oldOut instanceof ThreadLocalPrintStream) return (ThreadLocalPrintStream)oldOut; ThreadLocalPrintStream newOut = new ThreadLocalPrintStream(System.out); System.setOut(newOut); return newOut; }
public String end() { if (streamToRestore!=null) stream.setThreadLocalPrintStream(streamToRestore); else stream.clearThreadLocalPrintStream(); finished = true; return out.toString(); } public boolean isActive() {
/** creates a capturing context which sees the output to this stream, without interrupting the original target */ public OutputCapturingContext captureTee() { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream toRestore = setThreadLocalPrintStream(new TeeOutputStream(getDelegate(), out)); return new OutputCapturingContext(this, out, toRestore); }
OutputCapturingContext stdout = ThreadLocalPrintStream.stdout().captureTee(); OutputCapturingContext stderr = ThreadLocalPrintStream.stderr().captureTee();
/** simple example showing how a capture to stdout can be set up */ @Test public void testStdoutCapture() { OutputCapturingContext capture = ThreadLocalPrintStream.stdout().captureTee(); System.out.println("hello"); String out = capture.end(); Assert.assertEquals("hello", out.trim()); System.out.println("goodbye - not captured, restored normal output"); Assert.assertEquals("hello", out.trim()); }
@Test public void testStdoutCaptureDetail() { ThreadLocalPrintStream.stdout(); System.out.println("1 - not captured, but next goes to capture only"); OutputCapturingContext capture = ThreadLocalPrintStream.stdout().capture(); final String TWO = "2 - captured"; System.out.println(TWO); Assert.assertEquals(TWO, capture.getOutputSoFar().trim()); String out = capture.end(); Assert.assertEquals(TWO, out.trim()); System.out.println("3 - not captured, restored normal output"); Assert.assertEquals(TWO, capture.getOutputSoFar().trim()); }
@Test public void testStderrCaptureDetail() { ThreadLocalPrintStream.stderr(); System.err.println("1 - not captured, but next goes to capture only"); OutputCapturingContext capture = ThreadLocalPrintStream.stderr().capture(); final String TWO = "2 - captured"; System.err.println(TWO); Assert.assertEquals(TWO, capture.getOutputSoFar().trim()); String out = capture.end(); Assert.assertEquals(TWO, out.trim()); System.err.println("3 - not captured, restored normal output"); Assert.assertEquals(TWO, capture.getOutputSoFar().trim()); }
/** constructor which installs a ByteArrayOutputStream to this stream */ public OutputCapturingContext(ThreadLocalPrintStream stream) { this.stream = stream; this.out = new ByteArrayOutputStream(); this.streamToRestore = stream.setThreadLocalPrintStream(out); } /** constructor for a capturing context which is already installed */
@Test public void testStdoutCaptureTeeDetail() { ThreadLocalPrintStream.stdout(); System.out.println("1 - not captured, but next go to capture and stdout"); OutputCapturingContext capture1 = ThreadLocalPrintStream.stdout().captureTee(); OutputCapturingContext capture2 = ThreadLocalPrintStream.stdout().captureTee(); final String TWO = "2 - captured"; System.out.println(TWO); Assert.assertEquals(TWO, capture1.getOutputSoFar().trim()); Assert.assertEquals(TWO, capture2.getOutputSoFar().trim()); String out2 = capture2.end(); final String THREE = "3 - captured by 1"; System.out.println(THREE); String out1 = capture1.end(); System.out.println("4 - not captured, restored normal output"); Assert.assertEquals(TWO, out2.trim()); Assert.assertEquals(TWO+Os.LINE_SEPARATOR+THREE, out1.trim()); }
/** installs a thread local print stream to System.err if one is not already set; * caller may then #capture and #captureTee on it. * @return the ThreadLocalPrintStream which System.err is using */ public synchronized static ThreadLocalPrintStream stderr() { PrintStream oldErr = System.err; if (oldErr instanceof ThreadLocalPrintStream) return (ThreadLocalPrintStream)oldErr; ThreadLocalPrintStream newErr = new ThreadLocalPrintStream(System.err); System.setErr(newErr); return newErr; }