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 static org.apache.commons.vfs2.VfsTestUtils.assertSameMessage;
20
21 import java.io.IOException;
22 import java.net.URL;
23 import java.net.URLConnection;
24
25 import org.junit.Test;
26
27
28
29
30 public class UrlTests extends AbstractProviderTestCase {
31
32
33
34
35
36 @Override
37 protected Capability[] getRequiredCapabilities() {
38 return new Capability[] {Capability.URI};
39 }
40
41 @Test
42 public void testReservedCharacterSpace() throws FileSystemException {
43 try (FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) {
44 final URL url = fileObject.getURL();
45 final String string = url.toString();
46 assertTrue(string, string.contains("file%20with%20spaces.txt"));
47 }
48 try (FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) {
49 final URL url = fileObject.getURL();
50 final String string = url.toString();
51 assertTrue(string, string.contains("file%20with%20spaces.txt"));
52 }
53 }
54
55
56
57
58 @Test
59 public void testUnknownURL() throws Exception {
60
61 final FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
62 assertFalse(unknownFile.exists());
63
64 final URLConnection connection = unknownFile.getURL().openConnection();
65 try {
66 connection.getInputStream();
67 fail();
68 } catch (final IOException e) {
69 assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
70 }
71 assertEquals(-1, connection.getContentLength());
72 }
73
74
75
76
77 @Test
78 public void testURL() throws Exception {
79 final FileObject file = getReadFolder().resolveFile("some-dir/");
80 final URL url = file.getURL();
81
82 assertEquals(file.getName().getURI(), url.toExternalForm());
83
84 final URL parentURL = new URL(url, "..");
85 assertEquals(file.getParent().getURL(), parentURL);
86
87 final URL rootURL = new URL(url, "/");
88 assertEquals(file.getFileSystem().getRoot().getURL(), rootURL);
89 }
90
91
92
93
94 @Test
95 public void testURLContent() throws Exception {
96 testURLContent(getReadFolder());
97 }
98
99 private void testURLContent(final FileObject readFolder) throws FileSystemException, IOException, Exception {
100
101 FileObject file = readFolder.resolveFile("file1.txt");
102 assertTrue(file.toString(), file.exists());
103
104 URLConnection urlCon = file.getURL().openConnection();
105 assertSameURLContent(FILE1_CONTENT, urlCon);
106
107
108 file = readFolder.resolveFile("empty.txt");
109 assertTrue(file.exists());
110
111 urlCon = file.getURL().openConnection();
112 assertSameURLContent("", urlCon);
113 }
114
115
116
117
118 @Test
119 public void testURLContentProvider() throws Exception {
120
121 final FileObject file = getReadFolder().resolveFile("file1.txt");
122 assertTrue(file.exists());
123
124 final String uri = file.getURL().toExternalForm();
125 final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions();
126
127 final FileObject f1 = getManager().resolveFile(uri, options);
128 final FileObject f2 = getManager().resolveFile(uri, options);
129
130 assertEquals("Two files resolved by URI must be equals on " + uri, f1, f2);
131 assertSame("Resolving two times should not produce new filesystem on " + uri, f1.getFileSystem(),
132 f2.getFileSystem());
133 }
134
135 }