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 boolean alpha1First = false;
96 if ( result.getReason().indexOf( "alpha-1" ) > 0 )
102 assertEquals( "check reason",
103 "Artifact version 1.0-alpha-1 found in the repository but missing in the metadata.",
104 result.getReason() );
108 assertEquals( "check reason",
109 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
110 result.getReason() );
112 result = (Result) failures.next();
115 assertEquals( "check reason",
116 "Artifact version 1.0-alpha-1 found in the repository but missing in the metadata.",
117 result.getReason() );
121 assertEquals( "check reason",
122 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
123 result.getReason() );
125 assertFalse( "check no more failures", failures.hasNext() );
128 public void testSnapshotMetadataMissingVersioning()
131 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
133 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
135 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
137 Iterator failures = reportingDatabase.getMetadataIterator();
138 assertTrue( "check there is a failure", failures.hasNext() );
139 MetadataResults results = (MetadataResults) failures.next();
140 failures = results.getFailures().iterator();
141 assertTrue( "check there is a failure", failures.hasNext() );
142 assertMetadata( metadata, results );
143 Result result = (Result) failures.next();
144 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
145 assertFalse( "check no more failures", failures.hasNext() );
148 public void testMetadataValidVersions()
150 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
152 Versioning versioning = new Versioning();
153 versioning.addVersion( "1.0-alpha-1" );
154 versioning.addVersion( "1.0-alpha-2" );
155 versioning.setLastUpdated( "20050611.202020" );
157 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
159 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
161 Iterator failures = reportingDatabase.getMetadataIterator();
162 assertFalse( "check there are no failures", failures.hasNext() );
165 public void testMetadataMissingADirectory()
167 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
169 Versioning versioning = new Versioning();
170 versioning.addVersion( "1.0-alpha-1" );
171 versioning.setLastUpdated( "20050611.202020" );
173 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
175 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
177 Iterator failures = reportingDatabase.getMetadataIterator();
178 assertTrue( "check there is a failure", failures.hasNext() );
179 MetadataResults results = (MetadataResults) failures.next();
180 failures = results.getFailures().iterator();
181 assertTrue( "check there is a failure", failures.hasNext() );
182 Result result = (Result) failures.next();
183 assertMetadata( metadata, results );
184 // TODO: should be more robust
185 assertEquals( "check reason",
186 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
187 result.getReason() );
188 assertFalse( "check no more failures", failures.hasNext() );
191 public void testMetadataInvalidArtifactVersion()
193 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
195 Versioning versioning = new Versioning();
196 versioning.addVersion( "1.0-alpha-1" );
197 versioning.addVersion( "1.0-alpha-2" );
198 versioning.addVersion( "1.0-alpha-3" );
199 versioning.setLastUpdated( "20050611.202020" );
201 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
203 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
205 Iterator failures = reportingDatabase.getMetadataIterator();
206 assertTrue( "check there is a failure", failures.hasNext() );
207 MetadataResults results = (MetadataResults) failures.next();
208 failures = results.getFailures().iterator();
209 assertTrue( "check there is a failure", failures.hasNext() );
210 Result result = (Result) failures.next();
211 assertMetadata( metadata, results );
212 // TODO: should be more robust
213 assertEquals( "check reason",
214 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
215 result.getReason() );
216 assertFalse( "check no more failures", failures.hasNext() );
219 public void testMoreThanOneMetadataVersionErrors()
221 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
223 Versioning versioning = new Versioning();
224 versioning.addVersion( "1.0-alpha-1" );
225 versioning.addVersion( "1.0-alpha-3" );
226 versioning.setLastUpdated( "20050611.202020" );
228 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
230 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
232 Iterator failures = reportingDatabase.getMetadataIterator();
233 assertTrue( "check there is a failure", failures.hasNext() );
234 MetadataResults results = (MetadataResults) failures.next();
235 failures = results.getFailures().iterator();
236 assertTrue( "check there is a failure", failures.hasNext() );
237 Result result = (Result) failures.next();
238 assertMetadata( metadata, results );
239 // TODO: should be more robust
240 assertEquals( "check reason",
241 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
242 result.getReason() );
243 assertTrue( "check there is a 2nd failure", failures.hasNext() );
244 result = (Result) failures.next();
245 // TODO: should be more robust
246 assertEquals( "check reason",
247 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
248 result.getReason() );
249 assertFalse( "check no more failures", failures.hasNext() );
252 public void testValidPluginMetadata()
254 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
255 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
256 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
258 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
260 Iterator failures = reportingDatabase.getMetadataIterator();
261 assertFalse( "check there are no failures", failures.hasNext() );
264 public void testMissingMetadataPlugin()
266 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
267 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
268 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
269 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
271 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
273 Iterator failures = reportingDatabase.getMetadataIterator();
274 assertTrue( "check there is a failure", failures.hasNext() );
275 MetadataResults results = (MetadataResults) failures.next();
276 failures = results.getFailures().iterator();
277 assertTrue( "check there is a failure", failures.hasNext() );
278 Result result = (Result) failures.next();
279 // TODO: should be more robust
280 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
281 result.getReason() );
282 assertFalse( "check no more failures", failures.hasNext() );
285 public void testIncompletePluginMetadata()
287 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
288 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
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",
300 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
301 result.getReason() );
302 assertFalse( "check no more failures", failures.hasNext() );
305 public void testInvalidPluginArtifactId()
307 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
308 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
309 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
310 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
311 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
313 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
315 Iterator failures = reportingDatabase.getMetadataIterator();
316 assertTrue( "check there is a failure", failures.hasNext() );
317 MetadataResults results = (MetadataResults) failures.next();
318 failures = results.getFailures().iterator();
319 assertTrue( "check there is a failure", failures.hasNext() );
320 Result result = (Result) failures.next();
321 // TODO: should be more robust
322 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default3",
323 result.getReason() );
324 assertTrue( "check there is a 2nd failure", failures.hasNext() );
325 result = (Result) failures.next();
326 // TODO: should be more robust
327 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default4",
328 result.getReason() );
329 assertFalse( "check no more failures", failures.hasNext() );
332 public void testInvalidPluginPrefix()
334 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
335 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
336 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
338 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
340 Iterator failures = reportingDatabase.getMetadataIterator();
341 assertTrue( "check there is a failure", failures.hasNext() );
342 MetadataResults results = (MetadataResults) failures.next();
343 failures = results.getFailures().iterator();
344 assertTrue( "check there is a failure", failures.hasNext() );
345 Result result = (Result) failures.next();
346 // TODO: should be more robust
347 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
348 assertTrue( "check there is a 2nd failure", failures.hasNext() );
349 result = (Result) failures.next();
350 // TODO: should be more robust
351 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
352 result.getReason() );
353 assertFalse( "check no more failures", failures.hasNext() );
356 public void testDuplicatePluginPrefixes()
358 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
359 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
360 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
362 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
364 Iterator failures = reportingDatabase.getMetadataIterator();
365 assertTrue( "check there is a failure", failures.hasNext() );
366 MetadataResults results = (MetadataResults) failures.next();
367 failures = results.getFailures().iterator();
368 assertTrue( "check there is a failure", failures.hasNext() );
369 Result result = (Result) failures.next();
370 // TODO: should be more robust
371 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
372 assertFalse( "check no more failures", failures.hasNext() );
375 public void testValidSnapshotMetadata()
378 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
380 Snapshot snapshot = new Snapshot();
381 snapshot.setBuildNumber( 1 );
382 snapshot.setTimestamp( "20050611.202024" );
384 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
386 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
388 Iterator failures = reportingDatabase.getMetadataIterator();
389 assertFalse( "check there are no failures", failures.hasNext() );
392 public void testInvalidSnapshotMetadata()
395 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
397 Snapshot snapshot = new Snapshot();
398 snapshot.setBuildNumber( 2 );
399 snapshot.setTimestamp( "20050611.202024" );
401 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
403 badMetadataReportProcessor.processMetadata( metadata, repository, reportingDatabase );
405 Iterator failures = reportingDatabase.getMetadataIterator();
406 assertTrue( "check there is a failure", failures.hasNext() );
407 MetadataResults results = (MetadataResults) failures.next();
408 failures = results.getFailures().iterator();
409 assertTrue( "check there is a failure", failures.hasNext() );
410 Result result = (Result) failures.next();
411 assertMetadata( metadata, results );
412 // TODO: should be more robust
413 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
414 result.getReason() );
415 assertFalse( "check no more failures", failures.hasNext() );
418 private static void assertMetadata( RepositoryMetadata metadata, MetadataResults results )
420 assertEquals( "check metadata", metadata.getGroupId(), results.getGroupId() );
421 assertEquals( "check metadata", metadata.getArtifactId(), results.getArtifactId() );
422 assertEquals( "check metadata", metadata.getBaseVersion(), results.getVersion() );
425 private Plugin createMetadataPlugin( String artifactId, String prefix )
427 Plugin plugin = new Plugin();
428 plugin.setArtifactId( artifactId );
429 plugin.setName( artifactId );
430 plugin.setPrefix( prefix );