1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.core.socket;
17
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.OutputStream;
21import java.net.InetAddress;
22import java.net.Socket;
23
24/**
25 * Test (fake) subclass of Socket that performs no network access and allows setting the
26 * inputStream and OutputStream for the socket.
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32public final class StubSocket extends Socket {
33
34    private InetAddress inetAddress;
35    private InetAddress localAddress;
36    private InputStream inputStream;
37    private OutputStream outputStream;
38
39    /**
40     * Construct a new instance using the specified InputStream and OutputStream
41     * @param inputStream - the InputStream to use
42     * @param outputStream - the OutputStream to use
43     */
44    public StubSocket(InputStream inputStream, OutputStream outputStream) {
45        this(null, inputStream, outputStream);
46    }
47
48    /**
49     * Construct a new instance using the specified host, InputStream and OutputStream
50     * @param inetAddress - the InetAddress for this socket
51     * @param inputStream - the InputStream to use
52     * @param outputStream - the OutputStream to use
53     */
54    public StubSocket(InetAddress inetAddress, InputStream inputStream, OutputStream outputStream) {
55        this.inetAddress = inetAddress;
56        this.inputStream = inputStream;
57        this.outputStream = outputStream;
58    }
59
60    /**
61     * Override the superclass implementation. If the local inetAddress is not null,
62     * return that. Otherwise return super.getInetAddress().
63     * @see java.net.Socket#getInetAddress()
64     */
65    public InetAddress getInetAddress() {
66        return (inetAddress != null) ? inetAddress : super.getInetAddress();
67    }
68
69    /**
70     * Override the superclass implementation. If the local localAddress is not
71     * null, return that. Otherwise return super.getLocalAddress();
72     * @see java.net.Socket#getLocalAddress()
73     */
74    public InetAddress getLocalAddress() {
75        return (localAddress != null) ? localAddress : super.getLocalAddress();
76    }
77
78    /**
79     * Override the superclass implementation to provide the predefined InputStream
80     * @see java.net.Socket#getInputStream()
81     */
82    public InputStream getInputStream() throws IOException {
83        return inputStream;
84    }
85
86    /**
87     * Override the superclass implementation to provide the predefined OutputStream
88     * @see java.net.Socket#getOutputStream()
89     */
90    public OutputStream getOutputStream() throws IOException {
91        return outputStream;
92    }
93
94    //-------------------------------------------------------------------------
95    // Test-specific helper methods
96    //-------------------------------------------------------------------------
97
98    public void _setLocalAddress(InetAddress localAddress) {
99        this.localAddress = localAddress;
100    }
101}
102