maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject cvs commit: maven-wagon/wagon-provider-api/src/test/java/org/apache/maven/wagon PathUtilsTest.java
Date Thu, 10 Feb 2005 03:20:59 GMT
brett       2005/02/09 19:20:59

  Modified:    wagon-provider-api/src/main/java/org/apache/maven/wagon/repository
                        Repository.java
               wagon-provider-api/src/main/java/org/apache/maven/wagon
                        PathUtils.java
               wagon-provider-api/src/test/java/org/apache/maven/wagon/repository
                        RepositoryTest.java
               wagon-provider-api/src/test/java/org/apache/maven/wagon
                        PathUtilsTest.java
  Log:
  improve file:// URL handling, according to the RFCs
  
  Revision  Changes    Path
  1.11      +1 -3      maven-wagon/wagon-provider-api/src/main/java/org/apache/maven/wagon/repository/Repository.java
  
  Index: Repository.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-provider-api/src/main/java/org/apache/maven/wagon/repository/Repository.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Repository.java	9 Feb 2005 08:45:42 -0000	1.10
  +++ Repository.java	10 Feb 2005 03:20:58 -0000	1.11
  @@ -149,8 +149,6 @@
               sb.append( port );
           }
   
  -        sb.append( "/" );
  -
           sb.append( basedir );
   
           return sb.toString();
  
  
  
  1.4       +101 -11   maven-wagon/wagon-provider-api/src/main/java/org/apache/maven/wagon/PathUtils.java
  
  Index: PathUtils.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-provider-api/src/main/java/org/apache/maven/wagon/PathUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PathUtils.java	29 Jun 2004 18:14:28 -0000	1.3
  +++ PathUtils.java	10 Feb 2005 03:20:59 -0000	1.4
  @@ -130,6 +130,20 @@
        */
       public static String host( final String url )
       {
  +        String authorization = authorization( url );
  +        int index = authorization.indexOf( '@' );
  +        if ( index >= 0 )
  +        {
  +            return authorization.substring( index + 1 );
  +        }
  +        else
  +        {
  +            return authorization;
  +        }
  +    }
  +
  +    private static String authorization( final String url )
  +    {
           if ( url == null )
           {
               return "localhost";
  @@ -139,7 +153,7 @@
   
           if ( protocol == null || protocol.equals( "file" ) )
           {
  -            return "";
  +            return "localhost";
           }
   
           String host = url.substring( url.indexOf( "://" ) + 3 ).trim();
  @@ -151,7 +165,16 @@
               host = host.substring( 0, pos );
           }
   
  -        pos = host.indexOf( ":" );
  +        pos = host.indexOf( '@' );
  +
  +        if ( pos > 0 )
  +        {
  +            pos = host.indexOf( ':', pos );
  +        }
  +        else
  +        {
  +            pos = host.indexOf( ":" );
  +        }
   
           if ( pos > 0 )
           {
  @@ -173,8 +196,7 @@
        */
       public static String protocol( final String url )
       {
  -
  -        final int pos = url.indexOf( "://" );
  +        final int pos = url.indexOf( ":" );
   
           if ( pos == -1 )
           {
  @@ -228,7 +250,7 @@
   
       /**
        * @param url 
  -     * 
  +     * @todo need to URL decode for spaces?
        * @return 
        */
       public static String basedir( final String url )
  @@ -239,11 +261,48 @@
   
           if ( protocol.equals( "file" ) )
           {
  -            retValue = url.substring( protocol.length() + 3 );
  +            retValue = url.substring( protocol.length() + 1 );
  +            // special case: if omitted // on protocol, keep path as is
  +            if ( retValue.startsWith( "//" ) )
  +            {
  +                retValue = retValue.substring( 2 );
  +
  +                if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|'
|| retValue.charAt( 1 ) == ':' ) )
  +                {
  +                    // special case: if there is a windows drive letter, then keep the
original return value
  +                    retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
  +                }
  +                else
  +                {
  +                    // Now we expect the host
  +                    int index = retValue.indexOf( "/" );
  +                    if ( index >= 0 )
  +                    {
  +                        retValue = retValue.substring( index + 1 );
  +                    }
  +
  +                    // special case: if there is a windows drive letter, then keep the
original return value
  +                    if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) ==
'|' || retValue.charAt( 1 ) == ':' ) )
  +                    {
  +                        retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
  +                    }
  +                    else if ( index >= 0 )
  +                    {
  +                        // leading / was previously stripped
  +                        retValue = "/" + retValue;
  +                    }
  +                }
  +            }
  +
  +            // special case: if there is a windows drive letter using |, switch to :
  +            if ( retValue.length() >= 2 && retValue.charAt( 1 ) == '|' )
  +            {
  +                retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
  +            }
           }
           else
           {
  -            final String host = PathUtils.host( url );
  +            final String host = PathUtils.authorization( url );
   
               final int port = PathUtils.port( url );
   
  @@ -251,12 +310,12 @@
   
               if ( port != WagonConstants.UNKNOWN_PORT )
               {
  -                pos = ( protocol + "://" + host + ":" + port + "/" ).length();
  +                pos = ( protocol + "://" + host + ":" + port ).length();
   
               }
               else
               {
  -                pos = ( protocol + "://" + host + "/" ).length();
  +                pos = ( protocol + "://" + host ).length();
               }
               if ( url.length() > pos )
               {
  @@ -268,9 +327,40 @@
   
           if ( retValue == null )
           {
  -            retValue = "";
  +            retValue = "/";
           }
           return retValue.trim();
       }
   
  +    public static String user( String url )
  +    {
  +        String host = authorization( url );
  +        int index = host.indexOf( '@' );
  +        if ( index > 0 ) {
  +            String userInfo = host.substring( 0, index );
  +            index = userInfo.indexOf( ':' );
  +            if ( index > 0 ) {
  +                return userInfo.substring( 0, index );
  +            }
  +            else if ( index < 0 )
  +            {
  +                return userInfo;
  +            }
  +        }
  +        return null;
  +    }
  +
  +    public static String password( String url )
  +    {
  +        String host = authorization( url );
  +        int index = host.indexOf( '@' );
  +        if ( index > 0 ) {
  +            String userInfo = host.substring( 0, index );
  +            index = userInfo.indexOf( ':' );
  +            if ( index >= 0 ) {
  +                return userInfo.substring( index + 1 );
  +            }
  +        }
  +        return null;
  +    }
   }
  
  
  
  1.5       +3 -3      maven-wagon/wagon-provider-api/src/test/java/org/apache/maven/wagon/repository/RepositoryTest.java
  
  Index: RepositoryTest.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-provider-api/src/test/java/org/apache/maven/wagon/repository/RepositoryTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RepositoryTest.java	9 Feb 2005 06:17:43 -0000	1.4
  +++ RepositoryTest.java	10 Feb 2005 03:20:59 -0000	1.5
  @@ -74,7 +74,7 @@
   
           assertEquals( "www.ibiblio.org", repository.getHost() );
   
  -        assertEquals( "", repository.getBasedir() );
  +        assertEquals( "/", repository.getBasedir() );
   
           assertEquals( WagonConstants.UNKNOWN_PORT, repository.getPort() );
   
  @@ -86,7 +86,7 @@
   
           assertEquals( "www.ibiblio.org", repository.getHost() );
   
  -        assertEquals( "maven", repository.getBasedir() );
  +        assertEquals( "/maven", repository.getBasedir() );
   
           assertEquals( 100, repository.getPort() );
   
  
  
  
  1.4       +58 -15    maven-wagon/wagon-provider-api/src/test/java/org/apache/maven/wagon/PathUtilsTest.java
  
  Index: PathUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-provider-api/src/test/java/org/apache/maven/wagon/PathUtilsTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PathUtilsTest.java	19 Jun 2004 14:16:21 -0000	1.3
  +++ PathUtilsTest.java	10 Feb 2005 03:20:59 -0000	1.4
  @@ -23,7 +23,8 @@
    * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
    * @version $Id$
    */
  -public class PathUtilsTest extends TestCase
  +public class PathUtilsTest
  +    extends TestCase
   {
       public void testFilenameResolving()
       {
  @@ -61,9 +62,7 @@
   
       public void testHostResolving()
       {
  -        assertEquals(
  -                "www.codehaus.org",
  -                PathUtils.host( "http://www.codehaus.org" ) );
  +        assertEquals( "www.codehaus.org", PathUtils.host( "http://www.codehaus.org" ) );
   
           assertEquals( "localhost", PathUtils.host( null ) );
   
  @@ -73,15 +72,62 @@
       {
           assertEquals( "http", PathUtils.protocol( "http://www.codehause.org" ) );
           assertEquals( "file", PathUtils.protocol( "file:///c:/temp" ) );
  +    }
  +
  +    public void testUserInfo()
  +    {
  +        String urlWithUsername = "http://brett@www.codehaus.org";
  +        assertEquals( "brett", PathUtils.user( urlWithUsername ) );
  +        assertNull( PathUtils.password( urlWithUsername ) );
  +        assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsername ) );
  +        assertEquals( "/", PathUtils.basedir( urlWithUsername ) );
  +        String urlWithUsernamePassword = "http://brett:porter@www.codehaus.org";
  +        assertEquals( "brett", PathUtils.user( urlWithUsernamePassword ) );
  +        assertEquals( "porter", PathUtils.password( urlWithUsernamePassword ) );
  +        assertEquals( "www.codehaus.org", PathUtils.host( urlWithUsernamePassword ) );
  +        assertEquals( "/", PathUtils.basedir( urlWithUsernamePassword ) );
  +    }
  +
  +    public void testFileBasedir()
  +    {
  +        // see http://www.mozilla.org/quality/networking/testing/filetests.html
  +        
  +        // strict forms
  +        assertEquals( "c:/temp", PathUtils.basedir( "file:///c|/temp" ) );
  +        assertEquals( "localhost", PathUtils.host( "file:///c|/temp" ) );
  +        assertEquals( "c:/temp", PathUtils.basedir( "file://localhost/c|/temp" ) );
  +        assertEquals( "localhost", PathUtils.host( "file://localhost/c|/temp" ) );
  +        assertEquals( "/temp", PathUtils.basedir( "file:///temp" ) );
  +        assertEquals( "localhost", PathUtils.host( "file:///temp" ) );
  +        assertEquals( "/temp", PathUtils.basedir( "file://localhost/temp" ) );
  +        assertEquals( "localhost", PathUtils.host( "file://localhost/temp" ) );
  +
  +        // strict form, with : for drive separator
  +        assertEquals( "c:/temp", PathUtils.basedir( "file:///c:/temp" ) );
  +        assertEquals( "localhost", PathUtils.host( "file:///c:/temp" ) );
  +        assertEquals( "c:/temp", PathUtils.basedir( "file://localhost/c:/temp" ) );
  +        assertEquals( "localhost", PathUtils.host( "file://localhost/c:/temp" ) );
   
  +        // convenience forms
  +        assertEquals( "c:/temp", PathUtils.basedir( "file://c:/temp" ) );
  +        assertEquals( "c:/temp", PathUtils.basedir( "file://c|/temp" ) );
  +        assertEquals( "c:/temp", PathUtils.basedir( "file:c:/temp" ) );
  +        assertEquals( "c:/temp", PathUtils.basedir( "file:c|/temp" ) );
  +        assertEquals( "/temp", PathUtils.basedir( "file:/temp" ) );
  +    }
  +
  +    public void testEmptyBasedir()
  +    {
  +        assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org:80" ) );
  +        assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org" ) );
  +        assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org:80/" ) );
  +        assertEquals( "/", PathUtils.basedir( "http://www.codehaus.org/" ) );
       }
   
       public void testPortResolving()
       {
           assertEquals( 80, PathUtils.port( "http://www.codehause.org:80/maven" ) );
  -        assertEquals(
  -                WagonConstants.UNKNOWN_PORT,
  -                PathUtils.port( "http://localhost/temp" ) );
  +        assertEquals( WagonConstants.UNKNOWN_PORT, PathUtils.port( "http://localhost/temp"
) );
   
           assertEquals( 10, PathUtils.port( "ftp://localhost:10" ) );
   
  @@ -89,15 +135,12 @@
   
       public void testPortBasedir()
       {
  -        assertEquals(
  -                "maven",
  -                PathUtils.basedir( "http://www.codehause.org:80/maven" ) );
  -        assertEquals( "temp", PathUtils.basedir( "http://localhost/temp" ) );
  +        assertEquals( "/maven", PathUtils.basedir( "http://www.codehause.org:80/maven"
) );
  +        assertEquals( "/temp", PathUtils.basedir( "http://localhost/temp" ) );
   
           assertEquals( "c:/temp", PathUtils.basedir( "file://c:/temp" ) );
  -        assertEquals( "", PathUtils.basedir( "http://localhost:80/" ) );
  -        assertEquals( "", PathUtils.basedir( "http://localhost/" ) );
  -
  +        assertEquals( "/", PathUtils.basedir( "http://localhost:80/" ) );
  +        assertEquals( "/", PathUtils.basedir( "http://localhost/" ) );
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-cvs-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-cvs-help@maven.apache.org


Mime
View raw message