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