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 testMetadataValidVersions()
77 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
79 Versioning versioning = new Versioning();
80 versioning.addVersion( "1.0-alpha-1" );
81 versioning.addVersion( "1.0-alpha-2" );
82 versioning.setLastUpdated( "20050611.202020" );
84 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
86 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
88 Iterator failures = reporter.getMetadataIterator();
89 assertFalse( "check there are no failures", failures.hasNext() );
92 public void testMetadataMissingADirectory()
94 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
96 Versioning versioning = new Versioning();
97 versioning.addVersion( "1.0-alpha-1" );
98 versioning.setLastUpdated( "20050611.202020" );
100 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
102 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
104 Iterator failures = reporter.getMetadataIterator();
105 assertTrue( "check there is a failure", failures.hasNext() );
106 MetadataResults results = (MetadataResults) failures.next();
107 failures = results.getFailures().iterator();
108 assertTrue( "check there is a failure", failures.hasNext() );
109 Result result = (Result) failures.next();
110 assertMetadata( metadata, results );
111 // TODO: should be more robust
112 assertEquals( "check reason",
113 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
114 result.getReason() );
115 assertFalse( "check no more failures", failures.hasNext() );
118 public void testMetadataInvalidArtifactVersion()
120 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
122 Versioning versioning = new Versioning();
123 versioning.addVersion( "1.0-alpha-1" );
124 versioning.addVersion( "1.0-alpha-2" );
125 versioning.addVersion( "1.0-alpha-3" );
126 versioning.setLastUpdated( "20050611.202020" );
128 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
130 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
132 Iterator failures = reporter.getMetadataIterator();
133 assertTrue( "check there is a failure", failures.hasNext() );
134 MetadataResults results = (MetadataResults) failures.next();
135 failures = results.getFailures().iterator();
136 assertTrue( "check there is a failure", failures.hasNext() );
137 Result result = (Result) failures.next();
138 assertMetadata( metadata, results );
139 // TODO: should be more robust
140 assertEquals( "check reason",
141 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
142 result.getReason() );
143 assertFalse( "check no more failures", failures.hasNext() );
146 public void testMoreThanOneMetadataVersionErrors()
148 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
150 Versioning versioning = new Versioning();
151 versioning.addVersion( "1.0-alpha-1" );
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 assertTrue( "check there is a 2nd failure", failures.hasNext() );
171 result = (Result) failures.next();
172 // TODO: should be more robust
173 assertEquals( "check reason",
174 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
175 result.getReason() );
176 assertFalse( "check no more failures", failures.hasNext() );
179 public void testValidPluginMetadata()
181 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
182 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
183 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
185 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
187 Iterator failures = reporter.getMetadataIterator();
188 assertFalse( "check there are no failures", failures.hasNext() );
191 public void testMissingMetadataPlugin()
193 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
194 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
195 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
196 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
198 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
200 Iterator failures = reporter.getMetadataIterator();
201 assertTrue( "check there is a failure", failures.hasNext() );
202 MetadataResults results = (MetadataResults) failures.next();
203 failures = results.getFailures().iterator();
204 assertTrue( "check there is a failure", failures.hasNext() );
205 Result result = (Result) failures.next();
206 // TODO: should be more robust
207 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
208 result.getReason() );
209 assertFalse( "check no more failures", failures.hasNext() );
212 public void testIncompletePluginMetadata()
214 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
215 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
217 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
219 Iterator failures = reporter.getMetadataIterator();
220 assertTrue( "check there is a failure", failures.hasNext() );
221 MetadataResults results = (MetadataResults) failures.next();
222 failures = results.getFailures().iterator();
223 assertTrue( "check there is a failure", failures.hasNext() );
224 Result result = (Result) failures.next();
225 // TODO: should be more robust
226 assertEquals( "check reason",
227 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
228 result.getReason() );
229 assertFalse( "check no more failures", failures.hasNext() );
232 public void testInvalidPluginArtifactId()
234 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
235 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
236 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
237 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
238 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
240 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
242 Iterator failures = reporter.getMetadataIterator();
243 assertTrue( "check there is a failure", failures.hasNext() );
244 MetadataResults results = (MetadataResults) failures.next();
245 failures = results.getFailures().iterator();
246 assertTrue( "check there is a failure", failures.hasNext() );
247 Result result = (Result) failures.next();
248 // TODO: should be more robust
249 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default3",
250 result.getReason() );
251 assertTrue( "check there is a 2nd failure", failures.hasNext() );
252 result = (Result) failures.next();
253 // TODO: should be more robust
254 assertEquals( "check reason", "Missing or empty artifactId in group metadata for plugin default4",
255 result.getReason() );
256 assertFalse( "check no more failures", failures.hasNext() );
259 public void testInvalidPluginPrefix()
261 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
262 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
263 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
265 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
267 Iterator failures = reporter.getMetadataIterator();
268 assertTrue( "check there is a failure", failures.hasNext() );
269 MetadataResults results = (MetadataResults) failures.next();
270 failures = results.getFailures().iterator();
271 assertTrue( "check there is a failure", failures.hasNext() );
272 Result result = (Result) failures.next();
273 // TODO: should be more robust
274 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
275 assertTrue( "check there is a 2nd failure", failures.hasNext() );
276 result = (Result) failures.next();
277 // TODO: should be more robust
278 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
279 result.getReason() );
280 assertFalse( "check no more failures", failures.hasNext() );
283 public void testDuplicatePluginPrefixes()
285 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
286 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
287 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
289 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
291 Iterator failures = reporter.getMetadataIterator();
292 assertTrue( "check there is a failure", failures.hasNext() );
293 MetadataResults results = (MetadataResults) failures.next();
294 failures = results.getFailures().iterator();
295 assertTrue( "check there is a failure", failures.hasNext() );
296 Result result = (Result) failures.next();
297 // TODO: should be more robust
298 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
299 assertFalse( "check no more failures", failures.hasNext() );
302 public void testValidSnapshotMetadata()
305 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
307 Snapshot snapshot = new Snapshot();
308 snapshot.setBuildNumber( 1 );
309 snapshot.setTimestamp( "20050611.202024" );
311 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
313 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
315 Iterator failures = reporter.getMetadataIterator();
316 assertFalse( "check there are no failures", failures.hasNext() );
319 public void testInvalidSnapshotMetadata()
322 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
324 Snapshot snapshot = new Snapshot();
325 snapshot.setBuildNumber( 2 );
326 snapshot.setTimestamp( "20050611.202024" );
328 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
330 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
332 Iterator failures = reporter.getMetadataIterator();
333 assertTrue( "check there is a failure", failures.hasNext() );
334 MetadataResults results = (MetadataResults) failures.next();
335 failures = results.getFailures().iterator();
336 assertTrue( "check there is a failure", failures.hasNext() );
337 Result result = (Result) failures.next();
338 assertMetadata( metadata, results );
339 // TODO: should be more robust
340 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
341 result.getReason() );
342 assertFalse( "check no more failures", failures.hasNext() );
345 private static void assertMetadata( RepositoryMetadata metadata, MetadataResults results )
347 assertEquals( "check metadata", metadata.getGroupId(), results.getGroupId() );
348 assertEquals( "check metadata", metadata.getArtifactId(), results.getArtifactId() );
349 assertEquals( "check metadata", metadata.getBaseVersion(), results.getVersion() );
352 private Plugin createMetadataPlugin( String artifactId, String prefix )
354 Plugin plugin = new Plugin();
355 plugin.setArtifactId( artifactId );
356 plugin.setName( artifactId );
357 plugin.setPrefix( prefix );