You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ep...@apache.org on 2006/03/13 12:17:36 UTC

svn commit: r385513 - in /maven/repository-manager/trunk/maven-repository-utils/src: main/java/org/apache/maven/repository/ArtifactUtils.java test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java

Author: epunzalan
Date: Mon Mar 13 03:17:31 2006
New Revision: 385513

URL: http://svn.apache.org/viewcvs?rev=385513&view=rev
Log:
unloaded ArtifactUtils.buildArtifact() not to handle m1 paths and created buildArtifactFromLegacyPath to do it instead.

Added unit tests for the legacy path

Added:
    maven/repository-manager/trunk/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java
Modified:
    maven/repository-manager/trunk/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java

Modified: maven/repository-manager/trunk/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java?rev=385513&r1=385512&r2=385513&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java (original)
+++ maven/repository-manager/trunk/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java Mon Mar 13 03:17:31 2006
@@ -10,6 +10,8 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.StringTokenizer;
+import java.util.LinkedList;
+import java.util.Iterator;
 
 /*
  * Copyright 2005-2006 The Apache Software Foundation.
@@ -191,35 +193,235 @@
                 }
             }
         }
-        else if ( pathParts.size() == 3 )
+
+        return artifact;
+    }
+
+    public static Artifact buildArtifactFromLegacyPath( String path, ArtifactFactory artifactFactory )
+    {
+        StringTokenizer tokens = new StringTokenizer( path, "/\\" );
+
+        Artifact result = null;
+
+        int numberOfTokens = tokens.countTokens();
+
+        if ( numberOfTokens == 3 )
         {
-            //maven 1.x path
+            String groupId = tokens.nextToken();
 
-            String filename = (String) pathParts.remove( 0 );
+            String type = tokens.nextToken();
 
-            int idx = filename.lastIndexOf( '-' );
-            if ( idx > 0 )
+            if ( type.endsWith( "s" ) )
             {
-                String extension = filename.substring( filename.lastIndexOf( '.' ) + 1 );
+                type = type.substring( 0, type.length() - 1 );
+
+                // contains artifactId, version, classifier, and extension.
+                String avceGlob = tokens.nextToken();
 
-                String version = filename.substring( idx + 1, filename.lastIndexOf( '.' ) );
+                LinkedList avceTokenList = new LinkedList();
 
-                String artifactId = filename.substring( 0, idx );
+                StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
+                while ( avceTokenizer.hasMoreTokens() )
+                {
+                    avceTokenList.addLast( avceTokenizer.nextToken() );
+                }
 
-                String types = (String) pathParts.remove( 0 );
+                String lastAvceToken = (String) avceTokenList.removeLast();
 
-                // remove the "s" in types
-                String type = types.substring( 0, types.length() -1 );
+                boolean valid = true;
 
-                if ( type.equals( extension ) )
+                // TODO: share with other discoverer, use artifact handlers instead
+                if ( lastAvceToken.endsWith( ".tar.gz" ) )
                 {
-                    String groupId = (String) pathParts.remove( 0 );
+                    type = "distribution-tgz";
 
-                    artifact = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
+                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
+
+                    avceTokenList.addLast( lastAvceToken );
+                }
+                else if ( lastAvceToken.endsWith( "sources.jar" ) )
+                {
+                    type = "java-source";
+
+                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
+
+                    avceTokenList.addLast( lastAvceToken );
+                }
+                else if ( lastAvceToken.endsWith( ".zip" ) )
+                {
+                    type = "distribution-zip";
+
+                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
+
+                    avceTokenList.addLast( lastAvceToken );
+                }
+                else
+                {
+                    int extPos = lastAvceToken.lastIndexOf( '.' );
+
+                    if ( extPos > 0 )
+                    {
+                        String ext = lastAvceToken.substring( extPos + 1 );
+                        if ( type.equals( ext ) )
+                        {
+                            lastAvceToken = lastAvceToken.substring( 0, extPos );
+
+                            avceTokenList.addLast( lastAvceToken );
+                        }
+                        else
+                        {
+                            //type does not match extension
+                            valid = false;
+                        }
+                    }
+                    else
+                    {
+                        // no extension
+                        valid = false;
+                    }
+                }
+
+                if ( valid )
+                {
+                    // let's discover the version, and whatever's leftover will be either
+                    // a classifier, or part of the artifactId, depending on position.
+                    // Since version is at the end, we have to move in from the back.
+                    Collections.reverse( avceTokenList );
+
+                    // TODO: this is obscene - surely a better way?
+                    String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
+                        "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
+                        "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
+                        "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
+                        "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
+                        "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
+                        "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)";
+
+                    StringBuffer classifierBuffer = new StringBuffer();
+                    StringBuffer versionBuffer = new StringBuffer();
+
+                    boolean firstVersionTokenEncountered = false;
+                    boolean firstToken = true;
+
+                    int tokensIterated = 0;
+                    for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+                    {
+                        String token = (String) it.next();
+
+                        boolean tokenIsVersionPart = token.matches( validVersionParts );
+
+                        StringBuffer bufferToUpdate;
+
+                        // NOTE: logic in code is reversed, since we're peeling off the back
+                        // Any token after the last versionPart will be in the classifier.
+                        // Any token UP TO first non-versionPart is part of the version.
+                        if ( !tokenIsVersionPart )
+                        {
+                            if ( firstVersionTokenEncountered )
+                            {
+                                //noinspection BreakStatement
+                                break;
+                            }
+                            else
+                            {
+                                bufferToUpdate = classifierBuffer;
+                            }
+                        }
+                        else
+                        {
+                            firstVersionTokenEncountered = true;
+
+                            bufferToUpdate = versionBuffer;
+                        }
+
+                        if ( firstToken )
+                        {
+                            firstToken = false;
+                        }
+                        else
+                        {
+                            bufferToUpdate.insert( 0, '-' );
+                        }
+
+                        bufferToUpdate.insert( 0, token );
+
+                        tokensIterated++;
+                    }
+
+                    // Now, restore the proper ordering so we can build the artifactId.
+                    Collections.reverse( avceTokenList );
+
+                    // if we didn't find a version, then punt. Use the last token
+                    // as the version, and set the classifier empty.
+                    if ( versionBuffer.length() < 1 )
+                    {
+                        if ( avceTokenList.size() > 1 )
+                        {
+                            int lastIdx = avceTokenList.size() - 1;
+
+                            versionBuffer.append( avceTokenList.get( lastIdx ) );
+                            avceTokenList.remove( lastIdx );
+                        }
+
+                        classifierBuffer.setLength( 0 );
+                    }
+                    else
+                    {
+                        // if everything is kosher, then pop off all the classifier and
+                        // version tokens, leaving the naked artifact id in the list.
+                        avceTokenList =
+                            new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
+                    }
+
+                    StringBuffer artifactIdBuffer = new StringBuffer();
+
+                    firstToken = true;
+                    for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+                    {
+                        String token = (String) it.next();
+
+                        if ( firstToken )
+                        {
+                            firstToken = false;
+                        }
+                        else
+                        {
+                            artifactIdBuffer.append( '-' );
+                        }
+
+                        artifactIdBuffer.append( token );
+                    }
+
+                    String artifactId = artifactIdBuffer.toString();
+
+                    if ( artifactId.length() > 0 )
+                    {
+                        int lastVersionCharIdx = versionBuffer.length() - 1;
+                        if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
+                        {
+                            versionBuffer.setLength( lastVersionCharIdx );
+                        }
+
+                        String version = versionBuffer.toString();
+
+                        if ( version.length() >= 1 )
+                        {
+                            if ( classifierBuffer.length() > 0 )
+                            {
+                                result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
+                                                                                       classifierBuffer.toString() );
+                            }
+                            else
+                            {
+                                result = artifactFactory.createArtifact( groupId, artifactId, version,
+                                                                         Artifact.SCOPE_RUNTIME, type );
+                            }
+                        }
+                    }
                 }
             }
         }
 
-        return artifact;
+        return result;
     }
-}
+}
\ No newline at end of file

Added: maven/repository-manager/trunk/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java?rev=385513&view=auto
==============================================================================
--- maven/repository-manager/trunk/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java (added)
+++ maven/repository-manager/trunk/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsLegacyTest.java Mon Mar 13 03:17:31 2006
@@ -0,0 +1,110 @@
+package org.apache.maven.repository;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+/**
+ * @author Edwin Punzalan
+ */
+public class ArtifactUtilsLegacyTest
+    extends PlexusTestCase
+{
+    private ArtifactFactory factory;
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        factory = (ArtifactFactory) container.lookup( ArtifactFactory.ROLE );
+    }
+
+    protected void tearDown()
+        throws Exception
+    {
+        container.release( factory );
+
+        super.tearDown();
+    }
+
+    public void testWrongArtifactPackaging()
+        throws ComponentLookupException
+    {
+        String testPath = "org.apache.maven.test/jars/artifactId-1.0.jar.md5";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for wrong package extension", artifact );
+    }
+
+    public void testNoArtifactid()
+    {
+        String testPath = "groupId/jars/-1.0.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null when artifactId is missing", artifact );
+
+        testPath = "groupId/jars/1.0.jar";
+
+        artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null when artifactId is missing", artifact );
+    }
+
+    public void testNoType()
+        throws ComponentLookupException
+    {
+        String testPath = "invalid/invalid/1/invalid-1";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for no type", artifact );
+    }
+
+    public void testSnapshot()
+        throws ComponentLookupException
+    {
+        String testPath = "org.apache.maven.test/jars/maven-model-1.0-SNAPSHOT.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Artifact path with invalid snapshot error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-SNAPSHOT" ), artifact );
+    }
+
+    public void testNormal()
+        throws ComponentLookupException
+    {
+        String testPath = "javax.sql/jars/jdbc-2.0.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Normal artifact path error", artifact );
+
+        assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
+    }
+
+    private Artifact getArtifactFromPath( String path )
+    {
+        return ArtifactUtils.buildArtifactFromLegacyPath( path, factory );
+    }
+
+    private Artifact createArtifact( String groupId, String artifactId, String version )
+    {
+        return factory.createArtifact( groupId, artifactId, version, null, "jar" );
+    }
+
+    private Artifact createArtifact( String groupId, String artifactId, String version, String type )
+    {
+        return factory.createArtifact( groupId, artifactId, version, null, type );
+    }
+
+    private Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier )
+    {
+        return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
+    }
+}