1 package org.apache.maven.repository.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 final static String EMPTY_STRING = "";
33 private final static String VALID = "temp";
35 protected MockArtifactReporter reporter;
37 protected Artifact artifact;
39 protected Model model;
41 protected DefaultArtifactReportProcessor processor;
43 protected void setUp()
47 reporter = new MockArtifactReporter();
48 artifact = new MockArtifact();
50 processor = new DefaultArtifactReportProcessor();
53 public void testNullArtifact()
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() ) );
64 public void testNoProjectDescriptor()
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() ) );
79 public void testArtifactFoundButNoDirectDependencies()
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 );
91 public void testArtifactNotFound()
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() ) );
106 public void testValidArtifactWithNullDependency()
108 MockArtifactFactory artifactFactory = new MockArtifactFactory();
109 processor.setArtifactFactory( artifactFactory );
111 setRequiredElements( artifact, VALID, VALID, VALID );
112 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
113 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
115 Dependency dependency = new Dependency();
116 setRequiredElements( dependency, VALID, VALID, VALID );
117 model.addDependency( dependency );
118 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
127 public void testValidArtifactWithValidSingleDependency()
129 MockArtifactFactory artifactFactory = new MockArtifactFactory();
130 processor.setArtifactFactory( artifactFactory );
132 setRequiredElements( artifact, VALID, VALID, VALID );
133 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
134 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
136 Dependency dependency = new Dependency();
137 setRequiredElements( dependency, VALID, VALID, VALID );
138 model.addDependency( dependency );
139 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
148 public void testValidArtifactWithValidMultipleDependencies()
150 MockArtifactFactory artifactFactory = new MockArtifactFactory();
151 processor.setArtifactFactory( artifactFactory );
153 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
154 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
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 );
177 public void testValidArtifactWithAnInvalidDependency()
179 MockArtifactFactory artifactFactory = new MockArtifactFactory();
180 processor.setArtifactFactory( artifactFactory );
182 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
183 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
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 );
205 Iterator failures = reporter.getArtifactFailureIterator();
206 ArtifactResult result = (ArtifactResult) failures.next();
207 assertTrue( ArtifactReporter.DEPENDENCY_NOT_FOUND.equals( result.getReason() ) );
210 public void testEmptyGroupId()
212 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
213 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
214 processor.setRepositoryQueryLayer( queryLayer );
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 );
222 Iterator failures = reporter.getArtifactFailureIterator();
223 ArtifactResult result = (ArtifactResult) failures.next();
224 assertTrue( ArtifactReporter.EMPTY_GROUP_ID.equals( result.getReason() ) );
227 public void testEmptyArtifactId()
229 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
230 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
231 processor.setRepositoryQueryLayer( queryLayer );
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 );
239 Iterator failures = reporter.getArtifactFailureIterator();
240 ArtifactResult result = (ArtifactResult) failures.next();
241 assertTrue( ArtifactReporter.EMPTY_ARTIFACT_ID.equals( result.getReason() ) );
244 public void testEmptyVersion()
246 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
247 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
248 processor.setRepositoryQueryLayer( queryLayer );
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 );
256 Iterator failures = reporter.getArtifactFailureIterator();
257 ArtifactResult result = (ArtifactResult) failures.next();
258 assertTrue( ArtifactReporter.EMPTY_VERSION.equals( result.getReason() ) );
261 public void testNullGroupId()
263 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
264 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
265 processor.setRepositoryQueryLayer( queryLayer );
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 );
273 Iterator failures = reporter.getArtifactFailureIterator();
274 ArtifactResult result = (ArtifactResult) failures.next();
275 assertTrue( ArtifactReporter.EMPTY_GROUP_ID.equals( result.getReason() ) );
278 public void testNullArtifactId()
280 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
281 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
282 processor.setRepositoryQueryLayer( queryLayer );
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 );
290 Iterator failures = reporter.getArtifactFailureIterator();
291 ArtifactResult result = (ArtifactResult) failures.next();
292 assertTrue( ArtifactReporter.EMPTY_ARTIFACT_ID.equals( result.getReason() ) );
295 public void testNullVersion()
297 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
298 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
299 processor.setRepositoryQueryLayer( queryLayer );
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 );
307 Iterator failures = reporter.getArtifactFailureIterator();
308 ArtifactResult result = (ArtifactResult) failures.next();
309 assertTrue( ArtifactReporter.EMPTY_VERSION.equals( result.getReason() ) );
312 public void testMultipleFailures()
314 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
315 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
316 processor.setRepositoryQueryLayer( queryLayer );
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 );
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() ) );
333 public void testValidArtifactWithInvalidDependencyGroupId()
335 MockArtifactFactory artifactFactory = new MockArtifactFactory();
336 processor.setArtifactFactory( artifactFactory );
338 setRequiredElements( artifact, VALID, VALID, VALID );
339 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
340 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
342 Dependency dependency = new Dependency();
343 setRequiredElements( dependency, null, VALID, VALID );
344 model.addDependency( dependency );
345 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
353 Iterator failures = reporter.getArtifactFailureIterator();
354 ArtifactResult result = (ArtifactResult) failures.next();
355 assertTrue( ArtifactReporter.EMPTY_DEPENDENCY_GROUP_ID.equals( result.getReason() ) );
358 public void testValidArtifactWithInvalidDependencyArtifactId()
360 MockArtifactFactory artifactFactory = new MockArtifactFactory();
361 processor.setArtifactFactory( artifactFactory );
363 setRequiredElements( artifact, VALID, VALID, VALID );
364 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
365 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
367 Dependency dependency = new Dependency();
368 setRequiredElements( dependency, VALID, null, VALID );
369 model.addDependency( dependency );
370 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
378 Iterator failures = reporter.getArtifactFailureIterator();
379 ArtifactResult result = (ArtifactResult) failures.next();
380 assertTrue( ArtifactReporter.EMPTY_DEPENDENCY_ARTIFACT_ID.equals( result.getReason() ) );
383 public void testValidArtifactWithInvalidDependencyVersion()
385 MockArtifactFactory artifactFactory = new MockArtifactFactory();
386 processor.setArtifactFactory( artifactFactory );
388 setRequiredElements( artifact, VALID, VALID, VALID );
389 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
390 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
392 Dependency dependency = new Dependency();
393 setRequiredElements( dependency, VALID, VALID, null );
394 model.addDependency( dependency );
395 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
403 Iterator failures = reporter.getArtifactFailureIterator();
404 ArtifactResult result = (ArtifactResult) failures.next();
405 assertTrue( ArtifactReporter.EMPTY_DEPENDENCY_VERSION.equals( result.getReason() ) );
408 public void testValidArtifactWithInvalidDependencyRequiredElements()
410 MockArtifactFactory artifactFactory = new MockArtifactFactory();
411 processor.setArtifactFactory( artifactFactory );
413 setRequiredElements( artifact, VALID, VALID, VALID );
414 MockRepositoryQueryLayer queryLayer = new MockRepositoryQueryLayer();
415 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
417 Dependency dependency = new Dependency();
418 setRequiredElements( dependency, null, null, null );
419 model.addDependency( dependency );
420 queryLayer.addReturnValue( RepositoryQueryLayer.ARTIFACT_FOUND );
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 );
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() ) );
437 protected void tearDown()
446 protected void setRequiredElements( Artifact artifact, String groupId, String artifactId, String version )
448 artifact.setGroupId( groupId );
449 artifact.setArtifactId( artifactId );
450 artifact.setVersion( version );
453 protected void setRequiredElements( Dependency dependency, String groupId, String artifactId, String version )
455 dependency.setGroupId( groupId );
456 dependency.setArtifactId( artifactId );
457 dependency.setVersion( version );