summaryrefslogtreecommitdiff
path: root/tags/2.3/src/test/groovy/org/mockftpserver/fake/filesystem/AbstractFileSystemTestCase.groovy
blob: d3ceaf9f07f2fc1f67d3c771082d1ea95ff4dead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright 2010 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.fake.filesystem

import org.mockftpserver.test.AbstractGroovyTestCase

/**
 * Abstract superclass for tests of FileSystem implementation classes. Contains common
 * tests and test infrastructure. 
 *
 * @version $Revision$ - $Date$
 *
 * @author Chris Mair
 */
abstract class AbstractFileSystemTestCase extends AbstractGroovyTestCase {

    public static final FILENAME1 = "File1.txt"
    public static final FILENAME2 = "file2.txt"
    public static final DIR1 = "dir1"
    public static final NEW_DIRNAME = "testDir"
    public static final ILLEGAL_FILE = "xx/yy////z!<>?*z.txt"
    public static final EXISTING_FILE_CONTENTS = "abc 123 %^& xxx"
    public static final DATE = new Date()

    // These must be set by the concrete subclass (in its constructor)
    protected String NEW_DIR = null
    protected String NEW_FILE = null
    protected String EXISTING_DIR = null
    protected String EXISTING_FILE = null
    protected NO_SUCH_DIR = null
    protected NO_SUCH_FILE = null

    protected FileSystem fileSystem

    //-------------------------------------------------------------------------
    // Common Tests
    //-------------------------------------------------------------------------

    void testExists() {
        assert !fileSystem.exists(NEW_FILE)
        assert !fileSystem.exists(NEW_DIR)
        assert !fileSystem.exists(ILLEGAL_FILE)
        assert fileSystem.exists(EXISTING_FILE)
        assert fileSystem.exists(EXISTING_DIR)

        shouldFailWithMessageContaining("path") { fileSystem.exists(null) }
    }

    void testIsDirectory() {
        assert fileSystem.isDirectory(EXISTING_DIR)
        assert !fileSystem.isDirectory(EXISTING_FILE)
        assert !fileSystem.isDirectory(NO_SUCH_DIR)
        assert !fileSystem.isDirectory(NO_SUCH_FILE)
        assert !fileSystem.isDirectory(ILLEGAL_FILE)

        shouldFailWithMessageContaining("path") { fileSystem.isDirectory(null) }
    }

    void testIsFile() {
        assert fileSystem.isFile(EXISTING_FILE)
        assert !fileSystem.isFile(EXISTING_DIR)
        assert !fileSystem.isFile(NO_SUCH_DIR)
        assert !fileSystem.isFile(NO_SUCH_FILE)
        assert !fileSystem.isFile(ILLEGAL_FILE)

        shouldFailWithMessageContaining("path") { fileSystem.isFile(null) }
    }

    void testAdd_Directory() {
        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
        fileSystem.add(new DirectoryEntry(NEW_DIR))
        assert fileSystem.exists(NEW_DIR), "After createDirectory"

        // Duplicate directory
        shouldThrowFileSystemExceptionWithMessageKey('filesystem.pathAlreadyExists') {
            fileSystem.add(new DirectoryEntry(NEW_DIR))
        }

        // The parent of the path does not exist
        shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') {
            fileSystem.add(new DirectoryEntry(NEW_DIR + "/abc/def"))
        }

