NlstCommandHandlerTest.groovy revision d150a70ca8f8ad6cc300cf56174c8846066a4ad4
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
23
24
25/**
26 * Tests for NlstCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class NlstCommandHandlerTest extends AbstractFakeCommandHandlerTest {
33
34    def DIR = "/usr"
35
36    void testHandleCommand_SingleFile() {
37        createFile("/usr/f1.txt")
38        handleCommandAndVerifySendDataReplies([DIR])
39        assertSessionData("f1.txt")
40    }
41
42    void testHandleCommand_FilesAndDirectories() {
43        createFile("/usr/f1.txt")
44        createDirectory("/usr/OtherFiles")
45        createFile("/usr/f2.txt")
46        createDirectory("/usr/Archive")
47        handleCommandAndVerifySendDataReplies([DIR])
48
49        def EXPECTED = ["f1.txt", "OtherFiles", "f2.txt", "Archive"] as Set
50        def actualLines = session.sentData[0].tokenize(endOfLine()) as Set
51        LOG.info("actualLines=$actualLines")
52        assert actualLines == EXPECTED
53    }
54
55    void testHandleCommand_NoPath_UseCurrentDirectory() {
56        createFile("/usr/f1.txt")
57        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR)
58        handleCommandAndVerifySendDataReplies([])
59        assertSessionData("f1.txt")
60    }
61
62    void testHandleCommand_EmptyDirectory() {
63        handleCommandAndVerifySendDataReplies([DIR])
64        assertSessionData("")
65    }
66
67    void testHandleCommand_PathSpecifiesAFile() {
68        createFile("/usr/f1.txt")
69        handleCommandAndVerifySendDataReplies(["/usr/f1.txt"])
70        assertSessionData("f1.txt")
71    }
72
73    void testHandleCommand_PathDoesNotExist() {
74        handleCommandAndVerifySendDataReplies(["/DoesNotExist"])
75        assertSessionData("")
76    }
77
78    void testHandleCommand_ListNamesThrowsException() {
79        overrideMethodToThrowFileSystemException("listNames")
80        handleCommand([DIR])
81        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
82        assertSessionReply(1, ReplyCodes.SYSTEM_ERROR, ERROR_MESSAGE_KEY)
83    }
84
85    //-------------------------------------------------------------------------
86    // Helper Methods
87    //-------------------------------------------------------------------------
88
89    CommandHandler createCommandHandler() {
90        new NlstCommandHandler()
91    }
92
93    Command createValidCommand() {
94        return new Command(CommandNames.NLST, [DIR])
95    }
96
97    void setUp() {
98        super.setUp()
99        createDirectory("/usr")
100    }
101
102}