chrismair | 00dc7bd | 2014-05-11 21:21:28 +0000 | [diff] [blame] | 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 | */
|
| 16 | package 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 | */
|
| 25 | class StubSession implements Session {
|
| 26 |
|
| 27 | Map attributes = [:]
|
| 28 | private List sentReplies = []
|
| 29 | List sentData = []
|
| 30 | //byte[] dataToRead
|
| 31 | Object dataToRead
|
| 32 | boolean closed
|
| 33 | InetAddress clientDataHost
|
| 34 | int clientDataPort
|
| 35 | boolean dataConnectionOpen = false
|
| 36 | int switchToPassiveModeReturnValue
|
| 37 | boolean switchedToPassiveMode = false
|
| 38 | InetAddress serverHost
|
| 39 |
|
| 40 | /**
|
| 41 | * @see org.mockftpserver.core.session.Session#close()
|
| 42 | */
|
| 43 | public void close() {
|
| 44 | closed = true
|
| 45 | }
|
| 46 |
|
| 47 | /**
|
| 48 | * @see org.mockftpserver.core.session.Session#closeDataConnection()
|
| 49 | */
|
| 50 | public void closeDataConnection() {
|
| 51 | dataConnectionOpen = false
|
| 52 | }
|
| 53 |
|
| 54 | /**
|
| 55 | * @see org.mockftpserver.core.session.Session#getAttribute(java.lang.String)
|
| 56 | */
|
| 57 | public Object getAttribute(String name) {
|
| 58 | return attributes[name]
|
| 59 | }
|
| 60 |
|
| 61 | /**
|
| 62 | * @see org.mockftpserver.core.session.Session#getAttributeNames()
|
| 63 | */
|
| 64 | public Set getAttributeNames() {
|
| 65 | return attributes.keySet()
|
| 66 | }
|
| 67 |
|
| 68 | /**
|
| 69 | * @see org.mockftpserver.core.session.Session#getClientHost()
|
| 70 | */
|
| 71 | public InetAddress getClientHost() {
|
| 72 | return null
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * @see org.mockftpserver.core.session.Session#getServerHost()
|
| 77 | */
|
| 78 | public InetAddress getServerHost() {
|
| 79 | return serverHost
|
| 80 | }
|
| 81 |
|
| 82 | /**
|
| 83 | * @see org.mockftpserver.core.session.Session#openDataConnection()
|
| 84 | */
|
| 85 | public void openDataConnection() {
|
| 86 | dataConnectionOpen = true
|
| 87 | }
|
| 88 |
|
| 89 | /**
|
| 90 | * @see org.mockftpserver.core.session.Session#readData()
|
| 91 | */
|
| 92 | public byte[] readData() {
|
| 93 | assert dataConnectionOpen, "The data connection must be OPEN"
|
| 94 | return dataToRead
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * @see org.mockftpserver.core.session.Session#readData()
|
| 99 | */
|
| 100 | public byte[] readData(int numBytes) {
|
| 101 | assert dataConnectionOpen, "The data connection must be OPEN"
|
| 102 | return dataToRead
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * @see org.mockftpserver.core.session.Session#removeAttribute(java.lang.String)
|
| 107 | */
|
| 108 | public void removeAttribute(String name) {
|
| 109 | attributes.remove(name)
|
| 110 | }
|
| 111 |
|
| 112 | /**
|
| 113 | * @see org.mockftpserver.core.session.Session#sendData(byte [], int)
|
| 114 | */
|
| 115 | public void sendData(byte[] data, int numBytes) {
|
| 116 | assert dataConnectionOpen, "The data connection must be OPEN"
|
| 117 | sentData << new String(data, 0, numBytes)
|
| 118 | }
|
| 119 |
|
| 120 | /**
|
| 121 | * @see org.mockftpserver.core.session.Session#sendReply(int, java.lang.String)
|
| 122 | */
|
| 123 | public void sendReply(int replyCode, String replyText) {
|
| 124 | sentReplies << [replyCode, replyText]
|
| 125 | }
|
| 126 |
|
| 127 | /**
|
| 128 | * @see org.mockftpserver.core.session.Session#setAttribute(java.lang.String, java.lang.Object)
|
| 129 | */
|
| 130 | public void setAttribute(String name, Object value) {
|
| 131 | attributes[name] = value
|
| 132 | }
|
| 133 |
|
| 134 | /**
|
| 135 | * @see org.mockftpserver.core.session.Session#switchToPassiveMode()
|
| 136 | */
|
| 137 | public int switchToPassiveMode() {
|
| 138 | switchedToPassiveMode = true
|
| 139 | return switchToPassiveModeReturnValue
|
| 140 | }
|
| 141 |
|
| 142 | /**
|
| 143 | * @see java.lang.Runnable#run()
|
| 144 | */
|
| 145 | public void run() {
|
| 146 |
|
| 147 | }
|
| 148 |
|
| 149 | //-------------------------------------------------------------------------
|
| 150 | // Stub-specific API - Helper methods not part of Session interface
|
| 151 | //-------------------------------------------------------------------------
|
| 152 |
|
| 153 | /**
|
| 154 | * @return the reply code for the session reply at the specified index
|
| 155 | */
|
| 156 | int getReplyCode(int replyIndex) {
|
| 157 | return getReply(replyIndex)[0]
|
| 158 | }
|
| 159 |
|
| 160 | /**
|
| 161 | * @return the reply message for the session reply at the specified index
|
| 162 | */
|
| 163 | String getReplyMessage(int replyIndex) {
|
| 164 | return getReply(replyIndex)[1]
|
| 165 | }
|
| 166 |
|
| 167 | /**
|
| 168 | * @return the String representation of this object, including property names and values of interest
|
| 169 | */
|
| 170 | String toString() {
|
| 171 | "StubSession[sentReplies=$sentReplies sentData=$sentData attributes=$attributes closed=$closed " +
|
| 172 | "clientDataHost=$clientDataHost clientDataPort=$clientDataPort]"
|
| 173 | }
|
| 174 |
|
| 175 | //-------------------------------------------------------------------------
|
| 176 | // Internal Helper Methods
|
| 177 | //-------------------------------------------------------------------------
|
| 178 |
|
| 179 | private List getReply(int replyIndex) {
|
| 180 | def reply = sentReplies[replyIndex]
|
| 181 | assert reply, "No reply for index [$replyIndex] sent for ${this}"
|
| 182 | return reply
|
| 183 | }
|
| 184 |
|
| 185 | }
|