KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > servlet > MockServletOutputStream


1 package com.mockobjects.servlet;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import javax.servlet.ServletOutputStream JavaDoc;
6 import com.mockobjects.ExpectationCounter;
7 import com.mockobjects.ExpectationValue;
8
9 public class MockServletOutputStream extends ServletOutputStream JavaDoc {
10     private ExpectationValue myWriteCalled = new ExpectationValue("MockServletOutputStream.write()");
11     private boolean myThrowException = false;
12     private ExpectationCounter myCloseCallCount = new ExpectationCounter("MockServletOutputstream.close()");
13     private ByteArrayOutputStream JavaDoc 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 JavaDoc {
33         myCloseCallCount.inc();
34     }
35
36     public String JavaDoc toString() {
37         return getContents();
38     }
39
40     public void write(int b) throws IOException JavaDoc {
41         myWriteCalled.setActual(true);
42         if (myThrowException)
43             throw new IOException JavaDoc("Test IOException generated by request");
44         myBuffer.write(b);
45     }
46
47     public void setupClearContents () {
48         myBuffer = new ByteArrayOutputStream JavaDoc();
49     }
50
51     public String JavaDoc getContents() {
52         return myBuffer.toString();
53     }
54
55     public void verify() {
56         myWriteCalled.verify();
57         myCloseCallCount.verify();
58     }
59 }
60
Popular Tags