]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1282] improve unit testing
authorBrett Porter <brett@apache.org>
Wed, 25 Nov 2009 13:00:39 +0000 (13:00 +0000)
committerBrett Porter <brett@apache.org>
Wed, 25 Nov 2009 13:00:39 +0000 (13:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@884084 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolver.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadata.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/Maven2RepositoryMetadataResolverTest.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/storage/maven2/MavenRepositoryMetadataReaderTest.java
archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/incomplete-metadata/1.0-SNAPSHOT/incomplete-metadata-1.0-20091101.112233-1.pom [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/incomplete-metadata/1.0-SNAPSHOT/maven-metadata.xml [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/invalid-pom/1.0/invalid-pom-1.0.pom [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/malformed-metadata/1.0-SNAPSHOT/malformed-metadata-1.0-20091101.112233-1.pom [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/malformed-metadata/1.0-SNAPSHOT/maven-metadata.xml [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/missing-metadata/1.0-SNAPSHOT/missing-metadata-1.0-20091101.112233-1.pom [new file with mode: 0644]
archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/plugins/maven-metadata.xml [new file with mode: 0644]

index a18d4f57da296f084cdafed92d7855f44704a83b..eb46e092172f891e59bb2fba2cff96b25ef7f138 100644 (file)
@@ -86,18 +86,21 @@ public class Maven2RepositoryMetadataResolver
         File basedir = new File( repositoryConfiguration.getLocation() );
         if ( VersionUtil.isSnapshot( projectVersion ) )
         {
-            // TODO: need much error handling here for incorrect metadata
             File metadataFile =
                 pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" );
             try
             {
                 MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile );
 
-                artifactVersion =
-                    artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
+                // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
                 MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion();
-                artifactVersion =
-                    artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
+                if ( snapshotVersion != null )
+                {
+                    artifactVersion =
+                        artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
+                    artifactVersion =
+                        artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
+                }
             }
             catch ( XMLException e )
             {
@@ -109,6 +112,12 @@ public class Maven2RepositoryMetadataResolver
         File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion,
                                            projectId + "-" + artifactVersion + ".pom" );
 
+        if ( !file.exists() )
+        {
+            // metadata could not be resolved
+            return null;
+        }
+
         ModelBuildingRequest req = new DefaultModelBuildingRequest();
         req.setProcessPlugins( false );
         req.setPomFile( file );
@@ -121,7 +130,8 @@ public class Maven2RepositoryMetadataResolver
         }
         catch ( ModelBuildingException e )
         {
-            throw new MetadataResolverException( "Unable to build Maven POM to derive metadata from: " + e.getMessage(), e );
+            throw new MetadataResolverException( "Unable to build Maven POM to derive metadata from: " + e.getMessage(),
+                                                 e );
         }
 
         ProjectVersionMetadata metadata = new ProjectVersionMetadata();
index b5ef400a467ccf7eb98705c5d3d00353c53cc058..c0eb02dddf20eaf1a20f064a985268ef245158a5 100644 (file)
@@ -196,5 +196,44 @@ public class MavenRepositoryMetadata
         {
             return name;
         }
+
+        @Override
+        public boolean equals( Object o )
+        {
+            if ( this == o )
+            {
+                return true;
+            }
+            if ( o == null || getClass() != o.getClass() )
+            {
+                return false;
+            }
+
+            Plugin plugin = (Plugin) o;
+
+            if ( !artifactId.equals( plugin.artifactId ) )
+            {
+                return false;
+            }
+            if ( name != null ? !name.equals( plugin.name ) : plugin.name != null )
+            {
+                return false;
+            }
+            if ( !prefix.equals( plugin.prefix ) )
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        @Override
+        public int hashCode()
+        {
+            int result = prefix.hashCode();
+            result = 31 * result + artifactId.hashCode();
+            result = 31 * result + ( name != null ? name.hashCode() : 0 );
+            return result;
+        }
     }
 }
index 853738abfa61b911b15ba6a2473baa4b35e61356..c9494adae410a5cd836681d396d8ba2a259ee4a8 100644 (file)
@@ -122,6 +122,54 @@ public class Maven2RepositoryMetadataResolverTest
         checkOrganizationApache( metadata );
     }
 
+    public void testGetProjectVersionMetadataForTimestampedSnapshotMissingMetadata()
+        throws MetadataResolverException
+    {
+        ProjectVersionMetadata metadata =
+            resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "missing-metadata", "1.0-SNAPSHOT" );
+        assertNull( metadata );
+    }
+
+    public void testGetProjectVersionMetadataForTimestampedSnapshotMalformedMetadata()
+        throws MetadataResolverException
+    {
+        ProjectVersionMetadata metadata =
+            resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "malformed-metadata", "1.0-SNAPSHOT" );
+        assertNull( metadata );
+    }
+
+    public void testGetProjectVersionMetadataForTimestampedSnapshotIncompleteMetadata()
+        throws MetadataResolverException
+    {
+        ProjectVersionMetadata metadata =
+            resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "incomplete-metadata", "1.0-SNAPSHOT" );
+        assertNull( metadata );
+    }
+
+    public void testGetProjectVersionMetadataForInvalidPom()
+    {
+        try
+        {
+            ProjectVersionMetadata metadata =
+                resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "invalid-pom", "1.0" );
+
+            fail( "Expected failure, but received metadata: " + metadata );
+        }
+        catch ( MetadataResolverException e )
+        {
+            assertTrue( true );
+        }
+    }
+
+    public void testGetProjectVersionMetadataForMissingPom()
+        throws MetadataResolverException
+    {
+        ProjectVersionMetadata metadata =
+            resolver.getProjectVersion( TEST_REPO_ID, "com.example.test", "missing-pom", "1.0" );
+        assertNull( metadata );
+
+    }
+
     private void checkApacheLicense( ProjectVersionMetadata metadata )
     {
         assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
index b88036f372dc82a3747c5185b378c0f44042f83a..c63f609ba9c4e047608644e36c2d5c0c921f3626 100644 (file)
@@ -33,6 +33,41 @@ import org.codehaus.plexus.spring.PlexusInSpringTestCase;
 public class MavenRepositoryMetadataReaderTest
     extends PlexusInSpringTestCase
 {
+    public void testGroupMetadata()
+        throws XMLException
+    {
+        File defaultRepoDir = new File( getBasedir(), "src/test/repositories/test" );
+        File metadataFile = new File( defaultRepoDir, "org/apache/maven/plugins/maven-metadata.xml" );
+
+        MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile );
+
+        assertNotNull( metadata );
+        assertEquals( "org.apache.maven.plugins", metadata.getGroupId() );
+        assertNull( metadata.getArtifactId() );
+        assertNull( metadata.getReleasedVersion() );
+        assertNull( metadata.getLatestVersion() );
+        assertTrue( metadata.getAvailableVersions().isEmpty() );
+        assertNull( metadata.getSnapshotVersion() );
+        assertNull( metadata.getLastUpdated() );
+
+        MavenRepositoryMetadata.Plugin cleanPlugin = new MavenRepositoryMetadata.Plugin();
+        cleanPlugin.setPrefix( "clean" );
+        cleanPlugin.setArtifactId( "maven-clean-plugin" );
+        cleanPlugin.setName( "Maven Clean Plugin" );
+
+        MavenRepositoryMetadata.Plugin compilerPlugin = new MavenRepositoryMetadata.Plugin();
+        compilerPlugin.setPrefix( "compiler" );
+        compilerPlugin.setArtifactId( "maven-compiler-plugin" );
+        compilerPlugin.setName( "Maven Compiler Plugin" );
+
+        MavenRepositoryMetadata.Plugin surefirePlugin = new MavenRepositoryMetadata.Plugin();
+        surefirePlugin.setPrefix( "surefire" );
+        surefirePlugin.setArtifactId( "maven-surefire-plugin" );
+        surefirePlugin.setName( "Maven Surefire Plugin" );
+
+        assertEquals( Arrays.asList( cleanPlugin, compilerPlugin, surefirePlugin ), metadata.getPlugins() );
+    }
+
     public void testProjectMetadata()
         throws XMLException
     {
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/incomplete-metadata/1.0-SNAPSHOT/incomplete-metadata-1.0-20091101.112233-1.pom b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/incomplete-metadata/1.0-SNAPSHOT/incomplete-metadata-1.0-20091101.112233-1.pom
new file mode 100644 (file)
index 0000000..27754c5
--- /dev/null
@@ -0,0 +1,24 @@
+<!--
+  ~ 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.
+  -->
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.example.test</groupId>
+  <artifactId>incomplete-metadata</artifactId>
+  <version>1.0-SNAPSHOT</version>
+</project>
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/incomplete-metadata/1.0-SNAPSHOT/maven-metadata.xml b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/incomplete-metadata/1.0-SNAPSHOT/maven-metadata.xml
new file mode 100644 (file)
index 0000000..9a19b19
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<metadata>
+  <groupId>com.example.test</groupId>
+  <artifactId>incomplete-metadata</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <versioning>
+    <!-- no snapshot element -->
+    <lastUpdated>20080801151215</lastUpdated>
+  </versioning>
+</metadata>
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/invalid-pom/1.0/invalid-pom-1.0.pom b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/invalid-pom/1.0/invalid-pom-1.0.pom
new file mode 100644 (file)
index 0000000..56e31b4
--- /dev/null
@@ -0,0 +1,22 @@
+<!--
+  ~ 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.
+  -->
+
+<this-is-not-the-pom-you-are-looking-for>
+  
+</this-is-not-the-pom-you-are-looking-for>
\ No newline at end of file
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/malformed-metadata/1.0-SNAPSHOT/malformed-metadata-1.0-20091101.112233-1.pom b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/malformed-metadata/1.0-SNAPSHOT/malformed-metadata-1.0-20091101.112233-1.pom
new file mode 100644 (file)
index 0000000..d55c8d0
--- /dev/null
@@ -0,0 +1,24 @@
+<!--
+  ~ 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.
+  -->
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.example.test</groupId>
+  <artifactId>malformed-metadata</artifactId>
+  <version>1.0-SNAPSHOT</version>
+</project>
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/malformed-metadata/1.0-SNAPSHOT/maven-metadata.xml b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/malformed-metadata/1.0-SNAPSHOT/maven-metadata.xml
new file mode 100644 (file)
index 0000000..9baddb1
--- /dev/null
@@ -0,0 +1,20 @@
+<!--
+  ~ 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.
+  -->
+
+    This is not the metadata you are looking for.
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/missing-metadata/1.0-SNAPSHOT/missing-metadata-1.0-20091101.112233-1.pom b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/com/example/test/missing-metadata/1.0-SNAPSHOT/missing-metadata-1.0-20091101.112233-1.pom
new file mode 100644 (file)
index 0000000..90040a4
--- /dev/null
@@ -0,0 +1,24 @@
+<!--
+  ~ 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.
+  -->
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.example.test</groupId>
+  <artifactId>missing-metadata</artifactId>
+  <version>1.0-SNAPSHOT</version>
+</project>
diff --git a/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/plugins/maven-metadata.xml b/archiva-modules/plugins/maven2-repository/src/test/repositories/test/org/apache/maven/plugins/maven-metadata.xml
new file mode 100644 (file)
index 0000000..f2d4f49
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<metadata>
+  <groupId>org.apache.maven.plugins</groupId>
+  <plugins>
+    <plugin>
+      <prefix>clean</prefix>
+      <artifactId>maven-clean-plugin</artifactId>
+      <name>Maven Clean Plugin</name>
+    </plugin>
+    <plugin>
+      <prefix>compiler</prefix>
+      <artifactId>maven-compiler-plugin</artifactId>
+      <name>Maven Compiler Plugin</name>
+    </plugin>
+    <plugin>
+      <prefix>surefire</prefix>
+      <artifactId>maven-surefire-plugin</artifactId>
+      <name>Maven Surefire Plugin</name>
+    </plugin>
+  </plugins>
+</metadata>