]> source.dussan.org Git - archiva.git/blob
3ad4bad881ea2c438d642c12242c15f62633c81a
[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     private ReportingDatabase reportDatabase;
47
48     protected void setUp()
49         throws Exception
50     {
51         super.setUp();
52
53         indexDirectory = getTestFile( "target/indexDirectory" );
54         FileUtils.deleteDirectory( indexDirectory );
55
56         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
57         artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "jar" );
58         model = new Model();
59
60         RepositoryArtifactIndexFactory factory =
61             (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
62
63         RepositoryArtifactIndex index = factory.createStandardIndex( indexDirectory );
64
65         RepositoryIndexRecordFactory recordFactory =
66             (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
67
68         index.indexRecords( Collections.singletonList( recordFactory.createRecord( artifact ) ) );
69
70         processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "duplicate" );
71
72         ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
73         reportDatabase = new ReportingDatabase( reportGroup );
74     }
75
76     public void testNullArtifactFile()
77         throws Exception
78     {
79         artifact.setFile( null );
80
81         processor.processArtifact( artifact, model, reportDatabase );
82
83         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
84         assertEquals( "Check warnings", 1, reportDatabase.getNumWarnings() );
85         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
86     }
87
88     public void testSuccessOnAlreadyIndexedArtifact()
89         throws Exception
90     {
91         processor.processArtifact( artifact, model, reportDatabase );
92
93         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
94         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
95         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
96     }
97
98     public void testSuccessOnDifferentGroupId()
99         throws Exception
100     {
101         artifact.setGroupId( "different.groupId" );
102         processor.processArtifact( artifact, model, reportDatabase );
103
104         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
105         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
106         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
107     }
108
109     public void testSuccessOnNewArtifact()
110         throws Exception
111     {
112         Artifact newArtifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "pom" );
113
114         processor.processArtifact( newArtifact, model, reportDatabase );
115
116         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
117         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
118         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
119     }
120
121     public void testFailure()
122         throws Exception
123     {
124         Artifact duplicate = createArtifact( artifact.getGroupId(), "snapshot-artifact", "1.0-alpha-1-SNAPSHOT",
125                                              artifact.getVersion(), artifact.getType() );
126         duplicate.setFile( artifact.getFile() );
127
128         processor.processArtifact( duplicate, model, reportDatabase );
129
130         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
131         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
132         assertEquals( "Check no failures", 1, reportDatabase.getNumFailures() );
133     }
134
135     private Artifact createArtifact( String groupId, String artifactId, String baseVersion, String version,
136                                      String type )
137     {
138         Artifact artifact = artifactFactory.createArtifact( groupId, artifactId, version, null, type );
139         artifact.setBaseVersion( baseVersion );
140         artifact.setRepository( repository );
141         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
142         return artifact;
143     }
144 }