]> source.dussan.org Git - archiva.git/blob
c1223137be0cedd736015a550dd15ff76e211216
[archiva.git] /
1 package org.apache.maven.archiva.reporting;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
20 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
21 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.model.Model;
25 import org.codehaus.plexus.util.FileUtils;
26
27 import java.io.File;
28 import java.util.Collections;
29
30 /**
31  * @author Edwin Punzalan
32  */
33 public class DuplicateArtifactFileReportProcessorTest
34     extends AbstractRepositoryReportsTestCase
35 {
36     private Artifact artifact;
37
38     private Model model;
39
40     private ArtifactReportProcessor processor;
41
42     private ArtifactFactory artifactFactory;
43
44     File indexDirectory;
45
46     protected void setUp()
47         throws Exception
48     {
49         super.setUp();
50
51         indexDirectory = getTestFile( "target/indexDirectory" );
52         FileUtils.deleteDirectory( indexDirectory );
53
54         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
55         artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "jar" );
56         model = new Model();
57
58         RepositoryArtifactIndexFactory factory =
59             (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
60
61         RepositoryArtifactIndex index = factory.createStandardIndex( indexDirectory );
62
63         RepositoryIndexRecordFactory recordFactory =
64             (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
65
66         index.indexRecords( Collections.singletonList( recordFactory.createRecord( artifact ) ) );
67
68         processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "duplicate" );
69     }
70
71     public void testNullArtifactFile()
72         throws Exception
73     {
74         artifact.setFile( null );
75
76         MockArtifactReporter reporter = new MockArtifactReporter();
77
78         processor.processArtifact( model, artifact, reporter, repository );
79
80         assertEquals( "Check no successes", 0, reporter.getSuccesses() );
81         assertEquals( "Check warnings", 1, reporter.getWarnings() );
82         assertEquals( "Check no failures", 0, reporter.getFailures() );
83     }
84
85     public void testSuccessOnAlreadyIndexedArtifact()
86         throws Exception
87     {
88         MockArtifactReporter reporter = new MockArtifactReporter();
89
90         processor.processArtifact( model, artifact, reporter, repository );
91
92         assertEquals( "Check no successes", 1, reporter.getSuccesses() );
93         assertEquals( "Check warnings", 0, reporter.getWarnings() );
94         assertEquals( "Check no failures", 0, reporter.getFailures() );
95     }
96
97     public void testSuccessOnDifferentGroupId()
98         throws Exception
99     {
100         MockArtifactReporter reporter = new MockArtifactReporter();
101
102         artifact.setGroupId( "different.groupId" );
103         processor.processArtifact( model, artifact, reporter, repository );
104
105         assertEquals( "Check no successes", 1, reporter.getSuccesses() );
106         assertEquals( "Check warnings", 0, reporter.getWarnings() );
107         assertEquals( "Check no failures", 0, reporter.getFailures() );
108     }
109
110     public void testSuccessOnNewArtifact()
111         throws Exception
112     {
113         Artifact newArtifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "pom" );
114
115         MockArtifactReporter reporter = new MockArtifactReporter();
116
117         processor.processArtifact( model, newArtifact, reporter, repository );
118
119         assertEquals( "Check no successes", 1, reporter.getSuccesses() );
120         assertEquals( "Check warnings", 0, reporter.getWarnings() );
121         assertEquals( "Check no failures", 0, reporter.getFailures() );
122     }
123
124     public void testFailure()
125         throws Exception
126     {
127         Artifact duplicate = createArtifact( artifact.getGroupId(), "snapshot-artifact", "1.0-alpha-1-SNAPSHOT",
128                                              artifact.getVersion(), artifact.getType() );
129         duplicate.setFile( artifact.getFile() );
130
131         MockArtifactReporter reporter = new MockArtifactReporter();
132
133         processor.processArtifact( model, duplicate, reporter, repository );
134
135         assertEquals( "Check no successes", 0, reporter.getSuccesses() );
136         assertEquals( "Check warnings", 0, reporter.getWarnings() );
137         assertEquals( "Check no failures", 1, reporter.getFailures() );
138     }
139
140     private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
141                                      String type )
142     {
143         Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
144         artifact.setBaseVersion( baseVersion );
145         artifact.setRepository( repository );
146         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
147         return artifact;
148     }
149 }