]> source.dussan.org Git - archiva.git/commitdiff
RepositoryURL updates.
authorJoakim Erdfelt <joakime@apache.org>
Fri, 13 Apr 2007 19:50:44 +0000 (19:50 +0000)
committerJoakim Erdfelt <joakime@apache.org>
Fri, 13 Apr 2007 19:50:44 +0000 (19:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@528631 13f79535-47bb-0310-9956-ffa450edef68

archiva-jpox-database-refactor/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/ArchivaRepository.java
archiva-jpox-database-refactor/archiva-base/archiva-model/src/main/java/org/apache/maven/archiva/model/RepositoryURL.java
archiva-jpox-database-refactor/archiva-base/archiva-model/src/test/java/org/apache/maven/archiva/model/RepositoryURLTest.java [new file with mode: 0644]

index c1911b6ee4fbee433f81d8d88fc75dabfa76b01a..c1727242127e52345294cc8fc86e0d0152fe9a6d 100644 (file)
@@ -112,4 +112,16 @@ public class ArchivaRepository
     {
         return this.model.getName();
     }
+
+    public String toString()
+    {
+        StringBuffer sb = new StringBuffer();
+
+        sb.append( "ArchivaRepository[" );
+        sb.append( this.model.getId() ).append( "," );
+        sb.append( this.model.getUrl() );
+        sb.append( "]" );
+
+        return sb.toString();
+    }
 }
index 9141fcdc9541430cb04bcea6a70f1c65bcf170c5..c36dbf8665cd9deee3469a8185977a37980fefbd 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.maven.archiva.model;
  * under the License.
  */
 
-
 /**
  * RepositoryURL - Mutable (and protocol forgiving) URL object.
  *
@@ -50,7 +49,7 @@ public class RepositoryURL
 
         int pos;
 
-        pos = url.indexOf( "://" );
+        pos = url.indexOf( ":/" );
         if ( pos == ( -1 ) )
         {
             throw new IllegalArgumentException( "Invalid URL, unable to parse protocol:// from " + url );
@@ -58,8 +57,23 @@ public class RepositoryURL
 
         protocol = url.substring( 0, pos );
 
+        // Determine the post protocol position.
+        int postProtocolPos = protocol.length() + 1;
+        while ( url.charAt( postProtocolPos ) == '/' )
+        {
+            postProtocolPos++;
+        }
+        
+        // Handle special case with file protocol (which has no host, port, username, or password)
+        if ( "file".equals( protocol ) )
+        {
+            path = "/" + url.substring( postProtocolPos );
+
+            return;
+        }
+
         // attempt to find the start of the 'path'
-        pos = url.indexOf( "/", protocol.length() + 3 );
+        pos = url.indexOf( "/", postProtocolPos );
 
         // no path specified - ex "http://localhost"
         if ( pos == ( -1 ) )
@@ -76,7 +90,7 @@ public class RepositoryURL
         }
 
         // get the middle section ( username : password @ hostname : port )
-        String middle = url.substring( protocol.length() + 3, pos );
+        String middle = url.substring( postProtocolPos, pos );
 
         pos = middle.indexOf( '@' );
 
diff --git a/archiva-jpox-database-refactor/archiva-base/archiva-model/src/test/java/org/apache/maven/archiva/model/RepositoryURLTest.java b/archiva-jpox-database-refactor/archiva-base/archiva-model/src/test/java/org/apache/maven/archiva/model/RepositoryURLTest.java
new file mode 100644 (file)
index 0000000..358e692
--- /dev/null
@@ -0,0 +1,97 @@
+package org.apache.maven.archiva.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+
+/**
+ * RepositoryURLTest 
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class RepositoryURLTest
+    extends TestCase
+{
+    private static final String NO_HOST = null;
+
+    private static final String NO_PORT = null;
+
+    private static final String NO_USER = null;
+
+    private static final String NO_PASS = null;
+
+    private void assertURL( String url, String expectedProtocol, String expectedHost, String expectedPort,
+                            String expectedPath, String expectedUsername, String expectedPassword )
+    {
+        RepositoryURL rurl = new RepositoryURL( url );
+        assertEquals( "Protocol", expectedProtocol, rurl.getProtocol() );
+        assertEquals( "Host", expectedHost, rurl.getHost() );
+        assertEquals( "Port", expectedPort, rurl.getPort() );
+        assertEquals( "Path", expectedPath, rurl.getPath() );
+        assertEquals( "Username", expectedUsername, rurl.getUsername() );
+        assertEquals( "Password", expectedPassword, rurl.getPassword() );
+    }
+
+    public void testFileUrlNormal()
+    {
+        assertURL( "file:///home/joakim/code/test/this/", "file", NO_HOST, NO_PORT, "/home/joakim/code/test/this/",
+                   NO_USER, NO_PASS );
+    }
+
+    public void testFileUrlShort()
+    {
+        assertURL( "file:/home/joakim/code/test/this/", "file", NO_HOST, NO_PORT, "/home/joakim/code/test/this/",
+                   NO_USER, NO_PASS );
+    }
+
+    public void testHttpUrlPathless()
+    {
+        assertURL( "http://machine", "http", "machine", NO_PORT, "/", NO_USER, NO_PASS );
+    }
+
+    public void testHttpUrlWithPort()
+    {
+        assertURL( "http://machine:8080/", "http", "machine", "8080", "/", NO_USER, NO_PASS );
+    }
+
+    public void testHttpUrlWithUsernamePassword()
+    {
+        assertURL( "http://user:pass@machine/secured/", "http", "machine", NO_PORT, "/secured/", "user", "pass" );
+    }
+
+    public void testHttpUrlWithUsernameNoPassword()
+    {
+        assertURL( "http://user@machine/secured/", "http", "machine", NO_PORT, "/secured/", "user", NO_PASS );
+    }
+
+    public void testHttpUrlWithUsernamePasswordAndPort()
+    {
+        assertURL( "http://user:pass@machine:9090/secured/", "http", "machine", "9090", "/secured/", "user", "pass" );
+    }
+
+    public void testBogusWithPath()
+    {
+        // This should not fail.  The intent of RepositoryURL is to have it support oddball protocols that
+        // are used by maven-scm and maven-wagon (unlike java.net.URL)
+        assertURL( "bogus://a.machine.name.com/path/to/resource/file.txt", "bogus", "a.machine.name.com", NO_PORT,
+                   "/path/to/resource/file.txt", NO_USER, NO_PASS );
+    }
+}