RntoCommandHandlerTest.groovy revision 777c1c842d19df00d2529ccf43e4f4c26cfd39fb
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.command.Command 19import org.mockftpserver.core.command.CommandHandler 20import org.mockftpserver.core.command.CommandNames 21import org.mockftpserver.core.command.ReplyCodes 22import org.mockftpserver.core.session.SessionKeys 23import org.mockftpserver.fake.filesystem.FileSystemException 24 25/** 26 * Tests for RntoCommandHandler 27 * 28 * @version $Revision$ - $Date$ 29 * 30 * @author Chris Mair 31 */ 32class RntoCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest { 33 34 def FROM_FILE = "/from.txt" 35 def TO_FILE = "/file.txt" 36 37 void testHandleCommand() { 38 assert fileSystem.createFile(FROM_FILE) 39 commandHandler.handleCommand(createCommand([TO_FILE]), session) 40 assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_FILE, TO_FILE]) 41 assert !fileSystem.exists(FROM_FILE), FROM_FILE 42 assert fileSystem.exists(TO_FILE), TO_FILE 43 assert session.getAttribute(SessionKeys.RENAME_FROM) == null 44 } 45 46 void testHandleCommand_PathIsRelative() { 47 assert fileSystem.createFile(FROM_FILE) 48 session.setAttribute(SessionKeys.CURRENT_DIRECTORY, "/") 49 commandHandler.handleCommand(createCommand(["file.txt"]), session) 50 assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_FILE, 'file.txt']) 51 assert !fileSystem.exists(FROM_FILE), FROM_FILE 52 assert fileSystem.exists(TO_FILE), TO_FILE 53 assert session.getAttribute(SessionKeys.RENAME_FROM) == null 54 } 55 56 void testHandleCommand_FromFileNotSetInSession() { 57 session.removeAttribute(SessionKeys.RENAME_FROM) 58 testHandleCommand_MissingRequiredSessionAttribute() 59 } 60 61 void testHandleCommand_ToFilenameNotValid() { 62 assert fileSystem.createFile(FROM_FILE) 63 commandHandler.handleCommand(createCommand(["///"]), session) 64 assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, "///") 65 assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE 66 } 67 68 void testHandleCommand_ToFilenameSpecifiesADirectory() { 69 assert fileSystem.createDirectory(TO_FILE) 70 commandHandler.handleCommand(createCommand([TO_FILE]), session) 71 assertSessionReply(ReplyCodes.NEW_FILE_ERROR, ['filesystem.isDirectory', TO_FILE]) 72 assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE 73 } 74 75 void testHandleCommand_RenameFails() { 76 commandHandler.handleCommand(createCommand([TO_FILE]), session) 77 assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, ['filesystem.renameFailed', TO_FILE]) 78 assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE 79 } 80 81 void testHandleCommand_RenameThrowsException() { 82 def newMethod = {String from, String to -> throw new FileSystemException("bad", 'msgkey') } 83 overrideMethod(fileSystem, "rename", newMethod) 84 85 commandHandler.handleCommand(createCommand([TO_FILE]), session) 86 assertSessionReply(ReplyCodes.NEW_FILE_ERROR, ERROR_MESSAGE_KEY) 87 assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE 88 } 89 90 void testHandleCommand_MissingPathParameter() { 91 testHandleCommand_MissingRequiredParameter([]) 92 } 93 94 //------------------------------------------------------------------------- 95 // Helper Methods 96 //------------------------------------------------------------------------- 97 98 CommandHandler createCommandHandler() { 99 new RntoCommandHandler() 100 } 101 102 Command createValidCommand() { 103 return new Command(CommandNames.RNTO, [TO_FILE]) 104 } 105 106 void setUp() { 107 super.setUp() 108 session.setAttribute(SessionKeys.RENAME_FROM, FROM_FILE) 109 } 110 111}