]> source.dussan.org Git - archiva.git/blob
6d3733a1d59d5a37e98969ae019435afdf9efc9f
[archiva.git] /
1 package org.apache.maven.archiva.indexer.record;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
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;
28
29 import java.io.File;
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;
35
36 /**
37  * Test the minimal artifact index record.
38  *
39  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40  */
41 public class StandardArtifactIndexRecordFactoryTest
42     extends PlexusTestCase
43 {
44     private RepositoryIndexRecordFactory factory;
45
46     private ArtifactRepository repository;
47
48     private ArtifactFactory artifactFactory;
49
50     private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
51
52     private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
53
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"} );
56
57     private static final String JUNIT_DEPENDENCY = "junit:junit:3.8.1";
58
59     private static final String PLUGIN_API_DEPENDENCY = "org.apache.maven:maven-plugin-api:2.0";
60
61     protected void setUp()
62         throws Exception
63     {
64         super.setUp();
65
66         factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
67
68         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
69
70         ArtifactRepositoryFactory repositoryFactory =
71             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
72
73         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
74
75         File file = getTestFile( "src/test/managed-repository" );
76         repository =
77             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
78     }
79
80     public void testIndexedJar()
81         throws RepositoryIndexException
82     {
83         Artifact artifact = createArtifact( "test-jar" );
84
85         RepositoryIndexRecord record = factory.createRecord( artifact );
86
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" );
101
102         assertEquals( "check record", expectedRecord, record );
103     }
104
105     public void testIndexedJarWithClassifier()
106         throws RepositoryIndexException
107     {
108         Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
109
110         RepositoryIndexRecord record = factory.createRecord( artifact );
111
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" );
127
128         assertEquals( "check record", expectedRecord, record );
129     }
130
131     public void testIndexedJarAndPom()
132         throws RepositoryIndexException
133     {
134         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
135
136         RepositoryIndexRecord record = factory.createRecord( artifact );
137
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() );
156
157         assertEquals( "check record", expectedRecord, record );
158     }
159
160     public void testIndexedJarAndPomWithClassifier()
161         throws RepositoryIndexException
162     {
163         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
164
165         RepositoryIndexRecord record = factory.createRecord( artifact );
166
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() );
186
187         assertEquals( "check record", expectedRecord, record );
188     }
189
190     public void testIndexedJarWithParentPom()
191         throws RepositoryIndexException
192     {
193         Artifact artifact = createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" );
194
195         RepositoryIndexRecord record = factory.createRecord( artifact );
196
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 ) );
216
217         assertEquals( "check record", expectedRecord, record );
218     }
219
220     public void testIndexedPom()
221         throws RepositoryIndexException
222     {
223         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
224
225         RepositoryIndexRecord record = factory.createRecord( artifact );
226
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" );
243
244         assertEquals( "check record", expectedRecord, record );
245     }
246
247     public void testNonIndexedPom()
248         throws RepositoryIndexException
249     {
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" );
252
253         RepositoryIndexRecord record = factory.createRecord( artifact );
254
255         assertNull( "Check no record", record );
256
257         artifact = createArtifact( "test-plugin", "1.0", "pom" );
258
259         record = factory.createRecord( artifact );
260
261         assertNull( "Check no record", record );
262
263         artifact = createArtifact( "test-archetype", "1.0", "pom" );
264
265         record = factory.createRecord( artifact );
266
267         assertNull( "Check no record", record );
268
269         artifact = createArtifact( "test-skin", "1.0", "pom" );
270
271         record = factory.createRecord( artifact );
272
273         assertNull( "Check no record", record );
274     }
275
276     public void testIndexedPlugin()
277         throws RepositoryIndexException, IOException, XmlPullParserException
278     {
279         Artifact artifact = createArtifact( "test-plugin" );
280
281         RepositoryIndexRecord record = factory.createRecord( artifact );
282
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} ) );
304
305         assertEquals( "check record", expectedRecord, record );
306     }
307
308     public void testIndexedArchetype()
309         throws RepositoryIndexException, IOException, XmlPullParserException
310     {
311         Artifact artifact = createArtifact( "test-archetype" );
312
313         RepositoryIndexRecord record = factory.createRecord( artifact );
314
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" );
333
334         assertEquals( "check record", expectedRecord, record );
335     }
336
337     public void testIndexedSkin()
338         throws RepositoryIndexException, IOException, XmlPullParserException
339     {
340         Artifact artifact = createArtifact( "test-skin" );
341
342         RepositoryIndexRecord record = factory.createRecord( artifact );
343
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" );
361
362         assertEquals( "check record", expectedRecord, record );
363     }
364
365     public void testCorruptJar()
366         throws RepositoryIndexException
367     {
368         Artifact artifact = createArtifact( "test-corrupt-jar" );
369
370         RepositoryIndexRecord record = factory.createRecord( artifact );
371
372         assertNull( "Confirm no record is returned", record );
373     }
374
375     public void testDll()
376         throws RepositoryIndexException
377     {
378         Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
379
380         RepositoryIndexRecord record = factory.createRecord( artifact );
381
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" );
394
395         assertEquals( "check record", expectedRecord, record );
396     }
397
398     public void testMissingFile()
399         throws RepositoryIndexException
400     {
401         Artifact artifact = createArtifact( "test-foo" );
402
403         RepositoryIndexRecord record = factory.createRecord( artifact );
404
405         assertNull( "Confirm no record is returned", record );
406     }
407
408     private Artifact createArtifact( String artifactId )
409     {
410         return createArtifact( artifactId, "1.0", "jar" );
411     }
412
413     private Artifact createArtifact( String artifactId, String version, String type )
414     {
415         return createArtifact( artifactId, version, type, null );
416     }
417
418     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
419     {
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 );
426         return artifact;
427     }
428
429     private static List createDevelopers()
430     {
431         List developers = new ArrayList();
432         developers.add( "brett:Brett Porter:brett@apache.org" );
433         return developers;
434     }
435
436     private static List createDependencies()
437     {
438         List dependencies = new ArrayList();
439         dependencies.add( JUNIT_DEPENDENCY );
440         dependencies.add( "org.apache.maven:maven-project:2.0" );
441         return dependencies;
442     }
443 }