problem = t; } finally { stdout.end(); stderr.end(); (value!=null ? "RESULT: "+value.toString()+"\n" : "")+ (problem!=null ? "ERROR: "+problem.toString()+"\n" : "")+ (!stdout.isEmpty() ? "STDOUT: "+stdout.toString()+"\n" : "")+ (!stderr.isEmpty() ? "STDERR: "+stderr.toString()+"\n" : "")); value!=null ? value.toString() : null, problem!=null ? problem.toString() : null, stdout.toString(), stderr.toString());
@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()); }
/** creates a capturing context which eats the output to this stream, blocking the original target */ public OutputCapturingContext capture() { return new OutputCapturingContext(this); }
@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()); }
/** 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); }
@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()); }
@Override public String toString() { return getOutputSoFar(); } public boolean isEmpty() {
/** 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()); }