]> source.dussan.org Git - archiva.git/blob
70d617263c9717673864c8a83c72666c143d5173
[archiva.git] /
1 package org.apache.maven.repository.reporting;
2
3 import org.apache.maven.artifact.Artifact;
4 import org.apache.maven.artifact.factory.ArtifactFactory;
5 import org.apache.maven.model.Model;
6 import org.apache.maven.repository.digest.DefaultDigester;
7 import org.apache.maven.repository.indexing.ArtifactRepositoryIndex;
8 import org.codehaus.plexus.util.FileUtils;
9
10 import java.io.File;
11
12 /**
13  * @author Edwin Punzalan
14  */
15 public class DuplicateArtifactFileReportProcessorTest
16     extends AbstractRepositoryReportsTestCase
17 {
18     private MockArtifactReporter reporter;
19
20     private Artifact artifact;
21
22     private Model model;
23
24     private DuplicateArtifactFileReportProcessor processor;
25
26     private ArtifactFactory artifactFactory;
27
28     private String indexPath = new File( "target/.index" ).getAbsolutePath();
29
30     protected void setUp()
31         throws Exception
32     {
33         super.setUp();
34         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.class.getName() );
35         reporter = new MockArtifactReporter();
36         artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "jar" );
37         model = new Model();
38         processor = new DuplicateArtifactFileReportProcessor();
39         processor.setArtifactFactory( artifactFactory );
40
41         ArtifactRepositoryIndex index = new ArtifactRepositoryIndex( indexPath, repository, new DefaultDigester() );
42         index.indexArtifact( artifact );
43         index.optimize();
44         index.close();
45     }
46
47     protected void tearDown()
48         throws Exception
49     {
50         FileUtils.deleteDirectory( indexPath );
51
52         processor = null;
53         model = null;
54         artifact = null;
55         reporter = null;
56         super.tearDown();
57     }
58
59     public void testNullArtifactFile()
60         throws Exception
61     {
62         artifact.setFile( null );
63
64         processor.processArtifact( model, artifact, reporter, repository );
65
66         assertEquals( "Check no successes", 0, reporter.getSuccesses() );
67         assertEquals( "Check warnings", 1, reporter.getWarnings() );
68         assertEquals( "Check no failures", 0, reporter.getFailures() );
69     }
70
71     public void testSuccessOnAlreadyIndexedArtifact()
72         throws Exception
73     {
74         processor.processArtifact( model, artifact, reporter, repository );
75
76         assertEquals( "Check no successes", 1, reporter.getSuccesses() );
77         assertEquals( "Check warnings", 0, reporter.getWarnings() );
78         assertEquals( "Check no failures", 0, reporter.getFailures() );
79     }
80
81     public void testSuccessOnDifferentGroupId()
82         throws Exception
83     {
84         artifact.setGroupId( "different.groupId" );
85         processor.processArtifact( model, artifact, reporter, repository );
86
87         assertEquals( "Check no successes", 1, reporter.getSuccesses() );
88         assertEquals( "Check warnings", 0, reporter.getWarnings() );
89         assertEquals( "Check no failures", 0, reporter.getFailures() );
90     }
91
92     public void testSuccessOnNewArtifact()
93         throws Exception
94     {
95         Artifact newArtifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "pom" );
96
97         processor.processArtifact( model, newArtifact, reporter, repository );
98
99         assertEquals( "Check no successes", 1, reporter.getSuccesses() );
100         assertEquals( "Check warnings", 0, reporter.getWarnings() );
101         assertEquals( "Check no failures", 0, reporter.getFailures() );
102     }
103
104     public void testFailure()
105         throws Exception
106     {
107         Artifact duplicate = createArtifact( artifact.getGroupId(), "snapshot-artifact", "1.0-alpha-1-SNAPSHOT",
108                                              artifact.getVersion(), artifact.getType() );
109         duplicate.setFile( artifact.getFile() );
110
111         processor.processArtifact( model, duplicate, reporter, repository );
112
113         assertEquals( "Check no successes", 0, reporter.getSuccesses() );
114         assertEquals( "Check warnings", 0, reporter.getWarnings() );
115         assertEquals( "Check no failures", 1, reporter.getFailures() );
116     }
117
118     private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
119                                      String type )
120     {
121         Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
122         artifact.setBaseVersion( baseVersion );
123         artifact.setRepository( repository );
124         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
125         return artifact;
126     }
127 }