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.core.util.IoUtil
19import org.mockftpserver.fake.filesystem.AbstractFakeFileSystemTest
20
21/**
22 * Tests for FakeWindowsFileSystem.
23 *
24 * @version $Revision: $ - $Date: $
25 *
26 * @author Chris Mair
27 */
28class FakeWindowsFileSystemTest extends AbstractFakeFileSystemTest {
29
30     private static final String SEP = "\\"
31
32     FakeWindowsFileSystemTest() {
33         // These need to be set in the constructor because these values are used in setUp()
34         NEW_DIR = "d:/" + NEW_DIRNAME
35         NEW_FILE = "d:/NewFile.txt"
36         EXISTING_DIR = "d:/"
37         EXISTING_FILE = "d:/ExistingFile.txt"
38         NO_SUCH_DIR = 'x:/xx/yy'
39         NO_SUCH_FILE = "x:/xx/yy/zz.txt"
40     }
41
42     // -------------------------------------------------------------------------
43     // Tests
44     // -------------------------------------------------------------------------
45
46     void testOtherRoots() {
47         final String X = "x:/"
48         final String Y = "y:\\"
49         assertFalse(X, fileSystem.exists(X))
50         assertFalse(Y, fileSystem.exists(Y))
51
52         fileSystem.createDirectory(X)
53         fileSystem.createDirectory(Y)
54
55         assertTrue(X, fileSystem.exists(X))
56         assertTrue(Y, fileSystem.exists(Y))
57     }
58
59     void testPath() {
60         assert fileSystem.path(null, null) == ""
61         assert fileSystem.path(null, "abc") == "abc"
62         assert fileSystem.path("abc", null) == "abc"
63         assert fileSystem.path("", "") == ""
64         assert fileSystem.path("", "abc") == "abc"
65         assert fileSystem.path("abc", "") == "abc"
66         assert fileSystem.path("abc", "def") == "abc" + SEP + "def"
67         assert fileSystem.path("abc\\", "def") == "abc\\def"
68         assert fileSystem.path("c:/abc/", "def") == "c:/abc/def"
69         assert fileSystem.path("d:\\abc", "\\def") == "d:\\abc\\def"
70         assert fileSystem.path("abc", "/def") == "abc/def"
71     }
72
73     void testNormalize() {
74         assert fileSystem.normalize("a:\\") == "a:\\"
75         assert fileSystem.normalize("a:/") == "a:\\"
76         assert fileSystem.normalize("b:/abc") == path("b:","abc")
77         assert fileSystem.normalize("c:\\abc\\def") == path("c:","abc","def")
78         assert fileSystem.normalize("d:/abc/def") == path("d:","abc","def")
79         assert fileSystem.normalize("e:\\abc/def/..") == path("e:","abc")
80         assert fileSystem.normalize("f:/abc/def/../ghi") == path("f:","abc","ghi")
81         assert fileSystem.normalize("g:\\abc\\def\\.") == path("g:","abc","def")
82         assert fileSystem.normalize("h:/abc\\def\\./ghi") == path("h:","abc","def", "ghi")
83         assert fileSystem.normalize("c:\\abc").toLowerCase() == path("c:","abc")
84         assert fileSystem.normalize("c:/abc").toLowerCase() == path("c:","abc")
85         assert fileSystem.normalize("z:/abc").toLowerCase() == path("z:","abc")
86     }
87
88     void testGetName() {
89         assert fileSystem.getName("l:\\") == ""
90         assert fileSystem.getName("m:\\abc") == "abc"
91         assert fileSystem.getName("n:/abc\\def") == "def"
92         assert fileSystem.getName("o:/abc/def") == "def"
93     }
94
95     public void testGetParent() {
96         assert fileSystem.getParent("p:/") == null
97         assert fileSystem.getParent("q:\\abc") == "q:\\"
98         assert fileSystem.getParent("r:/abc\\def") == path("r:","abc")
99         assert fileSystem.getParent("s:\\abc/def") == path("s:","abc")
100     }
101
102     void testIsValidName() {
103         // \/:*?"<>|
104         [ "a:\\abc",
105           "c:/abc",
106           "d:/abc\\def",
107           "e:/abc\\d!ef",
108           "f:\\abc\\def\\h(ij)",
109           "g:\\abc",
110           "z:/abc/def",
111           "\\\\shared"
112           ].each {
113             assert fileSystem.isValidName(it), "[$it]"
114           }
115
116         [ "",
117           "abc",
118           "abc/def",
119           "a:/abc:",
120           "B:\\a*bc",
121           "C:/?abc",
122           "D:\\abc/<def",
123           "E:/abc/def>",
124           "aa:\\abc",
125           "X:X:/abc",
126           ":\\abc\\def",
127           "X:\\\\abc"
128           ].each {
129             assert !fileSystem.isValidName(it), "[$it]"
130         }
131     }
132
133     void testIsAbsolute() {
134         assert fileSystem.isAbsolute("c:\\")
135         assert fileSystem.isAbsolute("x:\\Documents")
136         assert fileSystem.isAbsolute("a:/")
137         assert fileSystem.isAbsolute("\\\\shared\\docs")
138
139         assert !fileSystem.isAbsolute("abc")
140         assert !fileSystem.isAbsolute("/usr")
141         assert !fileSystem.isAbsolute("c:usr")
142
143         shouldFailWithMessageContaining("path") { fileSystem.isAbsolute(null) }
144     }
145
146     //-------------------------------------------------------------------------
147     // Test setup
148     //-------------------------------------------------------------------------
149
150     void setUp() {
151         super.setUp()
152     }
153
154    //-----------------------------------------------------------------------------------
155    // Helper Methods
156    //-----------------------------------------------------------------------------------
157
158    /**
159     * Return a new instance of the FileSystem implementation class under test
160     *
161     * @return a new FileSystem instance
162     */
163    protected FileSystem createFileSystem() {
164        FakeWindowsFileSystem fs = new FakeWindowsFileSystem()
165        fs.addEntry(new DirectoryEntry(EXISTING_DIR))
166        fs.addEntry(new FileEntry(EXISTING_FILE, EXISTING_FILE_CONTENTS))
167        return fs
168    }
169
170    /**
171     * Return the specified paths concatenated with the system-dependent separator in between
172     * @param p1 - the first path
173     * @param p2 - the second path
174     * @return p1 + SEPARATOR + p2
175     */
176    private String path(String[] paths) {
177        return paths.join(SEP)
178    }
179}
180