1 package org.apache.maven.archiva.indexing.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.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;
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;
50 * Test the Lucene implementation of the artifact index.
52 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
54 public class LuceneMinimalArtifactIndexTest
55 extends PlexusTestCase
57 private RepositoryArtifactIndex index;
59 private ArtifactRepository repository;
61 private ArtifactFactory artifactFactory;
63 private File indexLocation;
65 private RepositoryIndexRecordFactory recordFactory;
67 protected void setUp()
72 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
74 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
76 ArtifactRepositoryFactory repositoryFactory =
77 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
79 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
81 File file = getTestFile( "src/test/managed-repository" );
83 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
85 RepositoryArtifactIndexFactory factory =
86 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
88 indexLocation = getTestFile( "target/test-index" );
90 FileUtils.deleteDirectory( indexLocation );
92 index = factory.createMinimalIndex( indexLocation );
95 public void testIndexExists()
96 throws IOException, RepositoryIndexException
98 assertFalse( "check index doesn't exist", index.exists() );
100 // create empty directory
101 indexLocation.mkdirs();
102 assertFalse( "check index doesn't exist even if directory does", index.exists() );
104 // create index, with no records
106 assertTrue( "check index is considered to exist", index.exists() );
108 // Test non-directory
109 FileUtils.deleteDirectory( indexLocation );
110 indexLocation.createNewFile();
114 fail( "Index operation should fail as the location is not valid" );
116 catch ( RepositoryIndexException e )
122 indexLocation.delete();
126 public void testAddRecordNoIndex()
127 throws IOException, RepositoryIndexException
129 Artifact artifact = createArtifact( "test-jar" );
131 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
132 index.indexRecords( Collections.singletonList( record ) );
134 IndexReader reader = IndexReader.open( indexLocation );
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() );
148 public void testAddRecordExistingEmptyIndex()
149 throws IOException, RepositoryIndexException
153 Artifact artifact = createArtifact( "test-jar" );
155 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
156 index.indexRecords( Collections.singletonList( record ) );
158 IndexReader reader = IndexReader.open( indexLocation );
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() );
171 public void testAddRecordInIndex()
172 throws IOException, RepositoryIndexException
176 Artifact artifact = createArtifact( "test-jar" );
178 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
179 index.indexRecords( Collections.singletonList( record ) );
182 record = recordFactory.createRecord( artifact );
183 index.indexRecords( Collections.singletonList( record ) );
185 IndexReader reader = IndexReader.open( indexLocation );
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() );
198 public void testDeleteRecordInIndex()
199 throws IOException, RepositoryIndexException
203 Artifact artifact = createArtifact( "test-jar" );
205 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
206 index.indexRecords( Collections.singletonList( record ) );
208 index.deleteRecords( Collections.singletonList( record ) );
210 IndexReader reader = IndexReader.open( indexLocation );
213 assertEquals( "No documents", 0, reader.numDocs() );
221 public void testDeleteRecordNotInIndex()
222 throws IOException, RepositoryIndexException
226 Artifact artifact = createArtifact( "test-jar" );
228 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
230 index.deleteRecords( Collections.singletonList( record ) );
232 IndexReader reader = IndexReader.open( indexLocation );
235 assertEquals( "No documents", 0, reader.numDocs() );
243 public void testDeleteRecordNoIndex()
244 throws IOException, RepositoryIndexException
246 Artifact artifact = createArtifact( "test-jar" );
248 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
249 index.deleteRecords( Collections.singleton( record ) );
251 assertFalse( index.exists() );
254 public void testAddPomRecord()
255 throws IOException, RepositoryIndexException
259 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
261 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
262 index.indexRecords( Collections.singletonList( record ) );
264 IndexReader reader = IndexReader.open( indexLocation );
267 assertEquals( "No documents", 0, reader.numDocs() );
275 public void testAddPlugin()
276 throws IOException, RepositoryIndexException, XmlPullParserException
280 Artifact artifact = createArtifact( "test-plugin" );
282 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
284 index.indexRecords( Collections.singletonList( record ) );
286 IndexReader reader = IndexReader.open( indexLocation );
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() );
300 private Artifact createArtifact( String artifactId )
302 return createArtifact( artifactId, "1.0", "jar" );
305 private Artifact createArtifact( String artifactId, String version, String type )
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 );
314 private void createEmptyIndex()
317 createIndex( Collections.EMPTY_LIST );
320 private void createIndex( List docments )
323 IndexWriter writer = new IndexWriter( indexLocation, new StandardAnalyzer(), true );
324 for ( Iterator i = docments.iterator(); i.hasNext(); )
326 Document document = (Document) i.next();
327 writer.addDocument( document );
333 private void assertRecord( Document document, Artifact artifact, String expectedChecksum, String expectedClasses )
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 ) );
345 private String getLastModified( File file )
347 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
348 dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
349 return dateFormat.format( new Date( file.lastModified() ) );