1 package org.apache.maven.archiva.reporting.processor;
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.archiva.reporting.group.ReportGroup;
22 import org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor;
23 import org.apache.maven.archiva.reporting.processor.DependencyArtifactReportProcessor;
24 import org.apache.maven.archiva.reporting.AbstractRepositoryReportsTestCase;
25 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import org.apache.maven.model.Dependency;
29 import org.apache.maven.model.Model;
31 import java.util.Iterator;
36 public class DependencyArtifactReportProcessorTest
37 extends AbstractRepositoryReportsTestCase
39 private static final String VALID_GROUP_ID = "groupId";
41 private static final String VALID_ARTIFACT_ID = "artifactId";
43 private static final String VALID_VERSION = "1.0-alpha-1";
45 private ReportingDatabase reportingDatabase;
49 private ArtifactReportProcessor processor;
51 private ArtifactFactory artifactFactory;
53 private static final String INVALID = "invalid";
55 protected void setUp()
60 processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "dependency" );
62 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
64 ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
65 reportingDatabase = new ReportingDatabase( reportGroup );
68 public void testArtifactFoundButNoDirectDependencies()
70 Artifact artifact = createValidArtifact();
71 processor.processArtifact( artifact, model, reportingDatabase );
72 assertEquals( 0, reportingDatabase.getNumFailures() );
73 assertEquals( 0, reportingDatabase.getNumWarnings() );
74 assertEquals( 0, reportingDatabase.getNumNotices() );
77 private Artifact createValidArtifact()
79 Artifact projectArtifact =
80 artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
81 projectArtifact.setRepository( repository );
82 return projectArtifact;
85 public void testArtifactNotFound()
87 Artifact artifact = artifactFactory.createProjectArtifact( INVALID, INVALID, INVALID );
88 artifact.setRepository( repository );
89 processor.processArtifact( artifact, model, reportingDatabase );
90 assertEquals( 1, reportingDatabase.getNumFailures() );
91 assertEquals( 0, reportingDatabase.getNumWarnings() );
92 assertEquals( 0, reportingDatabase.getNumNotices() );
93 Iterator failures = reportingDatabase.getArtifactIterator();
94 ArtifactResults results = (ArtifactResults) failures.next();
95 assertFalse( failures.hasNext() );
96 failures = results.getFailures().iterator();
97 Result result = (Result) failures.next();
98 assertEquals( "Artifact does not exist in the repository", result.getReason() );
101 public void testValidArtifactWithNullDependency()
103 Artifact artifact = createValidArtifact();
105 Dependency dependency = createValidDependency();
106 model.addDependency( dependency );
108 processor.processArtifact( artifact, model, reportingDatabase );
109 assertEquals( 0, reportingDatabase.getNumFailures() );
110 assertEquals( 0, reportingDatabase.getNumWarnings() );
111 assertEquals( 0, reportingDatabase.getNumNotices() );
114 private Dependency createValidDependency()
116 return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
119 public void testValidArtifactWithValidSingleDependency()
121 Artifact artifact = createValidArtifact();
123 Dependency dependency = createValidDependency();
124 model.addDependency( dependency );
126 processor.processArtifact( artifact, model, reportingDatabase );
127 assertEquals( 0, reportingDatabase.getNumFailures() );
128 assertEquals( 0, reportingDatabase.getNumWarnings() );
129 assertEquals( 0, reportingDatabase.getNumNotices() );
132 public void testValidArtifactWithValidMultipleDependencies()
134 Dependency dependency = createValidDependency();
135 model.addDependency( dependency );
136 model.addDependency( dependency );
137 model.addDependency( dependency );
138 model.addDependency( dependency );
139 model.addDependency( dependency );
141 Artifact artifact = createValidArtifact();
142 processor.processArtifact( artifact, model, reportingDatabase );
143 assertEquals( 0, reportingDatabase.getNumFailures() );
144 assertEquals( 0, reportingDatabase.getNumWarnings() );
145 assertEquals( 0, reportingDatabase.getNumNotices() );
148 public void testValidArtifactWithAnInvalidDependency()
150 Dependency dependency = createValidDependency();
151 model.addDependency( dependency );
152 model.addDependency( dependency );
153 model.addDependency( dependency );
154 model.addDependency( dependency );
155 model.addDependency( createDependency( INVALID, INVALID, INVALID ) );
157 Artifact artifact = createValidArtifact();
158 processor.processArtifact( artifact, model, reportingDatabase );
159 assertEquals( 1, reportingDatabase.getNumFailures() );
160 assertEquals( 0, reportingDatabase.getNumWarnings() );
161 assertEquals( 0, reportingDatabase.getNumNotices() );
163 Iterator failures = reportingDatabase.getArtifactIterator();
164 ArtifactResults results = (ArtifactResults) failures.next();
165 assertFalse( failures.hasNext() );
166 failures = results.getFailures().iterator();
167 Result result = (Result) failures.next();
168 assertEquals( getDependencyNotFoundMessage( createDependency( INVALID, INVALID, INVALID ) ),
169 result.getReason() );
172 public void testValidArtifactWithInvalidDependencyGroupId()
174 Artifact artifact = createValidArtifact();
176 Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
177 model.addDependency( dependency );
179 processor.processArtifact( artifact, model, reportingDatabase );
180 assertEquals( 1, reportingDatabase.getNumFailures() );
181 assertEquals( 0, reportingDatabase.getNumWarnings() );
182 assertEquals( 0, reportingDatabase.getNumNotices() );
184 Iterator failures = reportingDatabase.getArtifactIterator();
185 ArtifactResults results = (ArtifactResults) failures.next();
186 assertFalse( failures.hasNext() );
187 failures = results.getFailures().iterator();
188 Result result = (Result) failures.next();
189 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
192 private Dependency createDependency( String o, String valid, String s )
194 Dependency dependency = new Dependency();
195 dependency.setGroupId( o );
196 dependency.setArtifactId( valid );
197 dependency.setVersion( s );
201 public void testValidArtifactWithInvalidDependencyArtifactId()
203 Artifact artifact = createValidArtifact();
205 Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
206 model.addDependency( dependency );
208 processor.processArtifact( artifact, model, reportingDatabase );
209 assertEquals( 1, reportingDatabase.getNumFailures() );
210 assertEquals( 0, reportingDatabase.getNumWarnings() );
211 assertEquals( 0, reportingDatabase.getNumNotices() );
213 Iterator failures = reportingDatabase.getArtifactIterator();
214 ArtifactResults results = (ArtifactResults) failures.next();
215 assertFalse( failures.hasNext() );
216 failures = results.getFailures().iterator();
217 Result result = (Result) failures.next();
218 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
221 public void testValidArtifactWithIncorrectDependencyVersion()
223 Artifact artifact = createValidArtifact();
225 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
226 model.addDependency( dependency );
228 processor.processArtifact( artifact, model, reportingDatabase );
229 assertEquals( 1, reportingDatabase.getNumFailures() );
230 assertEquals( 0, reportingDatabase.getNumWarnings() );
232 Iterator failures = reportingDatabase.getArtifactIterator();
233 ArtifactResults results = (ArtifactResults) failures.next();
234 assertFalse( failures.hasNext() );
235 failures = results.getFailures().iterator();
236 Result result = (Result) failures.next();
237 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
240 public void testValidArtifactWithInvalidDependencyVersion()
242 Artifact artifact = createValidArtifact();
244 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
245 model.addDependency( dependency );
247 processor.processArtifact( artifact, model, reportingDatabase );
248 assertEquals( 1, reportingDatabase.getNumFailures() );
249 assertEquals( 0, reportingDatabase.getNumWarnings() );
250 assertEquals( 0, reportingDatabase.getNumNotices() );
252 Iterator failures = reportingDatabase.getArtifactIterator();
253 ArtifactResults results = (ArtifactResults) failures.next();
254 assertFalse( failures.hasNext() );
255 failures = results.getFailures().iterator();
256 Result result = (Result) failures.next();
257 assertEquals( getDependencyVersionInvalidMessage( dependency, "[" ), result.getReason() );
260 public void testValidArtifactWithInvalidDependencyVersionRange()
262 Artifact artifact = createValidArtifact();
264 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
265 model.addDependency( dependency );
267 processor.processArtifact( artifact, model, reportingDatabase );
268 assertEquals( 0, reportingDatabase.getNumFailures() );
269 assertEquals( 0, reportingDatabase.getNumWarnings() );
270 assertEquals( 0, reportingDatabase.getNumNotices() );
273 public void testValidArtifactWithMissingDependencyVersion()
275 Artifact artifact = createValidArtifact();
277 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
278 model.addDependency( dependency );
280 processor.processArtifact( artifact, model, reportingDatabase );
281 assertEquals( 1, reportingDatabase.getNumFailures() );
282 assertEquals( 0, reportingDatabase.getNumWarnings() );
283 assertEquals( 0, reportingDatabase.getNumNotices() );
285 Iterator failures = reportingDatabase.getArtifactIterator();
286 ArtifactResults results = (ArtifactResults) failures.next();
287 assertFalse( failures.hasNext() );
288 failures = results.getFailures().iterator();
289 Result result = (Result) failures.next();
290 assertEquals( getDependencyVersionInvalidMessage( dependency, null ), result.getReason() );
293 private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
295 return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
298 private static String getDependencyString( Dependency dependency )
300 return DependencyArtifactReportProcessor.getDependencyString( dependency );
303 private String getDependencyNotFoundMessage( Dependency dependency )
305 return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";