1 package com.mockobjects.io; 2 3 import com.mockobjects.ExpectationCounter; 4 import com.mockobjects.ExpectationSegment; 5 import com.mockobjects.Verifiable; 6 import com.mockobjects.util.Verifier; 7 8 import java.io.IOException ; 9 import java.io.Writer ; 10 11 33 public class MockWriter extends Writer implements Verifiable { 34 private ExpectationSegment segment = 35 new ExpectationSegment("String segment"); 36 private ExpectationCounter flushCallsCount = 37 new ExpectationCounter("flush calls"); 38 private ExpectationCounter closeCallsCount = 39 new ExpectationCounter("close calls"); 40 41 private boolean writeShouldThrowException = false; 42 private boolean flushShouldThrowException = false; 43 private boolean closeShouldThrowException = false; 44 45 51 public MockWriter() { 52 super(new Object ()); 57 } 58 59 65 public void setWriteShouldThrowException() { 66 writeShouldThrowException = true; 67 } 68 69 74 public void setFlushShouldThrowException() { 75 flushShouldThrowException = true; 76 } 77 78 83 public void setCloseShouldThrowException() { 84 closeShouldThrowException = true; 85 } 86 87 92 public void setExpectedFlushCalls(int calls) { 93 flushCallsCount.setExpected(calls); 94 } 95 96 101 public void setExpectedCloseCalls(int calls) { 102 closeCallsCount.setExpected(calls); 103 } 104 105 114 public void setExpectedSegment(String aString) { 115 segment.setExpected(aString); 116 } 117 118 123 public void write(char cbuf[], int off, int len) throws IOException { 124 if (writeShouldThrowException) { 125 throw new IOException ("Mock Exception"); 126 } 127 128 segment.setActual(new String (cbuf, off, len)); 129 } 130 131 140 public void flush() throws IOException { 141 flushCallsCount.inc(); 142 143 if (flushShouldThrowException) { 144 throw new IOException ("Mock Exception"); 145 } 146 } 147 148 159 public void close() throws IOException { 160 closeCallsCount.inc(); 161 162 if (closeShouldThrowException) { 163 throw new IOException ("Mock Exception"); 164 } 165 } 166 167 public void verify() { 168 Verifier.verifyObject(this); 172 } 173 } 174 | Popular Tags |