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