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