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.UserAccount
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 AbstractFakeCommandHandlerTestCase {
35
36    static final PARENT = '/'
37    static final DIRNAME = "usr"
38    static final DIR = p(PARENT, DIRNAME)
39    static final PERMISSIONS = new Permissions('rwx------')
40
41    void testHandleCommand() {
42        userAccount.defaultPermissionsForNewDirectory = PERMISSIONS
43        handleCommand([DIR])
44        assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIR])
45        assert fileSystem.exists(DIR)
46        def dirEntry = fileSystem.getEntry(DIR)
47        assert dirEntry.permissions == PERMISSIONS
48    }
49
50    void testHandleCommand_PathIsRelative() {
51        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, '/')
52        handleCommand([DIRNAME])
53        assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIRNAME])
54        assert fileSystem.exists(DIR)
55        def dirEntry = fileSystem.getEntry(DIR)
56        assert dirEntry.permissions == UserAccount.DEFAULT_PERMISSIONS_FOR_NEW_DIRECTORY
57    }
58
59    void testHandleCommand_ParentDirectoryDoesNotExist() {
60        handleCommand(['/abc/def'])
61        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.doesNotExist', '/abc'])
62    }
63
64    void testHandleCommand_PathSpecifiesAFile() {
65        createFile(DIR)
66        handleCommand([DIR])
67        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.alreadyExists', DIR])
68        assert fileSystem.exists(DIR)
69    }
70
71    void testHandleCommand_MissingPathParameter() {
72        testHandleCommand_MissingRequiredParameter([])
73    }
74
75    void testHandleCommand_NoWriteAccessToParentDirectory() {
76        fileSystem.getEntry(PARENT).permissions = new Permissions('r-xr-xr-x')
77        handleCommand([DIR])
78        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotWrite', PARENT])
79    }
80
81    void testHandleCommand_NoExecuteAccessToParentDirectory() {
82        fileSystem.getEntry(PARENT).permissions = new Permissions('rw-rw-rw-')
83        handleCommand([DIR])
84        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotExecute', PARENT])
85    }
86
87    void testHandleCommand_CreateDirectoryThrowsException() {
88        fileSystem.addMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
89        handleCommand([DIR])
90        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ERROR_MESSAGE_KEY)
91    }
92
93    void setUp() {
94        super.setUp()
95        createDirectory(PARENT)
96    }
97
98    //-------------------------------------------------------------------------
99    // Helper Methods
100    //-------------------------------------------------------------------------
101
102    CommandHandler createCommandHandler() {
103        new MkdCommandHandler()
104    }
105
106    Command createValidCommand() {
107        return new Command(CommandNames.MKD, [DIR])
108    }
109
110}