mirror of https://github.com/apache/archiva.git
[MRM-60] add a basic "old artifact" report
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@442459 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
434f84adba
commit
7e1fe8ef3c
|
@ -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" ) );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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" );
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<!--
|
||||
~ 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-artifact</role-hint>
|
||||
<implementation>org.apache.maven.archiva.reporting.OldArtifactReportProcessor</implementation>
|
||||
<configuration>
|
||||
<maxAge>10</maxAge>
|
||||
</configuration>
|
||||
</component>
|
||||
</components>
|
||||
</component-set>
|
Loading…
Reference in New Issue