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