]> source.dussan.org Git - archiva.git/blob
17b4723fd0b5d4a21b31440718f1655a8a53e13a
[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.artifact.Artifact;
20 import org.apache.maven.model.Dependency;
21 import org.apache.maven.model.Model;
22
23 import java.util.Iterator;
24
25 /**
26  * 
27  */
28 public class ArtifactReportProcessorTest
29     extends AbstractRepositoryReportsTestCase
30 {
31     private static final String EMPTY_STRING = "";
32
33     private static final String VALID = "temp";
34
35     private MockArtifactReporter reporter;
36
37     private Artifact artifact;
38
39     private Model model;
40
41     private DefaultArtifactReportProcessor processor;
42
43     private static final boolean ARTIFACT_FOUND = true;
44
45     private static final boolean ARTIFACT_NOT_FOUND = false;
46
47     protected void setUp()
48         throws Exception
49     {
50         super.setUp();
51         reporter = new MockArtifactReporter();
52         artifact = new MockArtifact();
53         model = new Model();
54         processor = new DefaultArtifactReportProcessor();
55     }
56
57     public void testNullArtifact()
58     {
59         processor.processArtifact( model, null, reporter, null );
60         assertEquals( 0, reporter.getSuccesses() );
61         assertEquals( 1, reporter.getFailures() );
62         assertEquals( 0, reporter.getWarnings() );
63         Iterator failures = reporter.getArtifactFailureIterator();
64         ArtifactResult result = (ArtifactResult) failures.next();
65         assertEquals( ArtifactReporter.NULL_ARTIFACT, result.getReason() );
66     }
67
68     public void testNoProjectDescriptor()
69     {
70         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
71         queryLayer.addReturnValue( ARTIFACT_FOUND );
72         processor.setRepositoryQueryLayer( queryLayer );
73         setRequiredElements( artifact, VALID, VALID, VALID );
74         processor.processArtifact( null, artifact, reporter, null );
75         assertEquals( 1, reporter.getSuccesses() );
76         assertEquals( 1, reporter.getFailures() );
77         assertEquals( 0, reporter.getWarnings() );
78         Iterator failures = reporter.getArtifactFailureIterator();
79         ArtifactResult result = (ArtifactResult) failures.next();
80         assertEquals( ArtifactReporter.NULL_MODEL, result.getReason() );
81     }
82
83     public void testArtifactFoundButNoDirectDependencies()
84     {
85         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
86         queryLayer.addReturnValue( ARTIFACT_FOUND );
87         processor.setRepositoryQueryLayer( queryLayer );
88         setRequiredElements( artifact, VALID, VALID, VALID );
89         processor.processArtifact( model, artifact, reporter, null );
90         assertEquals( 1, reporter.getSuccesses() );
91         assertEquals( 0, reporter.getFailures() );
92         assertEquals( 0, reporter.getWarnings() );
93     }
94
95     public void testArtifactNotFound()
96     {
97         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
98         queryLayer.addReturnValue( ARTIFACT_NOT_FOUND );
99         processor.setRepositoryQueryLayer( queryLayer );
100         setRequiredElements( artifact, VALID, VALID, VALID );
101         processor.processArtifact( model, artifact, reporter, null );
102         assertEquals( 0, reporter.getSuccesses() );
103         assertEquals( 1, reporter.getFailures() );
104         assertEquals( 0, reporter.getWarnings() );
105         Iterator failures = reporter.getArtifactFailureIterator();
106         ArtifactResult result = (ArtifactResult) failures.next();
107         assertEquals( ArtifactReporter.ARTIFACT_NOT_FOUND, result.getReason() );
108     }
109
110     public void testValidArtifactWithNullDependency()
111     {
112         MockArtifactFactory artifactFactory = new MockArtifactFactory();
113         processor.setArtifactFactory( artifactFactory );
114
115         setRequiredElements( artifact, VALID, VALID, VALID );
116         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
117         queryLayer.addReturnValue( ARTIFACT_FOUND );
118
119         Dependency dependency = new Dependency();
120         setRequiredElements( dependency, VALID, VALID, VALID );
121         model.addDependency( dependency );
122         queryLayer.addReturnValue( ARTIFACT_FOUND );
123
124         processor.setRepositoryQueryLayer( queryLayer );
125         processor.processArtifact( model, artifact, reporter, null );
126         assertEquals( 2, reporter.getSuccesses() );
127         assertEquals( 0, reporter.getFailures() );
128         assertEquals( 0, reporter.getWarnings() );
129     }
130
131     public void testValidArtifactWithValidSingleDependency()
132     {
133         MockArtifactFactory artifactFactory = new MockArtifactFactory();
134         processor.setArtifactFactory( artifactFactory );
135
136         setRequiredElements( artifact, VALID, VALID, VALID );
137         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
138         queryLayer.addReturnValue( ARTIFACT_FOUND );
139
140         Dependency dependency = new Dependency();
141         setRequiredElements( dependency, VALID, VALID, VALID );
142         model.addDependency( dependency );
143         queryLayer.addReturnValue( ARTIFACT_FOUND );
144
145         processor.setRepositoryQueryLayer( queryLayer );
146         processor.processArtifact( model, artifact, reporter, null );
147         assertEquals( 2, reporter.getSuccesses() );
148         assertEquals( 0, reporter.getFailures() );
149         assertEquals( 0, reporter.getWarnings() );
150     }
151
152     public void testValidArtifactWithValidMultipleDependencies()
153     {
154         MockArtifactFactory artifactFactory = new MockArtifactFactory();
155         processor.setArtifactFactory( artifactFactory );
156
157         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
158         queryLayer.addReturnValue( ARTIFACT_FOUND );
159
160         Dependency dependency = new Dependency();
161         setRequiredElements( dependency, VALID, VALID, VALID );
162         model.addDependency( dependency );
163         queryLayer.addReturnValue( ARTIFACT_FOUND );
164         model.addDependency( dependency );
165         queryLayer.addReturnValue( ARTIFACT_FOUND );
166         model.addDependency( dependency );
167         queryLayer.addReturnValue( ARTIFACT_FOUND );
168         model.addDependency( dependency );
169         queryLayer.addReturnValue( ARTIFACT_FOUND );
170         model.addDependency( dependency );
171         queryLayer.addReturnValue( ARTIFACT_FOUND );
172
173         setRequiredElements( artifact, VALID, VALID, VALID );
174         processor.setRepositoryQueryLayer( queryLayer );
175         processor.processArtifact( model, artifact, reporter, null );
176         assertEquals( 6, reporter.getSuccesses() );
177         assertEquals( 0, reporter.getFailures() );
178         assertEquals( 0, reporter.getWarnings() );
179     }
180
181     public void testValidArtifactWithAnInvalidDependency()
182     {
183         MockArtifactFactory artifactFactory = new MockArtifactFactory();
184         processor.setArtifactFactory( artifactFactory );
185
186         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
187         queryLayer.addReturnValue( ARTIFACT_FOUND );
188
189         Dependency dependency = new Dependency();
190         setRequiredElements( dependency, VALID, VALID, VALID );
191         model.addDependency( dependency );
192         queryLayer.addReturnValue( ARTIFACT_FOUND );
193         model.addDependency( dependency );
194         queryLayer.addReturnValue( ARTIFACT_FOUND );
195         model.addDependency( dependency );
196         queryLayer.addReturnValue( ARTIFACT_NOT_FOUND );
197         model.addDependency( dependency );
198         queryLayer.addReturnValue( ARTIFACT_FOUND );
199         model.addDependency( dependency );
200         queryLayer.addReturnValue( ARTIFACT_FOUND );
201
202         setRequiredElements( artifact, VALID, VALID, VALID );
203         processor.setRepositoryQueryLayer( queryLayer );
204         processor.processArtifact( model, artifact, reporter, null );
205         assertEquals( 5, reporter.getSuccesses() );
206         assertEquals( 1, reporter.getFailures() );
207         assertEquals( 0, reporter.getWarnings() );
208
209         Iterator failures = reporter.getArtifactFailureIterator();
210         ArtifactResult result = (ArtifactResult) failures.next();
211         assertEquals( ArtifactReporter.DEPENDENCY_NOT_FOUND, result.getReason() );
212     }
213
214     public void testEmptyGroupId()
215     {
216         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
217         queryLayer.addReturnValue( ARTIFACT_FOUND );
218         processor.setRepositoryQueryLayer( queryLayer );
219
220         setRequiredElements( artifact, EMPTY_STRING, VALID, VALID );
221         processor.processArtifact( model, artifact, reporter, null );
222         assertEquals( 0, reporter.getSuccesses() );
223         assertEquals( 1, reporter.getFailures() );
224         assertEquals( 0, reporter.getWarnings() );
225
226         Iterator failures = reporter.getArtifactFailureIterator();
227         ArtifactResult result = (ArtifactResult) failures.next();
228         assertEquals( ArtifactReporter.EMPTY_GROUP_ID, result.getReason() );
229     }
230
231     public void testEmptyArtifactId()
232     {
233         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
234         queryLayer.addReturnValue( ARTIFACT_FOUND );
235         processor.setRepositoryQueryLayer( queryLayer );
236
237         setRequiredElements( artifact, VALID, EMPTY_STRING, VALID );
238         processor.processArtifact( model, artifact, reporter, null );
239         assertEquals( 0, reporter.getSuccesses() );
240         assertEquals( 1, reporter.getFailures() );
241         assertEquals( 0, reporter.getWarnings() );
242
243         Iterator failures = reporter.getArtifactFailureIterator();
244         ArtifactResult result = (ArtifactResult) failures.next();
245         assertEquals( ArtifactReporter.EMPTY_ARTIFACT_ID, result.getReason() );
246     }
247
248     public void testEmptyVersion()
249     {
250         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
251         queryLayer.addReturnValue( ARTIFACT_FOUND );
252         processor.setRepositoryQueryLayer( queryLayer );
253
254         setRequiredElements( artifact, VALID, VALID, EMPTY_STRING );
255         processor.processArtifact( model, artifact, reporter, null );
256         assertEquals( 0, reporter.getSuccesses() );
257         assertEquals( 1, reporter.getFailures() );
258         assertEquals( 0, reporter.getWarnings() );
259
260         Iterator failures = reporter.getArtifactFailureIterator();
261         ArtifactResult result = (ArtifactResult) failures.next();
262         assertEquals( ArtifactReporter.EMPTY_VERSION, result.getReason() );
263     }
264
265     public void testNullGroupId()
266     {
267         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
268         queryLayer.addReturnValue( ARTIFACT_FOUND );
269         processor.setRepositoryQueryLayer( queryLayer );
270
271         setRequiredElements( artifact, null, VALID, VALID );
272         processor.processArtifact( model, artifact, reporter, null );
273         assertEquals( 0, reporter.getSuccesses() );
274         assertEquals( 1, reporter.getFailures() );
275         assertEquals( 0, reporter.getWarnings() );
276
277         Iterator failures = reporter.getArtifactFailureIterator();
278         ArtifactResult result = (ArtifactResult) failures.next();
279         assertEquals( ArtifactReporter.EMPTY_GROUP_ID, result.getReason() );
280     }
281
282     public void testNullArtifactId()
283     {
284         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
285         queryLayer.addReturnValue( ARTIFACT_FOUND );
286         processor.setRepositoryQueryLayer( queryLayer );
287
288         setRequiredElements( artifact, VALID, null, VALID );
289         processor.processArtifact( model, artifact, reporter, null );
290         assertEquals( 0, reporter.getSuccesses() );
291         assertEquals( 1, reporter.getFailures() );
292         assertEquals( 0, reporter.getWarnings() );
293
294         Iterator failures = reporter.getArtifactFailureIterator();
295         ArtifactResult result = (ArtifactResult) failures.next();
296         assertEquals( ArtifactReporter.EMPTY_ARTIFACT_ID, result.getReason() );
297     }
298
299     public void testNullVersion()
300     {
301         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
302         queryLayer.addReturnValue( ARTIFACT_FOUND );
303         processor.setRepositoryQueryLayer( queryLayer );
304
305         setRequiredElements( artifact, VALID, VALID, null );
306         processor.processArtifact( model, artifact, reporter, null );
307         assertEquals( 0, reporter.getSuccesses() );
308         assertEquals( 1, reporter.getFailures() );
309         assertEquals( 0, reporter.getWarnings() );
310
311         Iterator failures = reporter.getArtifactFailureIterator();
312         ArtifactResult result = (ArtifactResult) failures.next();
313         assertEquals( ArtifactReporter.EMPTY_VERSION, result.getReason() );
314     }
315
316     public void testMultipleFailures()
317     {
318         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
319         queryLayer.addReturnValue( ARTIFACT_FOUND );
320         processor.setRepositoryQueryLayer( queryLayer );
321
322         setRequiredElements( artifact, null, null, null );
323         processor.processArtifact( model, artifact, reporter, null );
324         assertEquals( 0, reporter.getSuccesses() );
325         assertEquals( 3, reporter.getFailures() );
326         assertEquals( 0, reporter.getWarnings() );
327
328         Iterator failures = reporter.getArtifactFailureIterator();
329         ArtifactResult result = (ArtifactResult) failures.next();
330         assertEquals( ArtifactReporter.EMPTY_GROUP_ID, result.getReason() );
331         result = (ArtifactResult) failures.next();
332         assertEquals( ArtifactReporter.EMPTY_ARTIFACT_ID, result.getReason() );
333         result = (ArtifactResult) failures.next();
334         assertEquals( ArtifactReporter.EMPTY_VERSION, result.getReason() );
335     }
336
337     public void testValidArtifactWithInvalidDependencyGroupId()
338     {
339         MockArtifactFactory artifactFactory = new MockArtifactFactory();
340         processor.setArtifactFactory( artifactFactory );
341
342         setRequiredElements( artifact, VALID, VALID, VALID );
343         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
344         queryLayer.addReturnValue( ARTIFACT_FOUND );
345
346         Dependency dependency = new Dependency();
347         setRequiredElements( dependency, null, VALID, VALID );
348         model.addDependency( dependency );
349         queryLayer.addReturnValue( ARTIFACT_FOUND );
350
351         processor.setRepositoryQueryLayer( queryLayer );
352         processor.processArtifact( model, artifact, reporter, null );
353         assertEquals( 1, reporter.getSuccesses() );
354         assertEquals( 1, reporter.getFailures() );
355         assertEquals( 0, reporter.getWarnings() );
356
357         Iterator failures = reporter.getArtifactFailureIterator();
358         ArtifactResult result = (ArtifactResult) failures.next();
359         assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_GROUP_ID, result.getReason() );
360     }
361
362     public void testValidArtifactWithInvalidDependencyArtifactId()
363     {
364         MockArtifactFactory artifactFactory = new MockArtifactFactory();
365         processor.setArtifactFactory( artifactFactory );
366
367         setRequiredElements( artifact, VALID, VALID, VALID );
368         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
369         queryLayer.addReturnValue( ARTIFACT_FOUND );
370
371         Dependency dependency = new Dependency();
372         setRequiredElements( dependency, VALID, null, VALID );
373         model.addDependency( dependency );
374         queryLayer.addReturnValue( ARTIFACT_FOUND );
375
376         processor.setRepositoryQueryLayer( queryLayer );
377         processor.processArtifact( model, artifact, reporter, null );
378         assertEquals( 1, reporter.getSuccesses() );
379         assertEquals( 1, reporter.getFailures() );
380         assertEquals( 0, reporter.getWarnings() );
381
382         Iterator failures = reporter.getArtifactFailureIterator();
383         ArtifactResult result = (ArtifactResult) failures.next();
384         assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_ARTIFACT_ID, result.getReason() );
385     }
386
387     public void testValidArtifactWithInvalidDependencyVersion()
388     {
389         MockArtifactFactory artifactFactory = new MockArtifactFactory();
390         processor.setArtifactFactory( artifactFactory );
391
392         setRequiredElements( artifact, VALID, VALID, VALID );
393         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
394         queryLayer.addReturnValue( ARTIFACT_FOUND );
395
396         Dependency dependency = new Dependency();
397         setRequiredElements( dependency, VALID, VALID, null );
398         model.addDependency( dependency );
399         queryLayer.addReturnValue( ARTIFACT_FOUND );
400
401         processor.setRepositoryQueryLayer( queryLayer );
402         processor.processArtifact( model, artifact, reporter, null );
403         assertEquals( 1, reporter.getSuccesses() );
404         assertEquals( 1, reporter.getFailures() );
405         assertEquals( 0, reporter.getWarnings() );
406
407         Iterator failures = reporter.getArtifactFailureIterator();
408         ArtifactResult result = (ArtifactResult) failures.next();
409         assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_VERSION, result.getReason() );
410     }
411
412     public void testValidArtifactWithInvalidDependencyRequiredElements()
413     {
414         MockArtifactFactory artifactFactory = new MockArtifactFactory();
415         processor.setArtifactFactory( artifactFactory );
416
417         setRequiredElements( artifact, VALID, VALID, VALID );
418         MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
419         queryLayer.addReturnValue( ARTIFACT_FOUND );
420
421         Dependency dependency = new Dependency();
422         setRequiredElements( dependency, null, null, null );
423         model.addDependency( dependency );
424         queryLayer.addReturnValue( ARTIFACT_FOUND );
425
426         processor.setRepositoryQueryLayer( queryLayer );
427         processor.processArtifact( model, artifact, reporter, null );
428         assertEquals( 1, reporter.getSuccesses() );
429         assertEquals( 3, reporter.getFailures() );
430         assertEquals( 0, reporter.getWarnings() );
431
432         Iterator failures = reporter.getArtifactFailureIterator();
433         ArtifactResult result = (ArtifactResult) failures.next();
434         assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_GROUP_ID, result.getReason() );
435         result = (ArtifactResult) failures.next();
436         assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_ARTIFACT_ID, result.getReason() );
437         result = (ArtifactResult) failures.next();
438         assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_VERSION, result.getReason() );
439     }
440
441     protected void tearDown()
442         throws Exception
443     {
444         model = null;
445         artifact = null;
446         reporter = null;
447         super.tearDown();
448     }
449
450     private void setRequiredElements( Artifact artifact, String groupId, String artifactId, String version )
451     {
452         artifact.setGroupId( groupId );
453         artifact.setArtifactId( artifactId );
454         artifact.setVersion( version );
455     }
456
457     private void setRequiredElements( Dependency dependency, String groupId, String artifactId, String version )
458     {
459         dependency.setGroupId( groupId );
460         dependency.setArtifactId( artifactId );
461         dependency.setVersion( version );
462     }
463 }