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