Home | History | Annotate | Download | only in command
      1 /*
      2  * Copyright 2010 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  */
     16 package org.mockftpserver.fake.command
     17 
     18 import org.mockftpserver.core.command.Command
     19 import org.mockftpserver.core.command.CommandHandler
     20 import org.mockftpserver.core.command.CommandNames
     21 import org.mockftpserver.core.command.ReplyCodes
     22 import org.mockftpserver.core.session.SessionKeys
     23 import org.mockftpserver.fake.filesystem.FileSystemException
     24 import org.mockftpserver.fake.filesystem.Permissions
     25 
     26 /**
     27  * Tests for RntoCommandHandler
     28  *
     29  * @version $Revision$ - $Date$
     30  *
     31  * @author Chris Mair
     32  */
     33 class RntoCommandHandlerTest extends AbstractFakeCommandHandlerTestCase {
     34 
     35     private static final DIR = '/'
     36     private static final FROM_FILE = "/from.txt"
     37     private static final TO_FILE = "/file.txt"
     38     private static final FROM_DIR = "/subdir"
     39 
     40     void testHandleCommand_SingleFile() {
     41         createFile(FROM_FILE)
     42         handleCommand([TO_FILE])
     43         assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_FILE, TO_FILE])
     44         assert !fileSystem.exists(FROM_FILE), FROM_FILE
     45         assert fileSystem.exists(TO_FILE), TO_FILE
     46         assertRenameFromSessionProperty(null)
     47     }
     48 
     49     void testHandleCommand_SingleFile_PathIsRelative() {
     50         createFile(FROM_FILE)
     51         handleCommand(["file.txt"])
     52         assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_FILE, 'file.txt'])
     53         assert !fileSystem.exists(FROM_FILE), FROM_FILE
     54         assert fileSystem.exists(TO_FILE), TO_FILE
     55         assertRenameFromSessionProperty(null)
     56     }
     57 
     58     void testHandleCommand_FromFileNotSetInSession() {
     59         session.removeAttribute(SessionKeys.RENAME_FROM)
     60         testHandleCommand_MissingRequiredSessionAttribute()
     61     }
     62 
     63     void testHandleCommand_ToFilenameNotValid() {
     64         createFile(FROM_FILE)
     65         handleCommand([""])
     66         assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, "")
     67         assertRenameFromSessionProperty(FROM_FILE)
     68     }
     69 
     70     void testHandleCommand_EmptyDirectory() {
     71         final TO_DIR = "/newdir"
     72         createDirectory(FROM_DIR)
     73         setRenameFromSessionProperty(FROM_DIR)
     74         handleCommand([TO_DIR])
     75         assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_DIR, TO_DIR])
     76         assert !fileSystem.exists(FROM_DIR), FROM_DIR
     77         assert fileSystem.exists(TO_DIR), TO_DIR
     78         assertRenameFromSessionProperty(null)
     79     }
     80 
     81     void testHandleCommand_DirectoryContainingFilesAndSubdirectory() {
     82         final TO_DIR = "/newdir"
     83         createDirectory(FROM_DIR)
     84         createFile(FROM_DIR + "/a.txt")
     85         createFile(FROM_DIR + "/b.txt")
     86         createDirectory(FROM_DIR + "/child/grandchild")
     87         setRenameFromSessionProperty(FROM_DIR)
     88         handleCommand([TO_DIR])
     89         assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_DIR, TO_DIR])
     90         assert !fileSystem.exists(FROM_DIR), FROM_DIR
     91         assert fileSystem.exists(TO_DIR), TO_DIR
     92         assert fileSystem.isFile(TO_DIR + "/a.txt")
     93         assert fileSystem.isFile(TO_DIR + "/b.txt")
     94         assert fileSystem.isDirectory(TO_DIR + "/child")
     95         assert fileSystem.isDirectory(TO_DIR + "/child/grandchild")
     96         assertRenameFromSessionProperty(null)
     97     }
     98 
     99     void testHandleCommand_ToDirectoryIsChildOfFromDirectory() {
    100         final TO_DIR = FROM_DIR + "/child"
    101         createDirectory(FROM_DIR)
    102         setRenameFromSessionProperty(FROM_DIR)
    103         handleCommand([TO_DIR])
    104         assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.renameFailed', TO_DIR])
    105         assertRenameFromSessionProperty(FROM_DIR)
    106     }
    107 
    108     void testHandleCommand_NoWriteAccessToDirectory() {
    109         createFile(FROM_FILE)
    110         fileSystem.getEntry(DIR).permissions = new Permissions('r-xr-xr-x')
    111         handleCommand([TO_FILE])
    112         assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.cannotWrite', DIR])
    113         assertRenameFromSessionProperty(FROM_FILE)
    114     }
    115 
    116     void testHandleCommand_FromFileDoesNotExist() {
    117         createDirectory(DIR)
    118         handleCommand([TO_FILE])
    119         assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, ['filesystem.doesNotExist', FROM_FILE])
    120         assertRenameFromSessionProperty(FROM_FILE)
    121     }
    122 
    123     void testHandleCommand_ToFileParentDirectoryDoesNotExist() {
    124         createFile(FROM_FILE)
    125         final BAD_DIR = p(DIR, 'SUB')
    126         final BAD_TO_FILE = p(BAD_DIR, 'Filename.txt')
    127         handleCommand([BAD_TO_FILE])
    128         assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, ['filesystem.doesNotExist', BAD_DIR])
    129         assertRenameFromSessionProperty(FROM_FILE)
    130     }
    131 
    132     void testHandleCommand_RenameThrowsException() {
    133         createDirectory(DIR)
    134         fileSystem.renameMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
    135         handleCommand([TO_FILE])
    136         assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ERROR_MESSAGE_KEY)
    137         assertRenameFromSessionProperty(FROM_FILE)
    138     }
    139 
    140     void testHandleCommand_MissingPathParameter() {
    141         testHandleCommand_MissingRequiredParameter([])
    142     }
    143 
    144     //-------------------------------------------------------------------------
    145     // Helper Methods
    146     //-------------------------------------------------------------------------
    147 
    148     CommandHandler createCommandHandler() {
    149         new RntoCommandHandler()
    150     }
    151 
    152     Command createValidCommand() {
    153         return new Command(CommandNames.RNTO, [TO_FILE])
    154     }
    155 
    156     void setUp() {
    157         super.setUp()
    158         setCurrentDirectory(DIR)
    159         setRenameFromSessionProperty(FROM_FILE)
    160     }
    161 
    162     private void setRenameFromSessionProperty(String renameFrom) {
    163         session.setAttribute(SessionKeys.RENAME_FROM, renameFrom)
    164     }
    165 
    166     private void assertRenameFromSessionProperty(String value) {
    167         assert session.getAttribute(SessionKeys.RENAME_FROM) == value
    168     }
    169 
    170 }