]> source.dussan.org Git - archiva.git/blob
107d614efc69e4a529fb75854f47d522a4775e68
[archiva.git] /
1 package org.apache.maven.archiva.reporting.processor;
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.archiva.reporting.group.ReportGroup;
23 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
24 import org.apache.maven.archiva.reporting.AbstractRepositoryReportsTestCase;
25 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import org.apache.maven.model.Model;
29 import org.apache.commons.io.FileUtils;
30
31 import java.io.File;
32 import java.util.Collections;
33
34 /**
35  * @author Edwin Punzalan
36  */
37 public class DuplicateArtifactFileReportProcessorTest
38     extends AbstractRepositoryReportsTestCase
39 {
40     private Artifact artifact;
41
42     private Model model;
43
44     private ArtifactReportProcessor processor;
45
46     private ArtifactFactory artifactFactory;
47
48     File indexDirectory;
49
50     private ReportingDatabase reportDatabase;
51
52     protected void setUp()
53         throws Exception
54     {
55         super.setUp();
56
57         indexDirectory = getTestFile( "target/indexDirectory" );
58         FileUtils.deleteDirectory( indexDirectory );
59
60         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
61         artifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "jar" );
62         System.out.println( "artifact = " + artifact );
63         model = new Model();
64
65         RepositoryArtifactIndexFactory factory =
66             (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
67
68         RepositoryArtifactIndex index = factory.createStandardIndex( indexDirectory );
69
70         RepositoryIndexRecordFactory recordFactory =
71             (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
72
73         index.indexRecords( Collections.singletonList( recordFactory.createRecord( artifact ) ) );
74
75         processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "duplicate" );
76
77         ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
78         reportDatabase = new ReportingDatabase( reportGroup );
79     }
80
81     public void testNullArtifactFile()
82         throws Exception
83     {
84         artifact.setFile( null );
85
86         processor.processArtifact( artifact, model, reportDatabase );
87
88         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
89         assertEquals( "Check warnings", 1, reportDatabase.getNumWarnings() );
90         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
91     }
92
93     public void testSuccessOnAlreadyIndexedArtifact()
94         throws Exception
95     {
96         processor.processArtifact( artifact, model, reportDatabase );
97
98         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
99         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
100         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
101     }
102
103     public void testSuccessOnDifferentGroupId()
104         throws Exception
105     {
106         artifact.setGroupId( "different.groupId" );
107         processor.processArtifact( artifact, model, reportDatabase );
108
109         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
110         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
111         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
112     }
113
114     public void testSuccessOnNewArtifact()
115         throws Exception
116     {
117         Artifact newArtifact = createArtifact( "groupId", "artifactId", "1.0-alpha-1", "1.0-alpha-1", "pom" );
118
119         processor.processArtifact( newArtifact, model, reportDatabase );
120
121         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
122         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
123         assertEquals( "Check no failures", 0, reportDatabase.getNumFailures() );
124     }
125
126     public void testFailure()
127         throws Exception
128     {
129         Artifact duplicate = createArtifact( artifact.getGroupId(), "snapshot-artifact", "1.0-alpha-1-SNAPSHOT",
130                                              artifact.getVersion(), artifact.getType() );
131         duplicate.setFile( artifact.getFile() );
132
133         processor.processArtifact( duplicate, model, reportDatabase );
134
135         assertEquals( "Check warnings", 0, reportDatabase.getNumWarnings() );
136         assertEquals( "Check no notices", 0, reportDatabase.getNumNotices() );
137         assertEquals( "Check no failures", 1, reportDatabase.getNumFailures() );
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 }