]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-43
authorEdwin L. Punzalan <epunzalan@apache.org>
Thu, 2 Feb 2006 08:52:25 +0000 (08:52 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Thu, 2 Feb 2006 08:52:25 +0000 (08:52 +0000)
Added unit tests for ArtifactUtils.java

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@374306 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-utils/pom.xml
maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java
maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsTest.java [new file with mode: 0644]

index 5d3938087c250d25162e7c7bbd99e53c429dfa19..f4104b4af46940f862820f160a745d54ea993b45 100644 (file)
   <artifactId>maven-repository-utils</artifactId>
   <name>Maven Repository Utilities</name>
   <dependencies>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-container-default</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
index 9de8321aebfdca5860b3fd2d2c4a5b5776e7e013..b634131f95255504513311c4de67f51fa0688be6 100644 (file)
@@ -34,6 +34,19 @@ public class ArtifactUtils
 {
     public static Artifact buildArtifact( File repositoryBase, String path, ArtifactRepository repository,
                                           ArtifactFactory artifactFactory )
+    {
+        Artifact artifact = buildArtifact( path, artifactFactory );
+
+        if ( artifact != null )
+        {
+            artifact.setRepository( repository );
+            artifact.setFile( new File( repositoryBase, path ) );
+        }
+
+        return artifact;
+    }
+
+    public static Artifact buildArtifact( String path, ArtifactFactory artifactFactory )
     {
         List pathParts = new ArrayList();
         StringTokenizer st = new StringTokenizer( path, "/\\" );
@@ -177,12 +190,6 @@ public class ArtifactUtils
             }
         }
 
-        if ( finalResult != null )
-        {
-            finalResult.setRepository( repository );
-            finalResult.setFile( new File( repositoryBase, path ) );
-        }
-
         return finalResult;
     }
 }
diff --git a/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsTest.java b/maven-repository-utils/src/test/java/org/apache/maven/repository/ArtifactUtilsTest.java
new file mode 100644 (file)
index 0000000..6b498c3
--- /dev/null
@@ -0,0 +1,226 @@
+package org.apache.maven.repository;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 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 ArtifactUtilsTest
+    extends PlexusTestCase
+{
+    private ArtifactFactory factory;
+
+    protected void tearDown()
+        throws Exception
+    {
+        container.release( factory );
+
+        super.tearDown();
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        factory = (ArtifactFactory) container.lookup( ArtifactFactory.ROLE );
+    }
+
+    public void testShortPath()
+        throws ComponentLookupException
+    {
+        String testPath = "invalid/invalid-1.0.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for short paths", artifact );
+    }
+
+    public void testWrongArtifactId()
+        throws ComponentLookupException
+    {
+        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for wrong ArtifactId", 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 testWrongVersion()
+        throws ComponentLookupException
+    {
+        String testPath = "invalid/invalid/1.0/invalid-2.0.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for wrong version", artifact );
+    }
+
+    public void testLongVersion()
+        throws ComponentLookupException
+    {
+        String testPath = "invalid/invalid/1.0/invalid-1.0b.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for long version", artifact );
+    }
+
+    public void testWrongSnapshotVersion()
+        throws ComponentLookupException
+    {
+        String testPath = "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for wrong snapshot version", artifact );
+    }
+
+    public void testSnapshotBaseVersion()
+        throws ComponentLookupException
+    {
+        String testPath = "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNull( "Artifact should be null for snapshot base version", artifact );
+    }
+
+    public void testPathWithClassifier()
+        throws ComponentLookupException
+    {
+        String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Artifact path with classifier error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
+    }
+
+    public void testWithJavaSourceInclusion()
+        throws ComponentLookupException
+    {
+        String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Artifact path with java source inclusion error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
+    }
+
+    public void testDistributionArtifacts()
+        throws ComponentLookupException
+    {
+        String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "tar.gz distribution artifact error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
+
+        testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
+
+        artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "zip distribution artifact error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
+    }
+
+    public void testSnapshot()
+        throws ComponentLookupException
+    {
+        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Artifact path with invalid snapshot error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
+
+        testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
+
+        artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Artifact path with snapshot error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
+    }
+
+    public void testNormal()
+        throws ComponentLookupException
+    {
+        String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Normal artifact path error", artifact );
+
+        assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
+    }
+
+    public void testSnapshotWithClassifier()
+        throws ComponentLookupException
+    {
+        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
+
+        Artifact artifact = getArtifactFromPath( testPath );
+
+        assertNotNull( "Artifact path with snapshot and classifier error", artifact );
+
+        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ), artifact );
+    }
+
+    private Artifact getArtifactFromPath( String path )
+        throws ComponentLookupException
+    {
+        return ArtifactUtils.buildArtifact( 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 );
+    }
+}