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.FileSystemException 24import org.mockftpserver.fake.filesystem.Permissions 25 26/** 27 * Tests for NlstCommandHandler 28 * 29 * @version $Revision$ - $Date$ 30 * 31 * @author Chris Mair 32 */ 33class NlstCommandHandlerTest extends AbstractFakeCommandHandlerTest { 34 35 def DIR = "/usr" 36 37 void testHandleCommand_SingleFile() { 38 createFile("/usr/f1.txt") 39 handleCommandAndVerifySendDataReplies([DIR]) 40 assertSessionData("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 } 55 56 void testHandleCommand_NoPath_UseCurrentDirectory() { 57 createFile("/usr/f1.txt") 58 session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR) 59 handleCommandAndVerifySendDataReplies([]) 60 assertSessionData("f1.txt") 61 } 62 63 void testHandleCommand_EmptyDirectory() { 64 handleCommandAndVerifySendDataReplies([DIR]) 65 assertSessionData("") 66 } 67 68 void testHandleCommand_PathSpecifiesAFile() { 69 createFile("/usr/f1.txt") 70 handleCommandAndVerifySendDataReplies(["/usr/f1.txt"]) 71 assertSessionData("f1.txt") 72 } 73 74 void testHandleCommand_PathDoesNotExist() { 75 handleCommandAndVerifySendDataReplies(["/DoesNotExist"]) 76 assertSessionData("") 77 } 78 79 void testHandleCommand_NoReadAccessToDirectory() { 80 fileSystem.getEntry(DIR).permissions = new Permissions('-wx-wx-wx') 81 handleCommand([DIR]) 82 assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK) 83 assertSessionReply(1, ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotRead', DIR]) 84 } 85 86 void testHandleCommand_ListNamesThrowsException() { 87 fileSystem.listNamesMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY) 88 handleCommand([DIR]) 89 assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK) 90 assertSessionReply(1, ReplyCodes.SYSTEM_ERROR, ERROR_MESSAGE_KEY) 91 } 92 93 //------------------------------------------------------------------------- 94 // Helper Methods 95 //------------------------------------------------------------------------- 96 97 CommandHandler createCommandHandler() { 98 new NlstCommandHandler() 99 } 100 101 Command createValidCommand() { 102 return new Command(CommandNames.NLST, [DIR]) 103 } 104 105 void setUp() { 106 super.setUp() 107 createDirectory(DIR) 108 } 109 110}