1 package com.mockobjects.io; 2 3 import java.io.PrintWriter ; 4 import java.io.StringWriter ; 5 import java.io.Writer ; 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 17 18 public class MockPrintWriter extends PrintWriter 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 ()); 25 } 26 27 private MockPrintWriter(Writer writer) { 28 super(writer); 29 } 30 31 public void setExpectedCloseCalls(int calls) { 32 myCloseCalls.setExpected(calls); 33 } 34 35 public void setExpectedSegment(String aString) { 36 mySegment.setExpected(aString); 37 } 38 39 public void write(String 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 |