1 package com.mockobjects.io; 2 3 import com.mockobjects.ExpectationCounter; 4 import com.mockobjects.ExpectationSegment; 5 import com.mockobjects.util.Verifier; 6 7 import java.io.OutputStream ; 8 import java.io.PrintStream ; 9 10 public class MockPrintStream extends PrintStream { 11 private ExpectationCounter myPrintlnCalls = new ExpectationCounter("MockPrintStream.println calls"); 12 private ExpectationSegment mySegment = new ExpectationSegment("String segment"); 13 private PrintStream myOldErrorStream; 14 15 public MockPrintStream() { 16 this(null); 17 } 18 19 public MockPrintStream(OutputStream out) { 20 super(out); 21 } 22 23 public MockPrintStream(OutputStream out, boolean autoFlush) { 24 super(out, autoFlush); 25 } 26 27 public void becomeErrorStream() { 28 myOldErrorStream = System.err; 29 System.setErr(this); 30 } 31 32 public void println(Object anObject) { 33 myPrintlnCalls.inc(); 34 } 35 36 public void println(String aString) { 37 println((Object ) aString); 38 mySegment.setActual(aString); 39 } 40 41 public void restoreErrorStream() { 42 System.setErr(myOldErrorStream); 43 } 44 45 public void setExpectedPrintlnCalls(int calls) { 46 myPrintlnCalls.setExpected(calls); 47 } 48 49 public void setExpectedStringSegment(String aString) { 50 mySegment.setExpected(aString); 51 } 52 53 public void verify() { 54 Verifier.verifyObject(this); 55 } 56 } 57 | Popular Tags |