diff --git a/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java b/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java new file mode 100644 index 000000000..f7b9f82ff --- /dev/null +++ b/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessor.java @@ -0,0 +1,88 @@ +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; + +/** + * Find artifacts in the repository that are considered old. + * + * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="old-artifact" + */ +public class OldArtifactReportProcessor + implements ArtifactReportProcessor +{ + private static final String ROLE_HINT = "old-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; + + public void processArtifact( 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 ( System.currentTimeMillis() - file.lastModified() > maxAge * 1000 ) + { + // TODO: reason could be an i18n key derived from the processor and the problem ID and the + reporter.addNotice( artifact, ROLE_HINT, "old-artifact", + "The artifact is older than the maximum age of " + maxAge + " seconds." ); + } + } + else + { + throw new IllegalStateException( "Couldn't find artifact " + file ); + } + } + + 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" ) ); + } + } +} diff --git a/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java b/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java index 6d6d08d26..bac57c5f7 100644 --- a/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java +++ b/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/AbstractRepositoryReportsTestCase.java @@ -38,20 +38,37 @@ public abstract class AbstractRepositoryReportsTestCase private ArtifactFactory artifactFactory; + private ArtifactRepositoryFactory factory; + + private ArtifactRepositoryLayout layout; + protected void setUp() throws Exception { super.setUp(); + File repositoryDirectory = getTestFile( "src/test/repository" ); - ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); repository = factory.createArtifactRepository( "repository", repositoryDirectory.toURL().toString(), layout, null, null ); artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); } + protected Artifact createArtifactFromRepository( File repository, String groupId, String artifactId, + String version ) + throws Exception + { + Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); + + artifact.setRepository( + factory.createArtifactRepository( "repository", repository.toURL().toString(), layout, null, null ) ); + + return artifact; + } + protected Artifact createArtifact( String groupId, String artifactId, String version ) { return createArtifact( groupId, artifactId, version, "jar" ); diff --git a/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessorTest.java b/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessorTest.java new file mode 100644 index 000000000..f1c5073d9 --- /dev/null +++ b/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/OldArtifactReportProcessorTest.java @@ -0,0 +1,96 @@ +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.util.Iterator; + +/** + * This class tests the OldArtifactReportProcessor. + */ +public class OldArtifactReportProcessorTest + extends AbstractRepositoryReportsTestCase +{ + private ArtifactReportProcessor artifactReportProcessor; + + private ReportingDatabase reportDatabase; + + public void setUp() + throws Exception + { + super.setUp(); + artifactReportProcessor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "old-artifact" ); + + ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "old-artifact" ); + reportDatabase = new ReportingDatabase( reportGroup ); + } + + public void testOldArtifact() + { + Artifact artifact = createArtifact( "org.apache.maven", "maven-model", "2.0" ); + + artifactReportProcessor.processArtifact( artifact, null, reportDatabase ); + assertEquals( 0, reportDatabase.getNumFailures() ); + assertEquals( 0, reportDatabase.getNumWarnings() ); + assertEquals( "Check notices", 1, reportDatabase.getNumNotices() ); + ArtifactResults results = (ArtifactResults) reportDatabase.getArtifactIterator().next(); + assertEquals( artifact.getArtifactId(), results.getArtifactId() ); + assertEquals( artifact.getGroupId(), results.getGroupId() ); + assertEquals( artifact.getVersion(), results.getVersion() ); + assertEquals( 1, results.getNotices().size() ); + Iterator i = results.getNotices().iterator(); + Result result = (Result) i.next(); + assertEquals( "old-artifact", result.getProcessor() ); + } + + public void testNewArtifact() + throws Exception + { + File repository = getTestFile( "target/test-repository" ); + + FileUtils.copyDirectoryStructure( getTestFile( "src/test/repository/groupId" ), + new File( repository, "groupId" ) ); + + Artifact artifact = createArtifactFromRepository( repository, "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 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 ); + } + } +} diff --git a/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldArtifactReportProcessorTest.xml b/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldArtifactReportProcessorTest.xml new file mode 100644 index 000000000..caee1840a --- /dev/null +++ b/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/OldArtifactReportProcessorTest.xml @@ -0,0 +1,28 @@ + + + + + + org.apache.maven.archiva.reporting.ArtifactReportProcessor + old-artifact + org.apache.maven.archiva.reporting.OldArtifactReportProcessor + + 10 + + + + \ No newline at end of file