CwdCommandHandlerTest.groovy revision ae0aaa3a69844af9a57ec1e0219adba82c9919d3
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.test.AbstractGroovyTest 19import org.mockftpserver.core.command.Command 20import org.mockftpserver.core.command.CommandHandler 21import org.mockftpserver.core.command.CommandNames 22import org.mockftpserver.core.session.StubSession 23import org.mockftpserver.core.session.SessionKeys 24import org.mockftpserver.fake.StubServerConfiguration 25import org.mockftpserver.fake.user.UserAccount 26import org.apache.log4j.Logger 27import org.mockftpserver.core.command.ReplyCodes 28import org.mockftpserver.core.util.AssertFailedException 29 30/** 31 * Tests for CwdCommandHandler 32 * 33 * @version $Revision: $ - $Date: $ 34 * 35 * @author Chris Mair 36 */ 37class CwdCommandHandlerTest extends AbstractFakeCommandHandlerTest { 38 39 def DIR = "/usr" 40 41 void testHandleCommand() { 42 assert fileSystem.createDirectory("/usr") 43 commandHandler.handleCommand(createCommand([DIR]), session) 44 assertSessionReply(ReplyCodes.CWD_OK) 45 assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == DIR 46 } 47 48 void testHandleCommand_PathDoesNotExistInFileSystem() { 49 commandHandler.handleCommand(createCommand([DIR]), session) 50 assertSessionReply(ReplyCodes.CWD_NO_SUCH_DIRECTORY) 51 assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null 52 } 53 54 void testHandleCommand_PathSpecifiesAFile() { 55 assert fileSystem.createFile(DIR) 56 commandHandler.handleCommand(createCommand([DIR]), session) 57 assertSessionReply(ReplyCodes.CWD_NO_SUCH_DIRECTORY) 58 assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null 59 } 60 61 void testHandleCommand_NotLoggedIn() { 62 session.removeAttribute(SessionKeys.USER_ACCOUNT) 63 testHandleCommand_MissingRequiredLogin() 64 } 65 66 void testHandleCommand_MissingPathParameter() { 67 testHandleCommand_MissingRequiredParameter([]) 68 } 69 70 //------------------------------------------------------------------------- 71 // Helper Methods 72 //------------------------------------------------------------------------- 73 74 CommandHandler createCommandHandler() { 75 new CwdCommandHandler() 76 } 77 78 Command createValidCommand() { 79 return new Command(CommandNames.CWD, [DIR]) 80 } 81 82 void setUp() { 83 super.setUp() 84 session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount) 85 } 86 87}