_AbstractFakeCommandHandlerTest.groovy revision 8a951b23d1281158e4201bf0d425ced009bfaa89
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.fake.command 17 18import org.mockftpserver.core.CommandSyntaxException 19import org.mockftpserver.core.IllegalStateException 20import org.mockftpserver.core.NotLoggedInException 21import org.mockftpserver.core.command.Command 22import org.mockftpserver.core.command.ReplyCodes 23import org.mockftpserver.core.session.Session 24import org.mockftpserver.core.session.SessionKeys 25import org.mockftpserver.core.session.StubSession 26import org.mockftpserver.fake.StubServerConfiguration 27import org.mockftpserver.fake.filesystem.ExistingFileOperationException 28import org.mockftpserver.fake.filesystem.FakeUnixFileSystem 29import org.mockftpserver.fake.filesystem.FileSystemException 30import org.mockftpserver.fake.filesystem.InvalidFilenameException 31import org.mockftpserver.fake.filesystem.NewFileOperationException 32import org.mockftpserver.test.AbstractGroovyTest 33 34/** 35 * Tests for AbstractFakeCommandHandler 36 * 37 * @version $Revision$ - $Date$ 38 * 39 * @author Chris Mair 40 */ 41class AbstractFakeCommandHandlerClassTest extends AbstractGroovyTest { 42 43 static PATH = "some/path" 44 static REPLY_CODE = 99 45 static ARG = "ABC" 46 static MSG = "text {0}" 47 static MSG_WITH_ARG = "text ABC" 48 private AbstractFakeCommandHandler commandHandler 49 private session 50 private serverConfiguration 51 private fileSystem 52 53 //------------------------------------------------------------------------- 54 // Tests 55 //------------------------------------------------------------------------- 56 57 void testHandleCommand() { 58 def command = new Command("C1", ["abc"]) 59 commandHandler.handleCommand(command, session) 60 assert commandHandler.handled 61 62 assertHandleCommandReplyCode(new CommandSyntaxException(""), ReplyCodes.COMMAND_SYNTAX_ERROR) 63 assertHandleCommandReplyCode(new IllegalStateException(""), ReplyCodes.ILLEGAL_STATE) 64 assertHandleCommandReplyCode(new NotLoggedInException(""), ReplyCodes.NOT_LOGGED_IN) 65 assertHandleCommandReplyCode(new ExistingFileOperationException(""), ReplyCodes.EXISTING_FILE_ERROR) 66 assertHandleCommandReplyCode(new NewFileOperationException(""), ReplyCodes.NEW_FILE_ERROR) 67 assertHandleCommandReplyCode(new InvalidFilenameException(""), ReplyCodes.FILENAME_NOT_VALID) 68 69 shouldFail { commandHandler.handleCommand(null, session) } 70 shouldFail { commandHandler.handleCommand(command, null) } 71 } 72 73 void testHandleCommand_FileSystemException() { 74 assertHandleCommandReplyCode(new FileSystemException(PATH), ReplyCodes.EXISTING_FILE_ERROR, PATH) 75 commandHandler.replyCodeForFileSystemException = ReplyCodes.NEW_FILE_ERROR 76 assertHandleCommandReplyCode(new FileSystemException(PATH), ReplyCodes.NEW_FILE_ERROR, PATH) 77 } 78 79 void testSendReply() { 80 commandHandler.sendReply(session, REPLY_CODE) 81 assert session.sentReplies[0] == [REPLY_CODE, MSG], session.sentReplies[0] 82 83 commandHandler.sendReply(session, REPLY_CODE, [ARG]) 84 assert session.sentReplies[1] == [REPLY_CODE, MSG_WITH_ARG], session.sentReplies[0] 85 86 shouldFail { commandHandler.sendReply(null, REPLY_CODE) } 87 shouldFail { commandHandler.sendReply(session, 0) } 88 } 89 90 void testAssertValidReplyCode() { 91 commandHandler.assertValidReplyCode(1) // no exception expected 92 shouldFail { commandHandler.assertValidReplyCode(0) } 93 } 94 95 void testGetRequiredParameter() { 96 def command = new Command("C1", ["abc"]) 97 assert commandHandler.getRequiredParameter(command) == "abc" 98 assert commandHandler.getRequiredParameter(command, 0) == "abc" 99 shouldFail(CommandSyntaxException) { commandHandler.getRequiredParameter(command, 1) } 100 101 command = new Command("C1", []) 102 shouldFail(CommandSyntaxException) { commandHandler.getRequiredParameter(command) } 103 } 104 105 void testGetRequiredSessionAttribute() { 106 shouldFail(IllegalStateException) { commandHandler.getRequiredSessionAttribute(session, "undefined") } 107 108 session.setAttribute("abc", "not empty") 109 commandHandler.getRequiredSessionAttribute(session, "abc") // no exception 110 111 session.setAttribute("abc", "") 112 commandHandler.getRequiredSessionAttribute(session, "abc") // no exception 113 } 114 115 void testVerifyLoggedIn() { 116 shouldFail(NotLoggedInException) { commandHandler.verifyLoggedIn(session) } 117 session.setAttribute(SessionKeys.USER_ACCOUNT, "OK") 118 commandHandler.verifyLoggedIn(session) // no exception expected 119 } 120 121 void testVerifyFileSystemCondition() { 122 commandHandler.verifyFileSystemCondition(true, PATH) // no exception expected 123 shouldFail(FileSystemException) { commandHandler.verifyFileSystemCondition(false, PATH) } 124 shouldFail(FileSystemException) { commandHandler.verifyFileSystemCondition([], PATH) } 125 } 126 127 void testGetRealPath() { 128 assert commandHandler.getRealPath(session, "/xxx") == "/xxx" 129 130 session.setAttribute(SessionKeys.CURRENT_DIRECTORY, "/usr/me") 131 assert commandHandler.getRealPath(session, null) == "/usr/me" 132 assert commandHandler.getRealPath(session, "/xxx") == "/xxx" 133 assert commandHandler.getRealPath(session, "xxx") == "/usr/me/xxx" 134 } 135 136 //------------------------------------------------------------------------- 137 // Test Setup 138 //------------------------------------------------------------------------- 139 140 void setUp() { 141 super.setUp() 142 commandHandler = new TestFakeCommandHandler() 143 session = new StubSession() 144 serverConfiguration = new StubServerConfiguration() 145 fileSystem = new FakeUnixFileSystem() 146 serverConfiguration.setFileSystem(fileSystem) 147 serverConfiguration.setTextForReplyCode(REPLY_CODE, MSG) 148 149 commandHandler.serverConfiguration = serverConfiguration 150 } 151 152 //------------------------------------------------------------------------- 153 // Helper Methods 154 //------------------------------------------------------------------------- 155 156 /** 157 * Assert that when the CommandHandler handleCommand() method throws the 158 * specified exception, that the expected reply is sent through the session. 159 */ 160 private void assertHandleCommandReplyCode(Throwable exception, int expected, text=null) { 161 commandHandler.exception = exception 162 def command = new Command("C1", ["abc"]) 163 session.sentReplies.clear() 164 commandHandler.handleCommand(command, session) 165 def sentReply = session.sentReplies[0][0] 166 assert sentReply == expected 167 if (text) { 168 def sentMessage = session.sentReplies[0][1] 169 assert sentMessage.contains(text), "sentMessage=[$sentMessage] text=[$text]" 170 } 171 } 172 173} 174 175/** 176 * Concrete subclass of AbstractFakeCommandHandler for testing 177 */ 178private class TestFakeCommandHandler extends AbstractFakeCommandHandler { 179 boolean handled = false 180 def exception 181 182 protected void handle(Command command, Session session) { 183 if (exception) { 184 throw exception 185 } 186 this.handled = true 187 } 188}