1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2;
18
19 import java.net.URI;
20
21 import org.junit.Test;
22
23
24
25
26 public class UriTests extends AbstractProviderTestCase {
27
28
29
30
31
32 @Override
33 protected Capability[] getRequiredCapabilities() {
34 return new Capability[] {Capability.URI};
35 }
36
37
38
39
40 @Test
41 public void testAbsoluteURI() throws Exception {
42 final FileObject readFolder = getReadFolder();
43
44
45 final String uri = readFolder.getName().getURI();
46 FileObject file = getManager().resolveFile(uri, readFolder.getFileSystem().getFileSystemOptions());
47 assertSame("file object", readFolder, file);
48
49
50 final String rootUri = readFolder.getName().getRootURI();
51 file = getManager().resolveFile(rootUri, readFolder.getFileSystem().getFileSystemOptions());
52 assertSame(readFolder.getFileSystem().getRoot(), file);
53 assertEquals(rootUri, file.getName().getRootURI());
54 assertEquals(rootUri, file.getName().getURI());
55 assertEquals(FileName.ROOT_PATH, file.getName().getPath());
56 }
57
58 @Test
59 public void testGetURI() throws Exception {
60 final FileObject fileObject = getReadFolder().resolveFile("some-dir/");
61 final URI uri = fileObject.getURI();
62
63
64 assertEquals(fileObject.getName().getURI(), uri.toString());
65 assertEquals(URI.create(fileObject.getName().getURI()), uri);
66
67 assertEquals(fileObject.getURL().toString(), fileObject.getURI().toString());
68 assertEquals(fileObject.getURL().toURI(), fileObject.getURI());
69 }
70
71 @Test
72 public void testReservedCharacterSpace() throws FileSystemException {
73 try (final FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) {
74 final URI url = fileObject.getURI();
75 final String string = url.toString();
76 assertTrue(string, string.contains("file%20with%20spaces.txt"));
77 }
78 try (final FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) {
79 final URI url = fileObject.getURI();
80 final String string = url.toString();
81 assertTrue(string, string.contains("file%20with%20spaces.txt"));
82 }
83 }
84
85
86
87
88 @Test
89 public void testURIContentProvider() throws Exception {
90
91 final FileObject fileObject = getReadFolder().resolveFile("file1.txt");
92 assertTrue(fileObject.exists());
93
94 final URI uri = fileObject.getURI();
95 final String uriStr = uri.toString();
96 final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions();
97
98 final FileObject f1 = getManager().resolveFile(uriStr, options);
99 final FileObject f2 = getManager().resolveFile(uriStr, options);
100
101 assertEquals("Two files resolved by URI must be equals on " + uriStr, f1, f2);
102 assertSame("Resolving two times should not produce new filesystem on " + uriStr, f1.getFileSystem(),
103 f2.getFileSystem());
104 }
105 }