1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/* 2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2010 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.test.AbstractGroovyTestCase 19bda3441225e0607b5ced8b538123fd7c7a417910chrismair 20bda3441225e0607b5ced8b538123fd7c7a417910chrismair/** 21bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Abstract superclass for tests of FileSystem implementation classes. Contains common 22bda3441225e0607b5ced8b538123fd7c7a417910chrismair * tests and test infrastructure. 23bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 24bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$ 25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 26bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair 27bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 28bda3441225e0607b5ced8b538123fd7c7a417910chrismairabstract class AbstractFileSystemTestCase extends AbstractGroovyTestCase { 29bda3441225e0607b5ced8b538123fd7c7a417910chrismair 30bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final FILENAME1 = "File1.txt" 31bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final FILENAME2 = "file2.txt" 32bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final DIR1 = "dir1" 33bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final NEW_DIRNAME = "testDir" 34bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final ILLEGAL_FILE = "xx/yy////z!<>?*z.txt" 35bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final EXISTING_FILE_CONTENTS = "abc 123 %^& xxx" 36bda3441225e0607b5ced8b538123fd7c7a417910chrismair public static final DATE = new Date() 37bda3441225e0607b5ced8b538123fd7c7a417910chrismair 38bda3441225e0607b5ced8b538123fd7c7a417910chrismair // These must be set by the concrete subclass (in its constructor) 39bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected String NEW_DIR = null 40bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected String NEW_FILE = null 41bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected String EXISTING_DIR = null 42bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected String EXISTING_FILE = null 43bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected NO_SUCH_DIR = null 44bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected NO_SUCH_FILE = null 45bda3441225e0607b5ced8b538123fd7c7a417910chrismair 46bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected FileSystem fileSystem 47bda3441225e0607b5ced8b538123fd7c7a417910chrismair 48bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 49bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Common Tests 50bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 51bda3441225e0607b5ced8b538123fd7c7a417910chrismair 52bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testExists() { 53bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_FILE) 54bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR) 55bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(ILLEGAL_FILE) 56bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(EXISTING_FILE) 57bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(EXISTING_DIR) 58bda3441225e0607b5ced8b538123fd7c7a417910chrismair 59bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.exists(null) } 60bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 61bda3441225e0607b5ced8b538123fd7c7a417910chrismair 62bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testIsDirectory() { 63bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.isDirectory(EXISTING_DIR) 64bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isDirectory(EXISTING_FILE) 65bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isDirectory(NO_SUCH_DIR) 66bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isDirectory(NO_SUCH_FILE) 67bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isDirectory(ILLEGAL_FILE) 68bda3441225e0607b5ced8b538123fd7c7a417910chrismair 69bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.isDirectory(null) } 70bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 71bda3441225e0607b5ced8b538123fd7c7a417910chrismair 72bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testIsFile() { 73bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.isFile(EXISTING_FILE) 74bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isFile(EXISTING_DIR) 75bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isFile(NO_SUCH_DIR) 76bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isFile(NO_SUCH_FILE) 77bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.isFile(ILLEGAL_FILE) 78bda3441225e0607b5ced8b538123fd7c7a417910chrismair 79bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.isFile(null) } 80bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 81bda3441225e0607b5ced8b538123fd7c7a417910chrismair 82bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testAdd_Directory() { 83bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR), "Before createDirectory" 84bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 85bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_DIR), "After createDirectory" 86bda3441225e0607b5ced8b538123fd7c7a417910chrismair 87bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Duplicate directory 88bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.pathAlreadyExists') { 89bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 90bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 91bda3441225e0607b5ced8b538123fd7c7a417910chrismair 92bda3441225e0607b5ced8b538123fd7c7a417910chrismair // The parent of the path does not exist 93bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') { 94bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR + "/abc/def")) 95bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 96bda3441225e0607b5ced8b538123fd7c7a417910chrismair 97bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFail(InvalidFilenameException) { fileSystem.add(new DirectoryEntry(ILLEGAL_FILE)) } 98bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.add(new DirectoryEntry(null)) } 99bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 100bda3441225e0607b5ced8b538123fd7c7a417910chrismair 101bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testAdd_File() { 102bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_FILE), "Before createFile" 103bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_FILE)) 104bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_FILE), "After createFile" 105bda3441225e0607b5ced8b538123fd7c7a417910chrismair 106bda3441225e0607b5ced8b538123fd7c7a417910chrismair // File already exists 107bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.pathAlreadyExists') { 108bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_FILE)) 109bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 110bda3441225e0607b5ced8b538123fd7c7a417910chrismair 111bda3441225e0607b5ced8b538123fd7c7a417910chrismair // The parent of the path does not exist 112bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') { 113bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_DIR + "/abc/def")) 114bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 115bda3441225e0607b5ced8b538123fd7c7a417910chrismair 116bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') { 117bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NO_SUCH_DIR)) 118bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 119bda3441225e0607b5ced8b538123fd7c7a417910chrismair 120bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFail(InvalidFilenameException) { fileSystem.add(new FileEntry(ILLEGAL_FILE)) } 121bda3441225e0607b5ced8b538123fd7c7a417910chrismair 122bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.add(new FileEntry(null)) } 123bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 124bda3441225e0607b5ced8b538123fd7c7a417910chrismair 125bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_NullFromPath() { 126bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("fromPath") { fileSystem.rename(null, FILENAME1) } 127bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 128bda3441225e0607b5ced8b538123fd7c7a417910chrismair 129bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_NullToPath() { 130bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("toPath") { fileSystem.rename(FILENAME1, null) } 131bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 132bda3441225e0607b5ced8b538123fd7c7a417910chrismair 133bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testListNames() { 134bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 135bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listNames(NEW_DIR) == [] 136bda3441225e0607b5ced8b538123fd7c7a417910chrismair 137bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(p(NEW_DIR, FILENAME1))) 138bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(p(NEW_DIR, FILENAME2))) 139bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(p(NEW_DIR, DIR1))) 140bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(p(NEW_DIR, DIR1, "/abc.def"))) 141bda3441225e0607b5ced8b538123fd7c7a417910chrismair 142bda3441225e0607b5ced8b538123fd7c7a417910chrismair List filenames = fileSystem.listNames(NEW_DIR) 143bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("filenames=" + filenames) 144bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(filenames, [FILENAME1, FILENAME2, DIR1]) 145bda3441225e0607b5ced8b538123fd7c7a417910chrismair 146bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Specify a filename instead of a directory name 147bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert [FILENAME1] == fileSystem.listNames(p(NEW_DIR, FILENAME1)) 148bda3441225e0607b5ced8b538123fd7c7a417910chrismair 149bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert [] == fileSystem.listNames(NO_SUCH_DIR) 150bda3441225e0607b5ced8b538123fd7c7a417910chrismair 151bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.listNames(null) } 152bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 153bda3441225e0607b5ced8b538123fd7c7a417910chrismair 154bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testListNames_Wildcards() { 155bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 156bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(p(NEW_DIR, 'abc.txt'))) 157bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(p(NEW_DIR, 'def.txt'))) 158bda3441225e0607b5ced8b538123fd7c7a417910chrismair 159bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '*.txt')), ['abc.txt', 'def.txt']) 160bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '*')), ['abc.txt', 'def.txt']) 161bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '???.???')), ['abc.txt', 'def.txt']) 162bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '*.exe')), []) 163bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, 'abc.???')), ['abc.txt']) 164bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, 'a?c.?xt')), ['abc.txt']) 165bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, 'd?f.*')), ['def.txt']) 166bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 167bda3441225e0607b5ced8b538123fd7c7a417910chrismair 168bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testListFiles() { 169bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 170bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert [] == fileSystem.listFiles(NEW_DIR) 171bda3441225e0607b5ced8b538123fd7c7a417910chrismair 172bda3441225e0607b5ced8b538123fd7c7a417910chrismair def path1 = p(NEW_DIR, FILENAME1) 173bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry1 = new FileEntry(path1) 174bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(fileEntry1) 175bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(NEW_DIR) == [fileEntry1] 176bda3441225e0607b5ced8b538123fd7c7a417910chrismair 177bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Specify a filename instead of a directory name 178bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, FILENAME1)) == [fileEntry1] 179bda3441225e0607b5ced8b538123fd7c7a417910chrismair 180bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry2 = new FileEntry(p(NEW_DIR, FILENAME2)) 181bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(fileEntry2) 182bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(NEW_DIR) as Set == [fileEntry1, fileEntry2] as Set 183bda3441225e0607b5ced8b538123fd7c7a417910chrismair 184bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Write to the file to get a non-zero length 185bda3441225e0607b5ced8b538123fd7c7a417910chrismair final byte[] CONTENTS = "1234567890".getBytes() 186bda3441225e0607b5ced8b538123fd7c7a417910chrismair OutputStream out = fileEntry1.createOutputStream(false) 187bda3441225e0607b5ced8b538123fd7c7a417910chrismair out.write(CONTENTS) 188bda3441225e0607b5ced8b538123fd7c7a417910chrismair out.close() 189bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(NEW_DIR) as Set == [fileEntry1, fileEntry2] as Set 190bda3441225e0607b5ced8b538123fd7c7a417910chrismair 191bda3441225e0607b5ced8b538123fd7c7a417910chrismair def dirEntry3 = new DirectoryEntry(p(NEW_DIR, DIR1)) 192bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(dirEntry3) 193bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(NEW_DIR) as Set == [fileEntry1, fileEntry2, dirEntry3] as Set 194bda3441225e0607b5ced8b538123fd7c7a417910chrismair 195bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(NO_SUCH_DIR) == [] 196bda3441225e0607b5ced8b538123fd7c7a417910chrismair 197bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.listFiles(null) } 198bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 199bda3441225e0607b5ced8b538123fd7c7a417910chrismair 200bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testListFiles_Wildcards() { 201bda3441225e0607b5ced8b538123fd7c7a417910chrismair def dirEntry = new DirectoryEntry(NEW_DIR) 202bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry1 = new FileEntry(p(NEW_DIR, 'abc.txt')) 203bda3441225e0607b5ced8b538123fd7c7a417910chrismair def fileEntry2 = new FileEntry(p(NEW_DIR, 'def.txt')) 204bda3441225e0607b5ced8b538123fd7c7a417910chrismair 205bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(dirEntry) 206bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(fileEntry1) 207bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(fileEntry2) 208bda3441225e0607b5ced8b538123fd7c7a417910chrismair 209bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, '*.txt')) as Set == [fileEntry1, fileEntry2] as Set 210bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, '*')) as Set == [fileEntry1, fileEntry2] as Set 211bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, '???.???')) as Set == [fileEntry1, fileEntry2] as Set 212bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, '*.exe')) as Set == [] as Set 213bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, 'abc.???')) as Set == [fileEntry1] as Set 214bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, 'a?c.?xt')) as Set == [fileEntry1] as Set 215bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.listFiles(p(NEW_DIR, 'd?f.*')) as Set == [fileEntry2] as Set 216bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 217bda3441225e0607b5ced8b538123fd7c7a417910chrismair 218bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testDelete() { 219bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_FILE)) 220bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.delete(NEW_FILE) 221bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_FILE) 222bda3441225e0607b5ced8b538123fd7c7a417910chrismair 223bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.delete(NO_SUCH_FILE) 224bda3441225e0607b5ced8b538123fd7c7a417910chrismair 225bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 226bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.delete(NEW_DIR) 227bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR) 228bda3441225e0607b5ced8b538123fd7c7a417910chrismair 229bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 230bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_DIR + "/abc.txt")) 231bda3441225e0607b5ced8b538123fd7c7a417910chrismair 232bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.delete(NEW_DIR), "Directory containing files" 233bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_DIR) 234bda3441225e0607b5ced8b538123fd7c7a417910chrismair 235bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.delete(null) } 236bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 237bda3441225e0607b5ced8b538123fd7c7a417910chrismair 238bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename() { 239bda3441225e0607b5ced8b538123fd7c7a417910chrismair final FROM_FILE = NEW_FILE + "2" 240bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(FROM_FILE)) 241bda3441225e0607b5ced8b538123fd7c7a417910chrismair 242bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(FROM_FILE, NEW_FILE) 243bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(NEW_FILE) 244bda3441225e0607b5ced8b538123fd7c7a417910chrismair 245bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 246bda3441225e0607b5ced8b538123fd7c7a417910chrismair 247bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Rename existing directory 248bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String TO_DIR = NEW_DIR + "2" 249bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(NEW_DIR, TO_DIR) 250bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR) 251bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(TO_DIR) 252bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 253bda3441225e0607b5ced8b538123fd7c7a417910chrismair 254bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_ToPathFileAlreadyExists() { 255bda3441225e0607b5ced8b538123fd7c7a417910chrismair final FROM_FILE = EXISTING_FILE 256bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String TO_FILE = NEW_FILE 257bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(TO_FILE)) 258bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.alreadyExists') { 259bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(FROM_FILE, TO_FILE) 260bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 261bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 262bda3441225e0607b5ced8b538123fd7c7a417910chrismair 263bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_FromPathDoesNotExist() { 264bda3441225e0607b5ced8b538123fd7c7a417910chrismair final TO_FILE2 = NEW_FILE + "2" 265bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.doesNotExist') { 266bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(NO_SUCH_FILE, TO_FILE2) 267bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 268bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(TO_FILE2), "After failed rename" 269bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 270bda3441225e0607b5ced8b538123fd7c7a417910chrismair 271bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_ToPathIsChildOfFromPath() { 272bda3441225e0607b5ced8b538123fd7c7a417910chrismair final FROM_DIR = NEW_DIR 273bda3441225e0607b5ced8b538123fd7c7a417910chrismair final TO_DIR = FROM_DIR + "/child" 274bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(FROM_DIR)) 275bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.renameFailed') { 276bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(FROM_DIR, TO_DIR) 277bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 278bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(TO_DIR), "After failed rename" 279bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 280bda3441225e0607b5ced8b538123fd7c7a417910chrismair 281bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_EmptyDirectory() { 282bda3441225e0607b5ced8b538123fd7c7a417910chrismair final FROM_DIR = NEW_DIR 283bda3441225e0607b5ced8b538123fd7c7a417910chrismair final TO_DIR = FROM_DIR + "2" 284bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(FROM_DIR)) 285bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(FROM_DIR, TO_DIR) 286bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(FROM_DIR) 287bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(TO_DIR) 288bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 289bda3441225e0607b5ced8b538123fd7c7a417910chrismair 290bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_DirectoryContainsFiles() { 291bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR)) 292bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_DIR + "/a.txt")) 293bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(NEW_DIR + "/b.txt")) 294bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new DirectoryEntry(NEW_DIR + "/subdir")) 295bda3441225e0607b5ced8b538123fd7c7a417910chrismair 296bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String TO_DIR = NEW_DIR + "2" 297bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(NEW_DIR, TO_DIR) 298bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR) 299bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR + "/a.txt") 300bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR + "/b.txt") 301bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(NEW_DIR + "/subdir") 302bda3441225e0607b5ced8b538123fd7c7a417910chrismair 303bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(TO_DIR) 304bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(TO_DIR + "/a.txt") 305bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(TO_DIR + "/b.txt") 306bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(TO_DIR + "/subdir") 307bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 308bda3441225e0607b5ced8b538123fd7c7a417910chrismair 309bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testRename_ParentOfToPathDoesNotExist() { 310bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String FROM_FILE = NEW_FILE 311bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String TO_FILE = fileSystem.path(NO_SUCH_DIR, "abc") 312bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.add(new FileEntry(FROM_FILE)) 313bda3441225e0607b5ced8b538123fd7c7a417910chrismair 314bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') { 315bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem.rename(FROM_FILE, TO_FILE) 316bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 317bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert fileSystem.exists(FROM_FILE) 318bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert !fileSystem.exists(TO_FILE) 319bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 320bda3441225e0607b5ced8b538123fd7c7a417910chrismair 321bda3441225e0607b5ced8b538123fd7c7a417910chrismair void testGetParent_Null() { 322bda3441225e0607b5ced8b538123fd7c7a417910chrismair shouldFailWithMessageContaining("path") { fileSystem.getParent(null) } 323bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 324bda3441225e0607b5ced8b538123fd7c7a417910chrismair 325bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 326bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Test setup 327bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 328bda3441225e0607b5ced8b538123fd7c7a417910chrismair 329bda3441225e0607b5ced8b538123fd7c7a417910chrismair void setUp() { 330bda3441225e0607b5ced8b538123fd7c7a417910chrismair super.setUp() 331bda3441225e0607b5ced8b538123fd7c7a417910chrismair fileSystem = createFileSystem() 332bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 333bda3441225e0607b5ced8b538123fd7c7a417910chrismair 334bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 335bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Helper Methods 336bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 337bda3441225e0607b5ced8b538123fd7c7a417910chrismair 338bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void shouldThrowFileSystemExceptionWithMessageKey(String messageKey, Closure closure) { 339bda3441225e0607b5ced8b538123fd7c7a417910chrismair def e = shouldThrow(FileSystemException, closure) 340bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert e.messageKey == messageKey, "Expected message key [$messageKey], but was [${e.messageKey}]" 341bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 342bda3441225e0607b5ced8b538123fd7c7a417910chrismair 343bda3441225e0607b5ced8b538123fd7c7a417910chrismair private verifyEntries(List expected, List actual) { 344bda3441225e0607b5ced8b538123fd7c7a417910chrismair expected.eachWithIndex {entry, index -> 345bda3441225e0607b5ced8b538123fd7c7a417910chrismair def entryStr = entry.toString() 346bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("expected=$entryStr") 347bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert actual.find {actualEntry -> actualEntry.toString() == entryStr } 348bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 349bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 350bda3441225e0607b5ced8b538123fd7c7a417910chrismair 351bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void assertSameIgnoringOrder(list1, list2) { 352bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Comparing $list1 to $list2") 353bda3441225e0607b5ced8b538123fd7c7a417910chrismair assert list1 as Set == list2 as Set, "list1=$list1 list2=$list2" 354bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 355bda3441225e0607b5ced8b538123fd7c7a417910chrismair 356bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 357bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Return a new instance of the FileSystem implementation class under test 358bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @return a new FileSystem instance 359bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws Exception 360bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 361bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected abstract FileSystem createFileSystem() 362bda3441225e0607b5ced8b538123fd7c7a417910chrismair 363bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 364bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify the contents of the file at the specified path read from its InputSteam 365bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 366bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param fileSystem - the FileSystem instance 367bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param expectedContents - the expected contents 368bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @throws IOException 369bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 370bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected abstract void verifyFileContents(FileSystem fileSystem, String path, String contents) throws Exception 371bda3441225e0607b5ced8b538123fd7c7a417910chrismair 372bda3441225e0607b5ced8b538123fd7c7a417910chrismair}