NlstCommandHandlerTest.groovy revision 70cc38904cf8c9abc6f9994cec83e2b7685a607d
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 24 25/** 26 * Tests for NlstCommandHandler 27 * 28 * @version $Revision$ - $Date$ 29 * 30 * @author Chris Mair 31 */ 32class NlstCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest { 33 34 def DIR = "/usr" 35 36 void testHandleCommand_SingleFile() { 37 assert fileSystem.createFile("/usr/f1.txt") 38 handleCommandAndVerifySendDataReplies([DIR]) 39 assertSessionData("f1.txt") 40 } 41 42 void testHandleCommand_FilesAndDirectories() { 43 assert fileSystem.createFile("/usr/f1.txt") 44 assert fileSystem.createDirectory("/usr/OtherFiles") 45 assert fileSystem.createFile("/usr/f2.txt") 46 assert fileSystem.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 assert fileSystem.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 assert fileSystem.createFile("/usr/f1.txt") 69 handleCommandAndVerifySendDataReplies(["/usr/f1.txt"]) 70 assertSessionData("") 71 } 72 73 void testHandleCommand_PathDoesNotExist() { 74 handleCommandAndVerifySendDataReplies(["/DoesNotExist"]) 75 assertSessionData("") 76 } 77 78 void testHandleCommand_ListNamesThrowsException() { 79 overrideMethodToThrowFileSystemException("listNames") 80 handleCommand([DIR]) 81 assertSessionReplies([ReplyCodes.SEND_DATA_INITIAL_OK, ReplyCodes.SYSTEM_ERROR]) 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}