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 reporter = new ReportingDatabase();
42 protected void setUp()
47 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
49 badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE );
52 public void testMetadataMissingLastUpdated()
54 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
56 Versioning versioning = new Versioning();
57 versioning.addVersion( "1.0-alpha-1" );
58 versioning.addVersion( "1.0-alpha-2" );
60 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
62 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
64 Iterator failures = reporter.getMetadataIterator();
65 assertTrue( "check there is a failure", failures.hasNext() );
66 MetadataResults results = (MetadataResults) failures.next();
67 failures = results.getFailures().iterator();
68 assertTrue( "check there is a failure", failures.hasNext() );
69 Result result = (Result) failures.next();
70 assertMetadata( metadata, results );
71 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
72 assertFalse( "check no more failures", failures.hasNext() );
75 public void testMetadataMissingVersioning()
77 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
79 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, null );
81 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
83 Iterator failures = reporter.getMetadataIterator();
84 assertTrue( "check there is a failure", failures.hasNext() );
85 MetadataResults results = (MetadataResults) failures.next();
86 failures = results.getFailures().iterator();
87 assertTrue( "check there is a failure", failures.hasNext() );
88 assertMetadata( metadata, results );
89 Result result = (Result) failures.next();
90 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
91 result = (Result) failures.next();
92 assertEquals( "check reason",
93 "Artifact version 1.0-alpha-1 found in the repository but missing in the metadata.",
95 result = (Result) failures.next();
96 assertEquals( "check reason",
97 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
99 assertFalse( "check no more failures", failures.hasNext() );
102 public void testSnapshotMetadataMissingVersioning()
105 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
107 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
109 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
111 Iterator failures = reporter.getMetadataIterator();
112 assertTrue( "check there is a failure", failures.hasNext() );
113 MetadataResults results = (MetadataResults) failures.next();
114 failures = results.getFailures().iterator();
115 assertTrue( "check there is a failure", failures.hasNext() );
116 assertMetadata( metadata, results );
117 Result result = (Result) failures.next();
118 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
119 assertFalse( "check no more failures", failures.hasNext() );
122 public void testMetadataValidVersions()
124 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
126 Versioning versioning = new Versioning();
127 versioning.addVersion( "1.0-alpha-1" );
128 versioning.addVersion( "1.0-alpha-2" );
129 versioning.setLastUpdated( "20050611.202020" );
131 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
133 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
135 Iterator failures = reporter.getMetadataIterator();
136 assertFalse( "check there are no failures", failures.hasNext() );
139 public void testMetadataMissingADirectory()
141 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
143 Versioning versioning = new Versioning();
144 versioning.addVersion( "1.0-alpha-1" );
145 versioning.setLastUpdated( "20050611.202020" );
147 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
149 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
151 Iterator failures = reporter.getMetadataIterator();
152 assertTrue( "check there is a failure", failures.hasNext() );
153 MetadataResults results = (MetadataResults) failures.next();
154 failures = results.getFailures().iterator();
155 assertTrue( "check there is a failure", failures.hasNext() );
156 Result result = (Result) failures.next();
157 assertMetadata( metadata, results );
158 // TODO: should be more robust
159 assertEquals( "check reason",
160 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
161 result.getReason() );
162 assertFalse( "check no more failures", failures.hasNext() );
165 public void testMetadataInvalidArtifactVersion()
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.addVersion( "1.0-alpha-2" );
172 versioning.addVersion( "1.0-alpha-3" );
173 versioning.setLastUpdated( "20050611.202020" );
175 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
177 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
179 Iterator failures = reporter.getMetadataIterator();
180 assertTrue( "check there is a failure", failures.hasNext() );
181 MetadataResults results = (MetadataResults) failures.next();
182 failures = results.getFailures().iterator();
183 assertTrue( "check there is a failure", failures.hasNext() );
184 Result result = (Result) failures.next();
185 assertMetadata( metadata, results );
186 // TODO: should be more robust
187 assertEquals( "check reason",
188 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
189 result.getReason() );
190 assertFalse( "check no more failures", failures.hasNext() );
193 public void testMoreThanOneMetadataVersionErrors()
195 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
197 Versioning versioning = new Versioning();
198 versioning.addVersion( "1.0-alpha-1" );
199 versioning.addVersion( "1.0-alpha-3" );
200 versioning.setLastUpdated( "20050611.202020" );
202 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
204 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
206 Iterator failures = reporter.getMetadataIterator();
207 assertTrue( "check there is a failure", failures.hasNext() );
208 MetadataResults results = (MetadataResults) failures.next();
209 failures = results.getFailures().iterator();
210 assertTrue( "check there is a failure", failures.hasNext() );
211 Result result = (Result) failures.next();
212 assertMetadata( metadata, results );
213 // TODO: should be more robust
214 assertEquals( "check reason",
215 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
216 result.getReason() );
217 assertTrue( "check there is a 2nd failure", failures.hasNext() );
218 result = (Result) failures.next();
219 // TODO: should be more robust
220 assertEquals( "check reason",
221 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
222 result.getReason() );
223 assertFalse( "check no more failures", failures.hasNext() );
226 public void testValidPluginMetadata()
228 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
229 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
230 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
232 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
234 Iterator failures = reporter.getMetadataIterator();
235 assertFalse( "check there are no failures", failures.hasNext() );
238 public void testMissingMetadataPlugin()
240 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
241 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
242 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
243 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
245 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
247 Iterator failures = reporter.getMetadataIterator();
248 assertTrue( "check there is a failure", failures.hasNext() );
249 MetadataResults results = (MetadataResults) failures.next();
250 failures = results.getFailures().iterator();
251 assertTrue( "check there is a failure", failures.hasNext() );
252 Result result = (Result) failures.next();
253 // TODO: should be more robust
254 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
255 result.getReason() );
256 assertFalse( "check no more failures", failures.hasNext() );
259 public void testIncompletePluginMetadata()
261 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
262 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
264 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
266 Iterator failures = reporter.getMetadataIterator();
267 assertTrue( "check there is a failure", failures.hasNext() );
268 MetadataResults results = (MetadataResults) failures.next();
269 failures = results.getFailures().iterator();
270 assertTrue( "check there is a failure", failures.hasNext() );
271 Result result = (Result) failures.next();
272 // TODO: should be more robust
273 assertEquals( "check reason",
274 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
275 result.getReason() );
276 assertFalse( "check no more failures", failures.hasNext() );
279 public void testInvalidPluginArtifactId()
281 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
282 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
283 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
284 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
285 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
287 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
289 Iterator failures = reporter.getMetadataIterator();
290 assertTrue( "check there is a failure", failures.hasNext() );
291 MetadataResults results = (MetadataResults) failures.next();
292 failures = results.getFailures().iterator();
293 assertTrue( "check there is a failure", failures.hasNext() );
294 Result result = (Result) failures.next();
295 // TODO: should be more robust
296 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default3",
297 result.getReason() );
298 assertTrue( "check there is a 2nd failure", failures.hasNext() );
299 result = (Result) failures.next();
300 // TODO: should be more robust
301 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default4",
302 result.getReason() );
303 assertFalse( "check no more failures", failures.hasNext() );
306 public void testInvalidPluginPrefix()
308 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
309 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
310 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
312 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
314 Iterator failures = reporter.getMetadataIterator();
315 assertTrue( "check there is a failure", failures.hasNext() );
316 MetadataResults results = (MetadataResults) failures.next();
317 failures = results.getFailures().iterator();
318 assertTrue( "check there is a failure", failures.hasNext() );
319 Result result = (Result) failures.next();
320 // TODO: should be more robust
321 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
322 assertTrue( "check there is a 2nd failure", failures.hasNext() );
323 result = (Result) failures.next();
324 // TODO: should be more robust
325 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
326 result.getReason() );
327 assertFalse( "check no more failures", failures.hasNext() );
330 public void testDuplicatePluginPrefixes()
332 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
333 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
334 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
336 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
338 Iterator failures = reporter.getMetadataIterator();
339 assertTrue( "check there is a failure", failures.hasNext() );
340 MetadataResults results = (MetadataResults) failures.next();
341 failures = results.getFailures().iterator();
342 assertTrue( "check there is a failure", failures.hasNext() );
343 Result result = (Result) failures.next();
344 // TODO: should be more robust
345 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
346 assertFalse( "check no more failures", failures.hasNext() );
349 public void testValidSnapshotMetadata()
352 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
354 Snapshot snapshot = new Snapshot();
355 snapshot.setBuildNumber( 1 );
356 snapshot.setTimestamp( "20050611.202024" );
358 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
360 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
362 Iterator failures = reporter.getMetadataIterator();
363 assertFalse( "check there are no failures", failures.hasNext() );
366 public void testInvalidSnapshotMetadata()
369 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
371 Snapshot snapshot = new Snapshot();
372 snapshot.setBuildNumber( 2 );
373 snapshot.setTimestamp( "20050611.202024" );
375 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
377 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
379 Iterator failures = reporter.getMetadataIterator();
380 assertTrue( "check there is a failure", failures.hasNext() );
381 MetadataResults results = (MetadataResults) failures.next();
382 failures = results.getFailures().iterator();
383 assertTrue( "check there is a failure", failures.hasNext() );
384 Result result = (Result) failures.next();
385 assertMetadata( metadata, results );
386 // TODO: should be more robust
387 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
388 result.getReason() );
389 assertFalse( "check no more failures", failures.hasNext() );
392 private static void assertMetadata( RepositoryMetadata metadata, MetadataResults results )
394 assertEquals( "check metadata", metadata.getGroupId(), results.getGroupId() );
395 assertEquals( "check metadata", metadata.getArtifactId(), results.getArtifactId() );
396 assertEquals( "check metadata", metadata.getBaseVersion(), results.getVersion() );
399 private Plugin createMetadataPlugin( String artifactId, String prefix )
401 Plugin plugin = new Plugin();
402 plugin.setArtifactId( artifactId );
403 plugin.setName( artifactId );
404 plugin.setPrefix( prefix );