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