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.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
22 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
23 import org.apache.maven.artifact.repository.metadata.Plugin;
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.Snapshot;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.Versioning;
29 import java.util.Iterator;
32 * @todo??? should use MetadataXpp3Reader instead ?
34 public class BadMetadataReportProcessorTest
35 extends AbstractRepositoryReportsTestCase
37 private ArtifactFactory artifactFactory;
39 private MetadataReportProcessor badMetadataReportProcessor;
41 protected void setUp()
46 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
48 badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE );
51 public void testMetadataMissingLastUpdated()
52 throws ReportProcessorException
54 ArtifactReporter reporter = new MockArtifactReporter();
56 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
58 Versioning versioning = new Versioning();
59 versioning.addVersion( "1.0-alpha-1" );
60 versioning.addVersion( "1.0-alpha-2" );
62 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
64 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
66 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
67 assertTrue( "check there is a failure", failures.hasNext() );
68 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
69 assertEquals( "check metadata", metadata, result.getMetadata() );
70 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
71 assertFalse( "check no more failures", failures.hasNext() );
74 public void testMetadataValidVersions()
75 throws ReportProcessorException
77 ArtifactReporter reporter = new MockArtifactReporter();
79 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
81 Versioning versioning = new Versioning();
82 versioning.addVersion( "1.0-alpha-1" );
83 versioning.addVersion( "1.0-alpha-2" );
84 versioning.setLastUpdated( "20050611.202020" );
86 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
88 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
90 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
91 assertFalse( "check there are no failures", failures.hasNext() );
94 public void testMetadataMissingADirectory()
95 throws ReportProcessorException
97 ArtifactReporter reporter = new MockArtifactReporter();
99 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
101 Versioning versioning = new Versioning();
102 versioning.addVersion( "1.0-alpha-1" );
103 versioning.setLastUpdated( "20050611.202020" );
105 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
107 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
109 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
110 assertTrue( "check there is a failure", failures.hasNext() );
111 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
112 assertEquals( "check metadata", metadata, result.getMetadata() );
113 // TODO: should be more robust
114 assertEquals( "check reason",
115 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
116 result.getReason() );
117 assertFalse( "check no more failures", failures.hasNext() );
120 public void testMetadataInvalidArtifactVersion()
121 throws ReportProcessorException
123 ArtifactReporter reporter = new MockArtifactReporter();
125 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
127 Versioning versioning = new Versioning();
128 versioning.addVersion( "1.0-alpha-1" );
129 versioning.addVersion( "1.0-alpha-2" );
130 versioning.addVersion( "1.0-alpha-3" );
131 versioning.setLastUpdated( "20050611.202020" );
133 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
135 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
137 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
138 assertTrue( "check there is a failure", failures.hasNext() );
139 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
140 assertEquals( "check metadata", metadata, result.getMetadata() );
141 // TODO: should be more robust
142 assertEquals( "check reason",
143 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
144 result.getReason() );
145 assertFalse( "check no more failures", failures.hasNext() );
148 public void testMoreThanOneMetadataVersionErrors()
149 throws ReportProcessorException
151 ArtifactReporter reporter = new MockArtifactReporter();
153 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
155 Versioning versioning = new Versioning();
156 versioning.addVersion( "1.0-alpha-1" );
157 versioning.addVersion( "1.0-alpha-3" );
158 versioning.setLastUpdated( "20050611.202020" );
160 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
162 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
164 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
165 assertTrue( "check there is a failure", failures.hasNext() );
166 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
167 assertEquals( "check metadata", metadata, result.getMetadata() );
168 // TODO: should be more robust
169 assertEquals( "check reason",
170 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
171 result.getReason() );
172 assertTrue( "check there is a 2nd failure", failures.hasNext() );
173 result = (RepositoryMetadataResult) failures.next();
174 // TODO: should be more robust
175 assertEquals( "check reason",
176 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
177 result.getReason() );
178 assertFalse( "check no more failures", failures.hasNext() );
181 public void testValidPluginMetadata()
182 throws ReportProcessorException
184 ArtifactReporter reporter = new MockArtifactReporter();
186 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
187 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
188 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
190 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
192 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
193 assertFalse( "check there are no failures", failures.hasNext() );
196 public void testMissingMetadataPlugin()
197 throws ReportProcessorException
199 ArtifactReporter reporter = new MockArtifactReporter();
201 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
202 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
203 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
204 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
206 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
208 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
209 assertTrue( "check there is a failure", failures.hasNext() );
210 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
211 // TODO: should be more robust
212 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
213 result.getReason() );
214 assertFalse( "check no more failures", failures.hasNext() );
217 public void testIncompletePluginMetadata()
218 throws ReportProcessorException
220 ArtifactReporter reporter = new MockArtifactReporter();
222 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
223 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
225 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
227 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
228 assertTrue( "check there is a failure", failures.hasNext() );
229 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
230 // TODO: should be more robust
231 assertEquals( "check reason",
232 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
233 result.getReason() );
234 assertFalse( "check no more failures", failures.hasNext() );
237 public void testInvalidPluginArtifactId()
238 throws ReportProcessorException
240 ArtifactReporter reporter = new MockArtifactReporter();
242 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
243 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
244 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
245 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
246 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
248 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
250 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
251 assertTrue( "check there is a failure", failures.hasNext() );
252 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
253 // TODO: should be more robust
254 assertEquals( "check reason", "Missing or empty artifactId in group metadata.", result.getReason() );
255 assertTrue( "check there is a 2nd failure", failures.hasNext() );
256 result = (RepositoryMetadataResult) failures.next();
257 // TODO: should be more robust
258 assertEquals( "check reason", "Missing or empty artifactId in group metadata.", result.getReason() );
259 assertFalse( "check no more failures", failures.hasNext() );
262 public void testInvalidPluginPrefix()
263 throws ReportProcessorException
265 ArtifactReporter reporter = new MockArtifactReporter();
267 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
268 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
269 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
271 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
273 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
274 assertTrue( "check there is a failure", failures.hasNext() );
275 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
276 // TODO: should be more robust
277 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
278 assertTrue( "check there is a 2nd failure", failures.hasNext() );
279 result = (RepositoryMetadataResult) failures.next();
280 // TODO: should be more robust
281 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
282 result.getReason() );
283 assertFalse( "check no more failures", failures.hasNext() );
286 public void testDuplicatePluginPrefixes()
287 throws ReportProcessorException
289 ArtifactReporter reporter = new MockArtifactReporter();
291 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
292 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
293 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
295 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
297 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
298 assertTrue( "check there is a failure", failures.hasNext() );
299 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
300 // TODO: should be more robust
301 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
302 assertFalse( "check no more failures", failures.hasNext() );
305 public void testValidSnapshotMetadata()
306 throws ReportProcessorException
308 ArtifactReporter reporter = new MockArtifactReporter();
311 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
313 Snapshot snapshot = new Snapshot();
314 snapshot.setBuildNumber( 1 );
315 snapshot.setTimestamp( "20050611.202024" );
317 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
319 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
321 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
322 assertFalse( "check there are no failures", failures.hasNext() );
325 public void testInvalidSnapshotMetadata()
326 throws ReportProcessorException
328 ArtifactReporter reporter = new MockArtifactReporter();
331 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
333 Snapshot snapshot = new Snapshot();
334 snapshot.setBuildNumber( 2 );
335 snapshot.setTimestamp( "20050611.202024" );
337 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
339 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
341 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
342 assertTrue( "check there is a failure", failures.hasNext() );
343 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
344 assertEquals( "check metadata", metadata, result.getMetadata() );
345 // TODO: should be more robust
346 assertEquals( "check reason", "Snapshot artifact 20050611.202024-2 does not exist.", result.getReason() );
347 assertFalse( "check no more failures", failures.hasNext() );
350 private Plugin createMetadataPlugin( String artifactId, String prefix )
352 Plugin plugin = new Plugin();
353 plugin.setArtifactId( artifactId );
354 plugin.setName( artifactId );
355 plugin.setPrefix( prefix );