1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.vfs.FileName; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.commons.vfs.FileSystemException; 21 import org.apache.commons.vfs.NameScope; 22 23 29 public class NamingTests 30 extends AbstractProviderTestCase 31 { 32 35 public void testRelativeURI() throws Exception 36 { 37 getManager().setBaseFile(getReadFolder()); 39 40 FileObject file = getManager().resolveFile("."); 42 assertSame("file object", getReadFolder(), file); 43 44 file = getManager().resolveFile("some-child"); 46 assertSame("file object", getReadFolder(), file.getParent()); 47 48 file = getManager().resolveFile("some-folder/some-file"); 50 assertSame("file object", getReadFolder(), file.getParent().getParent()); 51 52 file = getManager().resolveFile(".."); 54 assertSame("file object", getReadFolder().getParent(), file); 55 56 getManager().setBaseFile((FileObject) null); 58 } 59 60 63 public void testRelativeUriEncoding() throws Exception 64 { 65 getManager().setBaseFile(getReadFolder()); 67 final String path = getReadFolder().getName().getPath(); 68 69 FileObject file = getManager().resolveFile("%73%6f%6d%65%20%66%69%6c%65"); 71 assertEquals(path + "/some file", file.getName().getPathDecoded()); 72 73 file = getManager().resolveFile("%2e"); 75 assertEquals(path, file.getName().getPathDecoded()); 78 79 file = getManager().resolveFile("a%25"); 81 assertEquals(path + "/a%", file.getName().getPathDecoded()); 82 83 file = getManager().resolveFile("dir%2fchild"); 85 assertEquals(path + "/dir/child", file.getName().getPathDecoded()); 86 87 file = getManager().resolveFile("dir%5cchild"); 89 assertEquals(path + "/dir/child", file.getName().getPathDecoded()); 94 95 try 97 { 98 getManager().resolveFile("%"); 99 fail(); 100 } 101 catch (FileSystemException e) 102 { 103 } 104 105 try 107 { 108 getManager().resolveFile("%5"); 109 fail(); 110 } 111 catch (FileSystemException e) 112 { 113 } 114 115 try 117 { 118 getManager().resolveFile("%q"); 119 fail(); 120 } 121 catch (FileSystemException e) 122 { 123 } 124 125 getManager().setBaseFile((FileObject) null); 127 } 128 129 132 public void testRootFileName() throws Exception 133 { 134 final FileName rootName = getReadFolder().getFileSystem().getRoot().getName(); 136 137 assertEquals("root path", "/", rootName.getPath()); 139 140 assertEquals("root base name", "", rootName.getBaseName()); 142 143 assertNull("root parent", rootName.getParent()); 145 } 146 147 150 public void testChildName() throws Exception 151 { 152 final FileName baseName = getReadFolder().getName(); 153 final String basePath = baseName.getPath(); 154 final FileName name = getManager().resolveName(baseName, "some-child", NameScope.CHILD); 155 156 assertTrue("is absolute", basePath.startsWith("/")); 158 159 assertEquals("base name", "some-child", name.getBaseName()); 161 162 assertEquals("absolute path", basePath + "/some-child", name.getPath()); 164 165 assertEquals("parent absolute path", basePath, name.getParent().getPath()); 167 168 assertBadName(name, "a/b", NameScope.CHILD); 170 171 checkDescendentNames(name, NameScope.CHILD); 173 } 174 175 178 private void checkDescendentNames(final FileName name, 179 final NameScope scope) 180 throws Exception 181 { 182 assertTrue(!name.getPath().equals("/")); 184 assertTrue(!name.getPath().endsWith("/a")); 185 assertTrue(!name.getPath().endsWith("/a/b")); 186 187 String path = name.getPath() + "/a"; 189 assertSameName(path, name, path, scope); 190 assertSameName(path, name, "../" + name.getBaseName() + "/a", scope); 191 192 assertBadName(name, "", scope); 194 195 assertBadName(name, ".", scope); 197 assertBadName(name, "./", scope); 198 199 assertBadName(name, "..", scope); 201 assertBadName(name, "../a", scope); 202 assertBadName(name, "../" + name.getBaseName() + "a", scope); 203 assertBadName(name, "a/..", scope); 204 205 assertBadName(name, "/", scope); 207 assertBadName(name, "/a", scope); 208 assertBadName(name, "/a/b", scope); 209 assertBadName(name, name.getPath(), scope); 210 assertBadName(name, name.getPath() + "a", scope); 211 } 212 213 217 private void assertSameName(final String expectedPath, 218 final FileName baseName, 219 final String relName, 220 final NameScope scope) 221 throws Exception 222 { 223 FileName name = getManager().resolveName(baseName, relName, scope); 225 assertEquals(expectedPath, name.getPath()); 226 227 relName.replace('\\', '/'); 229 name = getManager().resolveName(baseName, relName, scope); 230 assertEquals(expectedPath, name.getPath()); 231 232 relName.replace('/', '\\'); 234 name = getManager().resolveName(baseName, relName, scope); 235 assertEquals(expectedPath, name.getPath()); 236 } 237 238 242 private void assertSameName(String expectedPath, 243 FileName baseName, 244 String relName) throws Exception 245 { 246 assertSameName(expectedPath, baseName, relName, NameScope.FILE_SYSTEM); 247 } 248 249 252 public void testNameResolution() throws Exception 253 { 254 final FileName baseName = getReadFolder().getName(); 255 final String parentPath = baseName.getParent().getPath(); 256 final String path = baseName.getPath(); 257 final String childPath = path + "/some-child"; 258 259 assertSameName(path, baseName, ""); 261 262 assertSameName(path, baseName, "."); 264 265 assertSameName(path, baseName, "./"); 267 268 assertSameName(path, baseName, ".//"); 270 271 assertSameName(path, baseName, ".///.///."); 273 assertSameName(path, baseName, "./\\/.\\//."); 274 275 assertSameName(path, baseName, "a/.."); 277 278 assertSameName(parentPath, baseName, ".."); 280 281 assertSameName(parentPath, baseName, "../"); 283 284 assertSameName(parentPath, baseName, "..//./"); 286 assertSameName(parentPath, baseName, "..//.\\"); 287 288 assertSameName(parentPath, baseName, "a/../.."); 290 291 assertSameName(childPath, baseName, "some-child"); 293 294 assertSameName(childPath, baseName, "./some-child"); 296 297 assertSameName(childPath, baseName, "./some-child/"); 299 300 assertSameName(childPath, baseName, "./some-child/././././"); 302 303 assertSameName(childPath, baseName, "a/../some-child"); 305 306 assertSameName(childPath, baseName, "a/b/../../some-child"); 308 } 309 310 313 public void testDescendentName() 314 throws Exception 315 { 316 final FileName baseName = getReadFolder().getName(); 317 318 String path = baseName.getPath() + "/some-child"; 320 assertSameName(path, baseName, "some-child", NameScope.DESCENDENT); 321 322 path = path + "/grand-child"; 324 assertSameName(path, baseName, "some-child/grand-child", NameScope.DESCENDENT); 325 326 assertSameName(path, baseName, "./some-child/grand-child", NameScope.DESCENDENT); 328 assertSameName(path, baseName, "./nada/../some-child/grand-child", NameScope.DESCENDENT); 329 assertSameName(path, baseName, "some-child/./grand-child", NameScope.DESCENDENT); 330 331 checkDescendentNames(baseName, NameScope.DESCENDENT); 333 } 334 335 338 public void testAbsoluteNames() throws Exception 339 { 340 FileName name = getReadFolder().getName(); 342 checkAbsoluteNames(name); 343 344 name = getReadFolder().getFileSystem().getRoot().getName(); 346 checkAbsoluteNames(name); 347 348 name = getManager().resolveName(name, "a/b/unknown"); 350 checkAbsoluteNames(name); 351 } 352 353 356 private void checkAbsoluteNames(final FileName name) throws Exception 357 { 358 assertSameName("/", name, "/"); 360 assertSameName("/", name, "//"); 361 assertSameName("/", name, "/."); 362 assertSameName("/", name, "/some file/.."); 363 364 assertSameName("/a", name, "/a"); 366 assertSameName("/a", name, "/./a"); 367 assertSameName("/a", name, "/a/."); 368 assertSameName("/a/b", name, "/a/b"); 369 370 assertBadName(name, "/..", NameScope.FILE_SYSTEM); 372 assertBadName(name, "/a/../..", NameScope.FILE_SYSTEM); 373 } 374 375 379 private void assertBadName(final FileName name, 380 final String relName, 381 final NameScope scope) 382 { 383 try 384 { 385 getManager().resolveName(name, relName, scope); 386 fail("expected failure"); 387 } 388 catch (FileSystemException e) 389 { 390 } 392 } 393 394 397 public void testAbsoluteNameConvert() throws Exception 398 { 399 final FileName baseName = getReadFolder().getName(); 400 401 String path = "/test1/test2"; 402 FileName name = getManager().resolveName(baseName, path); 403 assertEquals(path, name.getPath()); 404 405 testRelName(name, "child"); 407 testRelName(name, "child1/child2"); 408 409 testRelName(name, "."); 411 412 testRelName(name, ".."); 414 testRelName(name, "../.."); 415 416 testRelName(name, "../sibling"); 418 testRelName(name, "../sibling/child"); 419 420 testRelName(name, "../test2_not"); 422 testRelName(name, "../test2_not/child"); 423 testRelName(name, "../test"); 424 testRelName(name, "../test/child"); 425 426 testRelName(name, "../../unrelated"); 428 testRelName(name, "../../test"); 429 testRelName(name, "../../test/child"); 430 431 path = "/"; 433 name = getManager().resolveName(baseName, path); 434 assertEquals(path, name.getPath()); 435 436 testRelName(name, "child"); 438 testRelName(name, "child1/child2"); 439 440 testRelName(name, "."); 442 } 443 444 447 private void testRelName(final FileName baseName, 448 final String relPath) 449 throws Exception 450 { 451 final FileName expectedName = getManager().resolveName(baseName, relPath); 452 453 final String actualRelPath = baseName.getRelativeName(expectedName); 455 assertEquals(relPath, actualRelPath); 456 } 457 } 458 | Popular Tags |