Printer { public void pushWriter(Writer writer) { printWriter.flush(); persistentPrintWriter.flush(); stack.push(new StackEntry(internalWriter, printWriter, atStartOfLine)); this.internalWriter = writer != null ? writer : new NullWriter(); this.printWriter = asPrintWriter(internalWriter); teeWriter = null; } Printer(Writer writer); static Printer createStdOutPrinter(); Writer getWriter(); void pushWriter(Writer writer); PrintWriter asPrintWriter(); Writer popWriter(); void addPersistentWriter(Writer writer); void removePersistentWriter(Writer writer); Printer print(String output); Printer print(String format, Object ... args); Formatter asFormatter(); Printer startNewLine(); Printer flush(); Printer warn(String message); Printer warn(String format, Object ... args); boolean isPrintWarnings(); Printer setPrintWarnings(boolean printWarnings); Printer error(String message); Printer error(String format, Object ... args); Printer spaces(int n); List<String> getWarningsAndClear();  }
@Test public void testPushWriter() { StringWriter first = new StringWriter(); StringWriter second = new StringWriter(); Printer printer = new Printer(first); printer.print("first"); assertEquals("first", first.toString()); assertEquals("", second.toString()); printer.pushWriter(second); printer.print("second"); assertEquals("first", first.toString()); assertEquals("second", second.toString()); Writer popped = printer.popWriter(); assertSame(second, popped); printer.print("first"); assertEquals("firstfirst", first.toString()); assertEquals("second", second.toString()); }