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 reporter;
44 private ArtifactReportProcessor processor;
46 private ArtifactFactory artifactFactory;
48 private static final String INVALID = "invalid";
50 protected void setUp()
54 reporter = new ReportingDatabase();
56 processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "dependency" );
58 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
61 public void testArtifactFoundButNoDirectDependencies()
63 Artifact artifact = createValidArtifact();
64 processor.processArtifact( artifact, model, reporter );
65 assertEquals( 0, reporter.getNumFailures() );
66 assertEquals( 0, reporter.getNumWarnings() );
69 private Artifact createValidArtifact()
71 Artifact projectArtifact =
72 artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
73 projectArtifact.setRepository( repository );
74 return projectArtifact;
77 public void testArtifactNotFound()
79 Artifact artifact = artifactFactory.createProjectArtifact( INVALID, INVALID, INVALID );
80 artifact.setRepository( repository );
81 processor.processArtifact( artifact, model, reporter );
82 assertEquals( 1, reporter.getNumFailures() );
83 assertEquals( 0, reporter.getNumWarnings() );
84 Iterator failures = reporter.getArtifactIterator();
85 ArtifactResults results = (ArtifactResults) failures.next();
86 assertFalse( failures.hasNext() );
87 failures = results.getFailures().iterator();
88 Result result = (Result) failures.next();
89 assertEquals( "Artifact does not exist in the repository", result.getReason() );
92 public void testValidArtifactWithNullDependency()
94 Artifact artifact = createValidArtifact();
96 Dependency dependency = createValidDependency();
97 model.addDependency( dependency );
99 processor.processArtifact( artifact, model, reporter );
100 assertEquals( 0, reporter.getNumFailures() );
101 assertEquals( 0, reporter.getNumWarnings() );
104 private Dependency createValidDependency()
106 return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
109 public void testValidArtifactWithValidSingleDependency()
111 Artifact artifact = createValidArtifact();
113 Dependency dependency = createValidDependency();
114 model.addDependency( dependency );
116 processor.processArtifact( artifact, model, reporter );
117 assertEquals( 0, reporter.getNumFailures() );
118 assertEquals( 0, reporter.getNumWarnings() );
121 public void testValidArtifactWithValidMultipleDependencies()
123 Dependency dependency = createValidDependency();
124 model.addDependency( dependency );
125 model.addDependency( dependency );
126 model.addDependency( dependency );
127 model.addDependency( dependency );
128 model.addDependency( dependency );
130 Artifact artifact = createValidArtifact();
131 processor.processArtifact( artifact, model, reporter );
132 assertEquals( 0, reporter.getNumFailures() );
133 assertEquals( 0, reporter.getNumWarnings() );
136 public void testValidArtifactWithAnInvalidDependency()
138 Dependency dependency = createValidDependency();
139 model.addDependency( dependency );
140 model.addDependency( dependency );
141 model.addDependency( dependency );
142 model.addDependency( dependency );
143 model.addDependency( createDependency( INVALID, INVALID, INVALID ) );
145 Artifact artifact = createValidArtifact();
146 processor.processArtifact( artifact, model, reporter );
147 assertEquals( 1, reporter.getNumFailures() );
148 assertEquals( 0, reporter.getNumWarnings() );
150 Iterator failures = reporter.getArtifactIterator();
151 ArtifactResults results = (ArtifactResults) failures.next();
152 assertFalse( failures.hasNext() );
153 failures = results.getFailures().iterator();
154 Result result = (Result) failures.next();
155 assertEquals( getDependencyNotFoundMessage( createDependency( INVALID, INVALID, INVALID ) ),
156 result.getReason() );
159 public void testValidArtifactWithInvalidDependencyGroupId()
161 Artifact artifact = createValidArtifact();
163 Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
164 model.addDependency( dependency );
166 processor.processArtifact( artifact, model, reporter );
167 assertEquals( 1, reporter.getNumFailures() );
168 assertEquals( 0, reporter.getNumWarnings() );
170 Iterator failures = reporter.getArtifactIterator();
171 ArtifactResults results = (ArtifactResults) failures.next();
172 assertFalse( failures.hasNext() );
173 failures = results.getFailures().iterator();
174 Result result = (Result) failures.next();
175 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
178 private Dependency createDependency( String o, String valid, String s )
180 Dependency dependency = new Dependency();
181 dependency.setGroupId( o );
182 dependency.setArtifactId( valid );
183 dependency.setVersion( s );
187 public void testValidArtifactWithInvalidDependencyArtifactId()
189 Artifact artifact = createValidArtifact();
191 Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
192 model.addDependency( dependency );
194 processor.processArtifact( artifact, model, reporter );
195 assertEquals( 1, reporter.getNumFailures() );
196 assertEquals( 0, reporter.getNumWarnings() );
198 Iterator failures = reporter.getArtifactIterator();
199 ArtifactResults results = (ArtifactResults) failures.next();
200 assertFalse( failures.hasNext() );
201 failures = results.getFailures().iterator();
202 Result result = (Result) failures.next();
203 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
206 public void testValidArtifactWithIncorrectDependencyVersion()
208 Artifact artifact = createValidArtifact();
210 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
211 model.addDependency( dependency );
213 processor.processArtifact( artifact, model, reporter );
214 assertEquals( 1, reporter.getNumFailures() );
215 assertEquals( 0, reporter.getNumWarnings() );
217 Iterator failures = reporter.getArtifactIterator();
218 ArtifactResults results = (ArtifactResults) failures.next();
219 assertFalse( failures.hasNext() );
220 failures = results.getFailures().iterator();
221 Result result = (Result) failures.next();
222 assertEquals( getDependencyNotFoundMessage( dependency ), result.getReason() );
225 public void testValidArtifactWithInvalidDependencyVersion()
227 Artifact artifact = createValidArtifact();
229 Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
230 model.addDependency( dependency );
232 processor.processArtifact( artifact, model, reporter );
233 assertEquals( 1, reporter.getNumFailures() );
234 assertEquals( 0, reporter.getNumWarnings() );
236 Iterator failures = reporter.getArtifactIterator();
237 ArtifactResults results = (ArtifactResults) failures.next();
238 assertFalse( failures.hasNext() );
239 failures = results.getFailures().iterator();
240 Result result = (Result) failures.next();
241 assertEquals( getDependencyVersionInvalidMessage( dependency, "[" ), result.getReason() );
244 private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
246 return "Artifact's dependency " + dependency + " contains an invalid version " + version;
249 private String getDependencyNotFoundMessage( Dependency dependency )
251 return "Artifact's dependency " + dependency.toString() + " does not exist in the repository";