MkdCommandHandlerTest.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.FileSystemEntry
24import org.mockftpserver.fake.filesystem.FileSystemException
25import org.mockftpserver.fake.filesystem.Permissions
26
27/**
28 * Tests for MkdCommandHandler
29 *
30 * @version $Revision$ - $Date$
31 *
32 * @author Chris Mair
33 */
34class MkdCommandHandlerTest extends AbstractFakeCommandHandlerTest {
35
36    static final PARENT = '/'
37    static final DIRNAME = "usr"
38    static final DIR = p(PARENT, DIRNAME)
39
40    void testHandleCommand() {
41        handleCommand([DIR])
42        assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIR])
43        assert fileSystem.exists(DIR)
44    }
45
46    void testHandleCommand_PathIsRelative() {
47        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, '/')
48        handleCommand([DIRNAME])
49        assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIRNAME])
50        assert fileSystem.exists(DIR)
51    }
52
53    void testHandleCommand_ParentDirectoryDoesNotExist() {
54        handleCommand(['/abc/def'])
55        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.doesNotExist', '/abc'])
56    }
57
58    void testHandleCommand_PathSpecifiesAFile() {
59        createFile(DIR)
60        handleCommand([DIR])
61        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.alreadyExists', DIR])
62        assert fileSystem.exists(DIR)
63    }
64
65    void testHandleCommand_MissingPathParameter() {
66        testHandleCommand_MissingRequiredParameter([])
67    }
68
69    void testHandleCommand_NoWriteAccessToParentDirectory() {
70        fileSystem.getEntry(PARENT).permissions = new Permissions('r-xr-xr-x')
71        handleCommand([DIR])
72        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotWrite', PARENT])
73    }
74
75    void testHandleCommand_NoExecuteAccessToParentDirectory() {
76        fileSystem.getEntry(PARENT).permissions = new Permissions('rw-rw-rw-')
77        handleCommand([DIR])
78        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotExecute', PARENT])
79    }
80
81    void testHandleCommand_CreateDirectoryThrowsException() {
82        def newMethod = {FileSystemEntry entry -> throw new FileSystemException("Error thrown by method [$methodName]", ERROR_MESSAGE_KEY) }
83        overrideMethod(fileSystem, "add", newMethod)
84
85        handleCommand([DIR])
86        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ERROR_MESSAGE_KEY)
87    }
88
89    void setUp() {
90        super.setUp()
91        createDirectory(PARENT)
92    }
93
94    //-------------------------------------------------------------------------
95    // Helper Methods
96    //-------------------------------------------------------------------------
97
98    CommandHandler createCommandHandler() {
99        new MkdCommandHandler()
100    }
101
102    Command createValidCommand() {
103        return new Command(CommandNames.MKD, [DIR])
104    }
105
106}