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