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