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.archiva.reporting.model.ArtifactResults;
20 import org.apache.maven.archiva.reporting.model.Result;
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import org.apache.maven.model.Dependency;
24 import org.apache.maven.model.Model;
26 import java.util.Iterator;
31 public class DependencyArtifactReportProcessorTest
32 extends AbstractRepositoryReportsTestCase
34 private static final String VALID_GROUP_ID = "groupId";
36 private static final String VALID_ARTIFACT_ID = "artifactId";
38 private static final String VALID_VERSION = "1.0-alpha-1";
40 private ReportingDatabase reportingDatabase;
44 private ArtifactReportProcessor processor;
46 private ArtifactFactory artifactFactory;
48 private static final String INVALID = "invalid";
50 protected void setUp()
55 processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "dependency" );
57 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
59 ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
60 reportingDatabase = new ReportingDatabase( reportGroup );
63 public void testArtifactFoundButNoDirectDependencies()
65 Artifact artifact = createValidArtifact();
66 processor.processArtifact( artifact, model, reportingDatabase );
67 assertEquals( 0, reportingDatabase.getNumFailures() );
68 assertEquals( 0, reportingDatabase.getNumWarnings() );
71 private Artifact createValidArtifact()
73 Artifact projectArtifact =
74 artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
75 projectArtifact.setRepository( repository );
76 return projectArtifact;
79 public void testArtifactNotFound()
81 Artifact artifact = artifactFactory.createProjectArtifact( INVALID, INVALID, INVALID );
82 artifact.setRepository( repository );
83 processor.processArtifact( artifact, model, reportingDatabase );
84 assertEquals( 1, reportingDatabase.getNumFailures() );
85 assertEquals( 0, reportingDatabase.getNumWarnings() );
86 Iterator failures = reportingDatabase.getArtifactIterator();
87 ArtifactResults results = (ArtifactResults) failures.next();
88 assertFalse( failures.hasNext() );
89 failures = results.getFailures().iterator();
90 Result result = (Result) failures.next();
91 assertEquals( "Artifact does not exist in the repository", result.getReason() );
94 public void testValidArtifactWithNullDependency()
96 Artifact artifact = createValidArtifact();
98 Dependency dependency = createValidDependency();
99 model.addDependency( dependency );
101 processor.processArtifact( artifact, model, reportingDatabase );
102 assertEquals( 0, reportingDatabase.getNumFailures() );
103 assertEquals( 0, reportingDatabase.getNumWarnings() );
106 private Dependency createValidDependency()
108 return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
111 public void testValidArtifactWithValidSingleDependency()
113 Artifact artifact = createValidArtifact();
115 Dependency dependency = createValidDependency();
116 model.addDependency( dependency );
118 processor.processArtifact( artifact, model, reportingDatabase );
119 assertEquals( 0, reportingDatabase.getNumFailures() );
120 assertEquals( 0, reportingDatabase.getNumWarnings() );
123 public void testValidArtifactWithValidMultipleDependencies()
125 Dependency dependency = createValidDependency();
126 model.addDependency( dependency );
127 model.addDependency( dependency );
128 model.addDependency( dependency );
129 model.addDependency( dependency );
130 model.addDependency( dependency );
132 Artifact artifact = createValidArtifact();
133 processor.processArtifact( artifact, model, reportingDatabase );
134 assertEquals( 0, reportingDatabase.getNumFailures() );
135 assertEquals( 0, reportingDatabase.getNumWarnings() );
138 public void testValidArtifactWithAnInvalidDependency()
140 Dependency dependency = createValidDependency();
141 model.addDependency( dependency );
142 model.addDependency( dependency );
143 model.addDependency( dependency );
144 model.addDependency( dependency );
145 model.addDependency( createDependency( INVALID, INVALID, INVALID ) );
147 Artifact artifact = createValidArtifact();
148 processor.processArtifact( artifact, model, reportingDatabase );
149 assertEquals( 1, reportingDatabase.getNumFailures() );
150 assertEquals( 0, reportingDatabase.getNumWarnings() );
152 Iterator failures = reportingDatabase.getArtifactIterator();
153 ArtifactResults results = (ArtifactResults) failures.next();
154 assertFalse( failures.hasNext() );
155 failures = results.getFailures().iterator();
156 Result result = (Result) failures.next();
157 assertEquals( getDependencyNotFoundMessage( createDependency( INVALID, INVALID, INVALID ) ),
158 result.getReason() );
161 public void testValidArtifactWithInvalidDependencyGroupId()
163 Artifact artifact = createValidArtifact();
165 Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
166 model.addDependency( dependency );
168 processor.processArtifact( artifact, model, reportingDatabase );
169 assertEquals( 1, reportingDatabase.getNumFailures() );
170 assertEquals( 0, reportingDatabase.getNumWarnings() );
172 Iterator failures = reportingDatabase.getArtifactIterator();
173 ArtifactResults results = (ArtifactResults) failures.next();
174 assertFalse( failures.hasNext() );
175 failures = results.getFailures().iterator();
176 Result result = (Result) failures.next();
177 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
180 private Dependency createDependency( String o, String valid, String s )
182 Dependency dependency = new Dependency();
183 dependency.setGroupId( o );
184 dependency.setArtifactId( valid );
185 dependency.setVersion( s );
189 public void testValidArtifactWithInvalidDependencyArtifactId()
191 Artifact artifact = createValidArtifact();
193 Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
194 model.addDependency( dependency );
196 processor.processArtifact( artifact, model, reportingDatabase );
197 assertEquals( 1, reportingDatabase.getNumFailures() );
198 assertEquals( 0, reportingDatabase.getNumWarnings() );
200 Iterator failures = reportingDatabase.getArtifactIterator();
201 ArtifactResults results = (ArtifactResults) failures.next();
202 assertFalse( failures.hasNext() );
203 failures = results.getFailures().iterator();
204 Result result = (Result) failures.next();
205 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
208 public void testValidArtifactWithIncorrectDependencyVersion()
210 Artifact artifact = createValidArtifact();
212 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
213 model.addDependency( dependency );
215 processor.processArtifact( artifact, model, reportingDatabase );
216 assertEquals( 1, reportingDatabase.getNumFailures() );
217 assertEquals( 0, reportingDatabase.getNumWarnings() );
219 Iterator failures = reportingDatabase.getArtifactIterator();
220 ArtifactResults results = (ArtifactResults) failures.next();
221 assertFalse( failures.hasNext() );
222 failures = results.getFailures().iterator();
223 Result result = (Result) failures.next();
224 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
227 public void testValidArtifactWithInvalidDependencyVersion()
229 Artifact artifact = createValidArtifact();
231 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
232 model.addDependency( dependency );
234 processor.processArtifact( artifact, model, reportingDatabase );
235 assertEquals( 1, reportingDatabase.getNumFailures() );
236 assertEquals( 0, reportingDatabase.getNumWarnings() );
238 Iterator failures = reportingDatabase.getArtifactIterator();
239 ArtifactResults results = (ArtifactResults) failures.next();
240 assertFalse( failures.hasNext() );
241 failures = results.getFailures().iterator();
242 Result result = (Result) failures.next();
243 assertEquals( getDependencyVersionInvalidMessage( dependency, "[" ), result.getReason() );
246 public void testValidArtifactWithInvalidDependencyVersionRange()
248 Artifact artifact = createValidArtifact();
250 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
251 model.addDependency( dependency );
253 processor.processArtifact( artifact, model, reportingDatabase );
254 assertEquals( 0, reportingDatabase.getNumFailures() );
255 assertEquals( 0, reportingDatabase.getNumWarnings() );
258 public void testValidArtifactWithMissingDependencyVersion()
260 Artifact artifact = createValidArtifact();
262 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
263 model.addDependency( dependency );
265 processor.processArtifact( artifact, model, reportingDatabase );
266 assertEquals( 1, reportingDatabase.getNumFailures() );
267 assertEquals( 0, reportingDatabase.getNumWarnings() );
269 Iterator failures = reportingDatabase.getArtifactIterator();
270 ArtifactResults results = (ArtifactResults) failures.next();
271 assertFalse( failures.hasNext() );
272 failures = results.getFailures().iterator();
273 Result result = (Result) failures.next();
274 assertEquals( getDependencyVersionInvalidMessage( dependency, null ), result.getReason() );
277 private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
279 return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
282 private static String getDependencyString( Dependency dependency )
284 return DependencyArtifactReportProcessor.getDependencyString( dependency );
287 private String getDependencyNotFoundMessage( Dependency dependency )
289 return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";