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.RepositoryIndexRecord;
31 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory;
32 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
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 LuceneStandardArtifactIndexTest
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, "standard" );
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.createStandardIndex( 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 assertJarRecord( artifact, document );
141 assertEquals( "Check index size", 1, reader.numDocs() );
149 public void testAddRecordExistingEmptyIndex()
150 throws IOException, RepositoryIndexException
154 Artifact artifact = createArtifact( "test-jar" );
156 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
157 index.indexRecords( Collections.singletonList( record ) );
159 IndexReader reader = IndexReader.open( indexLocation );
162 Document document = reader.document( 0 );
163 assertJarRecord( artifact, document );
164 assertEquals( "Check index size", 1, reader.numDocs() );
172 public void testAddRecordInIndex()
173 throws IOException, RepositoryIndexException
177 Artifact artifact = createArtifact( "test-jar" );
179 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
180 index.indexRecords( Collections.singletonList( record ) );
183 record = recordFactory.createRecord( artifact );
184 index.indexRecords( Collections.singletonList( record ) );
186 IndexReader reader = IndexReader.open( indexLocation );
189 Document document = reader.document( 0 );
190 assertJarRecord( artifact, document );
191 assertEquals( "Check index size", 1, reader.numDocs() );
199 public void testAddPomRecord()
200 throws IOException, RepositoryIndexException
204 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
206 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
207 index.indexRecords( Collections.singletonList( record ) );
209 IndexReader reader = IndexReader.open( indexLocation );
212 Document document = reader.document( 0 );
213 assertPomRecord( artifact, document );
214 assertEquals( "Check index size", 1, reader.numDocs() );
222 public void testAddPlugin()
223 throws IOException, RepositoryIndexException, XmlPullParserException
227 Artifact artifact = createArtifact( "test-plugin" );
229 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
231 index.indexRecords( Collections.singletonList( record ) );
233 IndexReader reader = IndexReader.open( indexLocation );
236 Document document = reader.document( 0 );
237 assertPluginRecord( artifact, document );
238 assertEquals( "Check index size", 1, reader.numDocs() );
246 public void testDeleteRecordInIndex()
247 throws IOException, RepositoryIndexException
251 Artifact artifact = createArtifact( "test-jar" );
253 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
254 index.indexRecords( Collections.singletonList( record ) );
256 index.deleteRecords( Collections.singletonList( record ) );
258 IndexReader reader = IndexReader.open( indexLocation );
261 assertEquals( "No documents", 0, reader.numDocs() );
269 public void testDeleteRecordNotInIndex()
270 throws IOException, RepositoryIndexException
274 Artifact artifact = createArtifact( "test-jar" );
276 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
278 index.deleteRecords( Collections.singletonList( record ) );
280 IndexReader reader = IndexReader.open( indexLocation );
283 assertEquals( "No documents", 0, reader.numDocs() );
291 public void testDeleteRecordNoIndex()
292 throws IOException, RepositoryIndexException
294 Artifact artifact = createArtifact( "test-jar" );
296 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
297 index.deleteRecords( Collections.singleton( record ) );
299 assertFalse( index.exists() );
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( Artifact artifact, Document document, String expectedArtifactId, String expectedType,
336 String expectedMd5, String expectedSha1 )
338 assertEquals( "Check document filename", repository.pathOf( artifact ),
339 document.get( StandardIndexRecordFields.FILENAME ) );
340 assertEquals( "Check document groupId", "org.apache.maven.archiva.record",
341 document.get( StandardIndexRecordFields.GROUPID ) );
342 assertEquals( "Check document artifactId", expectedArtifactId,
343 document.get( StandardIndexRecordFields.ARTIFACTID ) );
344 assertEquals( "Check document version", "1.0", document.get( StandardIndexRecordFields.VERSION ) );
345 assertEquals( "Check document type", expectedType, document.get( StandardIndexRecordFields.TYPE ) );
346 assertEquals( "Check document repository", "test", document.get( StandardIndexRecordFields.REPOSITORY ) );
347 assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ),
348 document.get( StandardIndexRecordFields.LAST_MODIFIED ) );
349 assertEquals( "Check document md5", expectedMd5, document.get( StandardIndexRecordFields.MD5 ) );
350 assertEquals( "Check document sha1", expectedSha1, document.get( StandardIndexRecordFields.SHA1 ) );
351 assertEquals( "Check document file size", artifact.getFile().length(),
352 NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
353 assertNull( "Check document classifier", document.get( StandardIndexRecordFields.CLASSIFIER ) );
356 private void assertPomRecord( Artifact artifact, Document document )
358 assertRecord( artifact, document, "test-pom", "pom", "758e1ae96dff63dab7278a62e3eb174d",
359 "770fde06cd5c3dccb5f5e8c6754b8c4c77b98560" );
360 assertNull( "Check document classes", document.get( StandardIndexRecordFields.CLASSES ) );
361 assertNull( "Check document files", document.get( StandardIndexRecordFields.FILES ) );
362 assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
363 assertEquals( "Check document year", "2005", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
364 assertEquals( "Check document project name", "Maven Repository Manager Test POM",
365 document.get( StandardIndexRecordFields.PROJECT_NAME ) );
366 assertEquals( "Check document project description", "Description",
367 document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
368 assertEquals( "Check document packaging", "pom", document.get( StandardIndexRecordFields.PACKAGING ) );
371 private void assertJarRecord( Artifact artifact, Document document )
373 assertRecord( artifact, document, "test-jar", "jar", "3a0adc365f849366cd8b633cad155cb7",
374 "c66f18bf192cb613fc2febb4da541a34133eedc2" );
375 assertEquals( "Check document classes", "A\nb.B\nb.c.C", document.get( StandardIndexRecordFields.CLASSES ) );
376 assertEquals( "Check document files", "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class",
377 document.get( StandardIndexRecordFields.FILES ) );
378 assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
379 assertNull( "Check document projectName", document.get( StandardIndexRecordFields.PROJECT_NAME ) );
380 assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
381 assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
382 assertNull( "Check document packaging", document.get( StandardIndexRecordFields.PACKAGING ) );
385 private void assertPluginRecord( Artifact artifact, Document document )
387 assertRecord( artifact, document, "test-plugin", "maven-plugin", "3530896791670ebb45e17708e5d52c40",
388 "2cd2619d59a684e82e97471d2c2e004144c8f24e" );
389 assertEquals( "Check document classes", "org.apache.maven.archiva.record.MyMojo",
390 document.get( StandardIndexRecordFields.CLASSES ) );
391 assertEquals( "Check document files", "META-INF/MANIFEST.MF\n" +
392 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties\n" +
393 "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml\n" + "META-INF/maven/plugin.xml\n" +
394 "org/apache/maven/archiva/record/MyMojo.class", document.get( StandardIndexRecordFields.FILES ) );
395 assertEquals( "Check document pluginPrefix", "test", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
396 assertEquals( "Check document packaging", "maven-plugin", document.get( StandardIndexRecordFields.PACKAGING ) );
397 assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
398 assertEquals( "Check document project name", "Maven Mojo Archetype",
399 document.get( StandardIndexRecordFields.PROJECT_NAME ) );
400 assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
403 private String getLastModified( File file )
405 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
406 dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
407 return dateFormat.format( new Date( file.lastModified() ) );