1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs.provider.test;
18  
19  import org.apache.commons.AbstractVfsTestCase;
20  import org.apache.commons.vfs.FileSystemException;
21  import org.apache.commons.vfs.provider.GenericFileName;
22  import org.apache.commons.vfs.provider.URLFileNameParser;
23  
24  /***
25   * Some GenericFileName test cases.
26   *
27   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
28   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
29   */
30  public class GenericFileNameTestCase
31      extends AbstractVfsTestCase
32  {
33      /***
34       * Tests parsing a URI into its parts.
35       */
36      public void testParseUri() throws Exception
37      {
38          URLFileNameParser urlParser = new URLFileNameParser(21);
39          // Simple name
40          GenericFileName name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname/file");
41          assertEquals("ftp", name.getScheme());
42          assertNull(name.getUserName());
43          assertNull(name.getPassword());
44          assertEquals("hostname", name.getHostName());
45          assertEquals(21, name.getPort());
46          assertEquals(name.getDefaultPort(), name.getPort());
47          assertEquals("/file", name.getPath());
48          assertEquals("ftp://hostname/", name.getRootURI());
49          assertEquals("ftp://hostname/file", name.getURI());
50  
51          // Name with port
52          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname:9090/file");
53          assertEquals("ftp", name.getScheme());
54          assertNull(name.getUserName());
55          assertNull(name.getPassword());
56          assertEquals("hostname", name.getHostName());
57          assertEquals(9090, name.getPort());
58          assertEquals("/file", name.getPath());
59          assertEquals("ftp://hostname:9090/", name.getRootURI());
60          assertEquals("ftp://hostname:9090/file", name.getURI());
61  
62          // Name with no path
63          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname");
64          assertEquals("ftp", name.getScheme());
65          assertNull(name.getUserName());
66          assertNull(name.getPassword());
67          assertEquals("hostname", name.getHostName());
68          assertEquals(21, name.getPort());
69          assertEquals("/", name.getPath());
70          assertEquals("ftp://hostname/", name.getRootURI());
71          assertEquals("ftp://hostname/", name.getURI());
72  
73          // Name with username
74          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user@hostname/file");
75          assertEquals("ftp", name.getScheme());
76          assertEquals("user", name.getUserName());
77          assertNull(name.getPassword());
78          assertEquals("hostname", name.getHostName());
79          assertEquals(21, name.getPort());
80          assertEquals("/file", name.getPath());
81          assertEquals("ftp://user@hostname/", name.getRootURI());
82          assertEquals("ftp://user@hostname/file", name.getURI());
83  
84          // Name with username and password
85          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user:password@hostname/file");
86          assertEquals("ftp", name.getScheme());
87          assertEquals("user", name.getUserName());
88          assertEquals("password", name.getPassword());
89          assertEquals("hostname", name.getHostName());
90          assertEquals(21, name.getPort());
91          assertEquals("/file", name.getPath());
92          assertEquals("ftp://user:password@hostname/", name.getRootURI());
93          assertEquals("ftp://user:password@hostname/file", name.getURI());
94  
95          // Encoded username and password
96          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://%75ser%3A:%40@hostname");
97          assertEquals("ftp", name.getScheme());
98          assertEquals("user:", name.getUserName());
99          assertEquals("@", name.getPassword());
100         assertEquals("hostname", name.getHostName());
101         assertEquals(21, name.getPort());
102         assertEquals("/", name.getPath());
103         assertEquals("ftp://user%3a:%40@hostname/", name.getRootURI());
104         assertEquals("ftp://user%3a:%40@hostname/", name.getURI());
105     }
106 
107     /***
108      * Tests error handling in URI parser.
109      */
110     public void testBadlyFormedUri() throws Exception
111     {
112         // Does not start with ftp://
113         testBadlyFormedUri("ftp:", "vfs.provider/missing-double-slashes.error");
114         testBadlyFormedUri("ftp:/", "vfs.provider/missing-double-slashes.error");
115         testBadlyFormedUri("ftp:a", "vfs.provider/missing-double-slashes.error");
116 
117         // Missing hostname
118         testBadlyFormedUri("ftp://", "vfs.provider/missing-hostname.error");
119         testBadlyFormedUri("ftp://:21/file", "vfs.provider/missing-hostname.error");
120         testBadlyFormedUri("ftp:///file", "vfs.provider/missing-hostname.error");
121 
122         // Empty port
123         testBadlyFormedUri("ftp://host:", "vfs.provider/missing-port.error");
124         testBadlyFormedUri("ftp://host:/file", "vfs.provider/missing-port.error");
125         testBadlyFormedUri("ftp://host:port/file", "vfs.provider/missing-port.error");
126 
127         // Missing absolute path
128         testBadlyFormedUri("ftp://host:90a", "vfs.provider/missing-hostname-path-sep.error");
129         testBadlyFormedUri("ftp://host?a", "vfs.provider/missing-hostname-path-sep.error");
130     }
131 
132     /***
133      * Tests that parsing a URI fails with the expected error.
134      */
135     private void testBadlyFormedUri(final String uri, final String errorMsg)
136     {
137         try
138         {
139             new URLFileNameParser(80).parseUri(null, null, uri);
140             fail();
141         }
142         catch (final FileSystemException e)
143         {
144             assertSameMessage(errorMsg, uri, e);
145         }
146     }
147 }