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.RepositoryIndexRecord;
27 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
28 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
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.codehaus.plexus.util.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 LuceneStandardArtifactIndexTest
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, "standard" );
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.createStandardIndex( 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 assertJarRecord( artifact, document );
138 assertEquals( "Check index size", 1, reader.numDocs() );
146 public void testAddRecordExistingEmptyIndex()
147 throws IOException, RepositoryIndexException
151 Artifact artifact = createArtifact( "test-jar" );
153 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
154 index.indexRecords( Collections.singletonList( record ) );
156 IndexReader reader = IndexReader.open( indexLocation );
159 Document document = reader.document( 0 );
160 assertJarRecord( artifact, document );
161 assertEquals( "Check index size", 1, reader.numDocs() );
169 public void testAddRecordInIndex()
170 throws IOException, RepositoryIndexException
174 Artifact artifact = createArtifact( "test-jar" );
176 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
177 index.indexRecords( Collections.singletonList( record ) );
180 record = recordFactory.createRecord( artifact );
181 index.indexRecords( Collections.singletonList( record ) );
183 IndexReader reader = IndexReader.open( indexLocation );
186 Document document = reader.document( 0 );
187 assertJarRecord( artifact, document );
188 assertEquals( "Check index size", 1, reader.numDocs() );
196 public void testAddPomRecord()
197 throws IOException, RepositoryIndexException
201 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
203 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
204 index.indexRecords( Collections.singletonList( record ) );
206 IndexReader reader = IndexReader.open( indexLocation );
209 Document document = reader.document( 0 );
210 assertPomRecord( artifact, document );
211 assertEquals( "Check index size", 1, reader.numDocs() );
219 public void testAddPlugin()
220 throws IOException, RepositoryIndexException, XmlPullParserException
224 Artifact artifact = createArtifact( "test-plugin" );
226 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
228 index.indexRecords( Collections.singletonList( record ) );
230 IndexReader reader = IndexReader.open( indexLocation );
233 Document document = reader.document( 0 );
234 assertPluginRecord( artifact, document );
235 assertEquals( "Check index size", 1, reader.numDocs() );
243 public void testDeleteRecordInIndex()
244 throws IOException, RepositoryIndexException
248 Artifact artifact = createArtifact( "test-jar" );
250 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
251 index.indexRecords( Collections.singletonList( record ) );
253 index.deleteRecords( Collections.singletonList( record ) );
255 IndexReader reader = IndexReader.open( indexLocation );
258 assertEquals( "No documents", 0, reader.numDocs() );
266 public void testDeleteRecordNotInIndex()
267 throws IOException, RepositoryIndexException
271 Artifact artifact = createArtifact( "test-jar" );
273 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
275 index.deleteRecords( Collections.singletonList( record ) );
277 IndexReader reader = IndexReader.open( indexLocation );
280 assertEquals( "No documents", 0, reader.numDocs() );
288 public void testDeleteRecordNoIndex()
289 throws IOException, RepositoryIndexException
291 Artifact artifact = createArtifact( "test-jar" );
293 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
294 index.deleteRecords( Collections.singleton( record ) );
296 assertFalse( index.exists() );
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( Artifact artifact, Document document, String expectedArtifactId, String expectedType,
333 String expectedMd5, String expectedSha1 )
335 assertEquals( "Check document filename", repository.pathOf( artifact ),
336 document.get( StandardIndexRecordFields.FILENAME ) );
337 assertEquals( "Check document groupId", "org.apache.maven.archiva.record",
338 document.get( StandardIndexRecordFields.GROUPID ) );
339 assertEquals( "Check document artifactId", expectedArtifactId,
340 document.get( StandardIndexRecordFields.ARTIFACTID ) );
341 assertEquals( "Check document version", "1.0", document.get( StandardIndexRecordFields.VERSION ) );
342 assertEquals( "Check document type", expectedType, document.get( StandardIndexRecordFields.TYPE ) );
343 assertEquals( "Check document repository", "test", document.get( StandardIndexRecordFields.REPOSITORY ) );
344 assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ),
345 document.get( StandardIndexRecordFields.LAST_MODIFIED ) );
346 assertEquals( "Check document md5", expectedMd5, document.get( StandardIndexRecordFields.MD5 ) );
347 assertEquals( "Check document sha1", expectedSha1, document.get( StandardIndexRecordFields.SHA1 ) );
348 assertEquals( "Check document file size", artifact.getFile().length(),
349 NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
350 assertNull( "Check document classifier", document.get( StandardIndexRecordFields.CLASSIFIER ) );
353 private void assertPomRecord( Artifact artifact, Document document )
355 assertRecord( artifact, document, "test-pom", "pom", "98b4a1b708a90a8637aaf541bef5094f",
356 "d95348bee1666a46511260696292bfa0519b61c1" );
357 assertNull( "Check document classes", document.get( StandardIndexRecordFields.CLASSES ) );
358 assertNull( "Check document files", document.get( StandardIndexRecordFields.FILES ) );
359 assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
360 assertEquals( "Check document year", "2005", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
361 assertEquals( "Check document project name", "Maven Repository Manager Test POM",
362 document.get( StandardIndexRecordFields.PROJECT_NAME ) );
363 assertEquals( "Check document project description", "Description",
364 document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
365 assertEquals( "Check document packaging", "pom", document.get( StandardIndexRecordFields.PACKAGING ) );
368 private void assertJarRecord( Artifact artifact, Document document )
370 assertRecord( artifact, document, "test-jar", "jar", "3a0adc365f849366cd8b633cad155cb7",
371 "c66f18bf192cb613fc2febb4da541a34133eedc2" );
372 assertEquals( "Check document classes", "A\nb.B\nb.c.C", document.get( StandardIndexRecordFields.CLASSES ) );
373 assertEquals( "Check document files", "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class",
374 document.get( StandardIndexRecordFields.FILES ) );
375 assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
376 assertNull( "Check document projectName", document.get( StandardIndexRecordFields.PROJECT_NAME ) );
377 assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
378 assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
379 assertNull( "Check document packaging", document.get( StandardIndexRecordFields.PACKAGING ) );
382 private void assertPluginRecord( Artifact artifact, Document document )
384 assertRecord( artifact, document, "test-plugin", "maven-plugin", "3530896791670ebb45e17708e5d52c40",
385 "2cd2619d59a684e82e97471d2c2e004144c8f24e" );
386 assertEquals( "Check document classes", "org.apache.maven.archiva.record.MyMojo",
387 document.get( StandardIndexRecordFields.CLASSES ) );
388 assertEquals( "Check document files", "META-INF/MANIFEST.MF\n" +
389 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties\n" +
390 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml\n" + "META-INF/maven/plugin.xml\n" +
391 "org/apache/maven/archiva/record/MyMojo.class", document.get( StandardIndexRecordFields.FILES ) );
392 assertEquals( "Check document pluginPrefix", "test", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
393 assertEquals( "Check document packaging", "maven-plugin", document.get( StandardIndexRecordFields.PACKAGING ) );
394 assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
395 assertEquals( "Check document project name", "Maven Mojo Archetype",
396 document.get( StandardIndexRecordFields.PROJECT_NAME ) );
397 assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
400 private String getLastModified( File file )
402 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
403 dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
404 return dateFormat.format( new Date( file.lastModified() ) );