KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > NamingTests


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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.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 /**
24  * Test cases for file naming.
25  *
26  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27  * @todo Add tests for all FileName methods
28  */

29 public class NamingTests
30     extends AbstractProviderTestCase
31 {
32     /**
33      * Tests resolution of relative file names via the FS manager
34      */

35     public void testRelativeURI() throws Exception JavaDoc
36     {
37         // Build base dir
38
getManager().setBaseFile(getReadFolder());
39
40         // Locate the base dir
41
FileObject file = getManager().resolveFile(".");
42         assertSame("file object", getReadFolder(), file);
43
44         // Locate a child
45
file = getManager().resolveFile("some-child");
46         assertSame("file object", getReadFolder(), file.getParent());
47
48         // Locate a descendent
49
file = getManager().resolveFile("some-folder/some-file");
50         assertSame("file object", getReadFolder(), file.getParent().getParent());
51
52         // Locate parent
53
file = getManager().resolveFile("..");
54         assertSame("file object", getReadFolder().getParent(), file);
55
56         // free basefile
57
getManager().setBaseFile((FileObject) null);
58     }
59
60     /**
61      * Tests encoding of relative URI.
62      */

63     public void testRelativeUriEncoding() throws Exception JavaDoc
64     {
65         // Build base dir
66
getManager().setBaseFile(getReadFolder());
67         final String JavaDoc path = getReadFolder().getName().getPath();
68
69         // §1 Encode "some file"
70
FileObject file = getManager().resolveFile("%73%6f%6d%65%20%66%69%6c%65");
71         assertEquals(path + "/some file", file.getName().getPathDecoded());
72
73         // §2 Encode "."
74
file = getManager().resolveFile("%2e");
75         // 18-6-2005 imario@apache.org: no need to keep the "current directory"
76
// assertEquals(path + "/.", file.getName().getPathDecoded());
77
assertEquals(path, file.getName().getPathDecoded());
78
79         // §3 Encode '%'
80
file = getManager().resolveFile("a%25");
81         assertEquals(path + "/a%", file.getName().getPathDecoded());
82
83         // §4 Encode /
84
file = getManager().resolveFile("dir%2fchild");
85         assertEquals(path + "/dir/child", file.getName().getPathDecoded());
86
87         // §5 Encode \
88
file = getManager().resolveFile("dir%5cchild");
89         // 18-6-2005 imario@apache.org: all file separators normalized to "/"
90
// decided to do this to get the same behaviour as in §4 on windows
91
// platforms
92
// assertEquals(path + "/dir\\child", file.getName().getPathDecoded());
93
assertEquals(path + "/dir/child", file.getName().getPathDecoded());
94
95         // §6 Use "%" literal
96
try
97         {
98             getManager().resolveFile("%");
99             fail();
100         }
101         catch (FileSystemException e)
102         {
103         }
104
105         // §7 Not enough digits in encoded char
106
try
107         {
108             getManager().resolveFile("%5");
109             fail();
110         }
111         catch (FileSystemException e)
112         {
113         }
114
115         // §8 Invalid digit in encoded char
116
try
117         {
118             getManager().resolveFile("%q");
119             fail();
120         }
121         catch (FileSystemException e)
122         {
123         }
124
125         // free basefile
126
getManager().setBaseFile((FileObject) null);
127     }
128
129     /**
130      * Tests the root file name.
131      */

132     public void testRootFileName() throws Exception JavaDoc
133     {
134         // Locate the root file
135
final FileName rootName = getReadFolder().getFileSystem().getRoot().getName();
136
137         // Test that the root path is "/"
138
assertEquals("root path", "/", rootName.getPath());
139
140         // Test that the root basname is ""
141
assertEquals("root base name", "", rootName.getBaseName());
142
143         // Test that the root name has no parent
144
assertNull("root parent", rootName.getParent());
145     }
146
147     /**
148      * Tests child file names.
149      */

150     public void testChildName() throws Exception JavaDoc
151     {
152         final FileName baseName = getReadFolder().getName();
153         final String JavaDoc basePath = baseName.getPath();
154         final FileName name = getManager().resolveName(baseName, "some-child", NameScope.CHILD);
155
156         // Test path is absolute
157
assertTrue("is absolute", basePath.startsWith("/"));
158
159         // Test base name
160
assertEquals("base name", "some-child", name.getBaseName());
161
162         // Test absolute path
163
assertEquals("absolute path", basePath + "/some-child", name.getPath());
164
165         // Test parent path
166
assertEquals("parent absolute path", basePath, name.getParent().getPath());
167
168         // Try using a compound name to find a child
169
assertBadName(name, "a/b", NameScope.CHILD);
170
171         // Check other invalid names
172
checkDescendentNames(name, NameScope.CHILD);
173     }
174
175     /**
176      * Name resolution tests that are common for CHILD or DESCENDENT scope.
177      */

178     private void checkDescendentNames(final FileName name,
179                                       final NameScope scope)
180         throws Exception JavaDoc
181     {
182         // Make some assumptions about the name
183
assertTrue(!name.getPath().equals("/"));
184         assertTrue(!name.getPath().endsWith("/a"));
185         assertTrue(!name.getPath().endsWith("/a/b"));
186
187         // Test names with the same prefix
188
String JavaDoc path = name.getPath() + "/a";
189         assertSameName(path, name, path, scope);
190         assertSameName(path, name, "../" + name.getBaseName() + "/a", scope);
191
192         // Test an empty name
193
assertBadName(name, "", scope);
194
195         // Test . name
196
assertBadName(name, ".", scope);
197         assertBadName(name, "./", scope);
198
199         // Test ancestor names
200
assertBadName(name, "..", scope);
201         assertBadName(name, "../a", scope);
202         assertBadName(name, "../" + name.getBaseName() + "a", scope);
203         assertBadName(name, "a/..", scope);
204
205         // Test absolute names
206
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     /**
214      * Checks that a relative name resolves to the expected absolute path.
215      * Tests both forward and back slashes.
216      */

217     private void assertSameName(final String JavaDoc expectedPath,
218                                 final FileName baseName,
219                                 final String JavaDoc relName,
220                                 final NameScope scope)
221         throws Exception JavaDoc
222     {
223         // Try the supplied name
224
FileName name = getManager().resolveName(baseName, relName, scope);
225         assertEquals(expectedPath, name.getPath());
226
227         // Replace the separators
228
relName.replace('\\', '/');
229         name = getManager().resolveName(baseName, relName, scope);
230         assertEquals(expectedPath, name.getPath());
231
232         // And again
233
relName.replace('/', '\\');
234         name = getManager().resolveName(baseName, relName, scope);
235         assertEquals(expectedPath, name.getPath());
236     }
237
238     /**
239      * Checks that a relative name resolves to the expected absolute path.
240      * Tests both forward and back slashes.
241      */

242     private void assertSameName(String JavaDoc expectedPath,
243                                 FileName baseName,
244                                 String JavaDoc relName) throws Exception JavaDoc
245     {
246         assertSameName(expectedPath, baseName, relName, NameScope.FILE_SYSTEM);
247     }
248
249     /**
250      * Tests relative name resolution, relative to the base folder.
251      */

252     public void testNameResolution() throws Exception JavaDoc
253     {
254         final FileName baseName = getReadFolder().getName();
255         final String JavaDoc parentPath = baseName.getParent().getPath();
256         final String JavaDoc path = baseName.getPath();
257         final String JavaDoc childPath = path + "/some-child";
258
259         // Test empty relative path
260
assertSameName(path, baseName, "");
261
262         // Test . relative path
263
assertSameName(path, baseName, ".");
264
265         // Test ./ relative path
266
assertSameName(path, baseName, "./");
267
268         // Test .// relative path
269
assertSameName(path, baseName, ".//");
270
271         // Test .///.///. relative path
272
assertSameName(path, baseName, ".///.///.");
273         assertSameName(path, baseName, "./\\/.\\//.");
274
275         // Test <elem>/.. relative path
276
assertSameName(path, baseName, "a/..");
277
278         // Test .. relative path
279
assertSameName(parentPath, baseName, "..");
280
281         // Test ../ relative path
282
assertSameName(parentPath, baseName, "../");
283
284         // Test ..//./ relative path
285
assertSameName(parentPath, baseName, "..//./");
286         assertSameName(parentPath, baseName, "..//.\\");
287
288         // Test <elem>/../.. relative path
289
assertSameName(parentPath, baseName, "a/../..");
290
291         // Test <elem> relative path
292
assertSameName(childPath, baseName, "some-child");
293
294         // Test ./<elem> relative path
295
assertSameName(childPath, baseName, "./some-child");
296
297         // Test ./<elem>/ relative path
298
assertSameName(childPath, baseName, "./some-child/");
299
300         // Test <elem>/././././ relative path
301
assertSameName(childPath, baseName, "./some-child/././././");
302
303         // Test <elem>/../<elem> relative path
304
assertSameName(childPath, baseName, "a/../some-child");
305
306         // Test <elem>/<elem>/../../<elem> relative path
307
assertSameName(childPath, baseName, "a/b/../../some-child");
308     }
309
310     /**
311      * Tests descendent name resolution.
312      */

313     public void testDescendentName()
314         throws Exception JavaDoc
315     {
316         final FileName baseName = getReadFolder().getName();
317
318         // Test direct child
319
String JavaDoc path = baseName.getPath() + "/some-child";
320         assertSameName(path, baseName, "some-child", NameScope.DESCENDENT);
321
322         // Test compound name
323
path = path + "/grand-child";
324         assertSameName(path, baseName, "some-child/grand-child", NameScope.DESCENDENT);
325
326         // Test relative names
327
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         // Test badly formed descendent names
332
checkDescendentNames(baseName, NameScope.DESCENDENT);
333     }
334
335     /**
336      * Tests resolution of absolute names.
337      */

338     public void testAbsoluteNames() throws Exception JavaDoc
339     {
340         // Test against the base folder
341
FileName name = getReadFolder().getName();
342         checkAbsoluteNames(name);
343
344         // Test against the root
345
name = getReadFolder().getFileSystem().getRoot().getName();
346         checkAbsoluteNames(name);
347
348         // Test against some unknown file
349
name = getManager().resolveName(name, "a/b/unknown");
350         checkAbsoluteNames(name);
351     }
352
353     /**
354      * Tests resolution of absolute names.
355      */

356     private void checkAbsoluteNames(final FileName name) throws Exception JavaDoc
357     {
358         // Root
359
assertSameName("/", name, "/");
360         assertSameName("/", name, "//");
361         assertSameName("/", name, "/.");
362         assertSameName("/", name, "/some file/..");
363
364         // Some absolute names
365
assertSameName("/a", name, "/a");
366         assertSameName("/a", name, "/./a");
367         assertSameName("/a", name, "/a/.");
368         assertSameName("/a/b", name, "/a/b");
369
370         // Some bad names
371
assertBadName(name, "/..", NameScope.FILE_SYSTEM);
372         assertBadName(name, "/a/../..", NameScope.FILE_SYSTEM);
373     }
374
375     /**
376      * Asserts that a particular relative name is invalid for a particular
377      * scope.
378      */

379     private void assertBadName(final FileName name,
380                                final String JavaDoc 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             // TODO - should check error message
391
}
392     }
393
394     /**
395      * Tests conversion from absolute to relative names.
396      */

397     public void testAbsoluteNameConvert() throws Exception JavaDoc
398     {
399         final FileName baseName = getReadFolder().getName();
400
401         String JavaDoc path = "/test1/test2";
402         FileName name = getManager().resolveName(baseName, path);
403         assertEquals(path, name.getPath());
404
405         // Try child and descendent names
406
testRelName(name, "child");
407         testRelName(name, "child1/child2");
408
409         // Try own name
410
testRelName(name, ".");
411
412         // Try parent, and root
413
testRelName(name, "..");
414         testRelName(name, "../..");
415
416         // Try sibling and descendent of sibling
417
testRelName(name, "../sibling");
418         testRelName(name, "../sibling/child");
419
420         // Try siblings with similar names
421
testRelName(name, "../test2_not");
422         testRelName(name, "../test2_not/child");
423         testRelName(name, "../test");
424         testRelName(name, "../test/child");
425
426         // Try unrelated
427
testRelName(name, "../../unrelated");
428         testRelName(name, "../../test");
429         testRelName(name, "../../test/child");
430
431         // Test against root
432
path = "/";
433         name = getManager().resolveName(baseName, path);
434         assertEquals(path, name.getPath());
435
436         // Try child and descendent names (against root)
437
testRelName(name, "child");
438         testRelName(name, "child1/child2");
439
440         // Try own name (against root)
441
testRelName(name, ".");
442     }
443
444     /**
445      * Checks that a file name converts to an expected relative path
446      */

447     private void testRelName(final FileName baseName,
448                              final String JavaDoc relPath)
449         throws Exception JavaDoc
450     {
451         final FileName expectedName = getManager().resolveName(baseName, relPath);
452
453         // Convert to relative path, and check
454
final String JavaDoc actualRelPath = baseName.getRelativeName(expectedName);
455         assertEquals(relPath, actualRelPath);
456     }
457 }
458
Popular Tags