You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by br...@apache.org on 2006/09/12 09:20:43 UTC

svn commit: r442502 - in /maven/archiva/trunk/archiva-reports-standard/src: main/java/org/apache/maven/archiva/reporting/ test/java/org/apache/maven/archiva/reporting/ test/resources/org/apache/maven/archiva/reporting/

Author: brett
Date: Tue Sep 12 00:20:42 2006
New Revision: 442502

URL: http://svn.apache.org/viewvc?view=rev&rev=442502
Log:
[MRM-60] add an "old snapshot artifact" report

Added:
    maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessor.java   (with props)
    maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.java   (with props)
    maven/archiva/trunk/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.xml   (with props)
Modified:
    maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java
    maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java

Modified: maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java?view=diff&rev=442502&r1=442501&r2=442502
==============================================================================
--- maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java (original)
+++ maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java Tue Sep 12 00:20:42 2006
@@ -27,6 +27,7 @@
  * Find artifacts in the repository that are considered old.
  *
  * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="old-artifact"
+ * @todo make this configurable from the web interface
  */
 public class OldArtifactReportProcessor
     implements ArtifactReportProcessor

Added: maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessor.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessor.java?view=auto&rev=442502
==============================================================================
--- maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessor.java (added)
+++ maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessor.java Tue Sep 12 00:20:42 2006
@@ -0,0 +1,179 @@
+package org.apache.maven.archiva.reporting;
+
+/*
+ * 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.handler.DefaultArtifactHandler;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.model.Model;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Matcher;
+
+/**
+ * Find snapshot artifacts in the repository that are considered old.
+ *
+ * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="old-snapshot-artifact"
+ * @todo make this configurable from the web interface
+ */
+public class OldSnapshotArtifactReportProcessor
+    implements ArtifactReportProcessor
+{
+    private static final String ROLE_HINT = "old-snapshot-artifact";
+
+    /**
+     * The maximum age of an artifact before it is reported old, specified in seconds. The default is 1 year.
+     *
+     * @plexus.configuration default-value="31536000"
+     */
+    private int maxAge;
+
+    /**
+     * The maximum number of snapshots to retain within a given version. The default is 0, which keeps all snapshots
+     * that are within the age limits.
+     *
+     * @plexus.configuration default-value="0"
+     */
+    private int maxSnapshots;
+
+    public void processArtifact( final Artifact artifact, Model model, ReportingDatabase reporter )
+    {
+        ArtifactRepository repository = artifact.getRepository();
+
+        if ( !"file".equals( repository.getProtocol() ) )
+        {
+            // We can't check other types of URLs yet. Need to use Wagon, with an exists() method.
+            throw new UnsupportedOperationException(
+                "Can't process repository '" + repository.getUrl() + "'. Only file based repositories are supported" );
+        }
+
+        adjustDistributionArtifactHandler( artifact );
+
+        String artifactPath = repository.pathOf( artifact );
+
+        //get the location of the artifact itself
+        File file = new File( repository.getBasedir(), artifactPath );
+
+        if ( file.exists() )
+        {
+            if ( artifact.isSnapshot() )
+            {
+                Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() );
+                if ( m.matches() )
+                {
+                    long timestamp;
+                    try
+                    {
+                        timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).parse( m.group( 2 ) ).getTime();
+                    }
+                    catch ( ParseException e )
+                    {
+                        throw new IllegalStateException(
+                            "Shouldn't match timestamp pattern and not be able to parse it: " + m.group( 2 ) );
+                    }
+
+                    if ( System.currentTimeMillis() - timestamp > maxAge * 1000 )
+                    {
+                        addNotice( reporter, artifact, "snapshot-expired-time",
+                                   "The artifact is older than the maximum age of " + maxAge + " seconds." );
+                    }
+                    else if ( maxSnapshots > 0 )
+                    {
+                        File[] files = file.getParentFile().listFiles( new FilenameFilter()
+                        {
+                            public boolean accept( File file, String string )
+                            {
+                                return string.startsWith( artifact.getArtifactId() + "-" ) &&
+                                    string.endsWith( "." + artifact.getArtifactHandler().getExtension() );
+                            }
+                        } );
+
+                        List/*<Integer>*/ buildNumbers = new ArrayList();
+                        Integer currentBuild = null;
+                        for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
+                        {
+                            File f = (File) i.next();
+
+                            // trim to version
+                            int startIndex = artifact.getArtifactId().length() + 1;
+                            int extensionLength = artifact.getArtifactHandler().getExtension().length() + 1;
+                            int endIndex = f.getName().length() - extensionLength;
+                            String name = f.getName().substring( startIndex, endIndex );
+
+                            Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( name );
+
+                            if ( matcher.matches() )
+                            {
+                                Integer buildNumber = Integer.valueOf( matcher.group( 3 ) );
+
+                                buildNumbers.add( buildNumber );
+                                if ( name.equals( artifact.getVersion() ) )
+                                {
+                                    currentBuild = buildNumber;
+                                }
+                            }
+                        }
+
+                        // Prune back to expired build numbers
+                        Collections.sort( buildNumbers );
+                        for ( int i = 0; i < maxSnapshots && !buildNumbers.isEmpty(); i++ )
+                        {
+                            buildNumbers.remove( buildNumbers.size() - 1 );
+                        }
+
+                        if ( buildNumbers.contains( currentBuild ) )
+                        {
+                            addNotice( reporter, artifact, "snapshot-expired-count",
+                                       "The artifact is older than the maximum number of retained snapshot builds." );
+                        }
+                    }
+                }
+            }
+        }
+        else
+        {
+            throw new IllegalStateException( "Couldn't find artifact " + file );
+        }
+    }
+
+    private static void addNotice( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
+    {
+        // TODO: reason could be an i18n key derived from the processor and the problem ID and the
+        reporter.addNotice( artifact, ROLE_HINT, problem, reason );
+    }
+
+    private static void adjustDistributionArtifactHandler( Artifact artifact )
+    {
+        // need to tweak these as they aren't currently in the known type converters. TODO - add them in Maven
+        if ( "distribution-zip".equals( artifact.getType() ) )
+        {
+            artifact.setArtifactHandler( new DefaultArtifactHandler( "zip" ) );
+        }
+        else if ( "distribution-tgz".equals( artifact.getType() ) )
+        {
+            artifact.setArtifactHandler( new DefaultArtifactHandler( "tar.gz" ) );
+        }
+    }
+}

