]> source.dussan.org Git - archiva.git/blob
4800e22d286d0402998a2bb87c9c485da4873e23
[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.reporting.model.ArtifactResults;
20 import org.apache.maven.archiva.reporting.model.Result;
21 import org.apache.maven.archiva.reporting.group.ReportGroup;
22 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
23 import org.apache.maven.archiva.reporting.processor.DependencyArtifactReportProcessor;
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.Dependency;
29 import org.apache.maven.model.Model;
30
31 import java.util.Iterator;
32
33 /**
34  *
35  */
36 public class DependencyArtifactReportProcessorTest
37     extends AbstractRepositoryReportsTestCase
38 {
39     private static final String VALID_GROUP_ID = "groupId";
40
41     private static final String VALID_ARTIFACT_ID = "artifactId";
42
43     private static final String VALID_VERSION = "1.0-alpha-1";
44
45     private ReportingDatabase reportingDatabase;
46
47     private Model model;
48
49     private ArtifactReportProcessor processor;
50
51     private ArtifactFactory artifactFactory;
52
53     private static final String INVALID = "invalid";
54
55     protected void setUp()
56         throws Exception
57     {
58         super.setUp();
59         model = new Model();
60         processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "dependency" );
61
62         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
63
64         ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
65         reportingDatabase = new ReportingDatabase( reportGroup );
66     }
67
68     public void testArtifactFoundButNoDirectDependencies()
69     {
70         Artifact artifact = createValidArtifact();
71         processor.processArtifact( artifact, model, reportingDatabase );
72         assertEquals( 0, reportingDatabase.getNumFailures() );
73         assertEquals( 0, reportingDatabase.getNumWarnings() );
74         assertEquals( 0, reportingDatabase.getNumNotices() );
75     }
76
77     private Artifact createValidArtifact()
78     {
79         Artifact projectArtifact =
80             artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
81         projectArtifact.setRepository( repository );
82         return projectArtifact;
83     }
84
85     public void testArtifactNotFound()
86     {
87         Artifact artifact = artifactFactory.createProjectArtifact( INVALID, INVALID, INVALID );
88         artifact.setRepository( repository );
89         processor.processArtifact( artifact, model, reportingDatabase );
90         assertEquals( 1, reportingDatabase.getNumFailures() );
91         assertEquals( 0, reportingDatabase.getNumWarnings() );
92         assertEquals( 0, reportingDatabase.getNumNotices() );
93         Iterator failures = reportingDatabase.getArtifactIterator();
94         ArtifactResults results = (ArtifactResults) failures.next();
95         assertFalse( failures.hasNext() );
96         failures = results.getFailures().iterator();
97         Result result = (Result) failures.next();
98         assertEquals( "Artifact does not exist in the repository", result.getReason() );
99     }
100
101     public void testValidArtifactWithNullDependency()
102     {
103         Artifact artifact = createValidArtifact();
104
105         Dependency dependency = createValidDependency();
106         model.addDependency( dependency );
107
108         processor.processArtifact( artifact, model, reportingDatabase );
109         assertEquals( 0, reportingDatabase.getNumFailures() );
110         assertEquals( 0, reportingDatabase.getNumWarnings() );
111         assertEquals( 0, reportingDatabase.getNumNotices() );
112     }
113
114     private Dependency createValidDependency()
115     {
116         return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
117     }
118
119     public void testValidArtifactWithValidSingleDependency()
120     {
121         Artifact artifact = createValidArtifact();
122
123         Dependency dependency = createValidDependency();
124         model.addDependency( dependency );
125
126         processor.processArtifact( artifact, model, reportingDatabase );
127         assertEquals( 0, reportingDatabase.getNumFailures() );
128         assertEquals( 0, reportingDatabase.getNumWarnings() );
129         assertEquals( 0, reportingDatabase.getNumNotices() );
130     }
131
132     public void testValidArtifactWithValidMultipleDependencies()
133     {
134         Dependency dependency = createValidDependency();
135         model.addDependency( dependency );
136         model.addDependency( dependency );
137         model.addDependency( dependency );
138         model.addDependency( dependency );
139         model.addDependency( dependency );
140
141         Artifact artifact = createValidArtifact();
142         processor.processArtifact( artifact, model, reportingDatabase );
143         assertEquals( 0, reportingDatabase.getNumFailures() );
144         assertEquals( 0, reportingDatabase.getNumWarnings() );
145         assertEquals( 0, reportingDatabase.getNumNotices() );
146     }
147
148     public void testValidArtifactWithAnInvalidDependency()
149     {
150         Dependency dependency = createValidDependency();
151         model.addDependency( dependency );
152         model.addDependency( dependency );
153         model.addDependency( dependency );
154         model.addDependency( dependency );
155         model.addDependency( createDependency( INVALID, INVALID, INVALID ) );
156
157         Artifact artifact = createValidArtifact();
158         processor.processArtifact( artifact, model, reportingDatabase );
159         assertEquals( 1, reportingDatabase.getNumFailures() );
160         assertEquals( 0, reportingDatabase.getNumWarnings() );
161         assertEquals( 0, reportingDatabase.getNumNotices() );
162
163         Iterator failures = reportingDatabase.getArtifactIterator();
164         ArtifactResults results = (ArtifactResults) failures.next();
165         assertFalse( failures.hasNext() );
166         failures = results.getFailures().iterator();
167         Result result = (Result) failures.next();
168         assertEquals( getDependencyNotFoundMessage( createDependency( INVALID, INVALID, INVALID ) ),
169                       result.getReason() );
170     }
171
172     public void testValidArtifactWithInvalidDependencyGroupId()
173     {
174         Artifact artifact = createValidArtifact();
175
176         Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
177         model.addDependency( dependency );
178
179         processor.processArtifact( artifact, model, reportingDatabase );
180         assertEquals( 1, reportingDatabase.getNumFailures() );
181         assertEquals( 0, reportingDatabase.getNumWarnings() );
182         assertEquals( 0, reportingDatabase.getNumNotices() );
183
184         Iterator failures = reportingDatabase.getArtifactIterator();
185         ArtifactResults results = (ArtifactResults) failures.next();
186         assertFalse( failures.hasNext() );
187         failures = results.getFailures().iterator();
188         Result result = (Result) failures.next();
189         assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
190     }
191
192     private Dependency createDependency( String o, String valid, String s )
193     {
194         Dependency dependency = new Dependency();
195         dependency.setGroupId( o );
196         dependency.setArtifactId( valid );
197         dependency.setVersion( s );
198         return dependency;
199     }
200
201     public void testValidArtifactWithInvalidDependencyArtifactId()
202     {
203         Artifact artifact = createValidArtifact();
204
205         Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
206         model.addDependency( dependency );
207
208         processor.processArtifact( artifact, model, reportingDatabase );
209         assertEquals( 1, reportingDatabase.getNumFailures() );
210         assertEquals( 0, reportingDatabase.getNumWarnings() );
211         assertEquals( 0, reportingDatabase.getNumNotices() );
212
213         Iterator failures = reportingDatabase.getArtifactIterator();
214         ArtifactResults results = (ArtifactResults) failures.next();
215         assertFalse( failures.hasNext() );
216         failures = results.getFailures().iterator();
217         Result result = (Result) failures.next();
218         assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
219     }
220
221     public void testValidArtifactWithIncorrectDependencyVersion()
222     {
223         Artifact artifact = createValidArtifact();
224
225         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
226         model.addDependency( dependency );
227
228         processor.processArtifact( artifact, model, reportingDatabase );
229         assertEquals( 1, reportingDatabase.getNumFailures() );
230         assertEquals( 0, reportingDatabase.getNumWarnings() );
231
232         Iterator failures = reportingDatabase.getArtifactIterator();
233         ArtifactResults results = (ArtifactResults) failures.next();
234         assertFalse( failures.hasNext() );
235         failures = results.getFailures().iterator();
236         Result result = (Result) failures.next();
237         assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
238     }
239
240     public void testValidArtifactWithInvalidDependencyVersion()
241     {
242         Artifact artifact = createValidArtifact();
243
244         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
245         model.addDependency( dependency );
246
247         processor.processArtifact( artifact, model, reportingDatabase );
248         assertEquals( 1, reportingDatabase.getNumFailures() );
249         assertEquals( 0, reportingDatabase.getNumWarnings() );
250         assertEquals( 0, reportingDatabase.getNumNotices() );
251
252         Iterator failures = reportingDatabase.getArtifactIterator();
253         ArtifactResults results = (ArtifactResults) failures.next();
254         assertFalse( failures.hasNext() );
255         failures = results.getFailures().iterator();
256         Result result = (Result) failures.next();
257         assertEquals( getDependencyVersionInvalidMessage( dependency, "[" ), result.getReason() );
258     }
259
260     public void testValidArtifactWithInvalidDependencyVersionRange()
261     {
262         Artifact artifact = createValidArtifact();
263
264         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
265         model.addDependency( dependency );
266
267         processor.processArtifact( artifact, model, reportingDatabase );
268         assertEquals( 0, reportingDatabase.getNumFailures() );
269         assertEquals( 0, reportingDatabase.getNumWarnings() );
270         assertEquals( 0, reportingDatabase.getNumNotices() );
271     }
272
273     public void testValidArtifactWithMissingDependencyVersion()
274     {
275         Artifact artifact = createValidArtifact();
276
277         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
278         model.addDependency( dependency );
279
280         processor.processArtifact( artifact, model, reportingDatabase );
281         assertEquals( 1, reportingDatabase.getNumFailures() );
282         assertEquals( 0, reportingDatabase.getNumWarnings() );
283         assertEquals( 0, reportingDatabase.getNumNotices() );
284
285         Iterator failures = reportingDatabase.getArtifactIterator();
286         ArtifactResults results = (ArtifactResults) failures.next();
287         assertFalse( failures.hasNext() );
288         failures = results.getFailures().iterator();
289         Result result = (Result) failures.next();
290         assertEquals( getDependencyVersionInvalidMessage( dependency, null ), result.getReason() );
291     }
292
293     private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
294     {
295         return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
296     }
297
298     private static String getDependencyString( Dependency dependency )
299     {
300         return DependencyArtifactReportProcessor.getDependencyString( dependency );
301     }
302
303     private String getDependencyNotFoundMessage( Dependency dependency )
304     {
305         return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";
306     }
307 }