1 package org.apache.maven.archiva.indexer.lucene;
4 * Copyright 2005-2006 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
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;
49 * Test the Lucene implementation of the artifact index.
51 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
53 public class LuceneMinimalArtifactIndexTest
54 extends PlexusTestCase
56 private RepositoryArtifactIndex index;
58 private ArtifactRepository repository;
60 private ArtifactFactory artifactFactory;
62 private File indexLocation;
64 private RepositoryIndexRecordFactory recordFactory;
66 protected void setUp()
71 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
73 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
75 ArtifactRepositoryFactory repositoryFactory =
76 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
78 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
80 File file = getTestFile( "src/test/managed-repository" );
82 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
84 RepositoryArtifactIndexFactory factory =
85 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
87 indexLocation = getTestFile( "target/test-index" );
89 FileUtils.deleteDirectory( indexLocation );
91 index = factory.createMinimalIndex( indexLocation );
94 public void testIndexExists()
95 throws IOException, RepositoryIndexException
97 assertFalse( "check index doesn't exist", index.exists() );
99 // create empty directory
100 indexLocation.mkdirs();
101 assertFalse( "check index doesn't exist even if directory does", index.exists() );
103 // create index, with no records
105 assertTrue( "check index is considered to exist", index.exists() );
107 // Test non-directory
108 FileUtils.deleteDirectory( indexLocation );
109 indexLocation.createNewFile();
113 fail( "Index operation should fail as the location is not valid" );
115 catch ( RepositoryIndexException e )
121 indexLocation.delete();
125 public void testAddRecordNoIndex()
126 throws IOException, RepositoryIndexException
128 Artifact artifact = createArtifact( "test-jar" );
130 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
131 index.indexRecords( Collections.singletonList( record ) );
133 IndexReader reader = IndexReader.open( indexLocation );
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() );
147 public void testAddRecordExistingEmptyIndex()
148 throws IOException, RepositoryIndexException
152 Artifact artifact = createArtifact( "test-jar" );
154 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
155 index.indexRecords( Collections.singletonList( record ) );
157 IndexReader reader = IndexReader.open( indexLocation );
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() );
170 public void testAddRecordInIndex()
171 throws IOException, RepositoryIndexException
175 Artifact artifact = createArtifact( "test-jar" );
177 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
178 index.indexRecords( Collections.singletonList( record ) );
181 record = recordFactory.createRecord( artifact );
182 index.indexRecords( Collections.singletonList( record ) );
184 IndexReader reader = IndexReader.open( indexLocation );
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() );
197 public void testDeleteRecordInIndex()
198 throws IOException, RepositoryIndexException
202 Artifact artifact = createArtifact( "test-jar" );
204 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
205 index.indexRecords( Collections.singletonList( record ) );
207 index.deleteRecords( Collections.singletonList( record ) );
209 IndexReader reader = IndexReader.open( indexLocation );
212 assertEquals( "No documents", 0, reader.numDocs() );
220 public void testDeleteRecordNotInIndex()
221 throws IOException, RepositoryIndexException
225 Artifact artifact = createArtifact( "test-jar" );
227 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
229 index.deleteRecords( Collections.singletonList( record ) );
231 IndexReader reader = IndexReader.open( indexLocation );
234 assertEquals( "No documents", 0, reader.numDocs() );
242 public void testDeleteRecordNoIndex()
243 throws IOException, RepositoryIndexException
245 Artifact artifact = createArtifact( "test-jar" );
247 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
248 index.deleteRecords( Collections.singleton( record ) );
250 assertFalse( index.exists() );
253 public void testAddPomRecord()
254 throws IOException, RepositoryIndexException
258 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
260 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
261 index.indexRecords( Collections.singletonList( record ) );
263 IndexReader reader = IndexReader.open( indexLocation );
266 assertEquals( "No documents", 0, reader.numDocs() );
274 public void testAddPlugin()
275 throws IOException, RepositoryIndexException, XmlPullParserException
279 Artifact artifact = createArtifact( "test-plugin" );
281 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
283 index.indexRecords( Collections.singletonList( record ) );
285 IndexReader reader = IndexReader.open( indexLocation );
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() );
299 private Artifact createArtifact( String artifactId )
301 return createArtifact( artifactId, "1.0", "jar" );
304 private Artifact createArtifact( String artifactId, String version, String type )
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 );
313 private void createEmptyIndex()
316 createIndex( Collections.EMPTY_LIST );
319 private void createIndex( List docments )
322 IndexWriter writer = new IndexWriter( indexLocation, LuceneRepositoryArtifactIndex.getAnalyzer(), true );
323 for ( Iterator i = docments.iterator(); i.hasNext(); )
325 Document document = (Document) i.next();
326 writer.addDocument( document );
332 private void assertRecord( Document document, Artifact artifact, String expectedChecksum, String expectedClasses )
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 ) );
344 private String getLastModified( File file )
346 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
347 dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
348 return dateFormat.format( new Date( file.lastModified() ) );