KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > io > MockPrintStream


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 JavaDoc;
8 import java.io.PrintStream JavaDoc;
9
10 public class MockPrintStream extends PrintStream JavaDoc {
11     private ExpectationCounter myPrintlnCalls = new ExpectationCounter("MockPrintStream.println calls");
12     private ExpectationSegment mySegment = new ExpectationSegment("String segment");
13     private PrintStream JavaDoc myOldErrorStream;
14
15     public MockPrintStream() {
16         this(null);
17     }
18
19     public MockPrintStream(OutputStream JavaDoc out) {
20         super(out);
21     }
22
23     public MockPrintStream(OutputStream JavaDoc 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 JavaDoc anObject) {
33         myPrintlnCalls.inc();
34     }
35
36     public void println(String JavaDoc aString) {
37         println((Object JavaDoc) 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 JavaDoc aString) {
50         mySegment.setExpected(aString);
51     }
52
53     public void verify() {
54         Verifier.verifyObject(this);
55     }
56 }
57
Popular Tags