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