RntoCommandHandlerTest.groovy revision 3523138583059ccc39bd3fbd43e2c077747eb1af
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
24import org.mockftpserver.fake.filesystem.Permissions
25
26/**
27 * Tests for RntoCommandHandler
28 *
29 * @version $Revision$ - $Date$
30 *
31 * @author Chris Mair
32 */
33class RntoCommandHandlerTest extends AbstractFakeCommandHandlerTest {
34
35    static final DIR = '/'
36    static final FROM_FILE = "/from.txt"
37    static final TO_FILE = "/file.txt"
38
39    void testHandleCommand() {
40        createFile(FROM_FILE)
41        handleCommand([TO_FILE])
42        assertSessionReply(ReplyCodes.RNTO_OK, ['rnto', FROM_FILE, TO_FILE])
43        assert !fileSystem.exists(FROM_FILE), FROM_FILE
44        assert fileSystem.exists(TO_FILE), TO_FILE
45        assertRenameFromSessionProperty(null)
46    }
47
48    void testHandleCommand_PathIsRelative() {
49        createFile(FROM_FILE)
50        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, "/")
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_ToFilenameSpecifiesADirectory() {
71        createDirectory(TO_FILE)
72        handleCommand([TO_FILE])
73        assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.isDirectory', TO_FILE])
74        assertRenameFromSessionProperty(FROM_FILE)
75    }
76
77    void testHandleCommand_NoWriteAccessToDirectory() {
78        createFile(FROM_FILE)
79        fileSystem.getEntry(DIR).permissions = new Permissions('r-xr-xr-x')
80        handleCommand([TO_FILE])
81        assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ['filesystem.cannotWrite', DIR])
82        assertRenameFromSessionProperty(FROM_FILE)
83    }
84
85    void testHandleCommand_FromFileDoesNotExist() {
86        createDirectory(DIR)
87        handleCommand([TO_FILE])
88        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, ['filesystem.pathDoesNotExist', FROM_FILE])
89        assertRenameFromSessionProperty(FROM_FILE)
90    }
91
92    void testHandleCommand_ToFileParentDirectoryDoesNotExist() {
93        createFile(FROM_FILE)
94        final BAD_DIR = p(DIR, 'SUB')
95        final BAD_TO_FILE = p(BAD_DIR, 'Filename.txt')
96        handleCommand([BAD_TO_FILE])
97        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, ['filesystem.pathDoesNotExist', BAD_DIR])
98        assertRenameFromSessionProperty(FROM_FILE)
99    }
100
101    void testHandleCommand_RenameThrowsException() {
102        createDirectory(DIR)
103        def newMethod = {String from, String to -> throw new FileSystemException("bad", 'msgkey') }
104        overrideMethod(fileSystem, "rename", newMethod)
105
106        handleCommand([TO_FILE])
107        assertSessionReply(ReplyCodes.WRITE_FILE_ERROR, ERROR_MESSAGE_KEY)
108        assertRenameFromSessionProperty(FROM_FILE)
109    }
110
111    void testHandleCommand_MissingPathParameter() {
112        testHandleCommand_MissingRequiredParameter([])
113    }
114
115    //-------------------------------------------------------------------------
116    // Helper Methods
117    //-------------------------------------------------------------------------
118
119    CommandHandler createCommandHandler() {
120        new RntoCommandHandler()
121    }
122
123    Command createValidCommand() {
124        return new Command(CommandNames.RNTO, [TO_FILE])
125    }
126
127    void setUp() {
128        super.setUp()
129        session.setAttribute(SessionKeys.RENAME_FROM, FROM_FILE)
130    }
131
132    private void assertRenameFromSessionProperty(String value) {
133        assert session.getAttribute(SessionKeys.RENAME_FROM) == value
134    }
135
136}