1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/*
2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2008 the original author or authors.
3bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License.
6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at
7bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
8bda3441225e0607b5ced8b538123fd7c7a417910chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software
11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and
14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License.
15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.fake.filesystem
17bda3441225e0607b5ced8b538123fd7c7a417910chrismair
18bda3441225e0607b5ced8b538123fd7c7a417910chrismair/**
19bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Tests for UnixFakeFileSystem.
20bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
21bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$
22bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
23bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair
24bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
25bda3441225e0607b5ced8b538123fd7c7a417910chrismairclass UnixFakeFileSystemTest extends AbstractFakeFileSystemTestCase {
26bda3441225e0607b5ced8b538123fd7c7a417910chrismair
27bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private static final String SEP = "/"
28bda3441225e0607b5ced8b538123fd7c7a417910chrismair
29bda3441225e0607b5ced8b538123fd7c7a417910chrismair    UnixFakeFileSystemTest() {
30bda3441225e0607b5ced8b538123fd7c7a417910chrismair        // These need to be set in the constructor because these values are used in setUp()
31bda3441225e0607b5ced8b538123fd7c7a417910chrismair        NEW_DIR = SEP + NEW_DIRNAME
32bda3441225e0607b5ced8b538123fd7c7a417910chrismair        NEW_FILE = "/NewFile.txt"
33bda3441225e0607b5ced8b538123fd7c7a417910chrismair        EXISTING_DIR = "/"
34bda3441225e0607b5ced8b538123fd7c7a417910chrismair        EXISTING_FILE = "/ExistingFile.txt"
35bda3441225e0607b5ced8b538123fd7c7a417910chrismair        NO_SUCH_DIR = "/xx/yy"
36bda3441225e0607b5ced8b538123fd7c7a417910chrismair        NO_SUCH_FILE = "/xx/yy/zz.txt"
37bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
38bda3441225e0607b5ced8b538123fd7c7a417910chrismair
39bda3441225e0607b5ced8b538123fd7c7a417910chrismair
40bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testListNames_FromRoot() {
41bda3441225e0607b5ced8b538123fd7c7a417910chrismair        final DIR = '/'
42bda3441225e0607b5ced8b538123fd7c7a417910chrismair        final FILENAME = 'abc.txt'
43bda3441225e0607b5ced8b538123fd7c7a417910chrismair        final FILE = p(DIR, FILENAME)
44bda3441225e0607b5ced8b538123fd7c7a417910chrismair
45bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert !fileSystem.exists(FILE)
46bda3441225e0607b5ced8b538123fd7c7a417910chrismair        fileSystem.add(new FileEntry(FILE))
47bda3441225e0607b5ced8b538123fd7c7a417910chrismair        def names = fileSystem.listNames(DIR)
48bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert names.find { it == FILENAME }
49bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
50bda3441225e0607b5ced8b538123fd7c7a417910chrismair
51bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testPath() {
52bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path(null, null) == ""
53bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path(null, "abc") == "abc"
54bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc", null) == "abc"
55bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("", "") == ""
56bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("", "abc") == "abc"
57bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc", "") == "abc"
58bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc", "DEF") == "abc/DEF"
59bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc/", "def") == "abc/def"
60bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("/abc/", "def") == "/abc/def"
61bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("/ABC", "/def") == "/ABC/def"
62bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc", "/def") == "abc/def"
63bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc", "def/..") == "abc"
64bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc", "./def") == "abc/def"
65bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.path("abc/.", null) == "abc"
66bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
67bda3441225e0607b5ced8b538123fd7c7a417910chrismair
68bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testNormalize() {
69bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/") == "/"
70bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/aBc") == "/aBc"
71bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/abc/DEF") == "/abc/DEF"
72bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/Abc/def/..") == "/Abc"
73bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/abc/def/../ghi") == "/abc/ghi"
74bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/abc/def/.") == "/abc/def"
75bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.normalize("/abc/def/./gHI") == "/abc/def/gHI"
76bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
77bda3441225e0607b5ced8b538123fd7c7a417910chrismair
78bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testGetName() {
79bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getName("/") == ""
80bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getName("/aBC") == "aBC"
81bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getName("/abc/def") == "def"
82bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getName("/abc/def/../GHI") == "GHI"
83bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
84bda3441225e0607b5ced8b538123fd7c7a417910chrismair
85bda3441225e0607b5ced8b538123fd7c7a417910chrismair    public void testGetParent() {
86bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getParent("/") == null
87bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getParent("/abc") == "/"
88bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.getParent("/abc/def") == "/abc"
89bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
90bda3441225e0607b5ced8b538123fd7c7a417910chrismair
91bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testIsValidName() {
92bda3441225e0607b5ced8b538123fd7c7a417910chrismair        ["/abc",
93bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "/test/",
94bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "/ABC/def",
95bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "/abc/d!ef",
96bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "/abc/DEF/h(ij)!@#\$%^&*()-_+=~`,.<>?;:[]{}\\|abc",
97bda3441225e0607b5ced8b538123fd7c7a417910chrismair        ].each {
98bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assert fileSystem.isValidName(it), "[$it]"
99bda3441225e0607b5ced8b538123fd7c7a417910chrismair        }
100bda3441225e0607b5ced8b538123fd7c7a417910chrismair
101bda3441225e0607b5ced8b538123fd7c7a417910chrismair        ["",
102bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "abc",
103bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "abc/def",
104bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "a:/abc:",
105bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "//a*bc",
106bda3441225e0607b5ced8b538123fd7c7a417910chrismair                "C:/?abc",
107bda3441225e0607b5ced8b538123fd7c7a417910chrismair        ].each {
108bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assert !fileSystem.isValidName(it), "[$it]"
109bda3441225e0607b5ced8b538123fd7c7a417910chrismair        }
110bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
111bda3441225e0607b5ced8b538123fd7c7a417910chrismair
112bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testIsAbsolute() {
113bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.isAbsolute("/")
114bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fileSystem.isAbsolute("/abc")
115bda3441225e0607b5ced8b538123fd7c7a417910chrismair
116bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert !fileSystem.isAbsolute("abc")
117bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert !fileSystem.isAbsolute("c:\\usr")
118bda3441225e0607b5ced8b538123fd7c7a417910chrismair
119bda3441225e0607b5ced8b538123fd7c7a417910chrismair        shouldFailWithMessageContaining("path") { fileSystem.isAbsolute(null) }
120bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
121bda3441225e0607b5ced8b538123fd7c7a417910chrismair
122bda3441225e0607b5ced8b538123fd7c7a417910chrismair    //-----------------------------------------------------------------------------------
123bda3441225e0607b5ced8b538123fd7c7a417910chrismair    // Helper Methods
124bda3441225e0607b5ced8b538123fd7c7a417910chrismair    //-----------------------------------------------------------------------------------
125bda3441225e0607b5ced8b538123fd7c7a417910chrismair
126bda3441225e0607b5ced8b538123fd7c7a417910chrismair    /**
127bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * Return a new instance of the FileSystem implementation class under test
128bda3441225e0607b5ced8b538123fd7c7a417910chrismair     * @return a new FileSystem instance
129bda3441225e0607b5ced8b538123fd7c7a417910chrismair     */
130bda3441225e0607b5ced8b538123fd7c7a417910chrismair    protected FileSystem createFileSystem() {
131bda3441225e0607b5ced8b538123fd7c7a417910chrismair        UnixFakeFileSystem fs = new UnixFakeFileSystem()
132bda3441225e0607b5ced8b538123fd7c7a417910chrismair        fs.add(new DirectoryEntry(EXISTING_DIR))
133bda3441225e0607b5ced8b538123fd7c7a417910chrismair        fs.add(new FileEntry(EXISTING_FILE, EXISTING_FILE_CONTENTS))
134bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert fs.createParentDirectoriesAutomatically
135bda3441225e0607b5ced8b538123fd7c7a417910chrismair        fs.createParentDirectoriesAutomatically = false
136bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return fs
137bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
138bda3441225e0607b5ced8b538123fd7c7a417910chrismair
139bda3441225e0607b5ced8b538123fd7c7a417910chrismair    protected Class getExpectedDirectoryListingFormatterClass() {
140bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return UnixDirectoryListingFormatter
141bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
142bda3441225e0607b5ced8b538123fd7c7a417910chrismair
143bda3441225e0607b5ced8b538123fd7c7a417910chrismair}
144