KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > test > GenericFileNameTestCase


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.provider.test;
17
18 import org.apache.commons.AbstractVfsTestCase;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.provider.GenericFileName;
21 import org.apache.commons.vfs.provider.URLFileNameParser;
22
23 /**
24  * Some GenericFileName test cases.
25  *
26  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27  */

28 public class GenericFileNameTestCase
29     extends AbstractVfsTestCase
30 {
31     /**
32      * Tests parsing a URI into its parts.
33      */

34     public void testParseUri() throws Exception JavaDoc
35     {
36         URLFileNameParser urlParser = new URLFileNameParser(21);
37         // Simple name
38
GenericFileName name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname/file");
39         assertEquals("ftp", name.getScheme());
40         assertNull(name.getUserName());
41         assertNull(name.getPassword());
42         assertEquals("hostname", name.getHostName());
43         assertEquals(21, name.getPort());
44         assertEquals(name.getDefaultPort(), name.getPort());
45         assertEquals("/file", name.getPath());
46         assertEquals("ftp://hostname/", name.getRootURI());
47         assertEquals("ftp://hostname/file", name.getURI());
48
49         // Name with port
50
name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname:9090/file");
51         assertEquals("ftp", name.getScheme());
52         assertNull(name.getUserName());
53         assertNull(name.getPassword());
54         assertEquals("hostname", name.getHostName());
55         assertEquals(9090, name.getPort());
56         assertEquals("/file", name.getPath());
57         assertEquals("ftp://hostname:9090/", name.getRootURI());
58         assertEquals("ftp://hostname:9090/file", name.getURI());
59
60         // Name with no path
61
name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname");
62         assertEquals("ftp", name.getScheme());
63         assertNull(name.getUserName());
64         assertNull(name.getPassword());
65         assertEquals("hostname", name.getHostName());
66         assertEquals(21, name.getPort());
67         assertEquals("/", name.getPath());
68         assertEquals("ftp://hostname/", name.getRootURI());
69         assertEquals("ftp://hostname/", name.getURI());
70
71         // Name with username
72
name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user@hostname/file");
73         assertEquals("ftp", name.getScheme());
74         assertEquals("user", name.getUserName());
75         assertNull(name.getPassword());
76         assertEquals("hostname", name.getHostName());
77         assertEquals(21, name.getPort());
78         assertEquals("/file", name.getPath());
79         assertEquals("ftp://user@hostname/", name.getRootURI());
80         assertEquals("ftp://user@hostname/file", name.getURI());
81
82         // Name with username and password
83
name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user:password@hostname/file");
84         assertEquals("ftp", name.getScheme());
85         assertEquals("user", name.getUserName());
86         assertEquals("password", name.getPassword());
87         assertEquals("hostname", name.getHostName());
88         assertEquals(21, name.getPort());
89         assertEquals("/file", name.getPath());
90         assertEquals("ftp://user:password@hostname/", name.getRootURI());
91         assertEquals("ftp://user:password@hostname/file", name.getURI());
92
93         // Encoded username and password
94
name = (GenericFileName) urlParser.parseUri(null, null, "ftp://%75ser%3A:%40@hostname");
95         assertEquals("ftp", name.getScheme());
96         assertEquals("user:", name.getUserName());
97         assertEquals("@", name.getPassword());
98         assertEquals("hostname", name.getHostName());
99         assertEquals(21, name.getPort());
100         assertEquals("/", name.getPath());
101         assertEquals("ftp://user%3a:%40@hostname/", name.getRootURI());
102         assertEquals("ftp://user%3a:%40@hostname/", name.getURI());
103     }
104
105     /**
106      * Tests error handling in URI parser.
107      */

108     public void testBadlyFormedUri() throws Exception JavaDoc
109     {
110         // Does not start with ftp://
111
testBadlyFormedUri("ftp:", "vfs.provider/missing-double-slashes.error");
112         testBadlyFormedUri("ftp:/", "vfs.provider/missing-double-slashes.error");
113         testBadlyFormedUri("ftp:a", "vfs.provider/missing-double-slashes.error");
114
115         // Missing hostname
116
testBadlyFormedUri("ftp://", "vfs.provider/missing-hostname.error");
117         testBadlyFormedUri("ftp://:21/file", "vfs.provider/missing-hostname.error");
118         testBadlyFormedUri("ftp:///file", "vfs.provider/missing-hostname.error");
119
120         // Empty port
121
testBadlyFormedUri("ftp://host:", "vfs.provider/missing-port.error");
122         testBadlyFormedUri("ftp://host:/file", "vfs.provider/missing-port.error");
123         testBadlyFormedUri("ftp://host:port/file", "vfs.provider/missing-port.error");
124
125         // Missing absolute path
126
testBadlyFormedUri("ftp://host:90a", "vfs.provider/missing-hostname-path-sep.error");
127         testBadlyFormedUri("ftp://host?a", "vfs.provider/missing-hostname-path-sep.error");
128     }
129
130     /**
131      * Tests that parsing a URI fails with the expected error.
132      */

133     private void testBadlyFormedUri(final String JavaDoc uri, final String JavaDoc errorMsg)
134     {
135         try
136         {
137             new URLFileNameParser(80).parseUri(null, null, uri);
138             fail();
139         }
140         catch (final FileSystemException e)
141         {
142             assertSameMessage(errorMsg, uri, e);
143         }
144     }
145 }
146
Popular Tags