]> source.dussan.org Git - archiva.git/blob
842228cb3492c02effca3870455e00fa225f8db5
[archiva.git] /
1 package org.apache.maven.archiva.indexing.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.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;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.List;
33
34 /**
35  * Test the minimal artifact index record.
36  *
37  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38  */
39 public class StandardArtifactIndexRecordFactoryTest
40     extends PlexusTestCase
41 {
42     private RepositoryIndexRecordFactory factory;
43
44     private ArtifactRepository repository;
45
46     private ArtifactFactory artifactFactory;
47
48     private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record";
49
50     private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} );
51
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"} );
54
55     protected void setUp()
56         throws Exception
57     {
58         super.setUp();
59
60         factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
61
62         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
63
64         ArtifactRepositoryFactory repositoryFactory =
65             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
66
67         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
68
69         File file = getTestFile( "src/test/managed-repository" );
70         repository =
71             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
72     }
73
74     public void testIndexedJar()
75         throws RepositoryIndexException
76     {
77         Artifact artifact = createArtifact( "test-jar" );
78
79         RepositoryIndexRecord record = factory.createRecord( artifact );
80
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" );
95
96         assertEquals( "check record", expectedRecord, record );
97     }
98
99     public void testIndexedJarWithClassifier()
100         throws RepositoryIndexException
101     {
102         Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" );
103
104         RepositoryIndexRecord record = factory.createRecord( artifact );
105
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" );
121
122         assertEquals( "check record", expectedRecord, record );
123     }
124
125     public void testIndexedJarAndPom()
126         throws RepositoryIndexException
127     {
128         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" );
129
130         RepositoryIndexRecord record = factory.createRecord( artifact );
131
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" );
148
149         assertEquals( "check record", expectedRecord, record );
150     }
151
152     public void testIndexedJarAndPomWithClassifier()
153         throws RepositoryIndexException
154     {
155         Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" );
156
157         RepositoryIndexRecord record = factory.createRecord( artifact );
158
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" );
176
177         assertEquals( "check record", expectedRecord, record );
178     }
179
180     public void testIndexedJarWithParentPom()
181         throws RepositoryIndexException
182     {
183         Artifact artifact = createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" );
184
185         RepositoryIndexRecord record = factory.createRecord( artifact );
186
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" );
205
206         assertEquals( "check record", expectedRecord, record );
207     }
208
209     public void testIndexedPom()
210         throws RepositoryIndexException
211     {
212         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
213
214         RepositoryIndexRecord record = factory.createRecord( artifact );
215
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" );
232
233         assertEquals( "check record", expectedRecord, record );
234     }
235
236     public void testNonIndexedPom()
237         throws RepositoryIndexException
238     {
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" );
241
242         RepositoryIndexRecord record = factory.createRecord( artifact );
243
244         assertNull( "Check no record", record );
245
246         artifact = createArtifact( "test-plugin", "1.0", "pom" );
247
248         record = factory.createRecord( artifact );
249
250         assertNull( "Check no record", record );
251
252         artifact = createArtifact( "test-archetype", "1.0", "pom" );
253
254         record = factory.createRecord( artifact );
255
256         assertNull( "Check no record", record );
257     }
258
259     public void testIndexedPlugin()
260         throws RepositoryIndexException, IOException, XmlPullParserException
261     {
262         Artifact artifact = createArtifact( "test-plugin" );
263
264         RepositoryIndexRecord record = factory.createRecord( artifact );
265
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" );
286
287         assertEquals( "check record", expectedRecord, record );
288     }
289
290     public void testIndexedArchetype()
291         throws RepositoryIndexException, IOException, XmlPullParserException
292     {
293         Artifact artifact = createArtifact( "test-archetype" );
294
295         RepositoryIndexRecord record = factory.createRecord( artifact );
296
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" );
315
316         assertEquals( "check record", expectedRecord, record );
317     }
318
319     public void testCorruptJar()
320         throws RepositoryIndexException
321     {
322         Artifact artifact = createArtifact( "test-corrupt-jar" );
323
324         RepositoryIndexRecord record = factory.createRecord( artifact );
325
326         assertNull( "Confirm no record is returned", record );
327     }
328
329     public void testDll()
330         throws RepositoryIndexException
331     {
332         Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" );
333
334         RepositoryIndexRecord record = factory.createRecord( artifact );
335
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" );
348
349         assertEquals( "check record", expectedRecord, record );
350     }
351
352     public void testMissingFile()
353         throws RepositoryIndexException
354     {
355         Artifact artifact = createArtifact( "test-foo" );
356
357         RepositoryIndexRecord record = factory.createRecord( artifact );
358
359         assertNull( "Confirm no record is returned", record );
360     }
361
362     private Artifact createArtifact( String artifactId )
363     {
364         return createArtifact( artifactId, "1.0", "jar" );
365     }
366
367     private Artifact createArtifact( String artifactId, String version, String type )
368     {
369         return createArtifact( artifactId, version, type, null );
370     }
371
372     private Artifact createArtifact( String artifactId, String version, String type, String classifier )
373     {
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 );
380         return artifact;
381     }
382 }