StubSession.groovy revision 47fb67a4e600f339064de4c08f10279accc95e92
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 35 /** 36 * @see org.mockftpserver.core.session.Session#close() 37 */ 38 public void close() { 39 closed = true 40 } 41 42 /** 43 * @see org.mockftpserver.core.session.Session#closeDataConnection() 44 */ 45 public void closeDataConnection() { 46 47 } 48 49 /** 50 * @see org.mockftpserver.core.session.Session#getAttribute(java.lang.String) 51 */ 52 public Object getAttribute(String name) { 53 return attributes[name] 54 } 55 56 /** 57 * @see org.mockftpserver.core.session.Session#getAttributeNames() 58 */ 59 public Set getAttributeNames() { 60 return attributes.keySet() 61 } 62 63 /** 64 * @see org.mockftpserver.core.session.Session#getClientHost() 65 */ 66 public InetAddress getClientHost() { 67 return null 68 } 69 70 /** 71 * @see org.mockftpserver.core.session.Session#getServerHost() 72 */ 73 public InetAddress getServerHost() { 74 return null 75 } 76 77 /** 78 * @see org.mockftpserver.core.session.Session#openDataConnection() 79 */ 80 public void openDataConnection() { 81 82 } 83 84 /** 85 * @see org.mockftpserver.core.session.Session#readData() 86 */ 87 public byte[] readData() { 88 return dataToRead 89 } 90 91 /** 92 * @see org.mockftpserver.core.session.Session#removeAttribute(java.lang.String) 93 */ 94 public void removeAttribute(String name) { 95 attributes.remove(name) 96 } 97 98 /** 99 * @see org.mockftpserver.core.session.Session#sendData(byte [], int) 100 */ 101 public void sendData(byte[] data, int numBytes) { 102 sentData << new String(data, 0, numBytes) 103 } 104 105 /** 106 * @see org.mockftpserver.core.session.Session#sendReply(int, java.lang.String) 107 */ 108 public void sendReply(int replyCode, String replyText) { 109 sentReplies << [replyCode, replyText] 110 } 111 112 /** 113 * @see org.mockftpserver.core.session.Session#setAttribute(java.lang.String, java.lang.Object) 114 */ 115 public void setAttribute(String name, Object value) { 116 attributes[name] = value 117 } 118 119 /** 120 * @see org.mockftpserver.core.session.Session#switchToPassiveMode() 121 */ 122 public int switchToPassiveMode() { 123 return 0 124 } 125 126 /** 127 * @see java.lang.Runnable#run() 128 */ 129 public void run() { 130 131 } 132 133 //------------------------------------------------------------------------- 134 // Stub-specific API - Helper methods not part of Session interface 135 //------------------------------------------------------------------------- 136 137 String toString() { 138 "StubSession[sentReplies=$sentReplies sentData=$sentData attributes=$attributes closed=$closed " + 139 "clientDataHost=$clientDataHost clientDataPort=$clientDataPort]" 140 } 141 142} 143