1 package org.apache.maven.archiva.reporting.processor;
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.archiva.reporting.group.ReportGroup;
22 import org.apache.maven.archiva.reporting.processor.MetadataReportProcessor;
23 import org.apache.maven.archiva.reporting.AbstractRepositoryReportsTestCase;
24 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.factory.ArtifactFactory;
27 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
29 import org.apache.maven.artifact.repository.metadata.Plugin;
30 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
31 import org.apache.maven.artifact.repository.metadata.Snapshot;
32 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
33 import org.apache.maven.artifact.repository.metadata.Versioning;
35 import java.util.Iterator;
37 public class BadMetadataReportProcessorTest
38 extends AbstractRepositoryReportsTestCase
40 private ArtifactFactory artifactFactory;
42 private MetadataReportProcessor badMetadataReportProcessor;
44 private ReportingDatabase reportingDatabase;
46 protected void setUp()
51 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
53 badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE, "bad-metadata" );
55 ReportGroup reportGroup = (ReportGroup) lookup( ReportGroup.ROLE, "health" );
56 reportingDatabase = new ReportingDatabase( reportGroup );
59 public void testMetadataMissingLastUpdated()
61 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
63 Versioning versioning = new Versioning();
64 versioning.addVersion( "1.0-alpha-1" );
65 versioning.addVersion( "1.0-alpha-2" );
67 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
69 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
71 Iterator failures = reportingDatabase.getMetadataIterator();
72 assertTrue( "check there is a failure", failures.hasNext() );
73 MetadataResults results = (MetadataResults) failures.next();
74 failures = results.getFailures().iterator();
75 assertTrue( "check there is a failure", failures.hasNext() );
76 Result result = (Result) failures.next();
77 assertMetadata( metadata, results );
78 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
79 assertFalse( "check no more failures", failures.hasNext() );
82 public void testMetadataMissingVersioning()
84 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
86 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, null );
88 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
90 Iterator failures = reportingDatabase.getMetadataIterator();
91 assertTrue( "check there is a failure", failures.hasNext() );
92 MetadataResults results = (MetadataResults) failures.next();
93 failures = results.getFailures().iterator();
94 assertTrue( "check there is a failure", failures.hasNext() );
95 assertMetadata( metadata, results );
96 Result result = (Result) failures.next();
97 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
98 result = (Result) failures.next();
99 boolean alpha1First = false;
100 if ( result.getReason().indexOf( "alpha-1" ) > 0 )
106 assertEquals( "check reason",
107 "Artifact version 1.0-alpha-1 found in the repository but missing in the metadata.",
108 result.getReason() );
112 assertEquals( "check reason",
113 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
114 result.getReason() );
116 result = (Result) failures.next();
119 assertEquals( "check reason",
120 "Artifact version 1.0-alpha-1 found in the repository but missing in the metadata.",
121 result.getReason() );
125 assertEquals( "check reason",
126 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
127 result.getReason() );
129 assertFalse( "check no more failures", failures.hasNext() );
132 public void testSnapshotMetadataMissingVersioning()
135 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
137 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
139 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
141 Iterator failures = reportingDatabase.getMetadataIterator();
142 assertTrue( "check there is a failure", failures.hasNext() );
143 MetadataResults results = (MetadataResults) failures.next();
144 failures = results.getFailures().iterator();
145 assertTrue( "check there is a failure", failures.hasNext() );
146 assertMetadata( metadata, results );
147 Result result = (Result) failures.next();
148 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
149 assertFalse( "check no more failures", failures.hasNext() );
152 public void testMetadataValidVersions()
154 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
156 Versioning versioning = new Versioning();
157 versioning.addVersion( "1.0-alpha-1" );
158 versioning.addVersion( "1.0-alpha-2" );
159 versioning.setLastUpdated( "20050611.202020" );
161 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
163 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
165 Iterator failures = reportingDatabase.getMetadataIterator();
166 assertFalse( "check there are no failures", failures.hasNext() );
169 public void testMetadataMissingADirectory()
171 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
173 Versioning versioning = new Versioning();
174 versioning.addVersion( "1.0-alpha-1" );
175 versioning.setLastUpdated( "20050611.202020" );
177 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
179 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
181 Iterator failures = reportingDatabase.getMetadataIterator();
182 assertTrue( "check there is a failure", failures.hasNext() );
183 MetadataResults results = (MetadataResults) failures.next();
184 failures = results.getFailures().iterator();
185 assertTrue( "check there is a failure", failures.hasNext() );
186 Result result = (Result) failures.next();
187 assertMetadata( metadata, results );
188 // TODO: should be more robust
189 assertEquals( "check reason",
190 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
191 result.getReason() );
192 assertFalse( "check no more failures", failures.hasNext() );
195 public void testMetadataInvalidArtifactVersion()
197 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
199 Versioning versioning = new Versioning();
200 versioning.addVersion( "1.0-alpha-1" );
201 versioning.addVersion( "1.0-alpha-2" );
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 assertFalse( "check no more failures", failures.hasNext() );
223 public void testMoreThanOneMetadataVersionErrors()
225 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
227 Versioning versioning = new Versioning();
228 versioning.addVersion( "1.0-alpha-1" );
229 versioning.addVersion( "1.0-alpha-3" );
230 versioning.setLastUpdated( "20050611.202020" );
232 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
234 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
236 Iterator failures = reportingDatabase.getMetadataIterator();
237 assertTrue( "check there is a failure", failures.hasNext() );
238 MetadataResults results = (MetadataResults) failures.next();
239 failures = results.getFailures().iterator();
240 assertTrue( "check there is a failure", failures.hasNext() );
241 Result result = (Result) failures.next();
242 assertMetadata( metadata, results );
243 // TODO: should be more robust
244 assertEquals( "check reason",
245 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
246 result.getReason() );
247 assertTrue( "check there is a 2nd failure", failures.hasNext() );
248 result = (Result) failures.next();
249 // TODO: should be more robust
250 assertEquals( "check reason",
251 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
252 result.getReason() );
253 assertFalse( "check no more failures", failures.hasNext() );
256 public void testValidPluginMetadata()
258 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
259 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
260 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
262 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
264 Iterator failures = reportingDatabase.getMetadataIterator();
265 assertFalse( "check there are no failures", failures.hasNext() );
268 public void testMissingMetadataPlugin()
270 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
271 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
272 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
273 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
275 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
277 Iterator failures = reportingDatabase.getMetadataIterator();
278 assertTrue( "check there is a failure", failures.hasNext() );
279 MetadataResults results = (MetadataResults) failures.next();
280 failures = results.getFailures().iterator();
281 assertTrue( "check there is a failure", failures.hasNext() );
282 Result result = (Result) failures.next();
283 // TODO: should be more robust
284 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
285 result.getReason() );
286 assertFalse( "check no more failures", failures.hasNext() );
289 public void testIncompletePluginMetadata()
291 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
292 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
294 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
296 Iterator failures = reportingDatabase.getMetadataIterator();
297 assertTrue( "check there is a failure", failures.hasNext() );
298 MetadataResults results = (MetadataResults) failures.next();
299 failures = results.getFailures().iterator();
300 assertTrue( "check there is a failure", failures.hasNext() );
301 Result result = (Result) failures.next();
302 // TODO: should be more robust
303 assertEquals( "check reason",
304 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
305 result.getReason() );
306 assertFalse( "check no more failures", failures.hasNext() );
309 public void testInvalidPluginArtifactId()
311 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
312 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
313 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
314 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
315 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
317 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
319 Iterator failures = reportingDatabase.getMetadataIterator();
320 assertTrue( "check there is a failure", failures.hasNext() );
321 MetadataResults results = (MetadataResults) failures.next();
322 failures = results.getFailures().iterator();
323 assertTrue( "check there is a failure", failures.hasNext() );
324 Result result = (Result) failures.next();
325 // TODO: should be more robust
326 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default3",
327 result.getReason() );
328 assertTrue( "check there is a 2nd failure", failures.hasNext() );
329 result = (Result) failures.next();
330 // TODO: should be more robust
331 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default4",
332 result.getReason() );
333 assertFalse( "check no more failures", failures.hasNext() );
336 public void testInvalidPluginPrefix()
338 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
339 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
340 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
342 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
344 Iterator failures = reportingDatabase.getMetadataIterator();
345 assertTrue( "check there is a failure", failures.hasNext() );
346 MetadataResults results = (MetadataResults) failures.next();
347 failures = results.getFailures().iterator();
348 assertTrue( "check there is a failure", failures.hasNext() );
349 Result result = (Result) failures.next();
350 // TODO: should be more robust
351 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
352 assertTrue( "check there is a 2nd failure", failures.hasNext() );
353 result = (Result) failures.next();
354 // TODO: should be more robust
355 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
356 result.getReason() );
357 assertFalse( "check no more failures", failures.hasNext() );
360 public void testDuplicatePluginPrefixes()
362 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
363 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
364 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
366 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
368 Iterator failures = reportingDatabase.getMetadataIterator();
369 assertTrue( "check there is a failure", failures.hasNext() );
370 MetadataResults results = (MetadataResults) failures.next();
371 failures = results.getFailures().iterator();
372 assertTrue( "check there is a failure", failures.hasNext() );
373 Result result = (Result) failures.next();
374 // TODO: should be more robust
375 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
376 assertFalse( "check no more failures", failures.hasNext() );
379 public void testValidSnapshotMetadata()
382 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
384 Snapshot snapshot = new Snapshot();
385 snapshot.setBuildNumber( 1 );
386 snapshot.setTimestamp( "20050611.202024" );
388 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
390 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
392 Iterator failures = reportingDatabase.getMetadataIterator();
393 assertFalse( "check there are no failures", failures.hasNext() );
396 public void testInvalidSnapshotMetadata()
399 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
401 Snapshot snapshot = new Snapshot();
402 snapshot.setBuildNumber( 2 );
403 snapshot.setTimestamp( "20050611.202024" );
405 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
407 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
409 Iterator failures = reportingDatabase.getMetadataIterator();
410 assertTrue( "check there is a failure", failures.hasNext() );
411 MetadataResults results = (MetadataResults) failures.next();
412 failures = results.getFailures().iterator();
413 assertTrue( "check there is a failure", failures.hasNext() );
414 Result result = (Result) failures.next();
415 assertMetadata( metadata, results );
416 // TODO: should be more robust
417 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
418 result.getReason() );
419 assertFalse( "check no more failures", failures.hasNext() );
422 private static void assertMetadata( RepositoryMetadata metadata, MetadataResults results )
424 assertEquals( "check metadata", metadata.getGroupId(), results.getGroupId() );
425 assertEquals( "check metadata", metadata.getArtifactId(), results.getArtifactId() );
426 assertEquals( "check metadata", metadata.getBaseVersion(), results.getVersion() );
429 private Plugin createMetadataPlugin( String artifactId, String prefix )
431 Plugin plugin = new Plugin();
432 plugin.setArtifactId( artifactId );
433 plugin.setName( artifactId );
434 plugin.setPrefix( prefix );