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