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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import org.apache.commons.vfs2.FileSystemException;
24  import org.apache.commons.vfs2.FileType;
25  import org.junit.jupiter.api.Test;
26  
27  public class UriParserTest {
28  
29      private static final String[] schemes = {"ftp", "file"};
30  
31      private void checkNormalizedPath(final String path, final String normalized) throws FileSystemException {
32          final StringBuilder pathBuilder = new StringBuilder(path);
33          UriParser.fixSeparators(pathBuilder);
34          UriParser.normalisePath(pathBuilder);
35          assertEquals(normalized, pathBuilder.toString());
36      }
37  
38      @Test
39      public void testColonInFileName() {
40          assertNull(UriParser.extractScheme("some/path/some:file"));
41      }
42  
43      @Test
44      public void testColonInFileNameAndNotSupportedScheme() {
45          assertNull(UriParser.extractScheme(schemes, "some:file"));
46      }
47  
48      @Test
49      public void testColonInFileNameWithPath() {
50          assertNull(UriParser.extractScheme(schemes, "some/path/some:file"));
51      }
52  
53      @Test
54      public void testColonNotFollowedBySlash() {
55          assertEquals("file", UriParser.extractScheme(schemes, "file:user/subdir/some/path/some:file"));
56      }
57  
58      @Test
59      public void testIPv6CheckUriEncoding() throws FileSystemException {
60          UriParser.checkUriEncoding("http://[fe80::14b5:1204:5410:64ca%en1]:8080");
61      }
62  
63      @Test
64      public void testNormalScheme() {
65          assertEquals("ftp", UriParser.extractScheme(schemes, "ftp://user:pass@host/some/path/some:file"));
66      }
67  
68      @Test
69      public void testNormalSchemeWithBuffer() {
70          final StringBuilder buffer = new StringBuilder();
71          UriParser.extractScheme(schemes, "ftp://user:pass@host/some/path/some:file", buffer);
72          assertEquals("//user:pass@host/some/path/some:file", buffer.toString());
73      }
74  
75      @Test
76      public void testOneSlashScheme() {
77          assertEquals("file", UriParser.extractScheme(schemes, "file:/user:pass@host/some/path/some:file"));
78      }
79      @Test
80      public void testOneSlashSchemeWithBuffer() {
81          final StringBuilder buffer = new StringBuilder();
82          UriParser.extractScheme(schemes, "file:/user:pass@host/some/path/some:file", buffer);
83          assertEquals("/user:pass@host/some/path/some:file", buffer.toString());
84      }
85  
86      @Test
87      public void testPathOfNormalizedPath() throws FileSystemException {
88          checkNormalizedPath("./Sub Folder/", "Sub Folder");
89          checkNormalizedPath("./Sub Folder/../", "");
90          checkNormalizedPath("./Sub Folder%2f..%2f", "");
91          checkNormalizedPath("File.txt", "File.txt");
92          checkNormalizedPath("./Sub Folder/./File.txt", "Sub Folder/File.txt");
93          checkNormalizedPath("./Sub Folder%2F.%2FFile.txt", "Sub Folder/File.txt");
94      }
95  
96      @Test
97      public void testTypeOfNormalizedPath() {
98          try {
99              assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder("")));
100             assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder("/")));
101             assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder(".")));
102             assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder("./")));
103             assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder("./Sub Folder/")));
104             assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder("./Sub Folder/.")));
105             assertEquals(FileType.FOLDER, UriParser.normalisePath(new StringBuilder("./Sub Folder/./")));
106 
107             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("File.txt")));
108             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("/File.txt")));
109             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("./File.txt")));
110             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("./Sub Folder/File.txt")));
111             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("./Sub Folder/./File.txt")));
112             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("./Sub Folder/./File.")));
113             assertEquals(FileType.FILE, UriParser.normalisePath(new StringBuilder("./Sub Folder/./File..")));
114 
115         } catch (final FileSystemException e) {
116             fail(e);
117         }
118     }
119 }