View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.apache;
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.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.*;
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      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      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      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      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      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     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     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 }