1 package org.apache.maven.archiva.indexing.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.indexing.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.Arrays;
32 import java.util.List;
35 * Test the minimal artifact index record.
37 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39 public class StandardArtifactIndexRecordFactoryTest
40 extends PlexusTestCase
42 private RepositoryIndexRecordFactory factory;
44 private ArtifactRepository repository;
46 private ArtifactFactory artifactFactory;
48 private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
50 private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
52 private static final List JAR_FILE_LIST =
53 Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "A.class", "b/B.class", "b/c/C.class"} );
55 protected void setUp()
60 factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
62 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
64 ArtifactRepositoryFactory repositoryFactory =
65 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
67 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
69 File file = getTestFile( "src/test/managed-repository" );
71 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
74 public void testIndexedJar()
75 throws RepositoryIndexException
77 Artifact artifact = createArtifact( "test-jar" );
79 RepositoryIndexRecord record = factory.createRecord( artifact );
81 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
82 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
83 expectedRecord.setFilename( repository.pathOf( artifact ) );
84 expectedRecord.setLastModified( artifact.getFile().lastModified() );
85 expectedRecord.setSize( artifact.getFile().length() );
86 expectedRecord.setClasses( JAR_CLASS_LIST );
87 expectedRecord.setArtifactId( "test-jar" );
88 expectedRecord.setGroupId( TEST_GROUP_ID );
89 expectedRecord.setBaseVersion( "1.0" );
90 expectedRecord.setVersion( "1.0" );
91 expectedRecord.setFiles( JAR_FILE_LIST );
92 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
93 expectedRecord.setType( "jar" );
94 expectedRecord.setRepository( "test" );
96 assertEquals( "check record", expectedRecord, record );
99 public void testIndexedJarWithClassifier()
100 throws RepositoryIndexException
102 Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
104 RepositoryIndexRecord record = factory.createRecord( artifact );
106 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
107 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
108 expectedRecord.setFilename( repository.pathOf( artifact ) );
109 expectedRecord.setLastModified( artifact.getFile().lastModified() );
110 expectedRecord.setSize( artifact.getFile().length() );
111 expectedRecord.setClasses( JAR_CLASS_LIST );
112 expectedRecord.setArtifactId( "test-jar" );
113 expectedRecord.setGroupId( TEST_GROUP_ID );
114 expectedRecord.setBaseVersion( "1.0" );
115 expectedRecord.setVersion( "1.0" );
116 expectedRecord.setFiles( JAR_FILE_LIST );
117 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
118 expectedRecord.setType( "jar" );
119 expectedRecord.setRepository( "test" );
120 expectedRecord.setClassifier( "jdk14" );
122 assertEquals( "check record", expectedRecord, record );
125 public void testIndexedJarAndPom()
126 throws RepositoryIndexException
128 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
130 RepositoryIndexRecord record = factory.createRecord( artifact );
132 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
133 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
134 expectedRecord.setFilename( repository.pathOf( artifact ) );
135 expectedRecord.setLastModified( artifact.getFile().lastModified() );
136 expectedRecord.setSize( artifact.getFile().length() );
137 expectedRecord.setClasses( JAR_CLASS_LIST );
138 expectedRecord.setArtifactId( "test-jar-and-pom" );
139 expectedRecord.setGroupId( TEST_GROUP_ID );
140 expectedRecord.setBaseVersion( "1.0-alpha-1" );
141 expectedRecord.setVersion( "1.0-alpha-1" );
142 expectedRecord.setFiles( JAR_FILE_LIST );
143 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
144 expectedRecord.setType( "jar" );
145 expectedRecord.setRepository( "test" );
146 expectedRecord.setPackaging( "jar" );
147 expectedRecord.setProjectName( "Test JAR and POM" );
149 assertEquals( "check record", expectedRecord, record );
152 public void testIndexedJarAndPomWithClassifier()
153 throws RepositoryIndexException
155 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
157 RepositoryIndexRecord record = factory.createRecord( artifact );
159 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
160 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
161 expectedRecord.setFilename( repository.pathOf( artifact ) );
162 expectedRecord.setLastModified( artifact.getFile().lastModified() );
163 expectedRecord.setSize( artifact.getFile().length() );
164 expectedRecord.setClasses( JAR_CLASS_LIST );
165 expectedRecord.setArtifactId( "test-jar-and-pom" );
166 expectedRecord.setGroupId( TEST_GROUP_ID );
167 expectedRecord.setBaseVersion( "1.0-alpha-1" );
168 expectedRecord.setVersion( "1.0-alpha-1" );
169 expectedRecord.setFiles( JAR_FILE_LIST );
170 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
171 expectedRecord.setType( "jar" );
172 expectedRecord.setRepository( "test" );
173 expectedRecord.setPackaging( "jar" );
174 expectedRecord.setProjectName( "Test JAR and POM" );
175 expectedRecord.setClassifier( "jdk14" );
177 assertEquals( "check record", expectedRecord, record );
180 public void testIndexedJarWithParentPom()
181 throws RepositoryIndexException
183 Artifact artifact = createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" );
185 RepositoryIndexRecord record = factory.createRecord( artifact );
187 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
188 expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" );
189 expectedRecord.setFilename( repository.pathOf( artifact ) );
190 expectedRecord.setLastModified( artifact.getFile().lastModified() );
191 expectedRecord.setSize( artifact.getFile().length() );
192 expectedRecord.setClasses( JAR_CLASS_LIST );
193 expectedRecord.setArtifactId( "test-child-pom" );
194 expectedRecord.setGroupId( TEST_GROUP_ID );
195 expectedRecord.setBaseVersion( "1.0-SNAPSHOT" );
196 expectedRecord.setVersion( "1.0-20060728.121314-1" );
197 expectedRecord.setFiles( JAR_FILE_LIST );
198 expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" );
199 expectedRecord.setType( "jar" );
200 expectedRecord.setRepository( "test" );
201 expectedRecord.setPackaging( "jar" );
202 expectedRecord.setProjectName( "Child Project" );
203 expectedRecord.setProjectDescription( "Description" );
204 expectedRecord.setInceptionYear( "2005" );
206 assertEquals( "check record", expectedRecord, record );
209 public void testIndexedPom()
210 throws RepositoryIndexException
212 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
214 RepositoryIndexRecord record = factory.createRecord( artifact );
216 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
217 expectedRecord.setMd5Checksum( "103d11ac601a42ccf2a2ae54d308c362" );
218 expectedRecord.setFilename( repository.pathOf( artifact ) );
219 expectedRecord.setLastModified( artifact.getFile().lastModified() );
220 expectedRecord.setSize( artifact.getFile().length() );
221 expectedRecord.setArtifactId( "test-pom" );
222 expectedRecord.setGroupId( TEST_GROUP_ID );
223 expectedRecord.setBaseVersion( "1.0" );
224 expectedRecord.setVersion( "1.0" );
225 expectedRecord.setSha1Checksum( "4c4d237c5366df877c3a636d5b6241822d090355" );
226 expectedRecord.setType( "pom" );
227 expectedRecord.setRepository( "test" );
228 expectedRecord.setPackaging( "pom" );
229 expectedRecord.setInceptionYear( "2005" );
230 expectedRecord.setProjectName( "Maven Repository Manager Test POM" );
231 expectedRecord.setProjectDescription( "Description" );
233 assertEquals( "check record", expectedRecord, record );
236 public void testNonIndexedPom()
237 throws RepositoryIndexException
239 // If we pass in only the POM that belongs to a JAR, then expect null not the POM
240 Artifact artifact = createArtifact( "test-jar-and-pom", "1.0", "pom" );
242 RepositoryIndexRecord record = factory.createRecord( artifact );
244 assertNull( "Check no record", record );
246 artifact = createArtifact( "test-plugin", "1.0", "pom" );
248 record = factory.createRecord( artifact );
250 assertNull( "Check no record", record );
252 artifact = createArtifact( "test-archetype", "1.0", "pom" );
254 record = factory.createRecord( artifact );
256 assertNull( "Check no record", record );
259 public void testIndexedPlugin()
260 throws RepositoryIndexException, IOException, XmlPullParserException
262 Artifact artifact = createArtifact( "test-plugin" );
264 RepositoryIndexRecord record = factory.createRecord( artifact );
266 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
267 expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" );
268 expectedRecord.setFilename( repository.pathOf( artifact ) );
269 expectedRecord.setLastModified( artifact.getFile().lastModified() );
270 expectedRecord.setSize( artifact.getFile().length() );
271 expectedRecord.setArtifactId( "test-plugin" );
272 expectedRecord.setGroupId( TEST_GROUP_ID );
273 expectedRecord.setBaseVersion( "1.0" );
274 expectedRecord.setVersion( "1.0" );
275 expectedRecord.setSha1Checksum( "2cd2619d59a684e82e97471d2c2e004144c8f24e" );
276 expectedRecord.setType( "maven-plugin" );
277 expectedRecord.setRepository( "test" );
278 expectedRecord.setClasses( Arrays.asList( new String[]{"org.apache.maven.archiva.record.MyMojo"} ) );
279 expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF",
280 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties",
281 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml", "META-INF/maven/plugin.xml",
282 "org/apache/maven/archiva/record/MyMojo.class"} ) );
283 expectedRecord.setPackaging( "maven-plugin" );
284 expectedRecord.setProjectName( "Maven Mojo Archetype" );
285 expectedRecord.setPluginPrefix( "test" );
287 assertEquals( "check record", expectedRecord, record );
290 public void testIndexedArchetype()
291 throws RepositoryIndexException, IOException, XmlPullParserException
293 Artifact artifact = createArtifact( "test-archetype" );
295 RepositoryIndexRecord record = factory.createRecord( artifact );
297 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
298 expectedRecord.setMd5Checksum( "52b7ea4b53818b8a5f4c329d88fd60d9" );
299 expectedRecord.setFilename( repository.pathOf( artifact ) );
300 expectedRecord.setLastModified( artifact.getFile().lastModified() );
301 expectedRecord.setSize( artifact.getFile().length() );
302 expectedRecord.setArtifactId( "test-archetype" );
303 expectedRecord.setGroupId( TEST_GROUP_ID );
304 expectedRecord.setBaseVersion( "1.0" );
305 expectedRecord.setVersion( "1.0" );
306 expectedRecord.setSha1Checksum( "05841f5e51c124f1729d86c1687438c36b9255d9" );
307 expectedRecord.setType( "maven-archetype" );
308 expectedRecord.setRepository( "test" );
309 expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "META-INF/maven/archetype.xml",
310 "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.properties",
311 "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.xml", "archetype-resources/pom.xml",
312 "archetype-resources/src/main/java/App.java", "archetype-resources/src/test/java/AppTest.java"} ) );
313 expectedRecord.setPackaging( "jar" );
314 expectedRecord.setProjectName( "Archetype - test-archetype" );
316 assertEquals( "check record", expectedRecord, record );
319 public void testCorruptJar()
320 throws RepositoryIndexException
322 Artifact artifact = createArtifact( "test-corrupt-jar" );
324 RepositoryIndexRecord record = factory.createRecord( artifact );
326 assertNull( "Confirm no record is returned", record );
329 public void testDll()
330 throws RepositoryIndexException
332 Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
334 RepositoryIndexRecord record = factory.createRecord( artifact );
336 StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord();
337 expectedRecord.setMd5Checksum( "d41d8cd98f00b204e9800998ecf8427e" );
338 expectedRecord.setFilename( repository.pathOf( artifact ) );
339 expectedRecord.setLastModified( artifact.getFile().lastModified() );
340 expectedRecord.setSize( artifact.getFile().length() );
341 expectedRecord.setArtifactId( "test-dll" );
342 expectedRecord.setGroupId( TEST_GROUP_ID );
343 expectedRecord.setBaseVersion( "1.0.1.34" );
344 expectedRecord.setVersion( "1.0.1.34" );
345 expectedRecord.setSha1Checksum( "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
346 expectedRecord.setType( "dll" );
347 expectedRecord.setRepository( "test" );
349 assertEquals( "check record", expectedRecord, record );
352 public void testMissingFile()
353 throws RepositoryIndexException
355 Artifact artifact = createArtifact( "test-foo" );
357 RepositoryIndexRecord record = factory.createRecord( artifact );
359 assertNull( "Confirm no record is returned", record );
362 private Artifact createArtifact( String artifactId )
364 return createArtifact( artifactId, "1.0", "jar" );
367 private Artifact createArtifact( String artifactId, String version, String type )
369 return createArtifact( artifactId, version, type, null );
372 private Artifact createArtifact( String artifactId, String version, String type, String classifier )
374 Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId,
375 VersionRange.createFromVersion( version ), type,
376 classifier, Artifact.SCOPE_RUNTIME );
377 artifact.isSnapshot();
378 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
379 artifact.setRepository( repository );