ListCommandHandlerTest.groovy revision 848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79
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.*
24
25/**
26 * Tests for ListCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class ListCommandHandlerTest extends AbstractFakeCommandHandlerTest {
33
34    private static final DIR = "/usr"
35    private static final NAME = "abc.txt"
36    private static final LAST_MODIFIED = new Date()
37
38    void testHandleCommand_SingleFile() {
39        final entry = new FileEntry(path: p(DIR, NAME), lastModified: LAST_MODIFIED, contents: "abc")
40        fileSystem.add(entry)
41        handleCommandAndVerifySendDataReplies([DIR])
42        assertSessionData(listingFor(entry))
43    }
44
45    void testHandleCommand_FilesAndDirectories() {
46        def DATA3 = "".padRight(1000, 'x')
47        final entry1 = new FileEntry(path: p(DIR, "abc.txt"), lastModified: LAST_MODIFIED, contents: "abc")
48        final entry2 = new DirectoryEntry(path: p(DIR, "OtherFiles"), lastModified: LAST_MODIFIED)
49        final entry3 = new FileEntry(path: p(DIR, "another_file.doc"), lastModified: LAST_MODIFIED, contents: DATA3)
50        fileSystem.add(entry1)
51        fileSystem.add(entry2)
52        fileSystem.add(entry3)
53
54        handleCommandAndVerifySendDataReplies([DIR])
55
56        def actualLines = session.sentData[0].tokenize(endOfLine()) as Set
57        LOG.info("actualLines=$actualLines")
58        def EXPECTED = [
59                listingFor(entry1),
60                listingFor(entry2),
61                listingFor(entry3)] as Set
62        assert actualLines == EXPECTED
63    }
64
65    void testHandleCommand_NoPath_UseCurrentDirectory() {
66        final entry = new FileEntry(path: p(DIR, NAME), lastModified: LAST_MODIFIED, contents: "abc")
67        fileSystem.add(entry)
68        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR)
69        handleCommandAndVerifySendDataReplies([])
70        assertSessionData(listingFor(entry))
71    }
72
73    void testHandleCommand_EmptyDirectory() {
74        handleCommandAndVerifySendDataReplies([DIR])
75        assertSessionData("")
76    }
77
78    void testHandleCommand_PathSpecifiesAFile() {
79        final entry = new FileEntry(path: p(DIR, NAME), lastModified: LAST_MODIFIED, contents: "abc")
80        fileSystem.add(entry)
81        handleCommandAndVerifySendDataReplies([p(DIR, NAME)])
82        assertSessionData(listingFor(entry))
83    }
84
85    void testHandleCommand_PathDoesNotExist() {
86        handleCommandAndVerifySendDataReplies(["/DoesNotExist"])
87        assertSessionData("")
88    }
89
90    void testHandleCommand_NoReadAccessToDirectory() {
91        fileSystem.getEntry(DIR).permissions = new Permissions('-wx-wx-wx')
92        handleCommand([DIR])
93        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
94        assertSessionReply(1, ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotRead', DIR])
95    }
96
97    void testHandleCommand_ListFilesThrowsException() {
98        fileSystem.listFilesMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
99        handleCommand([DIR])
100        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
101        assertSessionReply(1, ReplyCodes.SYSTEM_ERROR, ERROR_MESSAGE_KEY)
102    }
103
104    //-------------------------------------------------------------------------
105    // Helper Methods
106    //-------------------------------------------------------------------------
107
108    CommandHandler createCommandHandler() {
109        new ListCommandHandler()
110    }
111
112    Command createValidCommand() {
113        return new Command(CommandNames.LIST, [DIR])
114    }
115
116    void setUp() {
117        super.setUp()
118        createDirectory(DIR)
119        fileSystem.directoryListingFormatter = [format: {entry -> entry.toString()}] as DirectoryListingFormatter
120    }
121
122    private listingFor(FileSystemEntry fileSystemEntry) {
123        fileSystemEntry.toString()
124    }
125
126}