19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/* 29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2008 the original author or authors. 39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * 49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License"); 59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License. 69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at 79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * 89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * http://www.apache.org/licenses/LICENSE-2.0 99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * 109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software 119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS, 129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and 149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License. 159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */ 169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.fake.command 179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.test.AbstractGroovyTest 199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.Command 209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.CommandHandler 219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.CommandNames 229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.session.StubSession 239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.session.SessionKeys 249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.StubServerConfiguration 259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.user.UserAccount 269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.apache.log4j.Logger 279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.ReplyCodes 289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.FileInfo 299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport java.text.SimpleDateFormat 309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.FileEntry 319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.filesystem.DirectoryEntry 329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/** 349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Tests for ListCommandHandler 359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * 369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @version $Revision: $ - $Date: $ 379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * 389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @author Chris Mair 399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */ 409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairclass ListCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest { 419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair private static final SIZE_WIDTH = ListCommandHandler.SIZE_WIDTH 439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair private static final DIR = "/usr" 449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair private static final NAME = "abc.txt" 459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair private static final LAST_MODIFIED = new Date() 469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair private static final SIZE = 1000 479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def dateFormat 499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def lastModifiedFormatted 509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testHandleCommand_SingleFile() { 529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair fileSystem.addEntry(new FileEntry(path:p(DIR,NAME), lastModified:LAST_MODIFIED, contents:"abc")) 539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair handleCommandAndVerifySendDataReplies([DIR]) 549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assertSessionData(listingForFile(LAST_MODIFIED, "abc".size(), NAME),) 559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 579d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testHandleCommand_FilesAndDirectories() { 589d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def NAME1 = "abc.txt" 599d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def NAME2 = "OtherFiles" 609d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def NAME3 = "another_file.doc" 619d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def DATA1 = "abc" 629d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def DATA3 = "".padRight(1000, 'x') 639d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair fileSystem.addEntry(new FileEntry(path:p(DIR,NAME1), lastModified:LAST_MODIFIED, contents:DATA1)) 649d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair fileSystem.addEntry(new DirectoryEntry(path:p(DIR,NAME2), lastModified:LAST_MODIFIED)) 659d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair fileSystem.addEntry(new FileEntry(path:p(DIR,NAME3), lastModified:LAST_MODIFIED, contents:DATA3)) 669d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 679d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair handleCommandAndVerifySendDataReplies([DIR]) 689d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 699d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def actualLines = session.sentData[0].tokenize(endOfLine()) as Set 709d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair LOG.info("actualLines=$actualLines") 719d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def EXPECTED = [ 729d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair listingForFile(LAST_MODIFIED, DATA1.size(), NAME1), 739d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair listingForDirectory(LAST_MODIFIED, NAME2), 749d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair listingForFile(LAST_MODIFIED, DATA3.size(), NAME3) ] as Set 759d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assert actualLines == EXPECTED 769d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 779d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 789d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testHandleCommand_NoPath_UseCurrentDirectory() { 799d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair fileSystem.addEntry(new FileEntry(path:p(DIR,NAME), lastModified:LAST_MODIFIED, contents:"abc")) 809d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR) 819d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair handleCommandAndVerifySendDataReplies([]) 829d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assertSessionData(listingForFile(LAST_MODIFIED, "abc".size(), NAME),) 839d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 849d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 859d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testHandleCommand_EmptyDirectory() { 869d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair handleCommandAndVerifySendDataReplies([DIR]) 879d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assertSessionData("") 889d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 899d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 909d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testHandleCommand_PathSpecifiesAFile() { 919d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair fileSystem.addEntry(new FileEntry(path:p(DIR,NAME), lastModified:LAST_MODIFIED, contents:"abc")) 929d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair handleCommandAndVerifySendDataReplies([p(DIR,NAME)]) 939d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assertSessionData(listingForFile(LAST_MODIFIED, "abc".size(), NAME),) 949d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 959d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 969d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testHandleCommand_PathDoesNotExist() { 979d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair handleCommandAndVerifySendDataReplies(["/DoesNotExist"]) 989d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assertSessionData("") 999d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 1009d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1019d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testDirectoryListing_File() { 1029d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def fileInfo = FileInfo.forFile(NAME, SIZE, LAST_MODIFIED) 1039d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def sizeStr = SIZE.toString().padLeft(SIZE_WIDTH) 1049d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def expected = "$lastModifiedFormatted $sizeStr $NAME" 1059d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def actual = commandHandler.directoryListing(fileInfo) 1069d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assert actual == expected 1079d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 1089d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1099d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void testDirectoryListing_Directory() { 1109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def fileInfo = FileInfo.forDirectory(NAME, LAST_MODIFIED) 1119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def dirStr = "<DIR>".padRight(SIZE_WIDTH) 1129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def expected = "$lastModifiedFormatted $dirStr $NAME" 1139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def actual = commandHandler.directoryListing(fileInfo) 1149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assert actual == expected 1159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 1169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair //------------------------------------------------------------------------- 1189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair // Helper Methods 1199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair //------------------------------------------------------------------------- 1209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair CommandHandler createCommandHandler() { 1229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair new ListCommandHandler() 1239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 1249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair Command createValidCommand() { 1269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair return new Command(CommandNames.LIST, [DIR]) 1279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 1289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair void setUp() { 1309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair super.setUp() 1319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair assert fileSystem.createDirectory("/usr") 1329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair dateFormat = new SimpleDateFormat(ListCommandHandler.DATE_FORMAT) 1339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair lastModifiedFormatted = dateFormat.format(LAST_MODIFIED) 1349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 1359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair 1369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair private String listingForFile(lastModified, size, name) { 1379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair def lastModifiedFormatted = dateFormat.format(lastModified) 1389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair "$lastModifiedFormatted ${size.toString().padLeft(SIZE_WIDTH)} $name" 1399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair } 140 141 private String listingForDirectory(lastModified, name) { 142 def lastModifiedFormatted = dateFormat.format(lastModified) 143 "$lastModifiedFormatted ${'<DIR>'.padRight(SIZE_WIDTH)} $name" 144 } 145 146}