]> source.dussan.org Git - archiva.git/blob
c3b8c77f6a5ddae69b840e6889672c7e09552871
[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.MinimalIndexRecordFields;
31 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
32 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
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 LuceneMinimalArtifactIndexTest
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, "minimal" );
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.createMinimalIndex( 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             assertEquals( "Check document", repository.pathOf( artifact ),
141                           document.get( MinimalIndexRecordFields.FILENAME ) );
142             assertEquals( "Check index size", 1, reader.numDocs() );
143         }
144         finally
145         {
146             reader.close();
147         }
148     }
149
150     public void testAddRecordExistingEmptyIndex()
151         throws IOException, RepositoryIndexException
152     {
153         createEmptyIndex();
154
155         Artifact artifact = createArtifact( "test-jar" );
156
157         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
158         index.indexRecords( Collections.singletonList( record ) );
159
160         IndexReader reader = IndexReader.open( indexLocation );
161         try
162         {
163             Document document = reader.document( 0 );
164             assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" );
165             assertEquals( "Check index size", 1, reader.numDocs() );
166         }
167         finally
168         {
169             reader.close();
170         }
171     }
172
173     public void testAddRecordInIndex()
174         throws IOException, RepositoryIndexException
175     {
176         createEmptyIndex();
177
178         Artifact artifact = createArtifact( "test-jar" );
179
180         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
181         index.indexRecords( Collections.singletonList( record ) );
182
183         // Do it again
184         record = recordFactory.createRecord( artifact );
185         index.indexRecords( Collections.singletonList( record ) );
186
187         IndexReader reader = IndexReader.open( indexLocation );
188         try
189         {
190             Document document = reader.document( 0 );
191             assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" );
192             assertEquals( "Check index size", 1, reader.numDocs() );
193         }
194         finally
195         {
196             reader.close();
197         }
198     }
199
200     public void testDeleteRecordInIndex()
201         throws IOException, RepositoryIndexException
202     {
203         createEmptyIndex();
204
205         Artifact artifact = createArtifact( "test-jar" );
206
207         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
208         index.indexRecords( Collections.singletonList( record ) );
209
210         index.deleteRecords( Collections.singletonList( record ) );
211
212         IndexReader reader = IndexReader.open( indexLocation );
213         try
214         {
215             assertEquals( "No documents", 0, reader.numDocs() );
216         }
217         finally
218         {
219             reader.close();
220         }
221     }
222
223     public void testDeleteRecordNotInIndex()
224         throws IOException, RepositoryIndexException
225     {
226         createEmptyIndex();
227
228         Artifact artifact = createArtifact( "test-jar" );
229
230         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
231
232         index.deleteRecords( Collections.singletonList( record ) );
233
234         IndexReader reader = IndexReader.open( indexLocation );
235         try
236         {
237             assertEquals( "No documents", 0, reader.numDocs() );
238         }
239         finally
240         {
241             reader.close();
242         }
243     }
244
245     public void testDeleteRecordNoIndex()
246         throws IOException, RepositoryIndexException
247     {
248         Artifact artifact = createArtifact( "test-jar" );
249
250         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
251         index.deleteRecords( Collections.singleton( record ) );
252
253         assertFalse( index.exists() );
254     }
255
256     public void testAddPomRecord()
257         throws IOException, RepositoryIndexException
258     {
259         createEmptyIndex();
260
261         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
262
263         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
264         index.indexRecords( Collections.singletonList( record ) );
265
266         IndexReader reader = IndexReader.open( indexLocation );
267         try
268         {
269             assertEquals( "No documents", 0, reader.numDocs() );
270         }
271         finally
272         {
273             reader.close();
274         }
275     }
276
277     public void testAddPlugin()
278         throws IOException, RepositoryIndexException, XmlPullParserException
279     {
280         createEmptyIndex();
281
282         Artifact artifact = createArtifact( "test-plugin" );
283
284         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
285
286         index.indexRecords( Collections.singletonList( record ) );
287
288         IndexReader reader = IndexReader.open( indexLocation );
289         try
290         {
291             Document document = reader.document( 0 );
292             assertRecord( document, artifact, "3530896791670ebb45e17708e5d52c40",
293                           "org.apache.maven.archiva.record.MyMojo" );
294             assertEquals( "Check index size", 1, reader.numDocs() );
295         }
296         finally
297         {
298             reader.close();
299         }
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( Document document, Artifact artifact, String expectedChecksum, String expectedClasses )
336     {
337         assertEquals( "Check document filename", repository.pathOf( artifact ),
338                       document.get( MinimalIndexRecordFields.FILENAME ) );
339         assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ),
340                       document.get( MinimalIndexRecordFields.LAST_MODIFIED ) );
341         assertEquals( "Check document checksum", expectedChecksum, document.get( MinimalIndexRecordFields.MD5 ) );
342         assertEquals( "Check document size", artifact.getFile().length(),
343                       NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) );
344         assertEquals( "Check document classes", expectedClasses, document.get( MinimalIndexRecordFields.CLASSES ) );
345     }
346
347     private String getLastModified( File file )
348     {
349         SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
350         dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
351         return dateFormat.format( new Date( file.lastModified() ) );
352     }
353 }