1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.test;
18
19 import org.apache.commons.vfs.Capability;
20 import org.apache.commons.vfs.FileObject;
21
22 import java.io.IOException;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.net.URLConnection;
26
27 /***
28 * URL test cases for providers.
29 *
30 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
32 */
33 public class UrlTests
34 extends AbstractProviderTestCase
35 {
36 /***
37 * Returns the capabilities required by the tests of this test case. The
38 * tests are not run if the provider being tested does not support all
39 * the required capabilities. Return null or an empty array to always
40 * run the tests.
41 * <p/>
42 * <p>This implementation returns null.
43 */
44 protected Capability[] getRequiredCaps()
45 {
46 return new Capability[]{Capability.URI};
47 }
48
49 /***
50 * Tests url.
51 */
52 public void testURL() throws Exception
53 {
54 final FileObject file = getReadFolder().resolveFile("some-dir/");
55 final URL url = file.getURL();
56
57 assertEquals(file.getName().getURI(), url.toExternalForm());
58
59 final URL parentURL;
60 try
61 {
62 parentURL = new URL(url, "..");
63 }
64 catch (MalformedURLException e)
65 {
66 throw e;
67 }
68 assertEquals(file.getParent().getURL(), parentURL);
69
70 final URL rootURL = new URL(url, "/");
71 assertEquals(file.getFileSystem().getRoot().getURL(), rootURL);
72 }
73
74 /***
75 * Tests content.
76 */
77 public void testURLContent() throws Exception
78 {
79
80 FileObject file = getReadFolder().resolveFile("file1.txt");
81 assertTrue(file.exists());
82
83 URLConnection urlCon = file.getURL().openConnection();
84 assertSameURLContent(FILE1_CONTENT, urlCon);
85
86
87 file = getReadFolder().resolveFile("empty.txt");
88 assertTrue(file.exists());
89
90 urlCon = file.getURL().openConnection();
91 assertSameURLContent("", urlCon);
92 }
93
94 /***
95 * Tests that unknown files have no content.
96 */
97 public void testUnknownURL() throws Exception
98 {
99
100 final FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
101 assertFalse(unknownFile.exists());
102
103 final URLConnection connection = unknownFile.getURL().openConnection();
104 try
105 {
106 connection.getInputStream();
107 fail();
108 }
109 catch (final IOException e)
110 {
111 assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
112 }
113 assertEquals(-1, connection.getContentLength());
114 }
115
116 }