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