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 testMetadataValidVersions()
104 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
106 Versioning versioning = new Versioning();
107 versioning.addVersion( "1.0-alpha-1" );
108 versioning.addVersion( "1.0-alpha-2" );
109 versioning.setLastUpdated( "20050611.202020" );
111 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
113 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
115 Iterator failures = reporter.getMetadataIterator();
116 assertFalse( "check there are no failures", failures.hasNext() );
119 public void testMetadataMissingADirectory()
121 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
123 Versioning versioning = new Versioning();
124 versioning.addVersion( "1.0-alpha-1" );
125 versioning.setLastUpdated( "20050611.202020" );
127 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
129 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
131 Iterator failures = reporter.getMetadataIterator();
132 assertTrue( "check there is a failure", failures.hasNext() );
133 MetadataResults results = (MetadataResults) failures.next();
134 failures = results.getFailures().iterator();
135 assertTrue( "check there is a failure", failures.hasNext() );
136 Result result = (Result) failures.next();
137 assertMetadata( metadata, results );
138 // TODO: should be more robust
139 assertEquals( "check reason",
140 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
141 result.getReason() );
142 assertFalse( "check no more failures", failures.hasNext() );
145 public void testMetadataInvalidArtifactVersion()
147 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
149 Versioning versioning = new Versioning();
150 versioning.addVersion( "1.0-alpha-1" );
151 versioning.addVersion( "1.0-alpha-2" );
152 versioning.addVersion( "1.0-alpha-3" );
153 versioning.setLastUpdated( "20050611.202020" );
155 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
157 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
159 Iterator failures = reporter.getMetadataIterator();
160 assertTrue( "check there is a failure", failures.hasNext() );
161 MetadataResults results = (MetadataResults) failures.next();
162 failures = results.getFailures().iterator();
163 assertTrue( "check there is a failure", failures.hasNext() );
164 Result result = (Result) failures.next();
165 assertMetadata( metadata, results );
166 // TODO: should be more robust
167 assertEquals( "check reason",
168 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
169 result.getReason() );
170 assertFalse( "check no more failures", failures.hasNext() );
173 public void testMoreThanOneMetadataVersionErrors()
175 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
177 Versioning versioning = new Versioning();
178 versioning.addVersion( "1.0-alpha-1" );
179 versioning.addVersion( "1.0-alpha-3" );
180 versioning.setLastUpdated( "20050611.202020" );
182 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
184 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
186 Iterator failures = reporter.getMetadataIterator();
187 assertTrue( "check there is a failure", failures.hasNext() );
188 MetadataResults results = (MetadataResults) failures.next();
189 failures = results.getFailures().iterator();
190 assertTrue( "check there is a failure", failures.hasNext() );
191 Result result = (Result) failures.next();
192 assertMetadata( metadata, results );
193 // TODO: should be more robust
194 assertEquals( "check reason",
195 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
196 result.getReason() );
197 assertTrue( "check there is a 2nd failure", failures.hasNext() );
198 result = (Result) failures.next();
199 // TODO: should be more robust
200 assertEquals( "check reason",
201 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
202 result.getReason() );
203 assertFalse( "check no more failures", failures.hasNext() );
206 public void testValidPluginMetadata()
208 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
209 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
210 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
212 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
214 Iterator failures = reporter.getMetadataIterator();
215 assertFalse( "check there are no failures", failures.hasNext() );
218 public void testMissingMetadataPlugin()
220 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
221 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
222 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
223 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
225 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
227 Iterator failures = reporter.getMetadataIterator();
228 assertTrue( "check there is a failure", failures.hasNext() );
229 MetadataResults results = (MetadataResults) failures.next();
230 failures = results.getFailures().iterator();
231 assertTrue( "check there is a failure", failures.hasNext() );
232 Result result = (Result) failures.next();
233 // TODO: should be more robust
234 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
235 result.getReason() );
236 assertFalse( "check no more failures", failures.hasNext() );
239 public void testIncompletePluginMetadata()
241 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
242 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
244 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
246 Iterator failures = reporter.getMetadataIterator();
247 assertTrue( "check there is a failure", failures.hasNext() );
248 MetadataResults results = (MetadataResults) failures.next();
249 failures = results.getFailures().iterator();
250 assertTrue( "check there is a failure", failures.hasNext() );
251 Result result = (Result) failures.next();
252 // TODO: should be more robust
253 assertEquals( "check reason",
254 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
255 result.getReason() );
256 assertFalse( "check no more failures", failures.hasNext() );
259 public void testInvalidPluginArtifactId()
261 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
262 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
263 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
264 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
265 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
267 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
269 Iterator failures = reporter.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", "Missing or empty artifactId in group metadata for plugin default3",
277 result.getReason() );
278 assertTrue( "check there is a 2nd failure", failures.hasNext() );
279 result = (Result) failures.next();
280 // TODO: should be more robust
281 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default4",
282 result.getReason() );
283 assertFalse( "check no more failures", failures.hasNext() );
286 public void testInvalidPluginPrefix()
288 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
289 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
290 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
292 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
294 Iterator failures = reporter.getMetadataIterator();
295 assertTrue( "check there is a failure", failures.hasNext() );
296 MetadataResults results = (MetadataResults) failures.next();
297 failures = results.getFailures().iterator();
298 assertTrue( "check there is a failure", failures.hasNext() );
299 Result result = (Result) failures.next();
300 // TODO: should be more robust
301 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
302 assertTrue( "check there is a 2nd failure", failures.hasNext() );
303 result = (Result) failures.next();
304 // TODO: should be more robust
305 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
306 result.getReason() );
307 assertFalse( "check no more failures", failures.hasNext() );
310 public void testDuplicatePluginPrefixes()
312 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
313 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
314 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
316 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
318 Iterator failures = reporter.getMetadataIterator();
319 assertTrue( "check there is a failure", failures.hasNext() );
320 MetadataResults results = (MetadataResults) failures.next();
321 failures = results.getFailures().iterator();
322 assertTrue( "check there is a failure", failures.hasNext() );
323 Result result = (Result) failures.next();
324 // TODO: should be more robust
325 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
326 assertFalse( "check no more failures", failures.hasNext() );
329 public void testValidSnapshotMetadata()
332 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
334 Snapshot snapshot = new Snapshot();
335 snapshot.setBuildNumber( 1 );
336 snapshot.setTimestamp( "20050611.202024" );
338 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
340 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
342 Iterator failures = reporter.getMetadataIterator();
343 assertFalse( "check there are no failures", failures.hasNext() );
346 public void testInvalidSnapshotMetadata()
349 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
351 Snapshot snapshot = new Snapshot();
352 snapshot.setBuildNumber( 2 );
353 snapshot.setTimestamp( "20050611.202024" );
355 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
357 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
359 Iterator failures = reporter.getMetadataIterator();
360 assertTrue( "check there is a failure", failures.hasNext() );
361 MetadataResults results = (MetadataResults) failures.next();
362 failures = results.getFailures().iterator();
363 assertTrue( "check there is a failure", failures.hasNext() );
364 Result result = (Result) failures.next();
365 assertMetadata( metadata, results );
366 // TODO: should be more robust
367 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
368 result.getReason() );
369 assertFalse( "check no more failures", failures.hasNext() );
372 private static void assertMetadata( RepositoryMetadata metadata, MetadataResults results )
374 assertEquals( "check metadata", metadata.getGroupId(), results.getGroupId() );
375 assertEquals( "check metadata", metadata.getArtifactId(), results.getArtifactId() );
376 assertEquals( "check metadata", metadata.getBaseVersion(), results.getVersion() );
379 private Plugin createMetadataPlugin( String artifactId, String prefix )
381 Plugin plugin = new Plugin();
382 plugin.setArtifactId( artifactId );
383 plugin.setName( artifactId );
384 plugin.setPrefix( prefix );