1 package org.apache.maven.archiva.indexer.record;
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.indexer.RepositoryIndexException;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.artifact.factory.ArtifactFactory;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25 import org.apache.maven.artifact.versioning.VersionRange;
26 import org.codehaus.plexus.PlexusTestCase;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
37 * Test the minimal artifact index record.
39 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
41 public class StandardArtifactIndexRecordFactoryTest
42 extends PlexusTestCase
44 private RepositoryIndexRecordFactory factory;
46 private ArtifactRepository repository;
48 private ArtifactFactory artifactFactory;
50 private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
52 private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
54 private static final List JAR_FILE_LIST =
55 Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "A.class", "b/B.class", "b/c/C.class"} );
57 private static final String JUNIT_DEPENDENCY = "junit:junit:3.8.1";
59 private static final String PLUGIN_API_DEPENDENCY = "org.apache.maven:maven-plugin-api:2.0";
61 protected void setUp()
66 factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
68 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
70 ArtifactRepositoryFactory repositoryFactory =
71 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
73 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
75 File file = getTestFile( "src/test/managed-repository" );
77 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
80 public void testIndexedJar()
81 throws RepositoryIndexException
83 Artifact artifact = createArtifact( "test-jar" );
85 RepositoryIndexRecord record = factory.createRecord( artifact );
87 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
88 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
89 expectedRecord.setFilename( repository.pathOf( artifact ) );
90 expectedRecord.setLastModified( artifact.getFile().lastModified() );
91 expectedRecord.setSize( artifact.getFile().length() );
92 expectedRecord.setClasses( JAR_CLASS_LIST );
93 expectedRecord.setArtifactId( "test-jar" );
94 expectedRecord.setGroupId( TEST_GROUP_ID );
95 expectedRecord.setBaseVersion( "1.0" );
96 expectedRecord.setVersion( "1.0" );
97 expectedRecord.setFiles( JAR_FILE_LIST );
98 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
99 expectedRecord.setType( "jar" );
100 expectedRecord.setRepository( "test" );
102 assertEquals( "check record", expectedRecord, record );
105 public void testIndexedJarWithClassifier()
106 throws RepositoryIndexException
108 Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
110 RepositoryIndexRecord record = factory.createRecord( artifact );
112 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
113 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
114 expectedRecord.setFilename( repository.pathOf( artifact ) );
115 expectedRecord.setLastModified( artifact.getFile().lastModified() );
116 expectedRecord.setSize( artifact.getFile().length() );
117 expectedRecord.setClasses( JAR_CLASS_LIST );
118 expectedRecord.setArtifactId( "test-jar" );
119 expectedRecord.setGroupId( TEST_GROUP_ID );
120 expectedRecord.setBaseVersion( "1.0" );
121 expectedRecord.setVersion( "1.0" );
122 expectedRecord.setFiles( JAR_FILE_LIST );
123 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
124 expectedRecord.setType( "jar" );
125 expectedRecord.setRepository( "test" );
126 expectedRecord.setClassifier( "jdk14" );
128 assertEquals( "check record", expectedRecord, record );
131 public void testIndexedJarAndPom()
132 throws RepositoryIndexException
134 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
136 RepositoryIndexRecord record = factory.createRecord( artifact );
138 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
139 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
140 expectedRecord.setFilename( repository.pathOf( artifact ) );
141 expectedRecord.setLastModified( artifact.getFile().lastModified() );
142 expectedRecord.setSize( artifact.getFile().length() );
143 expectedRecord.setClasses( JAR_CLASS_LIST );
144 expectedRecord.setArtifactId( "test-jar-and-pom" );
145 expectedRecord.setGroupId( TEST_GROUP_ID );
146 expectedRecord.setBaseVersion( "1.0-alpha-1" );
147 expectedRecord.setVersion( "1.0-alpha-1" );
148 expectedRecord.setFiles( JAR_FILE_LIST );
149 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
150 expectedRecord.setType( "jar" );
151 expectedRecord.setRepository( "test" );
152 expectedRecord.setPackaging( "jar" );
153 expectedRecord.setProjectName( "Test JAR and POM" );
154 expectedRecord.setDependencies( createDependencies() );
155 expectedRecord.setDevelopers( createDevelopers() );
157 assertEquals( "check record", expectedRecord, record );
160 public void testIndexedJarAndPomWithClassifier()
161 throws RepositoryIndexException
163 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
165 RepositoryIndexRecord record = factory.createRecord( artifact );
167 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
168 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
169 expectedRecord.setFilename( repository.pathOf( artifact ) );
170 expectedRecord.setLastModified( artifact.getFile().lastModified() );
171 expectedRecord.setSize( artifact.getFile().length() );
172 expectedRecord.setClasses( JAR_CLASS_LIST );
173 expectedRecord.setArtifactId( "test-jar-and-pom" );
174 expectedRecord.setGroupId( TEST_GROUP_ID );
175 expectedRecord.setBaseVersion( "1.0-alpha-1" );
176 expectedRecord.setVersion( "1.0-alpha-1" );
177 expectedRecord.setFiles( JAR_FILE_LIST );
178 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
179 expectedRecord.setType( "jar" );
180 expectedRecord.setRepository( "test" );
181 expectedRecord.setPackaging( "jar" );
182 expectedRecord.setProjectName( "Test JAR and POM" );
183 expectedRecord.setClassifier( "jdk14" );
184 expectedRecord.setDependencies( createDependencies() );
185 expectedRecord.setDevelopers( createDevelopers() );
187 assertEquals( "check record", expectedRecord, record );
190 public void testIndexedJarWithParentPom()
191 throws RepositoryIndexException
193 Artifact artifact = createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" );
195 RepositoryIndexRecord record = factory.createRecord( artifact );
197 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
198 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
199 expectedRecord.setFilename( repository.pathOf( artifact ) );
200 expectedRecord.setLastModified( artifact.getFile().lastModified() );
201 expectedRecord.setSize( artifact.getFile().length() );
202 expectedRecord.setClasses( JAR_CLASS_LIST );
203 expectedRecord.setArtifactId( "test-child-pom" );
204 expectedRecord.setGroupId( TEST_GROUP_ID );
205 expectedRecord.setBaseVersion( "1.0-SNAPSHOT" );
206 expectedRecord.setVersion( "1.0-20060728.121314-1" );
207 expectedRecord.setFiles( JAR_FILE_LIST );
208 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
209 expectedRecord.setType( "jar" );
210 expectedRecord.setRepository( "test" );
211 expectedRecord.setPackaging( "jar" );
212 expectedRecord.setProjectName( "Child Project" );
213 expectedRecord.setProjectDescription( "Description" );
214 expectedRecord.setInceptionYear( "2005" );
215 expectedRecord.setDependencies( Collections.singletonList( JUNIT_DEPENDENCY ) );
217 assertEquals( "check record", expectedRecord, record );
220 public void testIndexedPom()
221 throws RepositoryIndexException
223 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
225 RepositoryIndexRecord record = factory.createRecord( artifact );
227 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
228 expectedRecord.setMd5Checksum( "98b4a1b708a90a8637aaf541bef5094f" );
229 expectedRecord.setFilename( repository.pathOf( artifact ) );
230 expectedRecord.setLastModified( artifact.getFile().lastModified() );
231 expectedRecord.setSize( artifact.getFile().length() );
232 expectedRecord.setArtifactId( "test-pom" );
233 expectedRecord.setGroupId( TEST_GROUP_ID );
234 expectedRecord.setBaseVersion( "1.0" );
235 expectedRecord.setVersion( "1.0" );
236 expectedRecord.setSha1Checksum( "d95348bee1666a46511260696292bfa0519b61c1" );
237 expectedRecord.setType( "pom" );
238 expectedRecord.setRepository( "test" );
239 expectedRecord.setPackaging( "pom" );
240 expectedRecord.setInceptionYear( "2005" );
241 expectedRecord.setProjectName( "Maven Repository Manager Test POM" );
242 expectedRecord.setProjectDescription( "Description" );
244 assertEquals( "check record", expectedRecord, record );
247 public void testNonIndexedPom()
248 throws RepositoryIndexException
250 // If we pass in only the POM that belongs to a JAR, then expect null not the POM
251 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0", "pom" );
253 RepositoryIndexRecord record = factory.createRecord( artifact );
255 assertNull( "Check no record", record );
257 artifact = createArtifact( "test-plugin", "1.0", "pom" );
259 record = factory.createRecord( artifact );
261 assertNull( "Check no record", record );
263 artifact = createArtifact( "test-archetype", "1.0", "pom" );
265 record = factory.createRecord( artifact );
267 assertNull( "Check no record", record );
269 artifact = createArtifact( "test-skin", "1.0", "pom" );
271 record = factory.createRecord( artifact );
273 assertNull( "Check no record", record );
276 public void testIndexedPlugin()
277 throws RepositoryIndexException, IOException, XmlPullParserException
279 Artifact artifact = createArtifact( "test-plugin" );
281 RepositoryIndexRecord record = factory.createRecord( artifact );
283 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
284 expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" );
285 expectedRecord.setFilename( repository.pathOf( artifact ) );
286 expectedRecord.setLastModified( artifact.getFile().lastModified() );
287 expectedRecord.setSize( artifact.getFile().length() );
288 expectedRecord.setArtifactId( "test-plugin" );
289 expectedRecord.setGroupId( TEST_GROUP_ID );
290 expectedRecord.setBaseVersion( "1.0" );
291 expectedRecord.setVersion( "1.0" );
292 expectedRecord.setSha1Checksum( "2cd2619d59a684e82e97471d2c2e004144c8f24e" );
293 expectedRecord.setType( "maven-plugin" );
294 expectedRecord.setRepository( "test" );
295 expectedRecord.setClasses( Arrays.asList( new String[]{"org.apache.maven.archiva.record.MyMojo"} ) );
296 expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF",
297 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties",
298 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml", "META-INF/maven/plugin.xml",
299 "org/apache/maven/archiva/record/MyMojo.class"} ) );
300 expectedRecord.setPackaging( "maven-plugin" );
301 expectedRecord.setProjectName( "Maven Mojo Archetype" );
302 expectedRecord.setPluginPrefix( "test" );
303 expectedRecord.setDependencies( Arrays.asList( new String[]{JUNIT_DEPENDENCY, PLUGIN_API_DEPENDENCY} ) );
305 assertEquals( "check record", expectedRecord, record );
308 public void testIndexedArchetype()
309 throws RepositoryIndexException, IOException, XmlPullParserException
311 Artifact artifact = createArtifact( "test-archetype" );
313 RepositoryIndexRecord record = factory.createRecord( artifact );
315 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
316 expectedRecord.setMd5Checksum( "52b7ea4b53818b8a5f4c329d88fd60d9" );
317 expectedRecord.setFilename( repository.pathOf( artifact ) );
318 expectedRecord.setLastModified( artifact.getFile().lastModified() );
319 expectedRecord.setSize( artifact.getFile().length() );
320 expectedRecord.setArtifactId( "test-archetype" );
321 expectedRecord.setGroupId( TEST_GROUP_ID );
322 expectedRecord.setBaseVersion( "1.0" );
323 expectedRecord.setVersion( "1.0" );
324 expectedRecord.setSha1Checksum( "05841f5e51c124f1729d86c1687438c36b9255d9" );
325 expectedRecord.setType( "maven-archetype" );
326 expectedRecord.setRepository( "test" );
327 expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "META-INF/maven/archetype.xml",
328 "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.properties",
329 "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.xml", "archetype-resources/pom.xml",
330 "archetype-resources/src/main/java/App.java", "archetype-resources/src/test/java/AppTest.java"} ) );
331 expectedRecord.setPackaging( "jar" );
332 expectedRecord.setProjectName( "Archetype - test-archetype" );
334 assertEquals( "check record", expectedRecord, record );
337 public void testIndexedSkin()
338 throws RepositoryIndexException, IOException, XmlPullParserException
340 Artifact artifact = createArtifact( "test-skin" );
342 RepositoryIndexRecord record = factory.createRecord( artifact );
344 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
345 expectedRecord.setMd5Checksum( "ba2d8a722f763db2950ad63119585f45" );
346 expectedRecord.setFilename( repository.pathOf( artifact ) );
347 expectedRecord.setLastModified( artifact.getFile().lastModified() );
348 expectedRecord.setSize( artifact.getFile().length() );
349 expectedRecord.setArtifactId( "test-skin" );
350 expectedRecord.setGroupId( TEST_GROUP_ID );
351 expectedRecord.setBaseVersion( "1.0" );
352 expectedRecord.setVersion( "1.0" );
353 expectedRecord.setSha1Checksum( "44855e3e56c18ce766db315a2d4c114d7a8c8ab0" );
354 expectedRecord.setType( "maven-skin" );
355 expectedRecord.setRepository( "test" );
356 expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "css/maven-theme.css",
357 "META-INF/maven/org.apache.maven.skins/test-skin/pom.xml",
358 "META-INF/maven/org.apache.maven.skins/test-skin/pom.properties"} ) );
359 expectedRecord.setPackaging( "jar" );
360 expectedRecord.setProjectName( "Skin - test-skin" );
362 assertEquals( "check record", expectedRecord, record );
365 public void testCorruptJar()
366 throws RepositoryIndexException
368 Artifact artifact = createArtifact( "test-corrupt-jar" );
370 RepositoryIndexRecord record = factory.createRecord( artifact );
372 assertNull( "Confirm no record is returned", record );
375 public void testDll()
376 throws RepositoryIndexException
378 Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
380 RepositoryIndexRecord record = factory.createRecord( artifact );
382 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
383 expectedRecord.setMd5Checksum( "d41d8cd98f00b204e9800998ecf8427e" );
384 expectedRecord.setFilename( repository.pathOf( artifact ) );
385 expectedRecord.setLastModified( artifact.getFile().lastModified() );
386 expectedRecord.setSize( artifact.getFile().length() );
387 expectedRecord.setArtifactId( "test-dll" );
388 expectedRecord.setGroupId( TEST_GROUP_ID );
389 expectedRecord.setBaseVersion( "1.0.1.34" );
390 expectedRecord.setVersion( "1.0.1.34" );
391 expectedRecord.setSha1Checksum( "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
392 expectedRecord.setType( "dll" );
393 expectedRecord.setRepository( "test" );
395 assertEquals( "check record", expectedRecord, record );
398 public void testMissingFile()
399 throws RepositoryIndexException
401 Artifact artifact = createArtifact( "test-foo" );
403 RepositoryIndexRecord record = factory.createRecord( artifact );
405 assertNull( "Confirm no record is returned", record );
408 private Artifact createArtifact( String artifactId )
410 return createArtifact( artifactId, "1.0", "jar" );
413 private Artifact createArtifact( String artifactId, String version, String type )
415 return createArtifact( artifactId, version, type, null );
418 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
420 Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId,
421 VersionRange.createFromVersion( version ), type,
422 classifier, Artifact.SCOPE_RUNTIME );
423 artifact.isSnapshot();
424 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
425 artifact.setRepository( repository );
429 private static List createDevelopers()
431 List developers = new ArrayList();
432 developers.add( "brett:Brett Porter:brett@apache.org" );
436 private static List createDependencies()
438 List dependencies = new ArrayList();
439 dependencies.add( JUNIT_DEPENDENCY );
440 dependencies.add( "org.apache.maven:maven-project:2.0" );