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