]> source.dussan.org Git - archiva.git/blob
3894e614ffbda1a518016b1f235501652f2a5294
[archiva.git] /
1 package org.apache.maven.archiva.reporting;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
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;
25
26 import java.util.Iterator;
27
28 /**
29  *
30  */
31 public class DependencyArtifactReportProcessorTest
32     extends AbstractRepositoryReportsTestCase
33 {
34     private static final String VALID_GROUP_ID = "groupId";
35
36     private static final String VALID_ARTIFACT_ID = "artifactId";
37
38     private static final String VALID_VERSION = "1.0-alpha-1";
39
40     private ReportingDatabase reporter;
41
42     private Model model;
43
44     private ArtifactReportProcessor processor;
45
46     private ArtifactFactory artifactFactory;
47
48     private static final String INVALID = "invalid";
49
50     protected void setUp()
51         throws Exception
52     {
53         super.setUp();
54         reporter = new ReportingDatabase();
55         model = new Model();
56         processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "dependency" );
57
58         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
59     }
60
61     public void testArtifactFoundButNoDirectDependencies()
62     {
63         Artifact artifact = createValidArtifact();
64         processor.processArtifact( artifact, model, reporter );
65         assertEquals( 0, reporter.getNumFailures() );
66         assertEquals( 0, reporter.getNumWarnings() );
67     }
68
69     private Artifact createValidArtifact()
70     {
71         Artifact projectArtifact =
72             artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
73         projectArtifact.setRepository( repository );
74         return projectArtifact;
75     }
76
77     public void testArtifactNotFound()
78     {
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() );
90     }
91
92     public void testValidArtifactWithNullDependency()
93     {
94         Artifact artifact = createValidArtifact();
95
96         Dependency dependency = createValidDependency();
97         model.addDependency( dependency );
98
99         processor.processArtifact( artifact, model, reporter );
100         assertEquals( 0, reporter.getNumFailures() );
101         assertEquals( 0, reporter.getNumWarnings() );
102     }
103
104     private Dependency createValidDependency()
105     {
106         return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
107     }
108
109     public void testValidArtifactWithValidSingleDependency()
110     {
111         Artifact artifact = createValidArtifact();
112
113         Dependency dependency = createValidDependency();
114         model.addDependency( dependency );
115
116         processor.processArtifact( artifact, model, reporter );
117         assertEquals( 0, reporter.getNumFailures() );
118         assertEquals( 0, reporter.getNumWarnings() );
119     }
120
121     public void testValidArtifactWithValidMultipleDependencies()
122     {
123         Dependency dependency = createValidDependency();
124         model.addDependency( dependency );
125         model.addDependency( dependency );
126         model.addDependency( dependency );
127         model.addDependency( dependency );
128         model.addDependency( dependency );
129
130         Artifact artifact = createValidArtifact();
131         processor.processArtifact( artifact, model, reporter );
132         assertEquals( 0, reporter.getNumFailures() );
133         assertEquals( 0, reporter.getNumWarnings() );
134     }
135
136     public void testValidArtifactWithAnInvalidDependency()
137     {
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 ) );
144
145         Artifact artifact = createValidArtifact();
146         processor.processArtifact( artifact, model, reporter );
147         assertEquals( 1, reporter.getNumFailures() );
148         assertEquals( 0, reporter.getNumWarnings() );
149
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() );
157     }
158
159     public void testValidArtifactWithInvalidDependencyGroupId()
160     {
161         Artifact artifact = createValidArtifact();
162
163         Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
164         model.addDependency( dependency );
165
166         processor.processArtifact( artifact, model, reporter );
167         assertEquals( 1, reporter.getNumFailures() );
168         assertEquals( 0, reporter.getNumWarnings() );
169
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() );
176     }
177
178     private Dependency createDependency( String o, String valid, String s )
179     {
180         Dependency dependency = new Dependency();
181         dependency.setGroupId( o );
182         dependency.setArtifactId( valid );
183         dependency.setVersion( s );
184         return dependency;
185     }
186
187     public void testValidArtifactWithInvalidDependencyArtifactId()
188     {
189         Artifact artifact = createValidArtifact();
190
191         Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
192         model.addDependency( dependency );
193
194         processor.processArtifact( artifact, model, reporter );
195         assertEquals( 1, reporter.getNumFailures() );
196         assertEquals( 0, reporter.getNumWarnings() );
197
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() );
204     }
205
206     public void testValidArtifactWithIncorrectDependencyVersion()
207     {
208         Artifact artifact = createValidArtifact();
209
210         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
211         model.addDependency( dependency );
212
213         processor.processArtifact( artifact, model, reporter );
214         assertEquals( 1, reporter.getNumFailures() );
215         assertEquals( 0, reporter.getNumWarnings() );
216
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() );
223     }
224
225     public void testValidArtifactWithInvalidDependencyVersion()
226     {
227         Artifact artifact = createValidArtifact();
228
229         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
230         model.addDependency( dependency );
231
232         processor.processArtifact( artifact, model, reporter );
233         assertEquals( 1, reporter.getNumFailures() );
234         assertEquals( 0, reporter.getNumWarnings() );
235
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() );
242     }
243
244     public void testValidArtifactWithInvalidDependencyVersionRange()
245     {
246         Artifact artifact = createValidArtifact();
247
248         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
249         model.addDependency( dependency );
250
251         processor.processArtifact( artifact, model, reporter );
252         assertEquals( 0, reporter.getNumFailures() );
253         assertEquals( 0, reporter.getNumWarnings() );
254     }
255
256     public void testValidArtifactWithMissingDependencyVersion()
257     {
258         Artifact artifact = createValidArtifact();
259
260         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
261         model.addDependency( dependency );
262
263         processor.processArtifact( artifact, model, reporter );
264         assertEquals( 1, reporter.getNumFailures() );
265         assertEquals( 0, reporter.getNumWarnings() );
266
267         Iterator failures = reporter.getArtifactIterator();
268         ArtifactResults results = (ArtifactResults) failures.next();
269         assertFalse( failures.hasNext() );
270         failures = results.getFailures().iterator();
271         Result result = (Result) failures.next();
272         assertEquals( getDependencyVersionInvalidMessage( dependency, null ), result.getReason() );
273     }
274
275     private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
276     {
277         return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
278     }
279
280     private static String getDependencyString( Dependency dependency )
281     {
282         return DependencyArtifactReportProcessor.getDependencyString( dependency );
283     }
284
285     private String getDependencyNotFoundMessage( Dependency dependency )
286     {
287         return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";
288     }
289 }