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