Propchange: maven/archiva/trunk/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java?view=diff&rev=442502&r1=442501&r2=442502
==============================================================================
--- maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java (original)
+++ maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java Tue Sep 12 00:20:42 2006
@@ -66,6 +66,8 @@
         artifact.setRepository(
             factory.createArtifactRepository( "repository", repository.toURL().toString(), layout, null, null ) );
 
+        artifact.isSnapshot();
+
         return artifact;
     }
 
@@ -78,6 +80,7 @@
     {
         Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
         artifact.setRepository( repository );
+        artifact.isSnapshot();
         return artifact;
     }
 

Added: maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.java
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.java?view=auto&rev=442502
==============================================================================
--- maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.java (added)
+++ maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.java Tue Sep 12 00:20:42 2006
@@ -0,0 +1,166 @@
+package org.apache.maven.archiva.reporting;
+
+/*
+ * 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.archiva.reporting.model.ArtifactResults;
+import org.apache.maven.archiva.reporting.model.Result;
+import org.apache.maven.artifact.Artifact;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+
+/**
+ * This class tests the OldArtifactReportProcessor.
+ */
+public class OldSnapshotArtifactReportProcessorTest
+    extends AbstractRepositoryReportsTestCase
+{
+    private ArtifactReportProcessor artifactReportProcessor;
+
+    private ReportingDatabase reportDatabase;
+
+    private File tempRepository;
+
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+        artifactReportProcessor =
+            (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "old-snapshot-artifact" );
+
+        ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "old-artifact" );
+        reportDatabase = new ReportingDatabase( reportGroup );
+        tempRepository = getTestFile( "target/test-repository" );
+        FileUtils.deleteDirectory( tempRepository );
+    }
+
+    public void testOldSnapshotArtifact()
+    {
+        Artifact artifact = createArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-20050611.202024-1", "pom" );
+
+        artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
+        assertEquals( 0, reportDatabase.getNumFailures() );
+        assertEquals( 0, reportDatabase.getNumWarnings() );
+        assertEquals( "Check notices", 1, reportDatabase.getNumNotices() );
+        Iterator artifactIterator = reportDatabase.getArtifactIterator();
+        assertArtifactResults( artifactIterator, artifact );
+    }
+
+    private static void assertArtifactResults( Iterator artifactIterator, Artifact artifact )
+    {
+        ArtifactResults results = (ArtifactResults) artifactIterator.next();
+        assertEquals( artifact.getArtifactId(), results.getArtifactId() );
+        assertEquals( artifact.getGroupId(), results.getGroupId() );
+        assertEquals( artifact.getVersion(), results.getVersion() );
+        assertFalse( artifact.getVersion().indexOf( "SNAPSHOT" ) >= 0 );
+        assertEquals( 1, results.getNotices().size() );
+        Iterator i = results.getNotices().iterator();
+        Result result = (Result) i.next();
+        assertEquals( "old-snapshot-artifact", result.getProcessor() );
+    }
+
+    public void testSNAPSHOTArtifact()
+    {
+        Artifact artifact = createArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "pom" );
+
+        artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
+        assertEquals( 0, reportDatabase.getNumFailures() );
+        assertEquals( 0, reportDatabase.getNumWarnings() );
+        assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
+    }
+
+    public void testNonSnapshotArtifact()
+    {
+        Artifact artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1" );
+
+        artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
+        assertEquals( 0, reportDatabase.getNumFailures() );
+        assertEquals( 0, reportDatabase.getNumWarnings() );
+        assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
+    }
+
+    public void testNewSnapshotArtifact()
+        throws Exception
+    {
+        File repository = getTestFile( "target/test-repository" );
+
+        File dir = new File( repository, "groupId/artifactId/1.0-alpha-1-SNAPSHOT" );
+        dir.mkdirs();
+
+        String date = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( new Date() );
+        FileUtils.fileWrite( new File( dir, "artifactId-1.0-alpha-1-" + date + "-1.jar" ).getAbsolutePath(), "foo" );
+
+        Artifact artifact =
+            createArtifactFromRepository( repository, "groupId", "artifactId", "1.0-alpha-1-" + date + "-1" );
+
+        artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
+        assertEquals( 0, reportDatabase.getNumFailures() );
+        assertEquals( 0, reportDatabase.getNumWarnings() );
+        assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
+    }
+
+    public void testTooManySnapshotArtifact()
+        throws Exception
+    {
+        File dir = new File( tempRepository, "groupId/artifactId/1.0-alpha-1-SNAPSHOT" );
+        dir.mkdirs();
+
+        String date = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( new Date() );
+        for ( int i = 1; i <= 5; i++ )
+        {
+            FileUtils.fileWrite( new File( dir, "artifactId-1.0-alpha-1-" + date + "-" + i + ".jar" ).getAbsolutePath(),
+                                 "foo" );
+        }
+
+        for ( int i = 1; i <= 5; i++ )
+        {
+            Artifact artifact = createArtifactFromRepository( tempRepository, "groupId", "artifactId",
+                                                              "1.0-alpha-1-" + date + "-" + i );
+            artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
+        }
+
+        assertEquals( 0, reportDatabase.getNumFailures() );
+        assertEquals( 0, reportDatabase.getNumWarnings() );
+        assertEquals( "Check notices", 3, reportDatabase.getNumNotices() );
+        Iterator artifactIterator = reportDatabase.getArtifactIterator();
+        for ( int i = 1; i <= 3; i++ )
+        {
+            String version = "1.0-alpha-1-" + date + "-" + i;
+            Artifact artifact = createArtifactFromRepository( tempRepository, "groupId", "artifactId", version );
+            assertArtifactResults( artifactIterator, artifact );
+        }
+    }
+
+    public void testMissingArtifact()
+        throws Exception
+    {
+        Artifact artifact = createArtifact( "foo", "bar", "XP" );
+
+        try
+        {
+            artifactReportProcessor.processArtifact( artifact, null, reportDatabase );
+            fail( "Should not have passed" );
+        }
+        catch ( IllegalStateException e )
+        {
+            assertTrue( true );
+        }
+    }
+}

Propchange: maven/archiva/trunk/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/archiva/trunk/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.xml
URL: http://svn.apache.org/viewvc/maven/archiva/trunk/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.xml?view=auto&rev=442502
==============================================================================
--- maven/archiva/trunk/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.xml (added)
+++ maven/archiva/trunk/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.xml Tue Sep 12 00:20:42 2006
@@ -0,0 +1,29 @@
+<!--
+  ~ 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.
+  -->
+
+<component-set>
+  <components>
+    <component>
+      <role>org.apache.maven.archiva.reporting.ArtifactReportProcessor</role>
+      <role-hint>old-snapshot-artifact</role-hint>
+      <implementation>org.apache.maven.archiva.reporting.OldSnapshotArtifactReportProcessor</implementation>
+      <configuration>
+        <maxAge>3600</maxAge>
+        <maxSnapshots>2</maxSnapshots>
+      </configuration>
+    </component>
+  </components>
+</component-set>
\ No newline at end of file

Propchange: maven/archiva/trunk/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldSnapshotArtifactReportProcessorTest.xml
------------------------------------------------------------------------------
    svn:eol-style = native