KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.mockobjects.io;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.StringWriter JavaDoc;
5 import java.io.Writer JavaDoc;
6
7 import com.mockobjects.ExpectationCounter;
8 import com.mockobjects.ExpectationSegment;
9 import com.mockobjects.Verifiable;
10 import com.mockobjects.util.Verifier;
11
12
13 /**
14  * @author steve@m3p.co.uk
15  * @author Francois Beausoleil (fbos@users.sourceforge.net)
16  */

17
18 public class MockPrintWriter extends PrintWriter JavaDoc implements Verifiable {
19     private ExpectationSegment mySegment = new ExpectationSegment("String segment");
20     private ExpectationCounter myCloseCalls = new ExpectationCounter("close calls");
21     private ExpectationCounter myFlushCalls = new ExpectationCounter("flush calls");
22
23     public MockPrintWriter() {
24         this(new StringWriter JavaDoc());
25     }
26
27     private MockPrintWriter(Writer JavaDoc writer) {
28         super(writer);
29     }
30
31     public void setExpectedCloseCalls(int calls) {
32         myCloseCalls.setExpected(calls);
33     }
34
35     public void setExpectedSegment(String JavaDoc aString) {
36         mySegment.setExpected(aString);
37     }
38
39     public void write(String JavaDoc s) {
40         super.write(s);
41         mySegment.setActual(s);
42     }
43
44     public void setExpectedFlushCalls(int calls) {
45         myFlushCalls.setExpected(calls);
46     }
47
48     public void flush() {
49         super.flush();
50         myFlushCalls.inc();
51     }
52
53     public void close() {
54         super.close();
55         myCloseCalls.inc();
56     }
57
58     public void verify() {
59         Verifier.verifyObject(this);
60     }
61 }
62
Popular Tags