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