1 package org.apache.maven.repository.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.artifact.Artifact;
25 import org.apache.maven.artifact.factory.ArtifactFactory;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
28 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29 import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
30 import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
31 import org.apache.maven.repository.indexing.RepositoryIndexException;
32 import org.apache.maven.repository.indexing.record.RepositoryIndexRecord;
33 import org.apache.maven.repository.indexing.record.RepositoryIndexRecordFactory;
34 import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
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 LuceneStandardArtifactIndexTest
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, "standard" );
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.createStandardIndex( 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 assertJarRecord( artifact, document );
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 assertJarRecord( artifact, document );
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 assertJarRecord( artifact, document );
189 assertEquals( "Check index size", 1, reader.numDocs() );
197 public void testAddPomRecord()
198 throws IOException, RepositoryIndexException
202 Artifact artifact = createArtifact( "test-pom", "1.0", "pom" );
204 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
205 index.indexRecords( Collections.singletonList( record ) );
207 IndexReader reader = IndexReader.open( indexLocation );
210 Document document = reader.document( 0 );
211 assertPomRecord( artifact, document );
212 assertEquals( "Check index size", 1, reader.numDocs() );
220 public void testAddPlugin()
221 throws IOException, RepositoryIndexException, XmlPullParserException
225 Artifact artifact = createArtifact( "test-plugin" );
227 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
229 index.indexRecords( Collections.singletonList( record ) );
231 IndexReader reader = IndexReader.open( indexLocation );
234 Document document = reader.document( 0 );
235 assertPluginRecord( artifact, document );
236 assertEquals( "Check index size", 1, reader.numDocs() );
244 public void testDeleteRecordInIndex()
245 throws IOException, RepositoryIndexException
249 Artifact artifact = createArtifact( "test-jar" );
251 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
252 index.indexRecords( Collections.singletonList( record ) );
254 index.deleteRecords( Collections.singletonList( record ) );
256 IndexReader reader = IndexReader.open( indexLocation );
259 assertEquals( "No documents", 0, reader.numDocs() );
267 public void testDeleteRecordNotInIndex()
268 throws IOException, RepositoryIndexException
272 Artifact artifact = createArtifact( "test-jar" );
274 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
276 index.deleteRecords( Collections.singletonList( record ) );
278 IndexReader reader = IndexReader.open( indexLocation );
281 assertEquals( "No documents", 0, reader.numDocs() );
289 public void testDeleteRecordNoIndex()
290 throws IOException, RepositoryIndexException
292 Artifact artifact = createArtifact( "test-jar" );
294 RepositoryIndexRecord record = recordFactory.createRecord( artifact );
295 index.deleteRecords( Collections.singleton( record ) );
297 assertFalse( index.exists() );
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.repository.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( Artifact artifact, Document document, String expectedArtifactId, String expectedType,
334 String expectedMd5, String expectedSha1 )
336 assertEquals( "Check document filename", repository.pathOf( artifact ),
337 document.get( StandardIndexRecordFields.FILENAME ) );
338 assertEquals( "Check document groupId", "org.apache.maven.repository.record",
339 document.get( StandardIndexRecordFields.GROUPID ) );
340 assertEquals( "Check document artifactId", expectedArtifactId,
341 document.get( StandardIndexRecordFields.ARTIFACTID ) );
342 assertEquals( "Check document version", "1.0", document.get( StandardIndexRecordFields.VERSION ) );
343 assertEquals( "Check document type", expectedType, document.get( StandardIndexRecordFields.TYPE ) );
344 assertEquals( "Check document repository", "test", document.get( StandardIndexRecordFields.REPOSITORY ) );
345 assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ),
346 document.get( StandardIndexRecordFields.LAST_MODIFIED ) );
347 assertEquals( "Check document md5", expectedMd5, document.get( StandardIndexRecordFields.MD5 ) );
348 assertEquals( "Check document sha1", expectedSha1, document.get( StandardIndexRecordFields.SHA1 ) );
349 assertEquals( "Check document file size", artifact.getFile().length(),
350 NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
351 assertNull( "Check document classifier", document.get( StandardIndexRecordFields.CLASSIFIER ) );
354 private void assertPomRecord( Artifact artifact, Document document )
356 assertRecord( artifact, document, "test-pom", "pom", "32dbef7ff11eb933bd8b7e7bcab85406",
357 "c3b374e394607e1e705e71c227f62641e8621ebe" );
358 assertNull( "Check document classes", document.get( StandardIndexRecordFields.CLASSES ) );
359 assertNull( "Check document files", document.get( StandardIndexRecordFields.FILES ) );
360 assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
361 assertEquals( "Check document year", "2005", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
362 assertEquals( "Check document project name", "Maven Repository Manager Test POM",
363 document.get( StandardIndexRecordFields.PROJECT_NAME ) );
364 assertEquals( "Check document project description", "Description",
365 document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
366 assertEquals( "Check document packaging", "pom", document.get( StandardIndexRecordFields.PACKAGING ) );
369 private void assertJarRecord( Artifact artifact, Document document )
371 assertRecord( artifact, document, "test-jar", "jar", "3a0adc365f849366cd8b633cad155cb7",
372 "c66f18bf192cb613fc2febb4da541a34133eedc2" );
373 assertEquals( "Check document classes", "A\nb.B\nb.c.C", document.get( StandardIndexRecordFields.CLASSES ) );
374 assertEquals( "Check document files", "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class",
375 document.get( StandardIndexRecordFields.FILES ) );
376 assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
377 assertNull( "Check document projectName", document.get( StandardIndexRecordFields.PROJECT_NAME ) );
378 assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
379 assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
380 assertNull( "Check document packaging", document.get( StandardIndexRecordFields.PACKAGING ) );
383 private void assertPluginRecord( Artifact artifact, Document document )
385 assertRecord( artifact, document, "test-plugin", "maven-plugin", "06f6fe25e46c4d4fb5be4f56a9bab0ee",
386 "382c1ebfb5d0c7d6061c2f8569fb53f8fc00fec2" );
387 assertEquals( "Check document classes", "org.apache.maven.repository.record.MyMojo",
388 document.get( StandardIndexRecordFields.CLASSES ) );
389 assertEquals( "Check document files", "META-INF/MANIFEST.MF\n" + "META-INF/maven/plugin.xml\n" +
390 "org/apache/maven/repository/record/MyMojo.class\n" +
391 "META-INF/maven/org.apache.maven.repository.record/test-plugin/pom.xml\n" +
392 "META-INF/maven/org.apache.maven.repository.record/test-plugin/pom.properties",
393 document.get( StandardIndexRecordFields.FILES ) );
394 assertEquals( "Check document pluginPrefix", "test", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
395 assertEquals( "Check document packaging", "maven-plugin", document.get( StandardIndexRecordFields.PACKAGING ) );
396 assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
397 assertEquals( "Check document project name", "Maven Mojo Archetype",
398 document.get( StandardIndexRecordFields.PROJECT_NAME ) );
399 assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
402 private String getLastModified( File file )
404 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US );
405 dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
406 return dateFormat.format( new Date( file.lastModified() ) );