1 package org.apache.maven.archiva.indexer.lucene;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
52 * Test the Lucene implementation of the artifact index.
54 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
56 public class LuceneMinimalArtifactIndexTest
57 extends PlexusTestCase
59 private RepositoryArtifactIndex index;
61 private ArtifactRepository repository;
63 private ArtifactFactory artifactFactory;
65 private File indexLocation;
67 private RepositoryIndexRecordFactory recordFactory;
69 protected void setUp()
74 recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" );
76 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
78 ArtifactRepositoryFactory repositoryFactory =
79 (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
81 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
83 File file = getTestFile( "src/test/managed-repository" );
85 repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null );
87 RepositoryArtifactIndexFactory factory =
88 (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" );
90 indexLocation = getTestFile( "target/test-index" );
92 FileUtils.deleteDirectory( indexLocation );
94 index = factory.createMinimalIndex( indexLocation );
97 public void testIndexExists()
98 throws IOException, RepositoryIndexException
100 assertFalse( "check index doesn't exist", index.exists() );
102 // create empty directory
103 indexLocation.mkdirs();
104 assertFalse( "check index doesn't exist even if directory does", index.exists() );
106 // create index, with no records
108 assertTrue( "check index is considered to exist", index.exists() );
110 // Test non-directory
111 FileUtils.deleteDirectory( indexLocation );
112 indexLocation.createNewFile();
116 fail( "Index operation should fail as the location is not valid" );
118 catch ( RepositoryIndexException e )
124 indexLocation.delete();
128 public void testAddRecordNoIndex()
129 throws IOException, RepositoryIndexException
131 Artifact artifact = createArtifact( "test-jar" );
133 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
134 index.indexRecords( Collections.singletonList( record ) );
136 IndexReader reader = IndexReader.open( indexLocation );
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() );
150 public void testAddRecordExistingEmptyIndex()
151 throws IOException, RepositoryIndexException
155 Artifact artifact = createArtifact( "test-jar" );
157 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
158 index.indexRecords( Collections.singletonList( record ) );
160 IndexReader reader = IndexReader.open( indexLocation );
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() );
173 public void testAddRecordInIndex()
174 throws IOException, RepositoryIndexException
178 Artifact artifact = createArtifact( "test-jar" );
180 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
181 index.indexRecords( Collections.singletonList( record ) );
184 record = recordFactory.createRecord( artifact );
185 index.indexRecords( Collections.singletonList( record ) );
187 IndexReader reader = IndexReader.open( indexLocation );
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() );
200 public void testDeleteRecordInIndex()
201 throws IOException, RepositoryIndexException
205 Artifact artifact = createArtifact( "test-jar" );
207 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
208 index.indexRecords( Collections.singletonList( record ) );
210 index.deleteRecords( Collections.singletonList( record ) );
212 IndexReader reader = IndexReader.open( indexLocation );
215 assertEquals( "No documents", 0, reader.numDocs() );
223 public void testDeleteRecordNotInIndex()
224 throws IOException, RepositoryIndexException
228 Artifact artifact = createArtifact( "test-jar" );
230 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
232 index.deleteRecords( Collections.singletonList( record ) );
234 IndexReader reader = IndexReader.open( indexLocation );
237 assertEquals( "No documents", 0, reader.numDocs() );
245 public void testDeleteRecordNoIndex()
246 throws IOException, RepositoryIndexException
248 Artifact artifact = createArtifact( "test-jar" );
250 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
251 index.deleteRecords( Collections.singleton( record ) );
253 assertFalse( index.exists() );
256 public void testAddPomRecord()
257 throws IOException, RepositoryIndexException
261 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
263 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
264 index.indexRecords( Collections.singletonList( record ) );
266 IndexReader reader = IndexReader.open( indexLocation );
269 assertEquals( "No documents", 0, reader.numDocs() );
277 public void testAddPlugin()
278 throws IOException, RepositoryIndexException, XmlPullParserException
282 Artifact artifact = createArtifact( "test-plugin" );
284 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
286 index.indexRecords( Collections.singletonList( record ) );
288 IndexReader reader = IndexReader.open( indexLocation );
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() );
302 private Artifact createArtifact( String artifactId )
304 return createArtifact( artifactId, "1.0", "jar" );
307 private Artifact createArtifact( String artifactId, String version, String type )
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 );
316 private void createEmptyIndex()
319 createIndex( Collections.EMPTY_LIST );
322 private void createIndex( List docments )
325 IndexWriter writer = new IndexWriter( indexLocation, LuceneRepositoryArtifactIndex.getAnalyzer(), true );
326 for ( Iterator i = docments.iterator(); i.hasNext(); )
328 Document document = (Document) i.next();
329 writer.addDocument( document );
335 private void assertRecord( Document document, Artifact artifact, String expectedChecksum, String expectedClasses )
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 ) );
347 private String getLastModified( File file )
349 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
350 dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
351 return dateFormat.format( new Date( file.lastModified() ) );