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;
31 public class BadMetadataReportProcessorTest
32 extends AbstractRepositoryReportsTestCase
34 private ArtifactFactory artifactFactory;
36 private MetadataReportProcessor badMetadataReportProcessor;
38 private ArtifactReporter reporter = new DefaultArtifactReporter();
40 protected void setUp()
45 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
47 badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE );
50 public void testMetadataMissingLastUpdated()
51 throws ReportProcessorException
53 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
55 Versioning versioning = new Versioning();
56 versioning.addVersion( "1.0-alpha-1" );
57 versioning.addVersion( "1.0-alpha-2" );
59 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
61 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
63 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
64 assertTrue( "check there is a failure", failures.hasNext() );
65 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
66 assertEquals( "check metadata", metadata, result.getMetadata() );
67 assertEquals( "check reason", "Missing lastUpdated element inside the metadata.", result.getReason() );
68 assertFalse( "check no more failures", failures.hasNext() );
71 public void testMetadataValidVersions()
72 throws ReportProcessorException
74 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
76 Versioning versioning = new Versioning();
77 versioning.addVersion( "1.0-alpha-1" );
78 versioning.addVersion( "1.0-alpha-2" );
79 versioning.setLastUpdated( "20050611.202020" );
81 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
83 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
85 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
86 assertFalse( "check there are no failures", failures.hasNext() );
89 public void testMetadataMissingADirectory()
90 throws ReportProcessorException
92 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
94 Versioning versioning = new Versioning();
95 versioning.addVersion( "1.0-alpha-1" );
96 versioning.setLastUpdated( "20050611.202020" );
98 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
100 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
102 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
103 assertTrue( "check there is a failure", failures.hasNext() );
104 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
105 assertEquals( "check metadata", metadata, result.getMetadata() );
106 // TODO: should be more robust
107 assertEquals( "check reason",
108 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
109 result.getReason() );
110 assertFalse( "check no more failures", failures.hasNext() );
113 public void testMetadataInvalidArtifactVersion()
114 throws ReportProcessorException
116 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
118 Versioning versioning = new Versioning();
119 versioning.addVersion( "1.0-alpha-1" );
120 versioning.addVersion( "1.0-alpha-2" );
121 versioning.addVersion( "1.0-alpha-3" );
122 versioning.setLastUpdated( "20050611.202020" );
124 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
126 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
128 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
129 assertTrue( "check there is a failure", failures.hasNext() );
130 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
131 assertEquals( "check metadata", metadata, result.getMetadata() );
132 // TODO: should be more robust
133 assertEquals( "check reason",
134 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
135 result.getReason() );
136 assertFalse( "check no more failures", failures.hasNext() );
139 public void testMoreThanOneMetadataVersionErrors()
140 throws ReportProcessorException
142 Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
144 Versioning versioning = new Versioning();
145 versioning.addVersion( "1.0-alpha-1" );
146 versioning.addVersion( "1.0-alpha-3" );
147 versioning.setLastUpdated( "20050611.202020" );
149 RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
151 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
153 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
154 assertTrue( "check there is a failure", failures.hasNext() );
155 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
156 assertEquals( "check metadata", metadata, result.getMetadata() );
157 // TODO: should be more robust
158 assertEquals( "check reason",
159 "Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
160 result.getReason() );
161 assertTrue( "check there is a 2nd failure", failures.hasNext() );
162 result = (RepositoryMetadataResult) failures.next();
163 // TODO: should be more robust
164 assertEquals( "check reason",
165 "Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
166 result.getReason() );
167 assertFalse( "check no more failures", failures.hasNext() );
170 public void testValidPluginMetadata()
171 throws ReportProcessorException
173 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
174 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
175 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
177 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
179 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
180 assertFalse( "check there are no failures", failures.hasNext() );
183 public void testMissingMetadataPlugin()
184 throws ReportProcessorException
186 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
187 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
188 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
189 metadata.getMetadata().addPlugin( createMetadataPlugin( "missing-plugin", "default3" ) );
191 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
193 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
194 assertTrue( "check there is a failure", failures.hasNext() );
195 Result result = (Result) failures.next();
196 // TODO: should be more robust
197 assertEquals( "check reason", "Metadata plugin missing-plugin not found in the repository",
198 result.getReason() );
199 assertFalse( "check no more failures", failures.hasNext() );
202 public void testIncompletePluginMetadata()
203 throws ReportProcessorException
205 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
206 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
208 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
210 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
211 assertTrue( "check there is a failure", failures.hasNext() );
212 Result result = (Result) failures.next();
213 // TODO: should be more robust
214 assertEquals( "check reason",
215 "Plugin snapshot-artifact is present in the repository but " + "missing in the metadata.",
216 result.getReason() );
217 assertFalse( "check no more failures", failures.hasNext() );
220 public void testInvalidPluginArtifactId()
221 throws ReportProcessorException
223 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
224 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
225 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default2" ) );
226 metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default3" ) );
227 metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default4" ) );
229 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
231 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
232 assertTrue( "check there is a failure", failures.hasNext() );
233 Result result = (Result) failures.next();
234 // TODO: should be more robust
235 assertEquals( "check reason", "Missing or empty artifactId in group metadata.", result.getReason() );
236 assertTrue( "check there is a 2nd failure", failures.hasNext() );
237 result = (Result) failures.next();
238 // TODO: should be more robust
239 assertEquals( "check reason", "Missing or empty artifactId in group metadata.", result.getReason() );
240 assertFalse( "check no more failures", failures.hasNext() );
243 public void testInvalidPluginPrefix()
244 throws ReportProcessorException
246 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
247 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", null ) );
248 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "" ) );
250 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
252 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
253 assertTrue( "check there is a failure", failures.hasNext() );
254 Result result = (Result) failures.next();
255 // TODO: should be more robust
256 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId artifactId.", result.getReason() );
257 assertTrue( "check there is a 2nd failure", failures.hasNext() );
258 result = (Result) failures.next();
259 // TODO: should be more robust
260 assertEquals( "check reason", "Missing or empty plugin prefix for artifactId snapshot-artifact.",
261 result.getReason() );
262 assertFalse( "check no more failures", failures.hasNext() );
265 public void testDuplicatePluginPrefixes()
266 throws ReportProcessorException
268 RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
269 metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
270 metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
272 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
274 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
275 assertTrue( "check there is a failure", failures.hasNext() );
276 Result result = (Result) failures.next();
277 // TODO: should be more robust
278 assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
279 assertFalse( "check no more failures", failures.hasNext() );
282 public void testValidSnapshotMetadata()
283 throws ReportProcessorException
286 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
288 Snapshot snapshot = new Snapshot();
289 snapshot.setBuildNumber( 1 );
290 snapshot.setTimestamp( "20050611.202024" );
292 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
294 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
296 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
297 assertFalse( "check there are no failures", failures.hasNext() );
300 public void testInvalidSnapshotMetadata()
301 throws ReportProcessorException
304 artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact", "1.0-alpha-1-SNAPSHOT", "type" );
306 Snapshot snapshot = new Snapshot();
307 snapshot.setBuildNumber( 2 );
308 snapshot.setTimestamp( "20050611.202024" );
310 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
312 badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
314 Iterator failures = reporter.getRepositoryMetadataFailureIterator();
315 assertTrue( "check there is a failure", failures.hasNext() );
316 RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
317 assertEquals( "check metadata", metadata, result.getMetadata() );
318 // TODO: should be more robust
319 assertEquals( "check reason", "Snapshot artifact 1.0-alpha-1-20050611.202024-2 does not exist.",
320 result.getReason() );
321 assertFalse( "check no more failures", failures.hasNext() );
324 private Plugin createMetadataPlugin( String artifactId, String prefix )
326 Plugin plugin = new Plugin();
327 plugin.setArtifactId( artifactId );
328 plugin.setName( artifactId );
329 plugin.setPrefix( prefix );