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