        shouldFail(InvalidFilenameException) { fileSystem.add(new DirectoryEntry(ILLEGAL_FILE)) }
        shouldFailWithMessageContaining("path") { fileSystem.add(new DirectoryEntry(null)) }
    }

    void testAdd_File() {
        assert !fileSystem.exists(NEW_FILE), "Before createFile"
        fileSystem.add(new FileEntry(NEW_FILE))
        assert fileSystem.exists(NEW_FILE), "After createFile"

        // File already exists
        shouldThrowFileSystemExceptionWithMessageKey('filesystem.pathAlreadyExists') {
            fileSystem.add(new FileEntry(NEW_FILE))
        }

        // The parent of the path does not exist
        shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') {
            fileSystem.add(new FileEntry(NEW_DIR + "/abc/def"))
        }

        shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') {
            fileSystem.add(new FileEntry(NO_SUCH_DIR))
        }

        shouldFail(InvalidFilenameException) { fileSystem.add(new FileEntry(ILLEGAL_FILE)) }

        shouldFailWithMessageContaining("path") { fileSystem.add(new FileEntry(null)) }
    }

    void testRename_NullFromPath() {
        shouldFailWithMessageContaining("fromPath") { fileSystem.rename(null, FILENAME1) }
    }

    void testRename_NullToPath() {
        shouldFailWithMessageContaining("toPath") { fileSystem.rename(FILENAME1, null) }
    }

    void testListNames() {
        fileSystem.add(new DirectoryEntry(NEW_DIR))
        assert fileSystem.listNames(NEW_DIR) == []

        fileSystem.add(new FileEntry(p(NEW_DIR, FILENAME1)))
        fileSystem.add(new FileEntry(p(NEW_DIR, FILENAME2)))
        fileSystem.add(new DirectoryEntry(p(NEW_DIR, DIR1)))
        fileSystem.add(new FileEntry(p(NEW_DIR, DIR1, "/abc.def")))

        List filenames = fileSystem.listNames(NEW_DIR)
        LOG.info("filenames=" + filenames)
        assertSameIgnoringOrder(filenames, [FILENAME1, FILENAME2, DIR1])

        // Specify a filename instead of a directory name
        assert [FILENAME1] == fileSystem.listNames(p(NEW_DIR, FILENAME1))

        assert [] == fileSystem.listNames(NO_SUCH_DIR)

        shouldFailWithMessageContaining("path") { fileSystem.listNames(null) }
    }

    void testListNames_Wildcards() {
        fileSystem.add(new DirectoryEntry(NEW_DIR))
        fileSystem.add(new FileEntry(p(NEW_DIR, 'abc.txt')))
        fileSystem.add(new FileEntry(p(NEW_DIR, 'def.txt')))

        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '*.txt')), ['abc.txt', 'def.txt'])
        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '*')), ['abc.txt', 'def.txt'])
        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '???.???')), ['abc.txt', 'def.txt'])
        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, '*.exe')), [])
        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, 'abc.???')), ['abc.txt'])
        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, 'a?c.?xt')), ['abc.txt'])
        assertSameIgnoringOrder(fileSystem.listNames(p(NEW_DIR, 'd?f.*')), ['def.txt'])
    }

    void testListFiles() {
        fileSystem.add(new DirectoryEntry(NEW_DIR))
        assert [] == fileSystem.listFiles(NEW_DIR)

        def path1 = p(NEW_DIR, FILENAME1)
        def fileEntry1 = new FileEntry(path1)
        fileSystem.add(fileEntry1)
        assert fileSystem.listFiles(NEW_DIR) == [fileEntry1]

        // Specify a filename instead of a directory name
        assert fileSystem.listFiles(p(NEW_DIR, FILENAME1)) == [fileEntry1]

        def fileEntry2 = new FileEntry(p(NEW_DIR, FILENAME2))
        fileSystem.add(fileEntry2)
        assert fileSystem.listFiles(NEW_DIR) as Set == [fileEntry1, fileEntry2] as Set

        // Write to the file to get a non-zero length
        final byte[] CONTENTS = "1234567890".getBytes()
        OutputStream out = fileEntry1.createOutputStream(false)
        out.write(CONTENTS)
        out.close()
        assert fileSystem.listFiles(NEW_DIR) as Set == [fileEntry1, fileEntry2] as Set

        def dirEntry3 = new DirectoryEntry(p(NEW_DIR, DIR1))
        fileSystem.add(dirEntry3)
        assert fileSystem.listFiles(NEW_DIR) as Set == [fileEntry1, fileEntry2, dirEntry3] as Set

        assert fileSystem.listFiles(NO_SUCH_DIR) == []

        shouldFailWithMessageContaining("path") { fileSystem.listFiles(null) }
    }

    void testListFiles_Wildcards() {
        def dirEntry = new DirectoryEntry(NEW_DIR)
        def fileEntry1 = new FileEntry(p(NEW_DIR, 'abc.txt'))
        def fileEntry2 = new FileEntry(p(NEW_DIR, 'def.txt'))

        fileSystem.add(dirEntry)
        fileSystem.add(fileEntry1)
        fileSystem.add(fileEntry2)

        assert fileSystem.listFiles(p(NEW_DIR, '*.txt')) as Set == [fileEntry1, fileEntry2] as Set
        assert fileSystem.listFiles(p(NEW_DIR, '*')) as Set == [fileEntry1, fileEntry2] as Set
        assert fileSystem.listFiles(p(NEW_DIR, '???.???')) as Set == [fileEntry1, fileEntry2] as Set
        assert fileSystem.listFiles(p(NEW_DIR, '*.exe')) as Set == [] as Set
        assert fileSystem.listFiles(p(NEW_DIR, 'abc.???')) as Set == [fileEntry1] as Set
        assert fileSystem.listFiles(p(NEW_DIR, 'a?c.?xt')) as Set == [fileEntry1] as Set
        assert fileSystem.listFiles(p(NEW_DIR, 'd?f.*')) as Set == [fileEntry2] as Set
    }

    void testDelete() {
        fileSystem.add(new FileEntry(NEW_FILE))
        assert fileSystem.delete(NEW_FILE)
        assert !fileSystem.exists(NEW_FILE)

        assert !fileSystem.delete(NO_SUCH_FILE)

        fileSystem.add(new DirectoryEntry(NEW_DIR))
        assert fileSystem.delete(NEW_DIR)
        assert !fileSystem.exists(NEW_DIR)

        fileSystem.add(new DirectoryEntry(NEW_DIR))
        fileSystem.add(new FileEntry(NEW_DIR + "/abc.txt"))

        assert !fileSystem.delete(NEW_DIR), "Directory containing files"
        assert fileSystem.exists(NEW_DIR)

        shouldFailWithMessageContaining("path") { fileSystem.delete(null) }
    }

    void testRename() {
        final FROM_FILE = NEW_FILE + "2"
        fileSystem.add(new FileEntry(FROM_FILE))

        fileSystem.rename(FROM_FILE, NEW_FILE)
        assert fileSystem.exists(NEW_FILE)

        fileSystem.add(new DirectoryEntry(NEW_DIR))

        // Rename existing directory
        final String TO_DIR = NEW_DIR + "2"
        fileSystem.rename(NEW_DIR, TO_DIR)
        assert !fileSystem.exists(NEW_DIR)
        assert fileSystem.exists(TO_DIR)
    }

    void testRename_ToPathFileAlreadyExists() {
        final FROM_FILE = EXISTING_FILE
        final String TO_FILE = NEW_FILE
        fileSystem.add(new FileEntry(TO_FILE))
         shouldThrowFileSystemExceptionWithMessageKey('filesystem.alreadyExists') {
             fileSystem.rename(FROM_FILE, TO_FILE) 
         }
    }

    void testRename_FromPathDoesNotExist() {
        final TO_FILE2 = NEW_FILE + "2"
        shouldThrowFileSystemExceptionWithMessageKey('filesystem.doesNotExist') {
            fileSystem.rename(NO_SUCH_FILE, TO_FILE2)
        }
        assert !fileSystem.exists(TO_FILE2), "After failed rename"
    }

    void testRename_ToPathIsChildOfFromPath() {
        final FROM_DIR = NEW_DIR
        final TO_DIR = FROM_DIR + "/child"
        fileSystem.add(new DirectoryEntry(FROM_DIR))
        shouldThrowFileSystemExceptionWithMessageKey('filesystem.renameFailed') {
            fileSystem.rename(FROM_DIR, TO_DIR)
        }
        assert !fileSystem.exists(TO_DIR), "After failed rename"
    }

    void testRename_EmptyDirectory() {
        final FROM_DIR = NEW_DIR
        final TO_DIR = FROM_DIR + "2"
        fileSystem.add(new DirectoryEntry(FROM_DIR))
        fileSystem.rename(FROM_DIR, TO_DIR)
        assert !fileSystem.exists(FROM_DIR)
        assert fileSystem.exists(TO_DIR)
    }

    void testRename_DirectoryContainsFiles() {
        fileSystem.add(new DirectoryEntry(NEW_DIR))
        fileSystem.add(new FileEntry(NEW_DIR + "/a.txt"))
        fileSystem.add(new FileEntry(NEW_DIR + "/b.txt"))
        fileSystem.add(new DirectoryEntry(NEW_DIR + "/subdir"))

        final String TO_DIR = NEW_DIR + "2"
        fileSystem.rename(NEW_DIR, TO_DIR)
        assert !fileSystem.exists(NEW_DIR)
        assert !fileSystem.exists(NEW_DIR + "/a.txt")
        assert !fileSystem.exists(NEW_DIR + "/b.txt")
        assert !fileSystem.exists(NEW_DIR + "/subdir")

        assert fileSystem.exists(TO_DIR)
        assert fileSystem.exists(TO_DIR + "/a.txt")
        assert fileSystem.exists(TO_DIR + "/b.txt")
        assert fileSystem.exists(TO_DIR + "/subdir")
    }

    void testRename_ParentOfToPathDoesNotExist() {
        final String FROM_FILE = NEW_FILE
        final String TO_FILE = fileSystem.path(NO_SUCH_DIR, "abc")
        fileSystem.add(new FileEntry(FROM_FILE))

        shouldThrowFileSystemExceptionWithMessageKey('filesystem.parentDirectoryDoesNotExist') {
            fileSystem.rename(FROM_FILE, TO_FILE)
        }
        assert fileSystem.exists(FROM_FILE)
        assert !fileSystem.exists(TO_FILE)
    }

    void testGetParent_Null() {
        shouldFailWithMessageContaining("path") { fileSystem.getParent(null) }
    }

    //-------------------------------------------------------------------------
    // Test setup
    //-------------------------------------------------------------------------

    void setUp() {
        super.setUp()
        fileSystem = createFileSystem()
    }

    //-------------------------------------------------------------------------
    // Helper Methods
    //-------------------------------------------------------------------------

    protected void shouldThrowFileSystemExceptionWithMessageKey(String messageKey, Closure closure) {
        def e = shouldThrow(FileSystemException, closure)
        assert e.messageKey == messageKey, "Expected message key [$messageKey], but was [${e.messageKey}]"
    }
    
    private verifyEntries(List expected, List actual) {
        expected.eachWithIndex {entry, index ->
            def entryStr = entry.toString()
            LOG.info("expected=$entryStr")
            assert actual.find {actualEntry -> actualEntry.toString() == entryStr }
        }
    }

    protected void assertSameIgnoringOrder(list1, list2) {
        LOG.info("Comparing $list1 to $list2")
        assert list1 as Set == list2 as Set, "list1=$list1  list2=$list2"
    }

    /**
     * Return a new instance of the FileSystem implementation class under test
     * @return a new FileSystem instance
     * @throws Exception
     */
    protected abstract FileSystem createFileSystem()

    /**
     * Verify the contents of the file at the specified path read from its InputSteam
     *
     * @param fileSystem - the FileSystem instance
     * @param expectedContents - the expected contents
     * @throws IOException
     */
    protected abstract void verifyFileContents(FileSystem fileSystem, String path, String contents) throws Exception

}