summaryrefslogtreecommitdiff
path: root/tags/2.3/src/test/groovy/org/mockftpserver/fake/filesystem/AbstractFakeFileSystemTestCase.groovy
blob: 52431db42b2fcffeff85d04a83aec89fce6c0fa1 (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
/*
 * Copyright 2008 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.core.util.IoUtil

/**
 * Tests for subclasses of AbstractFakeFileSystem. Subclasses must define
 *
 * @version $Revision$ - $Date$
 *
 * @author Chris Mair
 */
abstract class AbstractFakeFileSystemTestCase extends AbstractFileSystemTestCase {

    // -------------------------------------------------------------------------
    // Tests
    // -------------------------------------------------------------------------

    void testDefaultDirectoryListingFormatterClass() {
        assert fileSystem.directoryListingFormatter.class == expectedDirectoryListingFormatterClass
    }

    void testAdd_PathLocked() {
        def dirEntry = new DirectoryEntry(NEW_DIR)
        fileSystem.add(dirEntry)
        def fileEntry = new FileEntry(NEW_FILE)
        fileSystem.add(fileEntry)

        // The path should be locked for both entries
        shouldFail { dirEntry.setPath('abc') }
        shouldFail { fileEntry.setPath('abc') }
    }

    void testAdd_Directory_CreateParentDirectoriesAutomatically() {
        final NEW_SUBDIR = fileSystem.path(NEW_DIR, "sub")
        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
        assert !fileSystem.exists(NEW_SUBDIR), "Before createDirectory"

        fileSystem.createParentDirectoriesAutomatically = true
        fileSystem.add(new DirectoryEntry(NEW_SUBDIR))
        assert fileSystem.exists(NEW_DIR), "After createDirectory"
        assert fileSystem.exists(NEW_SUBDIR), "$NEW_SUBDIR: After createDirectory"
    }

    void testAdd_File_CreateParentDirectoriesAutomatically() {
        final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt")
        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"
        assert !fileSystem.exists(NEW_FILE_IN_SUBDIR), "Before createDirectory"

        fileSystem.createParentDirectoriesAutomatically = true
        fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR))
        assert fileSystem.exists(NEW_DIR), "After createDirectory"
        assert fileSystem.exists(NEW_FILE_IN_SUBDIR), "$NEW_FILE_IN_SUBDIR: After createDirectory"
    }

    void testAdd_File_CreateParentDirectoriesAutomatically_False() {
        fileSystem.createParentDirectoriesAutomatically = false
        final NEW_FILE_IN_SUBDIR = fileSystem.path(NEW_DIR, "abc.txt")
        assert !fileSystem.exists(NEW_DIR), "Before createDirectory"

        shouldFail(FileSystemException) { fileSystem.add(new FileEntry(NEW_FILE_IN_SUBDIR)) }
        assert !fileSystem.exists(NEW_DIR), "After createDirectory"
    }

    void testSetEntries() {
        fileSystem.createParentDirectoriesAutomatically = false
        def entries = [new FileEntry(NEW_FILE), new DirectoryEntry(NEW_DIR)]
        fileSystem.setEntries(entries)
        assert fileSystem.exists(NEW_DIR)
        assert fileSystem.exists(NEW_FILE)
    }

    void testToString() {
        String toString = fileSystem.toString()
        LOG.info("toString=" + toString)
        assert toString.contains(EXISTING_DIR)
        assert toString.contains(EXISTING_FILE)
    }

    void testFormatDirectoryListing() {
        def fileEntry = new FileEntry(path: 'abc')
        def formatter = [format: {f ->
            assert f == fileEntry
            return 'abc'
        }] as DirectoryListingFormatter
        fileSystem.directoryListingFormatter = formatter
        assert fileSystem.formatDirectoryListing(fileEntry) == 'abc'
    }

    void testFormatDirectoryListing_NullDirectoryListingFormatter() {
        fileSystem.directoryListingFormatter = null
        def fileEntry = new FileEntry('abc')
        shouldFailWithMessageContaining('directoryListingFormatter') { assert fileSystem.formatDirectoryListing(fileEntry) }
    }

    void testFormatDirectoryListing_NullFileSystemEntry() {
        def formatter = [format: {f -> }] as DirectoryListingFormatter
        fileSystem.directoryListingFormatter = formatter
        shouldFailWithMessageContaining('fileSystemEntry') { assert fileSystem.formatDirectoryListing(null) }
    }

    void testGetEntry() {
        assert fileSystem.getEntry(NO_SUCH_DIR) == null
        assert fileSystem.getEntry(NO_SUCH_FILE) == null

        assert fileSystem.getEntry(EXISTING_FILE).path == EXISTING_FILE
        assert fileSystem.getEntry(EXISTING_DIR).path == EXISTING_DIR

        def permissions = new Permissions('-wxrwx---')
        def fileEntry = new FileEntry(path: NEW_FILE, lastModified: DATE, contents: 'abc', owner: 'owner',
                group: 'group', permissions: permissions)
        fileSystem.add(fileEntry)
        def entry = fileSystem.getEntry(NEW_FILE)
        LOG.info(entry)
        assert entry.path == NEW_FILE
        assert !entry.directory
        assert entry.size == 3
        assert entry.owner == 'owner'
        assert entry.group == 'group'
        assert entry.permissions == permissions
    }

    void testNormalize_Null() {
        shouldFailWithMessageContaining("path") { fileSystem.normalize(null) }
    }

    void testGetName_Null() {
        shouldFailWithMessageContaining("path") { fileSystem.getName(null) }
    }

    //--------------------------------------------------------------------------
    // Abstract Methods
    //--------------------------------------------------------------------------

    protected abstract Class getExpectedDirectoryListingFormatterClass()

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

    /**
     * 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
     * @see org.mockftpserver.fake.filesystem.AbstractFileSystemTestCase#verifyFileContents(FileSystem,
     * String , String )
     */
    protected void verifyFileContents(FileSystem fileSystem, String path, String expectedContents) throws IOException {
        def fileEntry = fileSystem.getEntry(path)
        InputStream input = fileEntry.createInputStream()
        byte[] bytes = IoUtil.readBytes(input)
        LOG.info("bytes=[" + new String(bytes) + "]")
        assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes)
    }

}