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.testutil;
04:
05: import java.io.ByteArrayInputStream;
06: import java.io.ByteArrayOutputStream;
07: import java.io.IOException;
08: import java.io.InputStream;
09: import java.io.OutputStream;
10: import java.io.PipedInputStream;
11: import java.io.PipedOutputStream;
12: import java.net.InetSocketAddress;
13: import java.net.Socket;
14: import java.net.SocketAddress;
15:
16: public class MockSocket extends Socket {
17: InputStream input;
18:
19: OutputStream output;
20:
21: private String host;
22:
23: private boolean closed;
24:
25: public MockSocket() throws Exception {
26: PipedInputStream serverInput = new PipedInputStream();
27: PipedInputStream clientInput = new PipedInputStream();
28: PipedOutputStream serverOutput = new PipedOutputStream(
29: clientInput);
30: this .input = serverInput;
31: this .output = serverOutput;
32: }
33:
34: public MockSocket(String input) {
35: this .input = new ByteArrayInputStream(input.getBytes());
36: output = new ByteArrayOutputStream();
37: }
38:
39: public MockSocket(InputStream input, OutputStream output) {
40: this .input = input;
41: this .output = output;
42: }
43:
44: public InputStream getInputStream() {
45: return input;
46: }
47:
48: public OutputStream getOutputStream() {
49: return output;
50: }
51:
52: public void close() {
53: closed = true;
54: try {
55: input.close();
56: output.close();
57: } catch (IOException e) {
58: e.printStackTrace();
59: }
60: }
61:
62: public boolean isClosed() {
63: return closed;
64: }
65:
66: public String getOutput() throws Exception {
67: if (output instanceof ByteArrayOutputStream)
68: return ((ByteArrayOutputStream) output).toString("UTF-8");
69: else
70: return "";
71: }
72:
73: public void setHost(String host) {
74: this .host = host;
75: }
76:
77: public SocketAddress getRemoteSocketAddress() {
78: return new InetSocketAddress(host, 123);
79: }
80: }
|