StubSession.groovy revision dfb59d50631968ab1a13002ea5421ece93169851
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 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#removeAttribute(java.lang.String) 99 */ 100 public void removeAttribute(String name) { 101 attributes.remove(name) 102 } 103 104 /** 105 * @see org.mockftpserver.core.session.Session#sendData(byte [], int) 106 */ 107 public void sendData(byte[] data, int numBytes) { 108 assert dataConnectionOpen, "The data connection must be OPEN" 109 sentData << new String(data, 0, numBytes) 110 } 111 112 /** 113 * @see org.mockftpserver.core.session.Session#sendReply(int, java.lang.String) 114 */ 115 public void sendReply(int replyCode, String replyText) { 116 sentReplies << [replyCode, replyText] 117 } 118 119 /** 120 * @see org.mockftpserver.core.session.Session#setAttribute(java.lang.String, java.lang.Object) 121 */ 122 public void setAttribute(String name, Object value) { 123 attributes[name] = value 124 } 125 126 /** 127 * @see org.mockftpserver.core.session.Session#switchToPassiveMode() 128 */ 129 public int switchToPassiveMode() { 130 switchedToPassiveMode = true 131 return switchToPassiveModeReturnValue 132 } 133 134 /** 135 * @see java.lang.Runnable#run() 136 */ 137 public void run() { 138 139 } 140 141 //------------------------------------------------------------------------- 142 // Stub-specific API - Helper methods not part of Session interface 143 //------------------------------------------------------------------------- 144 145 /** 146 * @return the reply code for the session reply at the specified index 147 */ 148 int getReplyCode(int replyIndex) { 149 return getReply(replyIndex)[0] 150 } 151 152 /** 153 * @return the reply message for the session reply at the specified index 154 */ 155 String getReplyMessage(int replyIndex) { 156 return getReply(replyIndex)[1] 157 } 158 159 /** 160 * @return the String representation of this object, including property names and values of interest 161 */ 162 String toString() { 163 "StubSession[sentReplies=$sentReplies sentData=$sentData attributes=$attributes closed=$closed " + 164 "clientDataHost=$clientDataHost clientDataPort=$clientDataPort]" 165 } 166 167 //------------------------------------------------------------------------- 168 // Internal Helper Methods 169 //------------------------------------------------------------------------- 170 171 private List getReply(int replyIndex) { 172 def reply = sentReplies[replyIndex] 173 assert reply, "No reply for index [$replyIndex] sent for ${this}" 174 return reply 175 } 176 177} 178