]> source.dussan.org Git - archiva.git/blob
7385edfa2e59f92eaf40be1c36064e25f93d817b
[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.MinimalIndexRecordFields;
27 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
28 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
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 LuceneMinimalArtifactIndexTest
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, "minimal" );
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.createMinimalIndex( 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             assertEquals( "Check document", repository.pathOf( artifact ),
138                           document.get( MinimalIndexRecordFields.FILENAME ) );
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             assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" );
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             assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" );
189             assertEquals( "Check index size", 1, reader.numDocs() );
190         }
191         finally
192         {
193             reader.close();
194         }
195     }
196
197     public void testDeleteRecordInIndex()
198         throws IOException, RepositoryIndexException
199     {
200         createEmptyIndex();
201
202         Artifact artifact = createArtifact( "test-jar" );
203
204         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
205         index.indexRecords( Collections.singletonList( record ) );
206
207         index.deleteRecords( Collections.singletonList( record ) );
208
209         IndexReader reader = IndexReader.open( indexLocation );
210         try
211         {
212             assertEquals( "No documents", 0, reader.numDocs() );
213         }
214         finally
215         {
216             reader.close();
217         }
218     }
219
220     public void testDeleteRecordNotInIndex()
221         throws IOException, RepositoryIndexException
222     {
223         createEmptyIndex();
224
225         Artifact artifact = createArtifact( "test-jar" );
226
227         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
228
229         index.deleteRecords( Collections.singletonList( record ) );
230
231         IndexReader reader = IndexReader.open( indexLocation );
232         try
233         {
234             assertEquals( "No documents", 0, reader.numDocs() );
235         }
236         finally
237         {
238             reader.close();
239         }
240     }
241
242     public void testDeleteRecordNoIndex()
243         throws IOException, RepositoryIndexException
244     {
245         Artifact artifact = createArtifact( "test-jar" );
246
247         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
248         index.deleteRecords( Collections.singleton( record ) );
249
250         assertFalse( index.exists() );
251     }
252
253     public void testAddPomRecord()
254         throws IOException, RepositoryIndexException
255     {
256         createEmptyIndex();
257
258         Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
259
260         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
261         index.indexRecords( Collections.singletonList( record ) );
262
263         IndexReader reader = IndexReader.open( indexLocation );
264         try
265         {
266             assertEquals( "No documents", 0, reader.numDocs() );
267         }
268         finally
269         {
270             reader.close();
271         }
272     }
273
274     public void testAddPlugin()
275         throws IOException, RepositoryIndexException, XmlPullParserException
276     {
277         createEmptyIndex();
278
279         Artifact artifact = createArtifact( "test-plugin" );
280
281         RepositoryIndexRecord record = recordFactory.createRecord( artifact );
282
283         index.indexRecords( Collections.singletonList( record ) );
284
285         IndexReader reader = IndexReader.open( indexLocation );
286         try
287         {
288             Document document = reader.document( 0 );
289             assertRecord( document, artifact, "3530896791670ebb45e17708e5d52c40",
290                           "org.apache.maven.archiva.record.MyMojo" );
291             assertEquals( "Check index size", 1, reader.numDocs() );
292         }
293         finally
294         {
295             reader.close();
296         }
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( Document document, Artifact artifact, String expectedChecksum, String expectedClasses )
333     {
334         assertEquals( "Check document filename", repository.pathOf( artifact ),
335                       document.get( MinimalIndexRecordFields.FILENAME ) );
336         assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ),
337                       document.get( MinimalIndexRecordFields.LAST_MODIFIED ) );
338         assertEquals( "Check document checksum", expectedChecksum, document.get( MinimalIndexRecordFields.MD5 ) );
339         assertEquals( "Check document size", artifact.getFile().length(),
340                       NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) );
341         assertEquals( "Check document classes", expectedClasses, document.get( MinimalIndexRecordFields.CLASSES ) );
342     }
343
344     private String getLastModified( File file )
345     {
346         SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
347         dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
348         return dateFormat.format( new Date( file.lastModified() ) );
349     }
350 }