1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.http;
20
21 import java.net.URI;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.junit.Test;
27
28 import static org.junit.Assert.*;
29
30 public class UriUtilsTest {
31
32 private String resolve(URI base, String ref) {
33 return UriUtils.resolve(base, URI.create(ref)).toString();
34 }
35
36 @Test
37 public void testResolve_BaseEmptyPath() {
38 URI base = URI.create("http://host");
39 assertEquals("http://host/file.jar", resolve(base, "file.jar"));
40 assertEquals("http://host/dir/file.jar", resolve(base, "dir/file.jar"));
41 assertEquals("http://host?arg=val", resolve(base, "?arg=val"));
42 assertEquals("http://host/file?arg=val", resolve(base, "file?arg=val"));
43 assertEquals("http://host/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
44 }
45
46 @Test
47 public void testResolve_BaseRootPath() {
48 URI base = URI.create("http://host/");
49 assertEquals("http://host/file.jar", resolve(base, "file.jar"));
50 assertEquals("http://host/dir/file.jar", resolve(base, "dir/file.jar"));
51 assertEquals("http://host/?arg=val", resolve(base, "?arg=val"));
52 assertEquals("http://host/file?arg=val", resolve(base, "file?arg=val"));
53 assertEquals("http://host/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
54 }
55
56 @Test
57 public void testResolve_BasePathTrailingSlash() {
58 URI base = URI.create("http://host/sub/dir/");
59 assertEquals("http://host/sub/dir/file.jar", resolve(base, "file.jar"));
60 assertEquals("http://host/sub/dir/dir/file.jar", resolve(base, "dir/file.jar"));
61 assertEquals("http://host/sub/dir/?arg=val", resolve(base, "?arg=val"));
62 assertEquals("http://host/sub/dir/file?arg=val", resolve(base, "file?arg=val"));
63 assertEquals("http://host/sub/dir/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
64 }
65
66 @Test
67 public void testResolve_BasePathNoTrailingSlash() {
68 URI base = URI.create("http://host/sub/d%20r");
69 assertEquals("http://host/sub/d%20r/file.jar", resolve(base, "file.jar"));
70 assertEquals("http://host/sub/d%20r/dir/file.jar", resolve(base, "dir/file.jar"));
71 assertEquals("http://host/sub/d%20r?arg=val", resolve(base, "?arg=val"));
72 assertEquals("http://host/sub/d%20r/file?arg=val", resolve(base, "file?arg=val"));
73 assertEquals("http://host/sub/d%20r/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
74 }
75
76 private List<URI> getDirs(String base, String uri) {
77 return UriUtils.getDirectories((base != null) ? URI.create(base) : null, URI.create(uri));
78 }
79
80 private void assertUris(List<URI> actual, String... expected) {
81 List<String> uris = new ArrayList<>(actual.size());
82 for (URI uri : actual) {
83 uris.add(uri.toString());
84 }
85 assertEquals(Arrays.asList(expected), uris);
86 }
87
88 @Test
89 public void testGetDirectories_NoBase() {
90 List<URI> parents = getDirs(null, "http://host/repo/sub/dir/file.jar");
91 assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/", "http://host/repo/");
92
93 parents = getDirs(null, "http://host/repo/sub/dir/?file.jar");
94 assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/", "http://host/repo/");
95
96 parents = getDirs(null, "http://host/");
97 assertUris(parents);
98 }
99
100 @Test
101 public void testGetDirectories_ExplicitBaseTrailingSlash() {
102 List<URI> parents = getDirs("http://host/repo/", "http://host/repo/sub/dir/file.jar");
103 assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");
104
105 parents = getDirs("http://host/repo/", "http://host/repo/sub/dir/?file.jar");
106 assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");
107
108 parents = getDirs("http://host/repo/", "http://host/");
109 assertUris(parents);
110 }
111
112 @Test
113 public void testGetDirectories_ExplicitBaseNoTrailingSlash() {
114 List<URI> parents = getDirs("http://host/repo", "http://host/repo/sub/dir/file.jar");
115 assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");
116
117 parents = getDirs("http://host/repo", "http://host/repo/sub/dir/?file.jar");
118 assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");
119
120 parents = getDirs("http://host/repo", "http://host/");
121 assertUris(parents);
122 }
123 }