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