]> source.dussan.org Git - archiva.git/blob
1fa50264afe4d288f7aa2b70b66c67036d071815
[archiva.git] /
1 package org.apache.maven.archiva.indexer.lucene;
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.lucene.document.Document;
20 import org.apache.lucene.document.NumberTools;
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.IndexWriter;
23 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
24 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
25 import org.apache.maven.archiva.indexer.RepositoryIndexException;
26 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
27 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
28 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.factory.ArtifactFactory;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
33 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34 import org.codehaus.plexus.PlexusTestCase;
35 import org.apache.commons.io.FileUtils;
36 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.text.SimpleDateFormat;
41 import java.util.Collections;
42 import java.util.Date;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.TimeZone;
47
48 /**
49  * Test the Lucene implementation of the artifact index.
50  *
51  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
52  */
53 public class LuceneStandardArtifactIndexTest
54     extends PlexusTestCase
55 {
56     private RepositoryArtifactIndex index;
57
58     private ArtifactRepository repository;
59
60     private ArtifactFactory artifactFactory;
61
62     private File indexLocation;
63
64     private RepositoryIndexRecordFactory recordFactory;
65
66     protected void setUp()
67         throws Exception
68     {
69         super.setUp();
70
71         recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" );
72
73         artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
74
75         ArtifactRepositoryFactory repositoryFactory =
76             (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
77
78         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
79
80         File file = getTestFile( "src/test/managed-repository" );
81         repository =
82             repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
83
84         RepositoryArtifactIndexFactory factory =
85             (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
86
87         indexLocation = getTestFile( "target/test-index" );
88
89         FileUtils.deleteDirectory( indexLocation );
90
91         index = factory.createStandardIndex( indexLocation );
92     }
93
94     public void testIndexExists()
95         throws IOException, RepositoryIndexException
96     {
97         assertFalse( "check index doesn't exist", index.exists() );
98
99         // create empty directory
100         indexLocation.mkdirs();
101         assertFalse( "check index doesn't exist even if directory does", index.exists() );
102
103         // create index, with no records
104         createEmptyIndex();
105         assertTrue( "check index is considered to exist", index.exists() );
106
107         // Test non-directory
108         FileUtils.deleteDirectory( indexLocation );
109         indexLocation.createNewFile();
110         try
111         {
112             index.exists();
113             fail( "Index operation should fail as the location is not valid" );
114         }
115         catch ( RepositoryIndexException e )
116         {
117             // great
118         }
119         finally
120         {
121             indexLocation.delete();
122         }
123     }
124
125     public void testAddRecordNoIndex()
126         throws IOException, RepositoryIndexException
127     {
128         Artifact artifact = createArtifact( "test-jar" );
129
130         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
131         index.indexRecords( Collections.singletonList( record ) );
132
133         IndexReader reader = IndexReader.open( indexLocation );
134         try
135         {
136             Document document = reader.document( 0 );
137             assertJarRecord( artifact, document );
138             assertEquals( "Check index size", 1, reader.numDocs() );
139         }
140         finally
141         {
142             reader.close();
143         }
144     }
145
146     public void testAddRecordExistingEmptyIndex()
147         throws IOException, RepositoryIndexException
148     {
149         createEmptyIndex();
150
151         Artifact artifact = createArtifact( "test-jar" );
152
153         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
154         index.indexRecords( Collections.singletonList( record ) );
155
156         IndexReader reader = IndexReader.open( indexLocation );
157         try
158         {
159             Document document = reader.document( 0 );
160             assertJarRecord( artifact, document );
161             assertEquals( "Check index size", 1, reader.numDocs() );
162         }
163         finally
164         {
165             reader.close();
166         }
167     }
168
169     public void testAddRecordInIndex()
170         throws IOException, RepositoryIndexException
171     {
172         createEmptyIndex();
173
174         Artifact artifact = createArtifact( "test-jar" );
175
176         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
177         index.indexRecords( Collections.singletonList( record ) );
178
179         // Do it again
180         record = recordFactory.createRecord( artifact );
181         index.indexRecords( Collections.singletonList( record ) );
182
183         IndexReader reader = IndexReader.open( indexLocation );
184         try
185         {
186             Document document = reader.document( 0 );
187             assertJarRecord( artifact, document );
188             assertEquals( "Check index size", 1, reader.numDocs() );
189         }
190         finally
191         {
192             reader.close();
193         }
194     }
195
196     public void testAddPomRecord()
197         throws IOException, RepositoryIndexException
198     {
199         createEmptyIndex();
200
201         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
202
203         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
204         index.indexRecords( Collections.singletonList( record ) );
205
206         IndexReader reader = IndexReader.open( indexLocation );
207         try
208         {
209             Document document = reader.document( 0 );
210             assertPomRecord( artifact, document );
211             assertEquals( "Check index size", 1, reader.numDocs() );
212         }
213         finally
214         {
215             reader.close();
216         }
217     }
218
219     public void testAddPlugin()
220         throws IOException, RepositoryIndexException, XmlPullParserException
221     {
222         createEmptyIndex();
223
224         Artifact artifact = createArtifact( "test-plugin" );
225
226         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
227
228         index.indexRecords( Collections.singletonList( record ) );
229
230         IndexReader reader = IndexReader.open( indexLocation );
231         try
232         {
233             Document document = reader.document( 0 );
234             assertPluginRecord( artifact, document );
235             assertEquals( "Check index size", 1, reader.numDocs() );
236         }
237         finally
238         {
239             reader.close();
240         }
241     }
242
243     public void testDeleteRecordInIndex()
244         throws IOException, RepositoryIndexException
245     {
246         createEmptyIndex();
247
248         Artifact artifact = createArtifact( "test-jar" );
249
250         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
251         index.indexRecords( Collections.singletonList( record ) );
252
253         index.deleteRecords( Collections.singletonList( record ) );
254
255         IndexReader reader = IndexReader.open( indexLocation );
256         try
257         {
258             assertEquals( "No documents", 0, reader.numDocs() );
259         }
260         finally
261         {
262             reader.close();
263         }
264     }
265
266     public void testDeleteRecordNotInIndex()
267         throws IOException, RepositoryIndexException
268     {
269         createEmptyIndex();
270
271         Artifact artifact = createArtifact( "test-jar" );
272
273         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
274
275         index.deleteRecords( Collections.singletonList( record ) );
276
277         IndexReader reader = IndexReader.open( indexLocation );
278         try
279         {
280             assertEquals( "No documents", 0, reader.numDocs() );
281         }
282         finally
283         {
284             reader.close();
285         }
286     }
287
288     public void testDeleteRecordNoIndex()
289         throws IOException, RepositoryIndexException
290     {
291         Artifact artifact = createArtifact( "test-jar" );
292
293         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
294         index.deleteRecords( Collections.singleton( record ) );
295
296         assertFalse( index.exists() );
297     }
298
299     private Artifact createArtifact( String artifactId )
300     {
301         return createArtifact( artifactId, "1.0", "jar" );
302     }
303
304     private Artifact createArtifact( String artifactId, String version, String type )
305     {
306         Artifact artifact =
307             artifactFactory.createBuildArtifact( "org.apache.maven.archiva.record", artifactId, version, type );
308         artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
309         artifact.setRepository( repository );
310         return artifact;
311     }
312
313     private void createEmptyIndex()
314         throws IOException
315     {
316         createIndex( Collections.EMPTY_LIST );
317     }
318
319     private void createIndex( List docments )
320         throws IOException
321     {
322         IndexWriter writer = new IndexWriter( indexLocation, LuceneRepositoryArtifactIndex.getAnalyzer(), true );
323         for ( Iterator i = docments.iterator(); i.hasNext(); )
324         {
325             Document document = (Document) i.next();
326             writer.addDocument( document );
327         }
328         writer.optimize();
329         writer.close();
330     }
331
332     private void assertRecord( Artifact artifact, Document document, String expectedArtifactId, String expectedType,
333                                String expectedMd5, String expectedSha1 )
334     {
335         assertEquals( "Check document filename", repository.pathOf( artifact ),
336                       document.get( StandardIndexRecordFields.FILENAME ) );
337         assertEquals( "Check document groupId", "org.apache.maven.archiva.record",
338                       document.get( StandardIndexRecordFields.GROUPID ) );
339         assertEquals( "Check document artifactId", expectedArtifactId,
340                       document.get( StandardIndexRecordFields.ARTIFACTID ) );
341         assertEquals( "Check document version", "1.0", document.get( StandardIndexRecordFields.VERSION ) );
342         assertEquals( "Check document type", expectedType, document.get( StandardIndexRecordFields.TYPE ) );
343         assertEquals( "Check document repository", "test", document.get( StandardIndexRecordFields.REPOSITORY ) );
344         assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ),
345                       document.get( StandardIndexRecordFields.LAST_MODIFIED ) );
346         assertEquals( "Check document md5", expectedMd5, document.get( StandardIndexRecordFields.MD5 ) );
347         assertEquals( "Check document sha1", expectedSha1, document.get( StandardIndexRecordFields.SHA1 ) );
348         assertEquals( "Check document file size", artifact.getFile().length(),
349                       NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
350         assertNull( "Check document classifier", document.get( StandardIndexRecordFields.CLASSIFIER ) );
351     }
352
353     private void assertPomRecord( Artifact artifact, Document document )
354     {
355         assertRecord( artifact, document, "test-pom", "pom", "98b4a1b708a90a8637aaf541bef5094f",
356                       "d95348bee1666a46511260696292bfa0519b61c1" );
357         assertNull( "Check document classes", document.get( StandardIndexRecordFields.CLASSES ) );
358         assertNull( "Check document files", document.get( StandardIndexRecordFields.FILES ) );
359         assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
360         assertEquals( "Check document year", "2005", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
361         assertEquals( "Check document project name", "Maven Repository Manager Test POM",
362                       document.get( StandardIndexRecordFields.PROJECT_NAME ) );
363         assertEquals( "Check document project description", "Description",
364                       document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
365         assertEquals( "Check document packaging", "pom", document.get( StandardIndexRecordFields.PACKAGING ) );
366     }
367
368     private void assertJarRecord( Artifact artifact, Document document )
369     {
370         assertRecord( artifact, document, "test-jar", "jar", "3a0adc365f849366cd8b633cad155cb7",
371                       "c66f18bf192cb613fc2febb4da541a34133eedc2" );
372         assertEquals( "Check document classes", "A\nb.B\nb.c.C", document.get( StandardIndexRecordFields.CLASSES ) );
373         assertEquals( "Check document files", "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class",
374                       document.get( StandardIndexRecordFields.FILES ) );
375         assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
376         assertNull( "Check document projectName", document.get( StandardIndexRecordFields.PROJECT_NAME ) );
377         assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
378         assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
379         assertNull( "Check document packaging", document.get( StandardIndexRecordFields.PACKAGING ) );
380     }
381
382     private void assertPluginRecord( Artifact artifact, Document document )
383     {
384         assertRecord( artifact, document, "test-plugin", "maven-plugin", "3530896791670ebb45e17708e5d52c40",
385                       "2cd2619d59a684e82e97471d2c2e004144c8f24e" );
386         assertEquals( "Check document classes", "org.apache.maven.archiva.record.MyMojo",
387                       document.get( StandardIndexRecordFields.CLASSES ) );
388         assertEquals( "Check document files", "META-INF/MANIFEST.MF\n" +
389             "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties\n" +
390             "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml\n" + "META-INF/maven/plugin.xml\n" +
391             "org/apache/maven/archiva/record/MyMojo.class", document.get( StandardIndexRecordFields.FILES ) );
392         assertEquals( "Check document pluginPrefix", "test", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
393         assertEquals( "Check document packaging", "maven-plugin", document.get( StandardIndexRecordFields.PACKAGING ) );
394         assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
395         assertEquals( "Check document project name", "Maven Mojo Archetype",
396                       document.get( StandardIndexRecordFields.PROJECT_NAME ) );
397         assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
398     }
399
400     private String getLastModified( File file )
401     {
402         SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
403         dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
404         return dateFormat.format( new Date( file.lastModified() ) );
405     }
406 }