]> source.dussan.org Git - archiva.git/blob
1f94e276d870cdb5e1fd990f0d59defdb8e4d64d
[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 reportingDatabase;
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         model = new Model();
55         processor = (ArtifactReportProcessor) lookup( ArtifactReportProcessor.ROLE, "dependency" );
56
57         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
58
59         ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
60         reportingDatabase = new ReportingDatabase( reportGroup );
61     }
62
63     public void testArtifactFoundButNoDirectDependencies()
64     {
65         Artifact artifact = createValidArtifact();
66         processor.processArtifact( artifact, model, reportingDatabase );
67         assertEquals( 0, reportingDatabase.getNumFailures() );
68         assertEquals( 0, reportingDatabase.getNumWarnings() );
69     }
70
71     private Artifact createValidArtifact()
72     {
73         Artifact projectArtifact =
74             artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
75         projectArtifact.setRepository( repository );
76         return projectArtifact;
77     }
78
79     public void testArtifactNotFound()
80     {
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() );
92     }
93
94     public void testValidArtifactWithNullDependency()
95     {
96         Artifact artifact = createValidArtifact();
97
98         Dependency dependency = createValidDependency();
99         model.addDependency( dependency );
100
101         processor.processArtifact( artifact, model, reportingDatabase );
102         assertEquals( 0, reportingDatabase.getNumFailures() );
103         assertEquals( 0, reportingDatabase.getNumWarnings() );
104     }
105
106     private Dependency createValidDependency()
107     {
108         return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
109     }
110
111     public void testValidArtifactWithValidSingleDependency()
112     {
113         Artifact artifact = createValidArtifact();
114
115         Dependency dependency = createValidDependency();
116         model.addDependency( dependency );
117
118         processor.processArtifact( artifact, model, reportingDatabase );
119         assertEquals( 0, reportingDatabase.getNumFailures() );
120         assertEquals( 0, reportingDatabase.getNumWarnings() );
121     }
122
123     public void testValidArtifactWithValidMultipleDependencies()
124     {
125         Dependency dependency = createValidDependency();
126         model.addDependency( dependency );
127         model.addDependency( dependency );
128         model.addDependency( dependency );
129         model.addDependency( dependency );
130         model.addDependency( dependency );
131
132         Artifact artifact = createValidArtifact();
133         processor.processArtifact( artifact, model, reportingDatabase );
134         assertEquals( 0, reportingDatabase.getNumFailures() );
135         assertEquals( 0, reportingDatabase.getNumWarnings() );
136     }
137
138     public void testValidArtifactWithAnInvalidDependency()
139     {
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 ) );
146
147         Artifact artifact = createValidArtifact();
148         processor.processArtifact( artifact, model, reportingDatabase );
149         assertEquals( 1, reportingDatabase.getNumFailures() );
150         assertEquals( 0, reportingDatabase.getNumWarnings() );
151
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() );
159     }
160
161     public void testValidArtifactWithInvalidDependencyGroupId()
162     {
163         Artifact artifact = createValidArtifact();
164
165         Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
166         model.addDependency( dependency );
167
168         processor.processArtifact( artifact, model, reportingDatabase );
169         assertEquals( 1, reportingDatabase.getNumFailures() );
170         assertEquals( 0, reportingDatabase.getNumWarnings() );
171
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() );
178     }
179
180     private Dependency createDependency( String o, String valid, String s )
181     {
182         Dependency dependency = new Dependency();
183         dependency.setGroupId( o );
184         dependency.setArtifactId( valid );
185         dependency.setVersion( s );
186         return dependency;
187     }
188
189     public void testValidArtifactWithInvalidDependencyArtifactId()
190     {
191         Artifact artifact = createValidArtifact();
192
193         Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
194         model.addDependency( dependency );
195
196         processor.processArtifact( artifact, model, reportingDatabase );
197         assertEquals( 1, reportingDatabase.getNumFailures() );
198         assertEquals( 0, reportingDatabase.getNumWarnings() );
199
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() );
206     }
207
208     public void testValidArtifactWithIncorrectDependencyVersion()
209     {
210         Artifact artifact = createValidArtifact();
211
212         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
213         model.addDependency( dependency );
214
215         processor.processArtifact( artifact, model, reportingDatabase );
216         assertEquals( 1, reportingDatabase.getNumFailures() );
217         assertEquals( 0, reportingDatabase.getNumWarnings() );
218
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() );
225     }
226
227     public void testValidArtifactWithInvalidDependencyVersion()
228     {
229         Artifact artifact = createValidArtifact();
230
231         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
232         model.addDependency( dependency );
233
234         processor.processArtifact( artifact, model, reportingDatabase );
235         assertEquals( 1, reportingDatabase.getNumFailures() );
236         assertEquals( 0, reportingDatabase.getNumWarnings() );
237
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() );
244     }
245
246     public void testValidArtifactWithInvalidDependencyVersionRange()
247     {
248         Artifact artifact = createValidArtifact();
249
250         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
251         model.addDependency( dependency );
252
253         processor.processArtifact( artifact, model, reportingDatabase );
254         assertEquals( 0, reportingDatabase.getNumFailures() );
255         assertEquals( 0, reportingDatabase.getNumWarnings() );
256     }
257
258     public void testValidArtifactWithMissingDependencyVersion()
259     {
260         Artifact artifact = createValidArtifact();
261
262         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
263         model.addDependency( dependency );
264
265         processor.processArtifact( artifact, model, reportingDatabase );
266         assertEquals( 1, reportingDatabase.getNumFailures() );
267         assertEquals( 0, reportingDatabase.getNumWarnings() );
268
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() );
275     }
276
277     private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
278     {
279         return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
280     }
281
282     private static String getDependencyString( Dependency dependency )
283     {
284         return DependencyArtifactReportProcessor.getDependencyString( dependency );
285     }
286
287     private String getDependencyNotFoundMessage( Dependency dependency )
288     {
289         return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";
290     }
291 }