DeleCommandHandlerTest.groovy revision 3275b5ae2d141fda9e96af90c55d2f2f375c1093
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.fake.filesystem.Permissions
23
24/**
25 * Tests for DeleCommandHandler
26 *
27 * @version $Revision$ - $Date$
28 *
29 * @author Chris Mair
30 */
31class DeleCommandHandlerTest extends AbstractFakeCommandHandlerTest {
32
33    static final DIR = '/'
34    static final FILENAME = "f.txt"
35    static final FILE = p(DIR, FILENAME)
36
37    void testHandleCommand() {
38        createFile(FILE)
39        commandHandler.handleCommand(createCommand([FILE]), session)
40        assertSessionReply(ReplyCodes.DELE_OK, ['dele', FILE])
41        assert fileSystem.exists(FILE) == false
42    }
43
44    void testHandleCommand_PathIsRelative() {
45        createFile(FILE)
46        setCurrentDirectory("/")
47        commandHandler.handleCommand(createCommand([FILENAME]), session)
48        assertSessionReply(ReplyCodes.DELE_OK, ['dele', FILENAME])
49        assert fileSystem.exists(FILE) == false
50    }
51
52    void testHandleCommand_PathDoesNotExistInFileSystem() {
53        commandHandler.handleCommand(createCommand([FILE]), session)
54        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.isNotAFile', FILE])
55    }
56
57    void testHandleCommand_PathSpecifiesADirectory() {
58        createDirectory(FILE)
59        commandHandler.handleCommand(createCommand([FILE]), session)
60        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.isNotAFile', FILE])
61        assert fileSystem.exists(FILE)
62    }
63
64    void testHandleCommand_MissingPathParameter() {
65        testHandleCommand_MissingRequiredParameter([])
66    }
67
68    void testHandleCommand_DeleteThrowsException() {
69        createFile(FILE)
70        overrideMethodToThrowFileSystemException("delete")
71        handleCommand([FILE])
72        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ERROR_MESSAGE_KEY)
73    }
74
75    void testHandleCommand_NoWriteAccessToParentDirectory() {
76        createFile(FILE)
77        fileSystem.getEntry(DIR).permissions = new Permissions('r-xr-xr-x')
78        commandHandler.handleCommand(createCommand([FILE]), session)
79        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.cannotWrite', DIR])
80        assert fileSystem.exists(FILE)
81    }
82
83    //-------------------------------------------------------------------------
84    // Helper Methods
85    //-------------------------------------------------------------------------
86
87    CommandHandler createCommandHandler() {
88        new DeleCommandHandler()
89    }
90
91    Command createValidCommand() {
92        return new Command(CommandNames.DELE, [FILE])
93    }
94
95}