chrismair | 00dc7bd | 2014-05-11 21:21:28 +0000 | [diff] [blame] | 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 | */ |
| 16 | package org.mockftpserver.fake.command |
| 17 | |
| 18 | import org.mockftpserver.core.command.Command |
| 19 | import org.mockftpserver.core.command.CommandHandler |
| 20 | import org.mockftpserver.core.command.CommandNames |
| 21 | import org.mockftpserver.core.command.ReplyCodes |
| 22 | import org.mockftpserver.core.session.SessionKeys |
| 23 | import org.mockftpserver.fake.filesystem.FileSystemException |
| 24 | import org.mockftpserver.fake.filesystem.Permissions |
| 25 | |
| 26 | /** |
| 27 | * Tests for NlstCommandHandler |
| 28 | * |
| 29 | * @version $Revision$ - $Date$ |
| 30 | * |
| 31 | * @author Chris Mair |
| 32 | */ |
| 33 | class NlstCommandHandlerTest extends AbstractFakeCommandHandlerTestCase { |
| 34 | |
| 35 | def DIR = "/usr" |
| 36 | |
| 37 | void testHandleCommand_SingleFile() { |
| 38 | createFile("/usr/f1.txt") |
| 39 | handleCommandAndVerifySendDataReplies([DIR]) |
| 40 | assertSessionDataWithEndOfLine("f1.txt") |
| 41 | } |
| 42 | |
| 43 | void testHandleCommand_FilesAndDirectories() { |
| 44 | createFile("/usr/f1.txt") |
| 45 | createDirectory("/usr/OtherFiles") |
| 46 | createFile("/usr/f2.txt") |
| 47 | createDirectory("/usr/Archive") |
| 48 | handleCommandAndVerifySendDataReplies([DIR]) |
| 49 | |
| 50 | def EXPECTED = ["f1.txt", "OtherFiles", "f2.txt", "Archive"] as Set |
| 51 | def actualLines = session.sentData[0].tokenize(endOfLine()) as Set |
| 52 | LOG.info("actualLines=$actualLines") |
| 53 | assert actualLines == EXPECTED |
| 54 | assertSessionDataEndsWithEndOfLine() |
| 55 | } |
| 56 | |
| 57 | void testHandleCommand_NoPath_UseCurrentDirectory() { |
| 58 | createFile("/usr/f1.txt") |
| 59 | session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR) |
| 60 | handleCommandAndVerifySendDataReplies([]) |
| 61 | assertSessionDataWithEndOfLine("f1.txt") |
| 62 | } |
| 63 | |
| 64 | void testHandleCommand_EmptyDirectory() { |
| 65 | handleCommandAndVerifySendDataReplies([DIR]) |
| 66 | assertSessionData("") |
| 67 | } |
| 68 | |
| 69 | void testHandleCommand_PathSpecifiesAFile() { |
| 70 | createFile("/usr/f1.txt") |
| 71 | handleCommandAndVerifySendDataReplies(["/usr/f1.txt"]) |
| 72 | assertSessionDataWithEndOfLine("f1.txt") |
| 73 | } |
| 74 | |
| 75 | void testHandleCommand_PathDoesNotExist() { |
| 76 | handleCommandAndVerifySendDataReplies(["/DoesNotExist"]) |
| 77 | assertSessionData("") |
| 78 | } |
| 79 | |
| 80 | void testHandleCommand_NoReadAccessToDirectory() { |
| 81 | fileSystem.getEntry(DIR).permissions = new Permissions('-wx-wx-wx') |
| 82 | handleCommand([DIR]) |
| 83 | assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK) |
| 84 | assertSessionReply(1, ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotRead', DIR]) |
| 85 | } |
| 86 | |
| 87 | void testHandleCommand_ListNamesThrowsException() { |
| 88 | fileSystem.listNamesMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY) |
| 89 | handleCommand([DIR]) |
| 90 | assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK) |
| 91 | assertSessionReply(1, ReplyCodes.SYSTEM_ERROR, ERROR_MESSAGE_KEY) |
| 92 | } |
| 93 | |
| 94 | //------------------------------------------------------------------------- |
| 95 | // Helper Methods |
| 96 | //------------------------------------------------------------------------- |
| 97 | |
| 98 | CommandHandler createCommandHandler() { |
| 99 | new NlstCommandHandler() |
| 100 | } |
| 101 | |
| 102 | Command createValidCommand() { |
| 103 | return new Command(CommandNames.NLST, [DIR]) |
| 104 | } |
| 105 | |
| 106 | void setUp() { |
| 107 | super.setUp() |
| 108 | createDirectory(DIR) |
| 109 | } |
| 110 | |
| 111 | } |