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:
11: public boolean closed = false;
12:
13: public MockResponseSender() {
14: socket = new MockSocket("Mock");
15: }
16:
17: public MockResponseSender(Response response) throws Exception {
18: this ();
19: doSending(response);
20: }
21:
22: public void send(byte[] bytes) throws Exception {
23: socket.getOutputStream().write(bytes);
24: }
25:
26: public void close() throws Exception {
27: closed = true;
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(DefaultCloseTimeout);
41: }
42:
43: // Utility method that returns when this.closed is true. Throws an exception
44: // if the timeout is reached.
45: public void waitForClose(final long timeoutMillis) throws Exception {
46: long start = System.currentTimeMillis();
47: while (!closed) {
48: Thread.yield();
49: long now = System.currentTimeMillis();
50: if (now - start > timeoutMillis)
51: throw new Exception(
52: "MockResponseSender could not be closed");
53: }
54: }
55:
56: private int DefaultCloseTimeout = 6000;
57: }
|