View Javadoc
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.vfs2.provider.test;
18  
19  import org.apache.commons.vfs2.AbstractVfsTestCase;
20  import org.apache.commons.vfs2.FileSystemException;
21  import org.apache.commons.vfs2.provider.GenericFileName;
22  import org.apache.commons.vfs2.provider.URLFileNameParser;
23  import org.junit.Test;
24  
25  /**
26   * Some GenericFileName test cases.
27   */
28  public class GenericFileNameTestCase extends AbstractVfsTestCase {
29  
30      /**
31       * Tests error handling in URI parser.
32       */
33      @Test
34      public void testBadlyFormedUri() throws Exception {
35          // Does not start with ftp://
36          testBadlyFormedUri("ftp:", "vfs.provider/missing-double-slashes.error");
37          testBadlyFormedUri("ftp:/", "vfs.provider/missing-double-slashes.error");
38          testBadlyFormedUri("ftp:a", "vfs.provider/missing-double-slashes.error");
39  
40          // Missing hostname
41          testBadlyFormedUri("ftp://", "vfs.provider/missing-hostname.error");
42          testBadlyFormedUri("ftp://:21/file", "vfs.provider/missing-hostname.error");
43          testBadlyFormedUri("ftp:///file", "vfs.provider/missing-hostname.error");
44  
45          // Empty port
46          testBadlyFormedUri("ftp://host:", "vfs.provider/missing-port.error");
47          testBadlyFormedUri("ftp://host:/file", "vfs.provider/missing-port.error");
48          testBadlyFormedUri("ftp://host:port/file", "vfs.provider/missing-port.error");
49  
50          // Missing absolute path
51          testBadlyFormedUri("ftp://host:90a", "vfs.provider/missing-hostname-path-sep.error");
52          testBadlyFormedUri("ftp://host?a", "vfs.provider/missing-hostname-path-sep.error");
53      }
54  
55      /**
56       * Tests that parsing a URI fails with the expected error.
57       */
58      private void testBadlyFormedUri(final String uri, final String errorMsg) {
59          try {
60              new URLFileNameParser(80).parseUri(null, null, uri);
61              fail();
62          } catch (final FileSystemException e) {
63              assertSameMessage(errorMsg, uri, e);
64          }
65      }
66  
67      /**
68       * Tests parsing a URI into its parts.
69       */
70      @Test
71      public void testParseUri() throws Exception {
72          final URLFileNameParser urlParser = new URLFileNameParser(21);
73          // Simple name
74          GenericFileName name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname/file");
75          assertEquals("ftp", name.getScheme());
76          assertNull(name.getUserName());
77          assertNull(name.getPassword());
78          assertEquals("hostname", name.getHostName());
79          assertEquals(21, name.getPort());
80          assertEquals(name.getDefaultPort(), name.getPort());
81          assertEquals("/file", name.getPath());
82          assertEquals("ftp://hostname/", name.getRootURI());
83          assertEquals("ftp://hostname/file", name.getURI());
84  
85          // Name with port
86          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname:9090/file");
87          assertEquals("ftp", name.getScheme());
88          assertNull(name.getUserName());
89          assertNull(name.getPassword());
90          assertEquals("hostname", name.getHostName());
91          assertEquals(9090, name.getPort());
92          assertEquals("/file", name.getPath());
93          assertEquals("ftp://hostname:9090/", name.getRootURI());
94          assertEquals("ftp://hostname:9090/file", name.getURI());
95  
96          // Name with no path
97          name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname");
98          assertEquals("ftp", name.getScheme());
99          assertNull(name.getUserName());
100         assertNull(name.getPassword());
101         assertEquals("hostname", name.getHostName());
102         assertEquals(21, name.getPort());
103         assertEquals("/", name.getPath());
104         assertEquals("ftp://hostname/", name.getRootURI());
105         assertEquals("ftp://hostname/", name.getURI());
106 
107         // Name with username
108         name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user@hostname/file");
109         assertEquals("ftp", name.getScheme());
110         assertEquals("user", name.getUserName());
111         assertNull(name.getPassword());
112         assertEquals("hostname", name.getHostName());
113         assertEquals(21, name.getPort());
114         assertEquals("/file", name.getPath());
115         assertEquals("ftp://user@hostname/", name.getRootURI());
116         assertEquals("ftp://user@hostname/file", name.getURI());
117 
118         // Name with username and password
119         name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user:password@hostname/file");
120         assertEquals("ftp", name.getScheme());
121         assertEquals("user", name.getUserName());
122         assertEquals("password", name.getPassword());
123         assertEquals("hostname", name.getHostName());
124         assertEquals(21, name.getPort());
125         assertEquals("/file", name.getPath());
126         assertEquals("ftp://user:password@hostname/", name.getRootURI());
127         assertEquals("ftp://user:password@hostname/file", name.getURI());
128 
129         // Encoded username and password
130         name = (GenericFileName) urlParser.parseUri(null, null, "ftp://%75ser%3A:%40@hostname");
131         assertEquals("ftp", name.getScheme());
132         assertEquals("user:", name.getUserName());
133         assertEquals("@", name.getPassword());
134         assertEquals("hostname", name.getHostName());
135         assertEquals(21, name.getPort());
136         assertEquals("/", name.getPath());
137         assertEquals("ftp://user%3a:%40@hostname/", name.getRootURI());
138         assertEquals("ftp://user%3a:%40@hostname/", name.getURI());
139     }
140 }