]> source.dussan.org Git - archiva.git/blob
50e58fda0d4a602ab3401dbc65d252ae27ffd827
[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         assertEquals( 0, reportingDatabase.getNumNotices() );
70     }
71
72     private Artifact createValidArtifact()
73     {
74         Artifact projectArtifact =
75             artifactFactory.createProjectArtifact( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
76         projectArtifact.setRepository( repository );
77         return projectArtifact;
78     }
79
80     public void testArtifactNotFound()
81     {
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() );
94     }
95
96     public void testValidArtifactWithNullDependency()
97     {
98         Artifact artifact = createValidArtifact();
99
100         Dependency dependency = createValidDependency();
101         model.addDependency( dependency );
102
103         processor.processArtifact( artifact, model, reportingDatabase );
104         assertEquals( 0, reportingDatabase.getNumFailures() );
105         assertEquals( 0, reportingDatabase.getNumWarnings() );
106         assertEquals( 0, reportingDatabase.getNumNotices() );
107     }
108
109     private Dependency createValidDependency()
110     {
111         return createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, VALID_VERSION );
112     }
113
114     public void testValidArtifactWithValidSingleDependency()
115     {
116         Artifact artifact = createValidArtifact();
117
118         Dependency dependency = createValidDependency();
119         model.addDependency( dependency );
120
121         processor.processArtifact( artifact, model, reportingDatabase );
122         assertEquals( 0, reportingDatabase.getNumFailures() );
123         assertEquals( 0, reportingDatabase.getNumWarnings() );
124         assertEquals( 0, reportingDatabase.getNumNotices() );
125     }
126
127     public void testValidArtifactWithValidMultipleDependencies()
128     {
129         Dependency dependency = createValidDependency();
130         model.addDependency( dependency );
131         model.addDependency( dependency );
132         model.addDependency( dependency );
133         model.addDependency( dependency );
134         model.addDependency( dependency );
135
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() );
141     }
142
143     public void testValidArtifactWithAnInvalidDependency()
144     {
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 ) );
151
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() );
157
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() );
165     }
166
167     public void testValidArtifactWithInvalidDependencyGroupId()
168     {
169         Artifact artifact = createValidArtifact();
170
171         Dependency dependency = createDependency( INVALID, VALID_ARTIFACT_ID, VALID_VERSION );
172         model.addDependency( dependency );
173
174         processor.processArtifact( artifact, model, reportingDatabase );
175         assertEquals( 1, reportingDatabase.getNumFailures() );
176         assertEquals( 0, reportingDatabase.getNumWarnings() );
177         assertEquals( 0, reportingDatabase.getNumNotices() );
178
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() );
185     }
186
187     private Dependency createDependency( String o, String valid, String s )
188     {
189         Dependency dependency = new Dependency();
190         dependency.setGroupId( o );
191         dependency.setArtifactId( valid );
192         dependency.setVersion( s );
193         return dependency;
194     }
195
196     public void testValidArtifactWithInvalidDependencyArtifactId()
197     {
198         Artifact artifact = createValidArtifact();
199
200         Dependency dependency = createDependency( VALID_GROUP_ID, INVALID, VALID_VERSION );
201         model.addDependency( dependency );
202
203         processor.processArtifact( artifact, model, reportingDatabase );
204         assertEquals( 1, reportingDatabase.getNumFailures() );
205         assertEquals( 0, reportingDatabase.getNumWarnings() );
206         assertEquals( 0, reportingDatabase.getNumNotices() );
207
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() );
214     }
215
216     public void testValidArtifactWithIncorrectDependencyVersion()
217     {
218         Artifact artifact = createValidArtifact();
219
220         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, INVALID );
221         model.addDependency( dependency );
222
223         processor.processArtifact( artifact, model, reportingDatabase );
224         assertEquals( 1, reportingDatabase.getNumFailures() );
225         assertEquals( 0, reportingDatabase.getNumWarnings() );
226
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() );
233     }
234
235     public void testValidArtifactWithInvalidDependencyVersion()
236     {
237         Artifact artifact = createValidArtifact();
238
239         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[" );
240         model.addDependency( dependency );
241
242         processor.processArtifact( artifact, model, reportingDatabase );
243         assertEquals( 1, reportingDatabase.getNumFailures() );
244         assertEquals( 0, reportingDatabase.getNumWarnings() );
245         assertEquals( 0, reportingDatabase.getNumNotices() );
246
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() );
253     }
254
255     public void testValidArtifactWithInvalidDependencyVersionRange()
256     {
257         Artifact artifact = createValidArtifact();
258
259         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, "[1.0,)" );
260         model.addDependency( dependency );
261
262         processor.processArtifact( artifact, model, reportingDatabase );
263         assertEquals( 0, reportingDatabase.getNumFailures() );
264         assertEquals( 0, reportingDatabase.getNumWarnings() );
265         assertEquals( 0, reportingDatabase.getNumNotices() );
266     }
267
268     public void testValidArtifactWithMissingDependencyVersion()
269     {
270         Artifact artifact = createValidArtifact();
271
272         Dependency dependency = createDependency( VALID_GROUP_ID, VALID_ARTIFACT_ID, null );
273         model.addDependency( dependency );
274
275         processor.processArtifact( artifact, model, reportingDatabase );
276         assertEquals( 1, reportingDatabase.getNumFailures() );
277         assertEquals( 0, reportingDatabase.getNumWarnings() );
278         assertEquals( 0, reportingDatabase.getNumNotices() );
279
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() );
286     }
287
288     private String getDependencyVersionInvalidMessage( Dependency dependency, String version )
289     {
290         return "Artifact's dependency " + getDependencyString( dependency ) + " contains an invalid version " + version;
291     }
292
293     private static String getDependencyString( Dependency dependency )
294     {
295         return DependencyArtifactReportProcessor.getDependencyString( dependency );
296     }
297
298     private String getDependencyNotFoundMessage( Dependency dependency )
299     {
300         return "Artifact's dependency " + getDependencyString( dependency ) + " does not exist in the repository";
301     }
302 }