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