1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/* 2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2008 the original author or authors. 3bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License. 6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at 7bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 8bda3441225e0607b5ced8b538123fd7c7a417910chrismair * http://www.apache.org/licenses/LICENSE-2.0 9bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software 11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and 14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License. 15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.fake.filesystem 17bda3441225e0607b5ced8b538123fd7c7a417910chrismair 18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.util.IoUtil 19bda3441225e0607b5ced8b538123fd7c7a417910chrismair 20bda3441225e0607b5ced8b538123fd7c7a417910chrismair/** 21bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Tests for subclasses of AbstractFakeFileSystem. Subclasses must define 22bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 23bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$ 24bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair 26bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 27bda3441225e0607b5ced8b538123fd7c7a417910chrismairabstract class AbstractFakeFileSystemTestCase extends AbstractFileSystemTestCase { 28bda3441225e0607b5ced8b538123fd7c7a417910chrismair 29bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ------------------------------------------------------------------------- 30bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Tests 31bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ------------------------------------------------------------------------- 32bda3441225e0607b5ced8b538123fd7c7a417910chrismair 33bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testDefaultDirectoryListingFormatterClass() { 34bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.directoryListingFormatter.class == expectedDirectoryListingFormatterClass 35bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 36bda3441225e0607b5ced8b538123fd7c7a417910chrismair 37bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testAdd_PathLocked() { 38bda3441225e0607b5ced8b538123fd7c7a417910chrismair def dirEntry = new DirectoryEntry(NEW_DIR) 39bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(dirEntry) 40bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry = new FileEntry(NEW_FILE) 41bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(fileEntry) 42bda3441225e0607b5ced8b538123fd7c7a417910chrismair 43bda3441225e0607b5ced8b538123fd7c7a417910chrismair // The path should be locked for both entries 44bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFail { dirEntry.setPath('abc') } 45bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFail { fileEntry.setPath('abc') } 46bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 47bda3441225e0607b5ced8b538123fd7c7a417910chrismair 48bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testAdd_Directory_CreateParentDirectoriesAutomatically() { 49bda3441225e0607b5ced8b538123fd7c7a417910chrismair final NEW_SUBDIR = fileSystem.path(NEW_DIR, "sub") 50bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR), "Before createDirectory" 51bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_SUBDIR), "Before createDirectory" 52bda3441225e0607b5ced8b538123fd7c7a417910chrismair 53bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.createParentDirectoriesAutomatically = true 54bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_SUBDIR)) 55bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_DIR), "After createDirectory" 56bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_SUBDIR), "$NEW_SUBDIR: After createDirectory" 57bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 58bda3441225e0607b5ced8b538123fd7c7a417910chrismair 59bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testAdd_File_CreateParentDirectoriesAutomatically() { 60bda3441225e0607b5ced8b538123fd7c7a417910chrismair final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt") 61bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR), "Before createDirectory" 62bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_FILE_IN_SUBDIR), "Before createDirectory" 63bda3441225e0607b5ced8b538123fd7c7a417910chrismair 64bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.createParentDirectoriesAutomatically = true 65bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR)) 66bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_DIR), "After createDirectory" 67bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_FILE_IN_SUBDIR), "$NEW_FILE_IN_SUBDIR: After createDirectory" 68bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 69bda3441225e0607b5ced8b538123fd7c7a417910chrismair 70bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testAdd_File_CreateParentDirectoriesAutomatically_False() { 71bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.createParentDirectoriesAutomatically = false 72bda3441225e0607b5ced8b538123fd7c7a417910chrismair final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt") 73bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR), "Before createDirectory" 74bda3441225e0607b5ced8b538123fd7c7a417910chrismair 75bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFail(FileSystemException) { fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR)) } 76bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR), "After createDirectory" 77bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 78bda3441225e0607b5ced8b538123fd7c7a417910chrismair 79bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testSetEntries() { 80bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.createParentDirectoriesAutomatically = false 81bda3441225e0607b5ced8b538123fd7c7a417910chrismair def entries = [new FileEntry(NEW_FILE), new DirectoryEntry(NEW_DIR)] 82bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.setEntries(entries) 83bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_DIR) 84bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_FILE) 85bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 86bda3441225e0607b5ced8b538123fd7c7a417910chrismair 87bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testToString() { 88bda3441225e0607b5ced8b538123fd7c7a417910chrismair String toString = fileSystem.toString() 89bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("toString=" + toString) 90bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert toString.contains(EXISTING_DIR) 91bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert toString.contains(EXISTING_FILE) 92bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 93bda3441225e0607b5ced8b538123fd7c7a417910chrismair 94bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testFormatDirectoryListing() { 95bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry = new FileEntry(path: 'abc') 96bda3441225e0607b5ced8b538123fd7c7a417910chrismair def formatter = [format: {f -> 97bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert f == fileEntry 98bda3441225e0607b5ced8b538123fd7c7a417910chrismair return 'abc' 99bda3441225e0607b5ced8b538123fd7c7a417910chrismair }] as DirectoryListingFormatter 100bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.directoryListingFormatter = formatter 101bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.formatDirectoryListing(fileEntry) == 'abc' 102bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 103bda3441225e0607b5ced8b538123fd7c7a417910chrismair 104bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testFormatDirectoryListing_NullDirectoryListingFormatter() { 105bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.directoryListingFormatter = null 106bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry = new FileEntry('abc') 107bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining('directoryListingFormatter') { assert fileSystem.formatDirectoryListing(fileEntry) } 108bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 109bda3441225e0607b5ced8b538123fd7c7a417910chrismair 110bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testFormatDirectoryListing_NullFileSystemEntry() { 111bda3441225e0607b5ced8b538123fd7c7a417910chrismair def formatter = [format: {f -> }] as DirectoryListingFormatter 112bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.directoryListingFormatter = formatter 113bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining('fileSystemEntry') { assert fileSystem.formatDirectoryListing(null) } 114bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 115bda3441225e0607b5ced8b538123fd7c7a417910chrismair 116bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testGetEntry() { 117bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.getEntry(NO_SUCH_DIR) == null 118bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.getEntry(NO_SUCH_FILE) == null 119bda3441225e0607b5ced8b538123fd7c7a417910chrismair 120bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.getEntry(EXISTING_FILE).path == EXISTING_FILE 121bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.getEntry(EXISTING_DIR).path == EXISTING_DIR 122bda3441225e0607b5ced8b538123fd7c7a417910chrismair 123bda3441225e0607b5ced8b538123fd7c7a417910chrismair def permissions = new Permissions('-wxrwx---') 124bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry = new FileEntry(path: NEW_FILE, lastModified: DATE, contents: 'abc', owner: 'owner', 125bda3441225e0607b5ced8b538123fd7c7a417910chrismair group: 'group', permissions: permissions) 126bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(fileEntry) 127bda3441225e0607b5ced8b538123fd7c7a417910chrismair def entry = fileSystem.getEntry(NEW_FILE) 128bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info(entry) 129bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert entry.path == NEW_FILE 130bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !entry.directory 131bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert entry.size == 3 132bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert entry.owner == 'owner' 133bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert entry.group == 'group' 134bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert entry.permissions == permissions 135bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 136bda3441225e0607b5ced8b538123fd7c7a417910chrismair 137bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testNormalize_Null() { 138bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.normalize(null) } 139bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 140bda3441225e0607b5ced8b538123fd7c7a417910chrismair 141bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testGetName_Null() { 142bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.getName(null) } 143bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 144bda3441225e0607b5ced8b538123fd7c7a417910chrismair 145bda3441225e0607b5ced8b538123fd7c7a417910chrismair //-------------------------------------------------------------------------- 146bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Abstract Methods 147bda3441225e0607b5ced8b538123fd7c7a417910chrismair //-------------------------------------------------------------------------- 148bda3441225e0607b5ced8b538123fd7c7a417910chrismair 149bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected abstract Class getExpectedDirectoryListingFormatterClass() 150bda3441225e0607b5ced8b538123fd7c7a417910chrismair 151bda3441225e0607b5ced8b538123fd7c7a417910chrismair //-------------------------------------------------------------------------- 152bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Internal Helper Methods 153bda3441225e0607b5ced8b538123fd7c7a417910chrismair //-------------------------------------------------------------------------- 154bda3441225e0607b5ced8b538123fd7c7a417910chrismair 155bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 156bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify the contents of the file at the specified path read from its InputSteam 157bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 158bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param fileSystem - the FileSystem instance 159bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param expectedContents - the expected contents 160bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws IOException 161bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @see org.mockftpserver.fake.filesystem.AbstractFileSystemTestCase#verifyFileContents(FileSystem, 162bda3441225e0607b5ced8b538123fd7c7a417910chrismair * String , String ) 163bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 164bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void verifyFileContents(FileSystem fileSystem, String path, String expectedContents) throws IOException { 165bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry = fileSystem.getEntry(path) 166bda3441225e0607b5ced8b538123fd7c7a417910chrismair InputStream input = fileEntry.createInputStream() 167bda3441225e0607b5ced8b538123fd7c7a417910chrismair byte[] bytes = IoUtil.readBytes(input) 168bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("bytes=[" + new String(bytes) + "]") 169bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes) 170bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 171bda3441225e0607b5ced8b538123fd7c7a417910chrismair 172bda3441225e0607b5ced8b538123fd7c7a417910chrismair}