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.MetadataResults;
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.artifact.repository.metadata.ArtifactRepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.Plugin;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.Snapshot;
28 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
29 import org.apache.maven.artifact.repository.metadata.Versioning;
31 import java.util.Iterator;
33 public class BadMetadataReportProcessorTest
34 extends AbstractRepositoryReportsTestCase
36 private ArtifactFactory artifactFactory;
38 private MetadataReportProcessor badMetadataReportProcessor;
40 private ReportingDatabase reportingDatabase;
42 protected void setUp()
47 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
49 badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE, "bad-metadata" );
51 ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
52 reportingDatabase = new ReportingDatabase( reportGroup );
55 public void testMetadataMissingLastUpdated()
57 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
59 Versioning versioning = new Versioning();
60 versioning.addVersion( "1.0-alpha-1" );
61 versioning.addVersion( "1.0-alpha-2" );
63 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
65 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
67 Iterator failures = reportingDatabase.getMetadataIterator();
68 assertTrue( "check there is a failure", failures.hasNext() );
69 MetadataResults results = (MetadataResults) failures.next();
70 failures = results.getFailures().iterator();
71 assertTrue( "check there is a failure", failures.hasNext() );
72 Result result = (Result) failures.next();
73 assertMetadata( metadata, results );
74 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
75 assertFalse( "check no more failures", failures.hasNext() );
78 public void testMetadataMissingVersioning()
80 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
82 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, null );
84 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
86 Iterator failures = reportingDatabase.getMetadataIterator();
87 assertTrue( "check there is a failure", failures.hasNext() );
88 MetadataResults results = (MetadataResults) failures.next();
89 failures = results.getFailures().iterator();
90 assertTrue( "check there is a failure", failures.hasNext() );
91 assertMetadata( metadata, results );
92 Result result = (Result) failures.next();
93 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
94 result = (Result) failures.next();
95 assertEquals( "check reason",
96 "Artifact version 1.0-alpha-1 found in the repository but missing in the metadata.",
98 result = (Result) failures.next();
99 assertEquals( "check reason",
100 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
101 result.getReason() );
102 assertFalse( "check no more failures", failures.hasNext() );
105 public void testSnapshotMetadataMissingVersioning()
108 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
110 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
112 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
114 Iterator failures = reportingDatabase.getMetadataIterator();
115 assertTrue( "check there is a failure", failures.hasNext() );
116 MetadataResults results = (MetadataResults) failures.next();
117 failures = results.getFailures().iterator();
118 assertTrue( "check there is a failure", failures.hasNext() );
119 assertMetadata( metadata, results );
120 Result result = (Result) failures.next();
121 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
122 assertFalse( "check no more failures", failures.hasNext() );
125 public void testMetadataValidVersions()
127 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
129 Versioning versioning = new Versioning();
130 versioning.addVersion( "1.0-alpha-1" );
131 versioning.addVersion( "1.0-alpha-2" );
132 versioning.setLastUpdated( "20050611.202020" );
134 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
136 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
138 Iterator failures = reportingDatabase.getMetadataIterator();
139 assertFalse( "check there are no failures", failures.hasNext() );
142 public void testMetadataMissingADirectory()
144 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
146 Versioning versioning = new Versioning();
147 versioning.addVersion( "1.0-alpha-1" );
148 versioning.setLastUpdated( "20050611.202020" );
150 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
152 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
154 Iterator failures = reportingDatabase.getMetadataIterator();
155 assertTrue( "check there is a failure", failures.hasNext() );
156 MetadataResults results = (MetadataResults) failures.next();
157 failures = results.getFailures().iterator();
158 assertTrue( "check there is a failure", failures.hasNext() );
159 Result result = (Result) failures.next();
160 assertMetadata( metadata, results );
161 // TODO: should be more robust
162 assertEquals( "check reason",
163 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
164 result.getReason() );
165 assertFalse( "check no more failures", failures.hasNext() );
168 public void testMetadataInvalidArtifactVersion()
170 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
172 Versioning versioning = new Versioning();
173 versioning.addVersion( "1.0-alpha-1" );
174 versioning.addVersion( "1.0-alpha-2" );
175 versioning.addVersion( "1.0-alpha-3" );
176 versioning.setLastUpdated( "20050611.202020" );
178 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
180 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
182 Iterator failures = reportingDatabase.getMetadataIterator();
183 assertTrue( "check there is a failure", failures.hasNext() );
184 MetadataResults results = (MetadataResults) failures.next();
185 failures = results.getFailures().iterator();
186 assertTrue( "check there is a failure", failures.hasNext() );
187 Result result = (Result) failures.next();
188 assertMetadata( metadata, results );
189 // TODO: should be more robust
190 assertEquals( "check reason",
191 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
192 result.getReason() );
193 assertFalse( "check no more failures", failures.hasNext() );
196 public void testMoreThanOneMetadataVersionErrors()
198 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
200 Versioning versioning = new Versioning();
201 versioning.addVersion( "1.0-alpha-1" );
202 versioning.addVersion( "1.0-alpha-3" );
203 versioning.setLastUpdated( "20050611.202020" );
205 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
207 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
209 Iterator failures = reportingDatabase.getMetadataIterator();
210 assertTrue( "check there is a failure", failures.hasNext() );
211 MetadataResults results = (MetadataResults) failures.next();
212 failures = results.getFailures().iterator();
213 assertTrue( "check there is a failure", failures.hasNext() );
214 Result result = (Result) failures.next();
215 assertMetadata( metadata, results );
216 // TODO: should be more robust
217 assertEquals( "check reason",
218 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
219 result.getReason() );
220 assertTrue( "check there is a 2nd failure", failures.hasNext() );
221 result = (Result) failures.next();
222 // TODO: should be more robust
223 assertEquals( "check reason",
224 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
225 result.getReason() );
226 assertFalse( "check no more failures", failures.hasNext() );
229 public void testValidPluginMetadata()
231 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
232 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
233 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
235 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
237 Iterator failures = reportingDatabase.getMetadataIterator();
238 assertFalse( "check there are no failures", failures.hasNext() );
241 public void testMissingMetadataPlugin()
243 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
244 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
245 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
246 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
248 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
250 Iterator failures = reportingDatabase.getMetadataIterator();
251 assertTrue( "check there is a failure", failures.hasNext() );
252 MetadataResults results = (MetadataResults) failures.next();
253 failures = results.getFailures().iterator();
254 assertTrue( "check there is a failure", failures.hasNext() );
255 Result result = (Result) failures.next();
256 // TODO: should be more robust
257 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
258 result.getReason() );
259 assertFalse( "check no more failures", failures.hasNext() );
262 public void testIncompletePluginMetadata()
264 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
265 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
267 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
269 Iterator failures = reportingDatabase.getMetadataIterator();
270 assertTrue( "check there is a failure", failures.hasNext() );
271 MetadataResults results = (MetadataResults) failures.next();
272 failures = results.getFailures().iterator();
273 assertTrue( "check there is a failure", failures.hasNext() );
274 Result result = (Result) failures.next();
275 // TODO: should be more robust
276 assertEquals( "check reason",
277 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
278 result.getReason() );
279 assertFalse( "check no more failures", failures.hasNext() );
282 public void testInvalidPluginArtifactId()
284 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
285 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
286 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
287 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
288 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
290 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
292 Iterator failures = reportingDatabase.getMetadataIterator();
293 assertTrue( "check there is a failure", failures.hasNext() );
294 MetadataResults results = (MetadataResults) failures.next();
295 failures = results.getFailures().iterator();
296 assertTrue( "check there is a failure", failures.hasNext() );
297 Result result = (Result) failures.next();
298 // TODO: should be more robust
299 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default3",
300 result.getReason() );
301 assertTrue( "check there is a 2nd failure", failures.hasNext() );
302 result = (Result) failures.next();
303 // TODO: should be more robust
304 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default4",
305 result.getReason() );
306 assertFalse( "check no more failures", failures.hasNext() );
309 public void testInvalidPluginPrefix()
311 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
312 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
313 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
315 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
317 Iterator failures = reportingDatabase.getMetadataIterator();
318 assertTrue( "check there is a failure", failures.hasNext() );
319 MetadataResults results = (MetadataResults) failures.next();
320 failures = results.getFailures().iterator();
321 assertTrue( "check there is a failure", failures.hasNext() );
322 Result result = (Result) failures.next();
323 // TODO: should be more robust
324 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
325 assertTrue( "check there is a 2nd failure", failures.hasNext() );
326 result = (Result) failures.next();
327 // TODO: should be more robust
328 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
329 result.getReason() );
330 assertFalse( "check no more failures", failures.hasNext() );
333 public void testDuplicatePluginPrefixes()
335 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
336 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
337 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
339 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
341 Iterator failures = reportingDatabase.getMetadataIterator();
342 assertTrue( "check there is a failure", failures.hasNext() );
343 MetadataResults results = (MetadataResults) failures.next();
344 failures = results.getFailures().iterator();
345 assertTrue( "check there is a failure", failures.hasNext() );
346 Result result = (Result) failures.next();
347 // TODO: should be more robust
348 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
349 assertFalse( "check no more failures", failures.hasNext() );
352 public void testValidSnapshotMetadata()
355 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
357 Snapshot snapshot = new Snapshot();
358 snapshot.setBuildNumber( 1 );
359 snapshot.setTimestamp( "20050611.202024" );
361 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
363 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
365 Iterator failures = reportingDatabase.getMetadataIterator();
366 assertFalse( "check there are no failures", failures.hasNext() );
369 public void testInvalidSnapshotMetadata()
372 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
374 Snapshot snapshot = new Snapshot();
375 snapshot.setBuildNumber( 2 );
376 snapshot.setTimestamp( "20050611.202024" );
378 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
380 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
382 Iterator failures = reportingDatabase.getMetadataIterator();
383 assertTrue( "check there is a failure", failures.hasNext() );
384 MetadataResults results = (MetadataResults) failures.next();
385 failures = results.getFailures().iterator();
386 assertTrue( "check there is a failure", failures.hasNext() );
387 Result result = (Result) failures.next();
388 assertMetadata( metadata, results );
389 // TODO: should be more robust
390 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
391 result.getReason() );
392 assertFalse( "check no more failures", failures.hasNext() );
395 private static void assertMetadata( RepositoryMetadata metadata, MetadataResults results )
397 assertEquals( "check metadata", metadata.getGroupId(), results.getGroupId() );
398 assertEquals( "check metadata", metadata.getArtifactId(), results.getArtifactId() );
399 assertEquals( "check metadata", metadata.getBaseVersion(), results.getVersion() );
402 private Plugin createMetadataPlugin( String artifactId, String prefix )
404 Plugin plugin = new Plugin();
405 plugin.setArtifactId( artifactId );
406 plugin.setName( artifactId );
407 plugin.setPrefix( prefix );