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