UnixFakeFileSystemTest.groovy revision dfb59d50631968ab1a13002ea5421ece93169851
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
18/**
19 * Tests for UnixFakeFileSystem.
20 *
21 * @version $Revision$ - $Date$
22 *
23 * @author Chris Mair
24 */
25class UnixFakeFileSystemTest extends AbstractFakeFileSystemTestCase {
26
27    private static final String SEP = "/"
28
29    UnixFakeFileSystemTest() {
30        // These need to be set in the constructor because these values are used in setUp()
31        NEW_DIR = SEP + NEW_DIRNAME
32        NEW_FILE = "/NewFile.txt"
33        EXISTING_DIR = "/"
34        EXISTING_FILE = "/ExistingFile.txt"
35        NO_SUCH_DIR = "/xx/yy"
36        NO_SUCH_FILE = "/xx/yy/zz.txt"
37    }
38
39
40    void testListNames_FromRoot() {
41        final DIR = '/'
42        final FILENAME = 'abc.txt'
43        final FILE = p(DIR, FILENAME)
44
45        assert !fileSystem.exists(FILE)
46        fileSystem.add(new FileEntry(FILE))
47        def names = fileSystem.listNames(DIR)
48        assert names.find { it == FILENAME }
49    }
50
51    void testPath() {
52        assert fileSystem.path(null, null) == ""
53        assert fileSystem.path(null, "abc") == "abc"
54        assert fileSystem.path("abc", null) == "abc"
55        assert fileSystem.path("", "") == ""
56        assert fileSystem.path("", "abc") == "abc"
57        assert fileSystem.path("abc", "") == "abc"
58        assert fileSystem.path("abc", "DEF") == "abc/DEF"
59        assert fileSystem.path("abc/", "def") == "abc/def"
60        assert fileSystem.path("/abc/", "def") == "/abc/def"
61        assert fileSystem.path("/ABC", "/def") == "/ABC/def"
62        assert fileSystem.path("abc", "/def") == "abc/def"
63        assert fileSystem.path("abc", "def/..") == "abc"
64        assert fileSystem.path("abc", "./def") == "abc/def"
65        assert fileSystem.path("abc/.", null) == "abc"
66    }
67
68    void testNormalize() {
69        assert fileSystem.normalize("/") == "/"
70        assert fileSystem.normalize("/aBc") == "/aBc"
71        assert fileSystem.normalize("/abc/DEF") == "/abc/DEF"
72        assert fileSystem.normalize("/Abc/def/..") == "/Abc"
73        assert fileSystem.normalize("/abc/def/../ghi") == "/abc/ghi"
74        assert fileSystem.normalize("/abc/def/.") == "/abc/def"
75        assert fileSystem.normalize("/abc/def/./gHI") == "/abc/def/gHI"
76    }
77
78    void testGetName() {
79        assert fileSystem.getName("/") == ""
80        assert fileSystem.getName("/aBC") == "aBC"
81        assert fileSystem.getName("/abc/def") == "def"
82        assert fileSystem.getName("/abc/def/../GHI") == "GHI"
83    }
84
85    public void testGetParent() {
86        assert fileSystem.getParent("/") == null
87        assert fileSystem.getParent("/abc") == "/"
88        assert fileSystem.getParent("/abc/def") == "/abc"
89    }
90
91    void testIsValidName() {
92        ["/abc",
93                "/test/",
94                "/ABC/def",
95                "/abc/d!ef",
96                "/abc/DEF/h(ij)!@#\$%^&*()-_+=~`,.<>?;:[]{}\\|abc",
97        ].each {
98            assert fileSystem.isValidName(it), "[$it]"
99        }
100
101        ["",
102                "abc",
103                "abc/def",
104                "a:/abc:",
105                "//a*bc",
106                "C:/?abc",
107        ].each {
108            assert !fileSystem.isValidName(it), "[$it]"
109        }
110    }
111
112    void testIsAbsolute() {
113        assert fileSystem.isAbsolute("/")
114        assert fileSystem.isAbsolute("/abc")
115
116        assert !fileSystem.isAbsolute("abc")
117        assert !fileSystem.isAbsolute("c:\\usr")
118
119        shouldFailWithMessageContaining("path") { fileSystem.isAbsolute(null) }
120    }
121
122    //-----------------------------------------------------------------------------------
123    // Helper Methods
124    //-----------------------------------------------------------------------------------
125
126    /**
127     * Return a new instance of the FileSystem implementation class under test
128     * @return a new FileSystem instance
129     */
130    protected FileSystem createFileSystem() {
131        UnixFakeFileSystem fs = new UnixFakeFileSystem()
132        fs.add(new DirectoryEntry(EXISTING_DIR))
133        fs.add(new FileEntry(EXISTING_FILE, EXISTING_FILE_CONTENTS))
134        assert fs.createParentDirectoriesAutomatically
135        fs.createParentDirectoriesAutomatically = false
136        return fs
137    }
138
139    protected Class getExpectedDirectoryListingFormatterClass() {
140        return UnixDirectoryListingFormatter
141    }
142
143}
144