1 package com.mockobjects.servlet; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import javax.servlet.ServletOutputStream ; 6 import com.mockobjects.ExpectationCounter; 7 import com.mockobjects.ExpectationValue; 8 9 public class MockServletOutputStream extends ServletOutputStream { 10 private ExpectationValue myWriteCalled = new ExpectationValue("MockServletOutputStream.write()"); 11 private boolean myThrowException = false; 12 private ExpectationCounter myCloseCallCount = new ExpectationCounter("MockServletOutputstream.close()"); 13 private ByteArrayOutputStream myBuffer; 14 15 public MockServletOutputStream() { 16 super(); 17 setupClearContents(); 18 } 19 20 public void setExpectedCloseCalls(int closeCall) { 21 myCloseCallCount.setExpected(closeCall); 22 } 23 24 public void setExpectingWriteCalls(boolean expectingWriteCall) { 25 myWriteCalled.setExpected(expectingWriteCall); 26 } 27 28 public void setThrowIOException(boolean throwException) { 29 myThrowException = throwException; 30 } 31 32 public void close() throws IOException { 33 myCloseCallCount.inc(); 34 } 35 36 public String toString() { 37 return getContents(); 38 } 39 40 public void write(int b) throws IOException { 41 myWriteCalled.setActual(true); 42 if (myThrowException) 43 throw new IOException ("Test IOException generated by request"); 44 myBuffer.write(b); 45 } 46 47 public void setupClearContents () { 48 myBuffer = new ByteArrayOutputStream (); 49 } 50 51 public String getContents() { 52 return myBuffer.toString(); 53 } 54 55 public void verify() { 56 myWriteCalled.verify(); 57 myCloseCallCount.verify(); 58 } 59 } 60 | Popular Tags |