summaryrefslogtreecommitdiff
path: root/tags/2.1/src/test/java/org/mockftpserver/core/socket/StubSocket.java
blob: 3816bf0dd5c7e2160774159dbce0362eb967c608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright 2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.core.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * Test (fake) subclass of Socket that performs no network access and allows setting the 
 * inputStream and OutputStream for the socket.
 * 
 * @version $Revision$ - $Date$
 * 
 * @author Chris Mair
 */
public final class StubSocket extends Socket {

    private InetAddress inetAddress;
    private InetAddress localAddress;
    private InputStream inputStream;
    private OutputStream outputStream;
    
    /**
     * Construct a new instance using the specified InputStream and OutputStream
     * @param inputStream - the InputStream to use
     * @param outputStream - the OutputStream to use
     */
    public StubSocket(InputStream inputStream, OutputStream outputStream) {
        this(null, inputStream, outputStream);
    }
    
    /**
     * Construct a new instance using the specified host, InputStream and OutputStream
     * @param inetAddress - the InetAddress for this socket
     * @param inputStream - the InputStream to use
     * @param outputStream - the OutputStream to use
     */
    public StubSocket(InetAddress inetAddress, InputStream inputStream, OutputStream outputStream) {
        this.inetAddress = inetAddress;
        this.inputStream = inputStream;
        this.outputStream = outputStream;
    }
    
    /**
     * Override the superclass implementation. If the local inetAddress is not null, 
     * return that. Otherwise return super.getInetAddress().
     * @see java.net.Socket#getInetAddress()
     */
    public InetAddress getInetAddress() {
        return (inetAddress != null) ? inetAddress : super.getInetAddress();
    }
    
    /**
     * Override the superclass implementation. If the local localAddress is not
     * null, return that. Otherwise return super.getLocalAddress();
     * @see java.net.Socket#getLocalAddress()
     */
    public InetAddress getLocalAddress() {
        return (localAddress != null) ? localAddress : super.getLocalAddress();
    }
    
    /**
     * Override the superclass implementation to provide the predefined InputStream
     * @see java.net.Socket#getInputStream()
     */
    public InputStream getInputStream() throws IOException {
        return inputStream;
    }
    
    /**
     * Override the superclass implementation to provide the predefined OutputStream
     * @see java.net.Socket#getOutputStream()
     */
    public OutputStream getOutputStream() throws IOException {
        return outputStream;
    }
    
    //-------------------------------------------------------------------------
    // Test-specific helper methods
    //-------------------------------------------------------------------------

    public void _setLocalAddress(InetAddress localAddress) {
        this.localAddress = localAddress;
    }
}