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