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 RmdCommandHandler
28 *
29 * @version $Revision$ - $Date$
30 *
31 * @author Chris Mair
32 */
33class RmdCommandHandlerTest extends AbstractFakeCommandHandlerTest {
34
35    static final PARENT = '/'
36    static final DIR = p(PARENT, "usr")
37
38    void testHandleCommand() {
39        createDirectory(DIR)
40        handleCommand([DIR])
41        assertSessionReply(ReplyCodes.RMD_OK, ['rmd', DIR])
42        assert fileSystem.exists(DIR) == false
43    }
44
45    void testHandleCommand_PathIsRelative() {
46        def SUB = "sub"
47        createDirectory(p(DIR, SUB))
48        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR)
49        handleCommand([SUB])
50        assertSessionReply(ReplyCodes.RMD_OK, ['rmd', SUB])
51        assert fileSystem.exists(p(DIR, SUB)) == false
52    }
53
54    void testHandleCommand_PathDoesNotExistInFileSystem() {
55        handleCommand([DIR])
56        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.doesNotExist', DIR])
57    }
58
59    void testHandleCommand_PathSpecifiesAFile() {
60        createFile(DIR)
61        handleCommand([DIR])
62        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.isNotADirectory', DIR])
63        assert fileSystem.exists(DIR)
64    }
65
66    void testHandleCommand_DirectoryIsNotEmpty() {
67        final FILE = DIR + "/file.txt"
68        createFile(FILE)
69        handleCommand([DIR])
70        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.directoryIsNotEmpty', DIR])
71        assert fileSystem.exists(DIR)
72        assert fileSystem.exists(FILE)
73    }
74
75    void testHandleCommand_MissingPathParameter() {
76        testHandleCommand_MissingRequiredParameter([])
77    }
78
79    void testHandleCommand_ListNamesThrowsException() {
80        createDirectory(DIR)
81        fileSystem.listNamesMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
82        handleCommand([DIR])
83        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ERROR_MESSAGE_KEY)
84    }
85
86    void testHandleCommand_DeleteThrowsException() {
87        createDirectory(DIR)
88        fileSystem.deleteMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
89        handleCommand([DIR])
90        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ERROR_MESSAGE_KEY)
91    }
92
93    void testHandleCommand_NoWriteAccessToParentDirectory() {
94        createDirectory(DIR)
95        fileSystem.getEntry(PARENT).permissions = new Permissions('r-xr-xr-x')
96        handleCommand([DIR])
97        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotWrite', PARENT])
98        assert fileSystem.exists(DIR)
99    }
100
101    //-------------------------------------------------------------------------
102    // Helper Methods
103    //-------------------------------------------------------------------------
104
105    CommandHandler createCommandHandler() {
106        new RmdCommandHandler()
107    }
108
109    Command createValidCommand() {
110        return new Command(CommandNames.RMD, [DIR])
111    }
112
113}