You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2019/07/08 19:20:09 UTC

[maven] 02/03: [MNG-6535] Switch behaviour on relative URIs to match Path.normalize

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch MNG-6703
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 96c005acaf77d1ce0c59251394e7d125c3fa35de
Author: Joseph Walton <jo...@kafsemo.org>
AuthorDate: Sun Jul 7 14:42:20 2019 +1000

    [MNG-6535] Switch behaviour on relative URIs to match Path.normalize
    
    This method could be passed relative URIs, so improve handling rather
    than doubling down on the existing behaviour. Normalize relative URIs
    to remove redundant '../'.
    
    Also switch to JUnit annotations and drop some boilerplate.
---
 .../maven/model/path/DefaultUrlNormalizer.java     |  7 ++--
 .../maven/model/path/DefaultUrlNormalizerTest.java | 41 ++++++++++------------
 2 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultUrlNormalizer.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultUrlNormalizer.java
index 9b9216d..1260367 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultUrlNormalizer.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultUrlNormalizer.java
@@ -55,9 +55,12 @@ public class DefaultUrlNormalizer
                 parent = result.lastIndexOf( '/', parent );
                 if ( parent < 0 )
                 {
-                    break;
+                    result = result.substring( idx + 4);
+                }
+                else
+                {
+                    result = result.substring( 0, parent ) + result.substring( idx + 3 );
                 }
-                result = result.substring( 0, parent ) + result.substring( idx + 3 );
             }
         }
 
diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/path/DefaultUrlNormalizerTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/path/DefaultUrlNormalizerTest.java
index ac9489f..d0c6343 100644
--- a/maven-model-builder/src/test/java/org/apache/maven/model/path/DefaultUrlNormalizerTest.java
+++ b/maven-model-builder/src/test/java/org/apache/maven/model/path/DefaultUrlNormalizerTest.java
@@ -19,45 +19,31 @@ package org.apache.maven.model.path;
  * under the License.
  */
 
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
 
 /**
  * @author Benjamin Bentmann
  */
 public class DefaultUrlNormalizerTest
-    extends TestCase
 {
 
-    private UrlNormalizer normalizer;
-
-    @Override
-    protected void setUp()
-        throws Exception
-    {
-        super.setUp();
-
-        normalizer = new DefaultUrlNormalizer();
-    }
-
-    @Override
-    protected void tearDown()
-        throws Exception
-    {
-        normalizer = null;
-
-        super.tearDown();
-    }
+    private UrlNormalizer normalizer = new DefaultUrlNormalizer();
 
     private String normalize( String url )
     {
         return normalizer.normalize( url );
     }
 
+    @Test
     public void testNullSafe()
     {
         assertNull( normalize( null ) );
     }
 
+    @Test
     public void testTrailingSlash()
     {
         assertEquals( "", normalize( "" ) );
@@ -65,6 +51,7 @@ public class DefaultUrlNormalizerTest
         assertEquals( "http://server.org/dir/", normalize( "http://server.org/dir/" ) );
     }
 
+    @Test
     public void testRemovalOfParentRefs()
     {
         assertEquals( "http://server.org/child", normalize( "http://server.org/parent/../child" ) );
@@ -74,6 +61,7 @@ public class DefaultUrlNormalizerTest
         assertEquals( "http://server.org/child", normalize( "http://server.org/parent//../child" ) );
     }
 
+    @Test
     public void testPreservationOfDoubleSlashes()
     {
         assertEquals( "scm:hg:ssh://localhost//home/user", normalize( "scm:hg:ssh://localhost//home/user" ) );
@@ -82,8 +70,15 @@ public class DefaultUrlNormalizerTest
                       normalize( "[fetch=]http://server.org/[push=]ssh://server.org/" ) );
     }
 
-    public void testNormalizeInputWithNoParentDirectoriesToAscendTo()
+    @Test
+    public void relativeUriReferenceLeftUnaffectedWithNoParentDirectoryToAscendTo()
+    {
+        assertEquals( "/../", normalize("/../" ) );
+    }
+
+    @Test
+    public void parentDirectoryRemovedFromRelativeUriReference()
     {
-        assertEquals("a/../", normalize("a/../"));
+        assertEquals( "", normalize( "a/../" ) );
     }
 }