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() );
69 assertEquals( 0, reportingDatabase.getNumNotices() );
72 private Artifact createValidArtifact()
74 Artifact projectArtifact =
75 artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
76 projectArtifact.setRepository( repository );
77 return projectArtifact;
80 public void testArtifactNotFound()
82 Artifact artifact = artifactFactory.createProjectArtifact( INVALID, INVALID, INVALID );
83 artifact.setRepository( repository );
84 processor.processArtifact( artifact, model, reportingDatabase );
85 assertEquals( 1, reportingDatabase.getNumFailures() );
86 assertEquals( 0, reportingDatabase.getNumWarnings() );
87 assertEquals( 0, reportingDatabase.getNumNotices() );
88 Iterator failures = reportingDatabase.getArtifactIterator();
89 ArtifactResults results = (ArtifactResults) failures.next();
90 assertFalse( failures.hasNext() );
91 failures = results.getFailures().iterator();
92 Result result = (Result) failures.next();
93 assertEquals( "Artifact does not exist in the repository", result.getReason() );
96 public void testValidArtifactWithNullDependency()
98 Artifact artifact = createValidArtifact();
100 Dependency dependency = createValidDependency();
101 model.addDependency( dependency );
103 processor.processArtifact( artifact, model, reportingDatabase );
104 assertEquals( 0, reportingDatabase.getNumFailures() );
105 assertEquals( 0, reportingDatabase.getNumWarnings() );
106 assertEquals( 0, reportingDatabase.getNumNotices() );
109 private Dependency createValidDependency()
111 return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
114 public void testValidArtifactWithValidSingleDependency()
116 Artifact artifact = createValidArtifact();
118 Dependency dependency = createValidDependency();
119 model.addDependency( dependency );
121 processor.processArtifact( artifact, model, reportingDatabase );
122 assertEquals( 0, reportingDatabase.getNumFailures() );
123 assertEquals( 0, reportingDatabase.getNumWarnings() );
124 assertEquals( 0, reportingDatabase.getNumNotices() );
127 public void testValidArtifactWithValidMultipleDependencies()
129 Dependency dependency = createValidDependency();
130 model.addDependency( dependency );
131 model.addDependency( dependency );
132 model.addDependency( dependency );
133 model.addDependency( dependency );
134 model.addDependency( dependency );
136 Artifact artifact = createValidArtifact();
137 processor.processArtifact( artifact, model, reportingDatabase );
138 assertEquals( 0, reportingDatabase.getNumFailures() );
139 assertEquals( 0, reportingDatabase.getNumWarnings() );
140 assertEquals( 0, reportingDatabase.getNumNotices() );
143 public void testValidArtifactWithAnInvalidDependency()
145 Dependency dependency = createValidDependency();
146 model.addDependency( dependency );
147 model.addDependency( dependency );
148 model.addDependency( dependency );
149 model.addDependency( dependency );
150 model.addDependency( createDependency( INVALID, INVALID, INVALID ) );
152 Artifact artifact = createValidArtifact();
153 processor.processArtifact( artifact, model, reportingDatabase );
154 assertEquals( 1, reportingDatabase.getNumFailures() );
155 assertEquals( 0, reportingDatabase.getNumWarnings() );
156 assertEquals( 0, reportingDatabase.getNumNotices() );
158 Iterator failures = reportingDatabase.getArtifactIterator();
159 ArtifactResults results = (ArtifactResults) failures.next();
160 assertFalse( failures.hasNext() );
161 failures = results.getFailures().iterator();
162 Result result = (Result) failures.next();
163 assertEquals( getDependencyNotFoundMessage( createDependency( INVALID, INVALID, INVALID ) ),
164 result.getReason() );
167 public void testValidArtifactWithInvalidDependencyGroupId()
169 Artifact artifact = createValidArtifact();
171 Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
172 model.addDependency( dependency );
174 processor.processArtifact( artifact, model, reportingDatabase );
175 assertEquals( 1, reportingDatabase.getNumFailures() );
176 assertEquals( 0, reportingDatabase.getNumWarnings() );
177 assertEquals( 0, reportingDatabase.getNumNotices() );
179 Iterator failures = reportingDatabase.getArtifactIterator();
180 ArtifactResults results = (ArtifactResults) failures.next();
181 assertFalse( failures.hasNext() );
182 failures = results.getFailures().iterator();
183 Result result = (Result) failures.next();
184 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
187 private Dependency createDependency( String o, String valid, String s )
189 Dependency dependency = new Dependency();
190 dependency.setGroupId( o );
191 dependency.setArtifactId( valid );
192 dependency.setVersion( s );
196 public void testValidArtifactWithInvalidDependencyArtifactId()
198 Artifact artifact = createValidArtifact();
200 Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
201 model.addDependency( dependency );
203 processor.processArtifact( artifact, model, reportingDatabase );
204 assertEquals( 1, reportingDatabase.getNumFailures() );
205 assertEquals( 0, reportingDatabase.getNumWarnings() );
206 assertEquals( 0, reportingDatabase.getNumNotices() );
208 Iterator failures = reportingDatabase.getArtifactIterator();
209 ArtifactResults results = (ArtifactResults) failures.next();
210 assertFalse( failures.hasNext() );
211 failures = results.getFailures().iterator();
212 Result result = (Result) failures.next();
213 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
216 public void testValidArtifactWithIncorrectDependencyVersion()
218 Artifact artifact = createValidArtifact();
220 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
221 model.addDependency( dependency );
223 processor.processArtifact( artifact, model, reportingDatabase );
224 assertEquals( 1, reportingDatabase.getNumFailures() );
225 assertEquals( 0, reportingDatabase.getNumWarnings() );
227 Iterator failures = reportingDatabase.getArtifactIterator();
228 ArtifactResults results = (ArtifactResults) failures.next();
229 assertFalse( failures.hasNext() );
230 failures = results.getFailures().iterator();
231 Result result = (Result) failures.next();
232 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
235 public void testValidArtifactWithInvalidDependencyVersion()
237 Artifact artifact = createValidArtifact();
239 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
240 model.addDependency( dependency );
242 processor.processArtifact( artifact, model, reportingDatabase );
243 assertEquals( 1, reportingDatabase.getNumFailures() );
244 assertEquals( 0, reportingDatabase.getNumWarnings() );
245 assertEquals( 0, reportingDatabase.getNumNotices() );
247 Iterator failures = reportingDatabase.getArtifactIterator();
248 ArtifactResults results = (ArtifactResults) failures.next();
249 assertFalse( failures.hasNext() );
250 failures = results.getFailures().iterator();
251 Result result = (Result) failures.next();
252 assertEquals( getDependencyVersionInvalidMessage( dependency, "[" ), result.getReason() );
255 public void testValidArtifactWithInvalidDependencyVersionRange()
257 Artifact artifact = createValidArtifact();
259 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
260 model.addDependency( dependency );
262 processor.processArtifact( artifact, model, reportingDatabase );
263 assertEquals( 0, reportingDatabase.getNumFailures() );
264 assertEquals( 0, reportingDatabase.getNumWarnings() );
265 assertEquals( 0, reportingDatabase.getNumNotices() );
268 public void testValidArtifactWithMissingDependencyVersion()
270 Artifact artifact = createValidArtifact();
272 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
273 model.addDependency( dependency );
275 processor.processArtifact( artifact, model, reportingDatabase );
276 assertEquals( 1, reportingDatabase.getNumFailures() );
277 assertEquals( 0, reportingDatabase.getNumWarnings() );
278 assertEquals( 0, reportingDatabase.getNumNotices() );
280 Iterator failures = reportingDatabase.getArtifactIterator();
281 ArtifactResults results = (ArtifactResults) failures.next();
282 assertFalse( failures.hasNext() );
283 failures = results.getFailures().iterator();
284 Result result = (Result) failures.next();
285 assertEquals( getDependencyVersionInvalidMessage( dependency, null ), result.getReason() );
288 private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
290 return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
293 private static String getDependencyString( Dependency dependency )
295 return DependencyArtifactReportProcessor.getDependencyString( dependency );
298 private String getDependencyNotFoundMessage( Dependency dependency )
300 return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";