1 package org.apache.maven.archiva.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.model.Dependency;
21 import org.apache.maven.model.Model;
23 import java.util.Iterator;
28 public class ArtifactReportProcessorTest
29 extends AbstractRepositoryReportsTestCase
31 private static final String EMPTY_STRING = "";
33 private static final String VALID = "temp";
35 private ArtifactReporter reporter;
37 private Artifact artifact;
41 private DefaultArtifactReportProcessor processor;
43 private static final boolean ARTIFACT_FOUND = true;
45 private static final boolean ARTIFACT_NOT_FOUND = false;
47 protected void setUp()
51 reporter = new DefaultArtifactReporter();
52 artifact = new MockArtifact();
54 processor = new DefaultArtifactReportProcessor();
57 public void testNullArtifact()
59 processor.processArtifact( model, null, reporter, null );
60 assertEquals( 0, reporter.getNumSuccesses() );
61 assertEquals( 1, reporter.getNumFailures() );
62 assertEquals( 0, reporter.getNumWarnings() );
63 Iterator failures = reporter.getArtifactFailureIterator();
64 ArtifactResult result = (ArtifactResult) failures.next();
65 assertEquals( ArtifactReporter.NULL_ARTIFACT, result.getReason() );
68 public void testNoProjectDescriptor()
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.getNumSuccesses() );
76 assertEquals( 1, reporter.getNumFailures() );
77 assertEquals( 0, reporter.getNumWarnings() );
78 Iterator failures = reporter.getArtifactFailureIterator();
79 ArtifactResult result = (ArtifactResult) failures.next();
80 assertEquals( ArtifactReporter.NULL_MODEL, result.getReason() );
83 public void testArtifactFoundButNoDirectDependencies()
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.getNumSuccesses() );
91 assertEquals( 0, reporter.getNumFailures() );
92 assertEquals( 0, reporter.getNumWarnings() );
95 public void testArtifactNotFound()
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.getNumSuccesses() );
103 assertEquals( 1, reporter.getNumFailures() );
104 assertEquals( 0, reporter.getNumWarnings() );
105 Iterator failures = reporter.getArtifactFailureIterator();
106 ArtifactResult result = (ArtifactResult) failures.next();
107 assertEquals( ArtifactReporter.ARTIFACT_NOT_FOUND, result.getReason() );
110 public void testValidArtifactWithNullDependency()
112 MockArtifactFactory artifactFactory = new MockArtifactFactory();
113 processor.setArtifactFactory( artifactFactory );
115 setRequiredElements( artifact, VALID, VALID, VALID );
116 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
117 queryLayer.addReturnValue( ARTIFACT_FOUND );
119 Dependency dependency = new Dependency();
120 setRequiredElements( dependency, VALID, VALID, VALID );
121 model.addDependency( dependency );
122 queryLayer.addReturnValue( ARTIFACT_FOUND );
124 processor.setRepositoryQueryLayer( queryLayer );
125 processor.processArtifact( model, artifact, reporter, null );
126 assertEquals( 2, reporter.getNumSuccesses() );
127 assertEquals( 0, reporter.getNumFailures() );
128 assertEquals( 0, reporter.getNumWarnings() );
131 public void testValidArtifactWithValidSingleDependency()
133 MockArtifactFactory artifactFactory = new MockArtifactFactory();
134 processor.setArtifactFactory( artifactFactory );
136 setRequiredElements( artifact, VALID, VALID, VALID );
137 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
138 queryLayer.addReturnValue( ARTIFACT_FOUND );
140 Dependency dependency = new Dependency();
141 setRequiredElements( dependency, VALID, VALID, VALID );
142 model.addDependency( dependency );
143 queryLayer.addReturnValue( ARTIFACT_FOUND );
145 processor.setRepositoryQueryLayer( queryLayer );
146 processor.processArtifact( model, artifact, reporter, null );
147 assertEquals( 2, reporter.getNumSuccesses() );
148 assertEquals( 0, reporter.getNumFailures() );
149 assertEquals( 0, reporter.getNumWarnings() );
152 public void testValidArtifactWithValidMultipleDependencies()
154 MockArtifactFactory artifactFactory = new MockArtifactFactory();
155 processor.setArtifactFactory( artifactFactory );
157 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
158 queryLayer.addReturnValue( ARTIFACT_FOUND );
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 );
173 setRequiredElements( artifact, VALID, VALID, VALID );
174 processor.setRepositoryQueryLayer( queryLayer );
175 processor.processArtifact( model, artifact, reporter, null );
176 assertEquals( 6, reporter.getNumSuccesses() );
177 assertEquals( 0, reporter.getNumFailures() );
178 assertEquals( 0, reporter.getNumWarnings() );
181 public void testValidArtifactWithAnInvalidDependency()
183 MockArtifactFactory artifactFactory = new MockArtifactFactory();
184 processor.setArtifactFactory( artifactFactory );
186 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
187 queryLayer.addReturnValue( ARTIFACT_FOUND );
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 );
202 setRequiredElements( artifact, VALID, VALID, VALID );
203 processor.setRepositoryQueryLayer( queryLayer );
204 processor.processArtifact( model, artifact, reporter, null );
205 assertEquals( 5, reporter.getNumSuccesses() );
206 assertEquals( 1, reporter.getNumFailures() );
207 assertEquals( 0, reporter.getNumWarnings() );
209 Iterator failures = reporter.getArtifactFailureIterator();
210 ArtifactResult result = (ArtifactResult) failures.next();
211 assertEquals( ArtifactReporter.DEPENDENCY_NOT_FOUND, result.getReason() );
214 public void testEmptyGroupId()
216 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
217 queryLayer.addReturnValue( ARTIFACT_FOUND );
218 processor.setRepositoryQueryLayer( queryLayer );
220 setRequiredElements( artifact, EMPTY_STRING, VALID, VALID );
221 processor.processArtifact( model, artifact, reporter, null );
222 assertEquals( 0, reporter.getNumSuccesses() );
223 assertEquals( 1, reporter.getNumFailures() );
224 assertEquals( 0, reporter.getNumWarnings() );
226 Iterator failures = reporter.getArtifactFailureIterator();
227 ArtifactResult result = (ArtifactResult) failures.next();
228 assertEquals( ArtifactReporter.EMPTY_GROUP_ID, result.getReason() );
231 public void testEmptyArtifactId()
233 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
234 queryLayer.addReturnValue( ARTIFACT_FOUND );
235 processor.setRepositoryQueryLayer( queryLayer );
237 setRequiredElements( artifact, VALID, EMPTY_STRING, VALID );
238 processor.processArtifact( model, artifact, reporter, null );
239 assertEquals( 0, reporter.getNumSuccesses() );
240 assertEquals( 1, reporter.getNumFailures() );
241 assertEquals( 0, reporter.getNumWarnings() );
243 Iterator failures = reporter.getArtifactFailureIterator();
244 ArtifactResult result = (ArtifactResult) failures.next();
245 assertEquals( ArtifactReporter.EMPTY_ARTIFACT_ID, result.getReason() );
248 public void testEmptyVersion()
250 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
251 queryLayer.addReturnValue( ARTIFACT_FOUND );
252 processor.setRepositoryQueryLayer( queryLayer );
254 setRequiredElements( artifact, VALID, VALID, EMPTY_STRING );
255 processor.processArtifact( model, artifact, reporter, null );
256 assertEquals( 0, reporter.getNumSuccesses() );
257 assertEquals( 1, reporter.getNumFailures() );
258 assertEquals( 0, reporter.getNumWarnings() );
260 Iterator failures = reporter.getArtifactFailureIterator();
261 ArtifactResult result = (ArtifactResult) failures.next();
262 assertEquals( ArtifactReporter.EMPTY_VERSION, result.getReason() );
265 public void testNullGroupId()
267 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
268 queryLayer.addReturnValue( ARTIFACT_FOUND );
269 processor.setRepositoryQueryLayer( queryLayer );
271 setRequiredElements( artifact, null, VALID, VALID );
272 processor.processArtifact( model, artifact, reporter, null );
273 assertEquals( 0, reporter.getNumSuccesses() );
274 assertEquals( 1, reporter.getNumFailures() );
275 assertEquals( 0, reporter.getNumWarnings() );
277 Iterator failures = reporter.getArtifactFailureIterator();
278 ArtifactResult result = (ArtifactResult) failures.next();
279 assertEquals( ArtifactReporter.EMPTY_GROUP_ID, result.getReason() );
282 public void testNullArtifactId()
284 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
285 queryLayer.addReturnValue( ARTIFACT_FOUND );
286 processor.setRepositoryQueryLayer( queryLayer );
288 setRequiredElements( artifact, VALID, null, VALID );
289 processor.processArtifact( model, artifact, reporter, null );
290 assertEquals( 0, reporter.getNumSuccesses() );
291 assertEquals( 1, reporter.getNumFailures() );
292 assertEquals( 0, reporter.getNumWarnings() );
294 Iterator failures = reporter.getArtifactFailureIterator();
295 ArtifactResult result = (ArtifactResult) failures.next();
296 assertEquals( ArtifactReporter.EMPTY_ARTIFACT_ID, result.getReason() );
299 public void testNullVersion()
301 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
302 queryLayer.addReturnValue( ARTIFACT_FOUND );
303 processor.setRepositoryQueryLayer( queryLayer );
305 setRequiredElements( artifact, VALID, VALID, null );
306 processor.processArtifact( model, artifact, reporter, null );
307 assertEquals( 0, reporter.getNumSuccesses() );
308 assertEquals( 1, reporter.getNumFailures() );
309 assertEquals( 0, reporter.getNumWarnings() );
311 Iterator failures = reporter.getArtifactFailureIterator();
312 ArtifactResult result = (ArtifactResult) failures.next();
313 assertEquals( ArtifactReporter.EMPTY_VERSION, result.getReason() );
316 public void testMultipleFailures()
318 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
319 queryLayer.addReturnValue( ARTIFACT_FOUND );
320 processor.setRepositoryQueryLayer( queryLayer );
322 setRequiredElements( artifact, null, null, null );
323 processor.processArtifact( model, artifact, reporter, null );
324 assertEquals( 0, reporter.getNumSuccesses() );
325 assertEquals( 3, reporter.getNumFailures() );
326 assertEquals( 0, reporter.getNumWarnings() );
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() );
337 public void testValidArtifactWithInvalidDependencyGroupId()
339 MockArtifactFactory artifactFactory = new MockArtifactFactory();
340 processor.setArtifactFactory( artifactFactory );
342 setRequiredElements( artifact, VALID, VALID, VALID );
343 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
344 queryLayer.addReturnValue( ARTIFACT_FOUND );
346 Dependency dependency = new Dependency();
347 setRequiredElements( dependency, null, VALID, VALID );
348 model.addDependency( dependency );
349 queryLayer.addReturnValue( ARTIFACT_FOUND );
351 processor.setRepositoryQueryLayer( queryLayer );
352 processor.processArtifact( model, artifact, reporter, null );
353 assertEquals( 1, reporter.getNumSuccesses() );
354 assertEquals( 1, reporter.getNumFailures() );
355 assertEquals( 0, reporter.getNumWarnings() );
357 Iterator failures = reporter.getArtifactFailureIterator();
358 ArtifactResult result = (ArtifactResult) failures.next();
359 assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_GROUP_ID, result.getReason() );
362 public void testValidArtifactWithInvalidDependencyArtifactId()
364 MockArtifactFactory artifactFactory = new MockArtifactFactory();
365 processor.setArtifactFactory( artifactFactory );
367 setRequiredElements( artifact, VALID, VALID, VALID );
368 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
369 queryLayer.addReturnValue( ARTIFACT_FOUND );
371 Dependency dependency = new Dependency();
372 setRequiredElements( dependency, VALID, null, VALID );
373 model.addDependency( dependency );
374 queryLayer.addReturnValue( ARTIFACT_FOUND );
376 processor.setRepositoryQueryLayer( queryLayer );
377 processor.processArtifact( model, artifact, reporter, null );
378 assertEquals( 1, reporter.getNumSuccesses() );
379 assertEquals( 1, reporter.getNumFailures() );
380 assertEquals( 0, reporter.getNumWarnings() );
382 Iterator failures = reporter.getArtifactFailureIterator();
383 ArtifactResult result = (ArtifactResult) failures.next();
384 assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_ARTIFACT_ID, result.getReason() );
387 public void testValidArtifactWithInvalidDependencyVersion()
389 MockArtifactFactory artifactFactory = new MockArtifactFactory();
390 processor.setArtifactFactory( artifactFactory );
392 setRequiredElements( artifact, VALID, VALID, VALID );
393 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
394 queryLayer.addReturnValue( ARTIFACT_FOUND );
396 Dependency dependency = new Dependency();
397 setRequiredElements( dependency, VALID, VALID, null );
398 model.addDependency( dependency );
399 queryLayer.addReturnValue( ARTIFACT_FOUND );
401 processor.setRepositoryQueryLayer( queryLayer );
402 processor.processArtifact( model, artifact, reporter, null );
403 assertEquals( 1, reporter.getNumSuccesses() );
404 assertEquals( 1, reporter.getNumFailures() );
405 assertEquals( 0, reporter.getNumWarnings() );
407 Iterator failures = reporter.getArtifactFailureIterator();
408 ArtifactResult result = (ArtifactResult) failures.next();
409 assertEquals( ArtifactReporter.EMPTY_DEPENDENCY_VERSION, result.getReason() );
412 public void testValidArtifactWithInvalidDependencyRequiredElements()
414 MockArtifactFactory artifactFactory = new MockArtifactFactory();
415 processor.setArtifactFactory( artifactFactory );
417 setRequiredElements( artifact, VALID, VALID, VALID );
418 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
419 queryLayer.addReturnValue( ARTIFACT_FOUND );
421 Dependency dependency = new Dependency();
422 setRequiredElements( dependency, null, null, null );
423 model.addDependency( dependency );
424 queryLayer.addReturnValue( ARTIFACT_FOUND );
426 processor.setRepositoryQueryLayer( queryLayer );
427 processor.processArtifact( model, artifact, reporter, null );
428 assertEquals( 1, reporter.getNumSuccesses() );
429 assertEquals( 3, reporter.getNumFailures() );
430 assertEquals( 0, reporter.getNumWarnings() );
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() );
441 protected void tearDown()
450 private void setRequiredElements( Artifact artifact, String groupId, String artifactId, String version )
452 artifact.setGroupId( groupId );
453 artifact.setArtifactId( artifactId );
454 artifact.setVersion( version );
457 private void setRequiredElements( Dependency dependency, String groupId, String artifactId, String version )
459 dependency.setGroupId( groupId );
460 dependency.setArtifactId( artifactId );
461 dependency.setVersion( version );