ListCommandHandlerTest.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 java.text.SimpleDateFormat 19import org.mockftpserver.core.command.Command 20import org.mockftpserver.core.command.CommandHandler 21import org.mockftpserver.core.command.CommandNames 22import org.mockftpserver.core.command.ReplyCodes 23import org.mockftpserver.core.session.SessionKeys 24import org.mockftpserver.fake.filesystem.DirectoryEntry 25import org.mockftpserver.fake.filesystem.FileEntry 26import org.mockftpserver.fake.filesystem.FileInfo 27import org.mockftpserver.fake.filesystem.FileSystemException 28 29/** 30 * Tests for ListCommandHandler 31 * 32 * @version $Revision$ - $Date$ 33 * 34 * @author Chris Mair 35 */ 36class ListCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest { 37 38 private static final SIZE_WIDTH = ListCommandHandler.SIZE_WIDTH 39 private static final DIR = "/usr" 40 private static final NAME = "abc.txt" 41 private static final LAST_MODIFIED = new Date() 42 private static final SIZE = 1000 43 44 def dateFormat 45 def lastModifiedFormatted 46 47 void testHandleCommand_SingleFile() { 48 fileSystem.addEntry(new FileEntry(path:p(DIR,NAME), lastModified:LAST_MODIFIED, contents:"abc")) 49 handleCommandAndVerifySendDataReplies([DIR]) 50 assertSessionData(listingForFile(LAST_MODIFIED, "abc".size(), NAME),) 51 } 52 53 void testHandleCommand_FilesAndDirectories() { 54 def NAME1 = "abc.txt" 55 def NAME2 = "OtherFiles" 56 def NAME3 = "another_file.doc" 57 def DATA1 = "abc" 58 def DATA3 = "".padRight(1000, 'x') 59 fileSystem.addEntry(new FileEntry(path:p(DIR,NAME1), lastModified:LAST_MODIFIED, contents:DATA1)) 60 fileSystem.addEntry(new DirectoryEntry(path:p(DIR,NAME2), lastModified:LAST_MODIFIED)) 61 fileSystem.addEntry(new FileEntry(path:p(DIR,NAME3), lastModified:LAST_MODIFIED, contents:DATA3)) 62 63 handleCommandAndVerifySendDataReplies([DIR]) 64 65 def actualLines = session.sentData[0].tokenize(endOfLine()) as Set 66 LOG.info("actualLines=$actualLines") 67 def EXPECTED = [ 68 listingForFile(LAST_MODIFIED, DATA1.size(), NAME1), 69 listingForDirectory(LAST_MODIFIED, NAME2), 70 listingForFile(LAST_MODIFIED, DATA3.size(), NAME3) ] as Set 71 assert actualLines == EXPECTED 72 } 73 74 void testHandleCommand_NoPath_UseCurrentDirectory() { 75 fileSystem.addEntry(new FileEntry(path:p(DIR,NAME), lastModified:LAST_MODIFIED, contents:"abc")) 76 session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR) 77 handleCommandAndVerifySendDataReplies([]) 78 assertSessionData(listingForFile(LAST_MODIFIED, "abc".size(), NAME),) 79 } 80 81 void testHandleCommand_EmptyDirectory() { 82 handleCommandAndVerifySendDataReplies([DIR]) 83 assertSessionData("") 84 } 85 86 void testHandleCommand_PathSpecifiesAFile() { 87 fileSystem.addEntry(new FileEntry(path:p(DIR,NAME), lastModified:LAST_MODIFIED, contents:"abc")) 88 handleCommandAndVerifySendDataReplies([p(DIR,NAME)]) 89 assertSessionData(listingForFile(LAST_MODIFIED, "abc".size(), NAME),) 90 } 91 92 void testHandleCommand_PathDoesNotExist() { 93 handleCommandAndVerifySendDataReplies(["/DoesNotExist"]) 94 assertSessionData("") 95 } 96 97 void testHandleCommand_ListFilesThrowsException() { 98 overrideMethodToThrowFileSystemException("listFiles") 99 handleCommand([DIR]) 100 assertSessionReplies([ReplyCodes.SEND_DATA_INITIAL_OK, ReplyCodes.SYSTEM_ERROR]) 101 } 102 103 void testDirectoryListing_File() { 104 def fileInfo = FileInfo.forFile(NAME, SIZE, LAST_MODIFIED) 105 def sizeStr = SIZE.toString().padLeft(SIZE_WIDTH) 106 def expected = "$lastModifiedFormatted $sizeStr $NAME" 107 def actual = commandHandler.directoryListing(fileInfo) 108 assert actual == expected 109 } 110 111 void testDirectoryListing_Directory() { 112 def fileInfo = FileInfo.forDirectory(NAME, LAST_MODIFIED) 113 def dirStr = "<DIR>".padRight(SIZE_WIDTH) 114 def expected = "$lastModifiedFormatted $dirStr $NAME" 115 def actual = commandHandler.directoryListing(fileInfo) 116 assert actual == expected 117 } 118 119 //------------------------------------------------------------------------- 120 // Helper Methods 121 //------------------------------------------------------------------------- 122 123 CommandHandler createCommandHandler() { 124 new ListCommandHandler() 125 } 126 127 Command createValidCommand() { 128 return new Command(CommandNames.LIST, [DIR]) 129 } 130 131 void setUp() { 132 super.setUp() 133 assert fileSystem.createDirectory("/usr") 134 dateFormat = new SimpleDateFormat(ListCommandHandler.DATE_FORMAT) 135 lastModifiedFormatted = dateFormat.format(LAST_MODIFIED) 136 } 137 138 private String listingForFile(lastModified, size, name) { 139 def lastModifiedFormatted = dateFormat.format(lastModified) 140 "$lastModifiedFormatted ${size.toString().padLeft(SIZE_WIDTH)} $name" 141 } 142 143 private String listingForDirectory(lastModified, name) { 144 def lastModifiedFormatted = dateFormat.format(lastModified) 145 "$lastModifiedFormatted ${'<DIR>'.padRight(SIZE_WIDTH)} $name" 146 } 147 148}