1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.filesystem
17
18import org.mockftpserver.fake.filesystem.AbstractFakeFileSystemTest
19
20/**
21 * Tests for WindowsFakeFileSystem.
22 *
23 * @version $Revision$ - $Date$
24 *
25 * @author Chris Mair
26 */
27class WindowsFakeFileSystemTest extends AbstractFakeFileSystemTest {
28
29    private static final String SEP = "\\"
30
31    WindowsFakeFileSystemTest() {
32        // These need to be set in the constructor because these values are used in setUp()
33        NEW_DIR = "d:/" + NEW_DIRNAME
34        NEW_FILE = "d:/NewFile.txt"
35        EXISTING_DIR = "d:/"
36        EXISTING_FILE = "d:/ExistingFile.txt"
37        NO_SUCH_DIR = 'x:/xx/yy'
38        NO_SUCH_FILE = "x:/xx/yy/zz.txt"
39    }
40
41    // -------------------------------------------------------------------------
42    // Tests
43    // -------------------------------------------------------------------------
44
45    void testOtherRoots() {
46        final String X = "x:/"
47        final String Y = "y:\\"
48        assertFalse(X, fileSystem.exists(X))
49        assertFalse(Y, fileSystem.exists(Y))
50
51        fileSystem.add(new DirectoryEntry(X))
52        fileSystem.add(new DirectoryEntry(Y))
53
54        assertTrue(X, fileSystem.exists(X))
55        assertTrue(Y, fileSystem.exists(Y))
56    }
57
58    void testPath() {
59        assert fileSystem.path(null, null) == ""
60        assert fileSystem.path(null, "abc") == "abc"
61        assert fileSystem.path("abc", null) == "abc"
62        assert fileSystem.path("", "") == ""
63        assert fileSystem.path("", "abc") == "abc"
64        assert fileSystem.path("abc", "") == "abc"
65        assert fileSystem.path("abc", "def") == "abc" + SEP + "def"
66        assert fileSystem.path("abc\\", "def") == "abc\\def"
67        assert fileSystem.path("c:/abc/", "def") == "c:/abc/def"
68        assert fileSystem.path("d:\\abc", "\\def") == "d:\\abc\\def"
69        assert fileSystem.path("abc", "/def") == "abc/def"
70    }
71
72    void testNormalize() {
73        assert fileSystem.normalize("a:\\") == "a:\\"
74        assert fileSystem.normalize("a:/") == "a:\\"
75        assert fileSystem.normalize("b:/abc") == path("b:", "abc")
76        assert fileSystem.normalize("c:\\abc\\def") == path("c:", "abc", "def")
77        assert fileSystem.normalize("d:/abc/def") == path("d:", "abc", "def")
78        assert fileSystem.normalize("e:\\abc/def/..") == path("e:", "abc")
79        assert fileSystem.normalize("f:/abc/def/../ghi") == path("f:", "abc", "ghi")
80        assert fileSystem.normalize("g:\\abc\\def\\.") == path("g:", "abc", "def")
81        assert fileSystem.normalize("h:/abc\\def\\./ghi") == path("h:", "abc", "def", "ghi")
82        assert fileSystem.normalize("c:\\abc").toLowerCase() == path("c:", "abc")
83        assert fileSystem.normalize("c:/abc").toLowerCase() == path("c:", "abc")
84        assert fileSystem.normalize("z:/abc").toLowerCase() == path("z:", "abc")
85    }
86
87    void testGetName() {
88        assert fileSystem.getName("l:\\") == ""
89        assert fileSystem.getName("m:\\abc") == "abc"
90        assert fileSystem.getName("n:/abc\\def") == "def"
91        assert fileSystem.getName("o:/abc/def") == "def"
92    }
93
94    public void testGetParent() {
95        assert fileSystem.getParent("p:/") == null
96        assert fileSystem.getParent("q:\\abc") == "q:\\"
97        assert fileSystem.getParent("r:/abc\\def") == path("r:", "abc")
98        assert fileSystem.getParent("s:\\abc/def") == path("s:", "abc")
99    }
100
101    void testIsValidName() {
102        // \/:*?"<>|
103        ["a:\\abc",
104                "c:/abc",
105                "d:/abc\\def",
106                "e:/abc\\d!ef",
107                "f:\\abc\\def\\h(ij)",
108                "g:\\abc",
109                "z:/abc/def",
110                "\\\\shared"
111        ].each {
112            assert fileSystem.isValidName(it), "[$it]"
113        }
114
115        ["",
116                "abc",
117                "abc/def",
118                "a:/abc:",
119                "B:\\a*bc",
120                "C:/?abc",
121                "D:\\abc/<def",
122                "E:/abc/def>",
123                "aa:\\abc",
124                "X:X:/abc",
125                ":\\abc\\def",
126                "X:\\\\abc"
127        ].each {
128            assert !fileSystem.isValidName(it), "[$it]"
129        }
130    }
131
132    void testIsAbsolute() {
133        assert fileSystem.isAbsolute("c:\\")
134        assert fileSystem.isAbsolute("x:\\Documents")
135        assert fileSystem.isAbsolute("a:/")
136        assert fileSystem.isAbsolute("\\\\shared\\docs")
137
138        assert !fileSystem.isAbsolute("abc")
139        assert !fileSystem.isAbsolute("/usr")
140        assert !fileSystem.isAbsolute("c:usr")
141
142        shouldFailWithMessageContaining("path") { fileSystem.isAbsolute(null) }
143    }
144
145    void testCaseInsensitive() {
146        def fileEntry = fileSystem.getEntry(EXISTING_FILE)
147        assert fileEntry
148        assert fileEntry == fileSystem.getEntry(EXISTING_FILE.toLowerCase())
149    }
150
151    //-------------------------------------------------------------------------
152    // Test setup
153    //-------------------------------------------------------------------------
154
155    void setUp() {
156        super.setUp()
157    }
158
159    protected Class getExpectedDirectoryListingFormatterClass() {
160        return WindowsDirectoryListingFormatter
161    }
162
163    //-----------------------------------------------------------------------------------
164    // Helper Methods
165    //-----------------------------------------------------------------------------------
166
167    /**
168     * Return a new instance of the FileSystem implementation class under test
169     *
170     * @return a new FileSystem instance
171     */
172    protected FileSystem createFileSystem() {
173        WindowsFakeFileSystem fs = new WindowsFakeFileSystem()
174        fs.add(new DirectoryEntry(EXISTING_DIR))
175        fs.add(new FileEntry(EXISTING_FILE, EXISTING_FILE_CONTENTS))
176        fs.createParentDirectoriesAutomatically = false
177        return fs
178    }
179
180    /**
181     * Return the specified paths concatenated with the system-dependent separator in between
182     * @param p1 - the first path
183     * @param p2 - the second path
184     * @return p1 + SEPARATOR + p2
185     */
186    private String path(String[] paths) {
187        return paths.join(SEP)
188    }
189}
190