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        assert fileSystem.path("abc/def", "..") == "abc"
71        assert fileSystem.path("abc", "def/..") == "abc"
72        assert fileSystem.path("abc", "./def") == "abc\\def"
73        assert fileSystem.path("abc/.", null) == "abc"
74    }
75
76    void testNormalize() {
77        assert fileSystem.normalize("a:\\") == "a:\\"
78        assert fileSystem.normalize("a:/") == "a:\\"
79        assert fileSystem.normalize("b:/abc") == path("b:", "abc")
80        assert fileSystem.normalize("c:\\abc\\def") == path("c:", "abc", "def")
81        assert fileSystem.normalize("d:/abc/def") == path("d:", "abc", "def")
82        assert fileSystem.normalize("e:\\abc/def/..") == path("e:", "abc")
83        assert fileSystem.normalize("f:/abc/def/../ghi") == path("f:", "abc", "ghi")
84        assert fileSystem.normalize("g:\\abc\\def\\.") == path("g:", "abc", "def")
85        assert fileSystem.normalize("h:/abc\\def\\./ghi") == path("h:", "abc", "def", "ghi")
86        assert fileSystem.normalize("c:\\abc").toLowerCase() == path("c:", "abc")
87        assert fileSystem.normalize("c:/abc").toLowerCase() == path("c:", "abc")
88        assert fileSystem.normalize("z:/abc").toLowerCase() == path("z:", "abc")
89    }
90
91    void testGetName() {
92        assert fileSystem.getName("l:\\") == ""
93        assert fileSystem.getName("m:\\abc") == "abc"
94        assert fileSystem.getName("n:/abc\\def") == "def"
95        assert fileSystem.getName("o:/abc/def") == "def"
96    }
97
98    public void testGetParent() {
99        assert fileSystem.getParent("p:/") == null
100        assert fileSystem.getParent("q:\\abc") == "q:\\"
101        assert fileSystem.getParent("r:/abc\\def") == path("r:", "abc")
102        assert fileSystem.getParent("s:\\abc/def") == path("s:", "abc")
103    }
104
105    void testIsValidName() {
106        // \/:*?"<>|
107        ["a:\\abc",
108                "c:/abc",
109                "d:/abc\\def",
110                "e:/abc\\d!ef",
111                "f:\\abc\\def\\h(ij)",
112                "g:\\abc",
113                "z:/abc/def",
114                "\\\\shared"
115        ].each {
116            assert fileSystem.isValidName(it), "[$it]"
117        }
118
119        ["",
120                "abc",
121                "abc/def",
122                "a:/abc:",
123                "B:\\a*bc",
124                "C:/?abc",
125                "D:\\abc/<def",
126                "E:/abc/def>",
127                "aa:\\abc",
128                "X:X:/abc",
129                ":\\abc\\def",
130                "X:\\\\abc"
131        ].each {
132            assert !fileSystem.isValidName(it), "[$it]"
133        }
134    }
135
136    void testIsAbsolute() {
137        assert fileSystem.isAbsolute("c:\\")
138        assert fileSystem.isAbsolute("x:\\Documents")
139        assert fileSystem.isAbsolute("a:/")
140        assert fileSystem.isAbsolute("\\\\shared\\docs")
141
142        assert !fileSystem.isAbsolute("abc")
143        assert !fileSystem.isAbsolute("/usr")
144        assert !fileSystem.isAbsolute("c:usr")
145
146        shouldFailWithMessageContaining("path") { fileSystem.isAbsolute(null) }
147    }
148
149    void testCaseInsensitive() {
150        def fileEntry = fileSystem.getEntry(EXISTING_FILE)
151        assert fileEntry
152        assert fileEntry == fileSystem.getEntry(EXISTING_FILE.toLowerCase())
153    }
154
155    //-------------------------------------------------------------------------
156    // Test setup
157    //-------------------------------------------------------------------------
158
159    void setUp() {
160        super.setUp()
161    }
162
163    protected Class getExpectedDirectoryListingFormatterClass() {
164        return WindowsDirectoryListingFormatter
165    }
166
167    //-----------------------------------------------------------------------------------
168    // Helper Methods
169    //-----------------------------------------------------------------------------------
170
171    /**
172     * Return a new instance of the FileSystem implementation class under test
173     *
174     * @return a new FileSystem instance
175     */
176    protected FileSystem createFileSystem() {
177        WindowsFakeFileSystem fs = new WindowsFakeFileSystem()
178        fs.add(new DirectoryEntry(EXISTING_DIR))
179        fs.add(new FileEntry(EXISTING_FILE, EXISTING_FILE_CONTENTS))
180        fs.createParentDirectoriesAutomatically = false
181        return fs
182    }
183
184    /**
185     * Return the specified paths concatenated with the system-dependent separator in between
186     * @param p1 - the first path
187     * @param p2 - the second path
188     * @return p1 + SEPARATOR + p2
189     */
190    private String path(String[] paths) {
191        return paths.join(SEP)
192    }
193}
194