KickJava   Java API By Example, From Geeks To Geeks.

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


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.Capability;
19 import org.apache.commons.vfs.FileObject;
20
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25
26 /**
27  * URL test cases for providers.
28  *
29  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
30  */

31 public class UrlTests
32     extends AbstractProviderTestCase
33 {
34     /**
35      * Returns the capabilities required by the tests of this test case. The
36      * tests are not run if the provider being tested does not support all
37      * the required capabilities. Return null or an empty array to always
38      * run the tests.
39      * <p/>
40      * <p>This implementation returns null.
41      */

42     protected Capability[] getRequiredCaps()
43     {
44         return new Capability[]{Capability.URI};
45     }
46
47     /**
48      * Tests url.
49      */

50     public void testURL() throws Exception JavaDoc
51     {
52         final FileObject file = getReadFolder().resolveFile("some-dir/");
53         final URL JavaDoc url = file.getURL();
54
55         assertEquals(file.getName().getURI(), url.toExternalForm());
56
57         final URL JavaDoc parentURL;
58         try
59         {
60             parentURL = new URL JavaDoc(url, "..");
61         }
62         catch (MalformedURLException JavaDoc e)
63         {
64             throw e;
65         }
66         assertEquals(file.getParent().getURL(), parentURL);
67
68         final URL JavaDoc rootURL = new URL JavaDoc(url, "/");
69         assertEquals(file.getFileSystem().getRoot().getURL(), rootURL);
70     }
71
72     /**
73      * Tests content.
74      */

75     public void testURLContent() throws Exception JavaDoc
76     {
77         // Test non-empty file
78
FileObject file = getReadFolder().resolveFile("file1.txt");
79         assertTrue(file.exists());
80
81         URLConnection JavaDoc urlCon = file.getURL().openConnection();
82         assertSameURLContent(FILE1_CONTENT, urlCon);
83
84         // Test empty file
85
file = getReadFolder().resolveFile("empty.txt");
86         assertTrue(file.exists());
87
88         urlCon = file.getURL().openConnection();
89         assertSameURLContent("", urlCon);
90     }
91
92     /**
93      * Tests that unknown files have no content.
94      */

95     public void testUnknownURL() throws Exception JavaDoc
96     {
97         // Try getting the content of an unknown file
98
final FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
99         assertFalse(unknownFile.exists());
100
101         final URLConnection JavaDoc connection = unknownFile.getURL().openConnection();
102         try
103         {
104             connection.getInputStream();
105             fail();
106         }
107         catch (final IOException JavaDoc e)
108         {
109             assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
110         }
111         assertEquals(-1, connection.getContentLength());
112     }
113
114 }
115
Popular Tags