1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }