CwdCommandHandlerTest.groovy revision fd225022650f63bcba4e6c495e9356861393c002
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 CwdCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class CwdCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
33
34    def DIR = "/usr"
35
36    void testHandleCommand() {
37        assert fileSystem.createDirectory(DIR)
38        commandHandler.handleCommand(createCommand([DIR]), session)
39        assertSessionReply(ReplyCodes.CWD_OK, ['cwd', DIR])
40        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == DIR
41    }
42
43    void testHandleCommand_PathIsRelative() {
44        def SUB = "sub"
45        assert fileSystem.createDirectory(p(DIR, SUB))
46        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR)
47        commandHandler.handleCommand(createCommand([SUB]), session)
48        assertSessionReply(ReplyCodes.CWD_OK, ['cwd', SUB])
49        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == p(DIR, SUB)
50    }
51
52    void testHandleCommand_PathDoesNotExistInFileSystem() {
53        commandHandler.handleCommand(createCommand([DIR]), session)
54        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, DIR)
55        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null
56    }
57
58    void testHandleCommand_PathSpecifiesAFile() {
59        assert fileSystem.createFile(DIR)
60        commandHandler.handleCommand(createCommand([DIR]), session)
61        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, DIR)
62        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null
63    }
64
65    void testHandleCommand_DirectoryNotAccessible() {
66        assert fileSystem.createDirectory(DIR)
67        def dir = fileSystem.getEntry(DIR)
68        dir.permissions = Permissions.NONE
69        commandHandler.handleCommand(createCommand([DIR]), session)
70        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, DIR)
71        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null
72    }
73
74    void testHandleCommand_MissingPathParameter() {
75        testHandleCommand_MissingRequiredParameter([])
76    }
77
78    //-------------------------------------------------------------------------
79    // Helper Methods
80    //-------------------------------------------------------------------------
81
82    CommandHandler createCommandHandler() {
83        new CwdCommandHandler()
84    }
85
86    Command createValidCommand() {
87        return new Command(CommandNames.CWD, [DIR])
88    }
89
90    void setUp() {
91        super.setUp()
92        userAccount.username = 'user'
93    }
94
95}