01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.http;
04:
05: import java.net.Socket;
06: import fitnesse.testutil.MockSocket;
07:
08: public class MockResponseSender implements ResponseSender {
09: public MockSocket socket;
10: private boolean closed = false;
11:
12: public MockResponseSender() {
13: socket = new MockSocket("Mock");
14: }
15:
16: public MockResponseSender(Response response) throws Exception {
17: this ();
18: doSending(response);
19: }
20:
21: public void send(byte[] bytes) throws Exception {
22: socket.getOutputStream().write(bytes);
23: }
24:
25: public synchronized void close() throws Exception {
26: closed = true;
27: notify();
28: }
29:
30: public Socket getSocket() throws Exception {
31: return socket;
32: }
33:
34: public String sentData() throws Exception {
35: return socket.getOutput();
36: }
37:
38: public void doSending(Response response) throws Exception {
39: response.readyToSend(this );
40: waitForClose(10000);
41: }
42:
43: // Utility method that returns when this.closed is true. Throws an exception
44: // if the timeout is reached.
45: public synchronized void waitForClose(final long timeoutMillis)
46: throws Exception {
47: if (!closed) {
48: wait(timeoutMillis);
49: if (!closed)
50: throw new Exception(
51: "MockResponseSender could not be closed");
52: }
53: }
54:
55: public boolean isClosed() {
56: return closed;
57: }
58: }
|