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.apache.maven.model.path;
20  
21  import org.junit.jupiter.api.Test;
22  
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  
26  /**
27   */
28  class DefaultUrlNormalizerTest {
29  
30      private UrlNormalizer normalizer = new DefaultUrlNormalizer();
31  
32      private String normalize(String url) {
33          return normalizer.normalize(url);
34      }
35  
36      @Test
37      void testNullSafe() {
38          assertNull(normalize(null));
39      }
40  
41      @Test
42      void testTrailingSlash() {
43          assertEquals("", normalize(""));
44          assertEquals("http://server.org/dir", normalize("http://server.org/dir"));
45          assertEquals("http://server.org/dir/", normalize("http://server.org/dir/"));
46      }
47  
48      @Test
49      void testRemovalOfParentRefs() {
50          assertEquals("http://server.org/child", normalize("http://server.org/parent/../child"));
51          assertEquals("http://server.org/child", normalize("http://server.org/grand/parent/../../child"));
52  
53          assertEquals("http://server.org//child", normalize("http://server.org/parent/..//child"));
54          assertEquals("http://server.org/child", normalize("http://server.org/parent//../child"));
55      }
56  
57      @Test
58      void testPreservationOfDoubleSlashes() {
59          assertEquals("scm:hg:ssh://localhost//home/user", normalize("scm:hg:ssh://localhost//home/user"));
60          assertEquals("file:////UNC/server", normalize("file:////UNC/server"));
61          assertEquals(
62                  "[fetch=]http://server.org/[push=]ssh://server.org/",
63                  normalize("[fetch=]http://server.org/[push=]ssh://server.org/"));
64      }
65  
66      @Test
67      void absolutePathTraversalPastRootIsOmitted() {
68          assertEquals("/", normalize("/../"));
69      }
70  
71      @Test
72      void parentDirectoryRemovedFromRelativeUriReference() {
73          assertEquals("", normalize("a/../"));
74      }
75  
76      @Test
77      void leadingParentDirectoryNotRemovedFromRelativeUriReference() {
78          assertEquals("../", normalize("../"));
79      }
80  }