StubSession.groovy revision 69da7866acc1a1755230757bfdef41b22c8e9ad7
1/*
2 * Copyright 2008 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.session
17
18/**
19 * Stub implementation of the     {@link Session}     interface for testing
20 *
21 * @version $Revision$ - $Date$
22 *
23 * @author Chris Mair
24 */
25class StubSession implements Session {
26
27    Map attributes = [:]
28    List sentReplies = []
29    List sentData = []
30    byte[] dataToRead
31    boolean closed
32    InetAddress clientDataHost
33    int clientDataPort
34    boolean dataConnectionOpen = false
35
36    /**
37     * @see org.mockftpserver.core.session.Session#close()
38     */
39    public void close() {
40        closed = true
41    }
42
43    /**
44     * @see org.mockftpserver.core.session.Session#closeDataConnection()
45     */
46    public void closeDataConnection() {
47        dataConnectionOpen = false
48    }
49
50    /**
51     * @see org.mockftpserver.core.session.Session#getAttribute(java.lang.String)
52     */
53    public Object getAttribute(String name) {
54        return attributes[name]
55    }
56
57    /**
58     * @see org.mockftpserver.core.session.Session#getAttributeNames()
59     */
60    public Set getAttributeNames() {
61        return attributes.keySet()
62    }
63
64    /**
65     * @see org.mockftpserver.core.session.Session#getClientHost()
66     */
67    public InetAddress getClientHost() {
68        return null
69    }
70
71    /**
72     * @see org.mockftpserver.core.session.Session#getServerHost()
73     */
74    public InetAddress getServerHost() {
75        return null
76    }
77
78    /**
79     * @see org.mockftpserver.core.session.Session#openDataConnection()
80     */
81    public void openDataConnection() {
82        dataConnectionOpen = true
83    }
84
85    /**
86     * @see org.mockftpserver.core.session.Session#readData()
87     */
88    public byte[] readData() {
89        assert dataConnectionOpen, "The data connection must be OPEN"
90        return dataToRead
91    }
92
93    /**
94     * @see org.mockftpserver.core.session.Session#removeAttribute(java.lang.String)
95     */
96    public void removeAttribute(String name) {
97        attributes.remove(name)
98    }
99
100    /**
101     * @see org.mockftpserver.core.session.Session#sendData(byte [], int)
102     */
103    public void sendData(byte[] data, int numBytes) {
104        assert dataConnectionOpen, "The data connection must be OPEN"
105        sentData << new String(data, 0, numBytes)
106    }
107
108    /**
109     * @see org.mockftpserver.core.session.Session#sendReply(int, java.lang.String)
110     */
111    public void sendReply(int replyCode, String replyText) {
112        sentReplies << [replyCode, replyText]
113    }
114
115    /**
116     * @see org.mockftpserver.core.session.Session#setAttribute(java.lang.String, java.lang.Object)
117     */
118    public void setAttribute(String name, Object value) {
119        attributes[name] = value
120    }
121
122    /**
123     * @see org.mockftpserver.core.session.Session#switchToPassiveMode()
124     */
125    public int switchToPassiveMode() {
126        return 0
127    }
128
129    /**
130     * @see java.lang.Runnable#run()
131     */
132    public void run() {
133
134    }
135
136    //-------------------------------------------------------------------------
137    // Stub-specific API - Helper methods not part of Session interface
138    //-------------------------------------------------------------------------
139
140    String toString() {
141        "StubSession[sentReplies=$sentReplies  sentData=$sentData  attributes=$attributes  closed=$closed  " +
142                "clientDataHost=$clientDataHost  clientDataPort=$clientDataPort]"
143    }
144
145}
146