summaryrefslogtreecommitdiff
path: root/tags/2.5/src/test/groovy/org/mockftpserver/fake/filesystem/WindowsFakeFileSystemTest.groovy
blob: 0c415d1a397d49973d1a3dba767cf3ac893b0242 (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
/*
 * 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

/**
 * Tests for WindowsFakeFileSystem.
 *
 * @version $Revision$ - $Date$
 *
 * @author Chris Mair
 */
class WindowsFakeFileSystemTest extends AbstractFakeFileSystemTestCase {

    private static final String SEP = "\\"

    WindowsFakeFileSystemTest() {
        // These need to be set in the constructor because these values are used in setUp()
        NEW_DIR = "d:/" + NEW_DIRNAME
        NEW_FILE = "d:/NewFile.txt"
        EXISTING_DIR = "d:/"
        EXISTING_FILE = "d:/ExistingFile.txt"
        NO_SUCH_DIR = 'x:/xx/yy'
        NO_SUCH_FILE = "x:/xx/yy/zz.txt"
    }

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

    void testOtherRoots() {
        final String X = "x:/"
        final String Y = "y:\\"
        assertFalse(X, fileSystem.exists(X))
        assertFalse(Y, fileSystem.exists(Y))

        fileSystem.add(new DirectoryEntry(X))
        fileSystem.add(new DirectoryEntry(Y))

        assertTrue(X, fileSystem.exists(X))
        assertTrue(Y, fileSystem.exists(Y))
    }

    void testPath() {
        assert fileSystem.path(null, null) == ""
        assert fileSystem.path(null, "abc") == "abc"
        assert fileSystem.path("abc", null) == "abc"
        assert fileSystem.path("", "") == ""
        assert fileSystem.path("", "abc") == "abc"
        assert fileSystem.path("abc", "") == "abc"
        assert fileSystem.path("abc", "def") == "abc" + SEP + "def"
        assert fileSystem.path("abc\\", "def") == "abc\\def"
        assert fileSystem.path("c:/abc/", "def") == "c:\\abc\\def"
        assert fileSystem.path("d:\\abc", "\\def") == "d:\\abc\\def"
        assert fileSystem.path("abc", "/def") == "abc\\def"
        assert fileSystem.path("abc/def", "..") == "abc"
        assert fileSystem.path("abc", "def/..") == "abc"
        assert fileSystem.path("abc", "./def") == "abc\\def"
        assert fileSystem.path("abc/.", null) == "abc"
    }

    void testNormalize() {
        assert fileSystem.normalize("a:\\") == "a:\\"
        assert fileSystem.normalize("a:/") == "a:\\"
        assert fileSystem.normalize("b:/abc") == path("b:", "abc")
        assert fileSystem.normalize("c:\\abc\\def") == path("c:", "abc", "def")
        assert fileSystem.normalize("d:/abc/def") == path("d:", "abc", "def")
        assert fileSystem.normalize("e:\\abc/def/..") == path("e:", "abc")
        assert fileSystem.normalize("f:/abc/def/../ghi") == path("f:", "abc", "ghi")
        assert fileSystem.normalize("g:\\abc\\def\\.") == path("g:", "abc", "def")
        assert fileSystem.normalize("h:/abc\\def\\./ghi") == path("h:", "abc", "def", "ghi")
        assert fileSystem.normalize("c:\\abc").toLowerCase() == path("c:", "abc")
        assert fileSystem.normalize("c:/abc").toLowerCase() == path("c:", "abc")
        assert fileSystem.normalize("z:/abc").toLowerCase() == path("z:", "abc")
    }

    void testGetName() {
        assert fileSystem.getName("l:\\") == ""
        assert fileSystem.getName("m:\\abc") == "abc"
        assert fileSystem.getName("n:/abc\\def") == "def"
        assert fileSystem.getName("o:/abc/def") == "def"
    }

    public void testGetParent() {
        assert fileSystem.getParent("p:/") == null
        assert fileSystem.getParent("q:\\abc") == "q:\\"
        assert fileSystem.getParent("r:/abc\\def") == path("r:", "abc")
        assert fileSystem.getParent("s:\\abc/def") == path("s:", "abc")
    }

    void testIsValidName() {
        // \/:*?"<>|
        ["a:\\abc",
                "c:/abc",
                "d:/abc\\def",
                "e:/abc\\d!ef",
                "f:\\abc\\def\\h(ij)",
                "g:\\abc",
                "z:/abc/def",
                "\\\\shared"
        ].each {
            assert fileSystem.isValidName(it), "[$it]"
        }

        ["",
                "abc",
                "abc/def",
                "a:/abc:",
                "B:\\a*bc",
                "C:/?abc",
                "D:\\abc/<def",
                "E:/abc/def>",
                "aa:\\abc",
                "X:X:/abc",
                ":\\abc\\def",
                "X:\\\\abc"
        ].each {
            assert !fileSystem.isValidName(it), "[$it]"
        }
    }

    void testIsAbsolute() {
        assert fileSystem.isAbsolute("c:\\")
        assert fileSystem.isAbsolute("x:\\Documents")
        assert fileSystem.isAbsolute("a:/")
        assert fileSystem.isAbsolute("\\\\shared\\docs")

        assert !fileSystem.isAbsolute("abc")
        assert !fileSystem.isAbsolute("/usr")
        assert !fileSystem.isAbsolute("c:usr")

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

    void testCaseInsensitive() {
        def fileEntry = fileSystem.getEntry(EXISTING_FILE)
        assert fileEntry
        assert fileEntry == fileSystem.getEntry(EXISTING_FILE.toLowerCase())
    }

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

    void setUp() {
        super.setUp()
    }

    protected Class getExpectedDirectoryListingFormatterClass() {
        return WindowsDirectoryListingFormatter
    }

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

    /**
     * Return a new instance of the FileSystem implementation class under test
     *
     * @return a new FileSystem instance
     */
    protected FileSystem createFileSystem() {
        WindowsFakeFileSystem fs = new WindowsFakeFileSystem()
        fs.add(new DirectoryEntry(EXISTING_DIR))
        fs.add(new FileEntry(EXISTING_FILE, EXISTING_FILE_CONTENTS))
        fs.createParentDirectoriesAutomatically = false
        return fs
    }

    /**
     * Return the specified paths concatenated with the system-dependent separator in between
     * @param p1 - the first path
     * @param p2 - the second path
     * @return p1 + SEPARATOR + p2
     */
    private String path(String[] paths) {
        return paths.join(SEP)
    }
}