1 package org.apache.maven.repository.indexing;
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.DateField;
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.queryParser.QueryParser;
22 import org.apache.lucene.search.Hits;
23 import org.apache.lucene.search.IndexSearcher;
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.digest.DefaultDigester;
30 import org.apache.maven.repository.digest.Digester;
31 import org.codehaus.plexus.PlexusTestCase;
32 import org.codehaus.plexus.util.FileUtils;
37 * @author Edwin Punzalan
39 public class EclipseRepositoryIndexTest
40 extends PlexusTestCase
42 private ArtifactFactory artifactFactory;
44 private ArtifactRepository repository;
46 private File indexPath;
48 private Digester digester;
50 private long artifactFileTime;
52 private static final long TIME_DIFFERENCE = 10000L;
54 protected void setUp()
59 File repositoryDirectory = getTestFile( "src/test/repository" );
60 String repoDir = repositoryDirectory.toURL().toString();
61 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
62 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
63 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
64 digester = new DefaultDigester();
66 indexPath = getTestFile( "target/index" );
67 FileUtils.deleteDirectory( indexPath );
71 * Create an index that will be used for testing.
72 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
73 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
77 private EclipseRepositoryIndex createTestIndex()
80 EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( indexPath, repository, new DefaultDigester() );
82 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
83 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
84 artifactFileTime = artifact.getFile().lastModified();
85 indexer.indexArtifact( artifact );
89 long historicTime = artifactFileTime - TIME_DIFFERENCE;
91 artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
92 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
93 artifact.getFile().setLastModified( historicTime );
94 indexer.indexArtifact( artifact );
98 artifact = getArtifact( "test", "test-artifactId", "1.0" );
99 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
100 artifact.getFile().setLastModified( historicTime );
101 indexer.indexArtifact( artifact );
105 artifact = getArtifact( "test", "test-artifactId", "1.0" );
106 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
107 artifact.getFile().setLastModified( historicTime );
108 indexer.indexArtifact( artifact );
116 * Method for testing the exceptions thrown by ArtifactRepositoryIndex
120 public void testIndexerExceptions()
123 Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
124 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
128 File notIndexDir = new File( "pom.xml" );
129 EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( notIndexDir, repository, digester );
130 indexer.indexArtifact( artifact );
131 fail( "Must throw exception on non-directory index directory" );
133 catch ( RepositoryIndexException e )
140 File notIndexDir = new File( "" );
141 EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( notIndexDir, repository, digester );
142 indexer.indexArtifact( artifact );
143 fail( "Must throw an exception on a non-index directory" );
145 catch ( RepositoryIndexException e )
150 EclipseRepositoryIndex indexer = new EclipseRepositoryIndex( indexPath, repository, digester );
153 indexer.deleteIfIndexed( new Object() );
154 fail( "Must throw exception on object not of type artifact." );
156 catch ( RepositoryIndexException e )
163 * Test the ArtifactRepositoryIndex using a single-phrase search.
167 public void testSearch()
170 EclipseRepositoryIndex index = createTestIndex();
172 IndexSearcher searcher = new IndexSearcher( index.getIndexPath().getAbsolutePath() );
175 QueryParser parser = new QueryParser( "j", index.getAnalyzer() );
176 Hits hits = searcher.search( parser.parse( "maven-artifact-2.0.1.jar" ) );
178 assertEquals( "Total hits", 1, hits.length() );
179 Document doc = hits.doc( 0 );
180 assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) );
182 parser = new QueryParser( "s", index.getAnalyzer() );
183 hits = searcher.search( parser.parse( "78377" ) );
185 assertEquals( "Total hits", 1, hits.length() );
187 assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) );
189 parser = new QueryParser( "d", index.getAnalyzer() );
190 hits = searcher.search( parser.parse( DateField.timeToString( artifactFileTime ) ) );
192 assertEquals( "Total hits", 1, hits.length() );
194 assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) );
196 parser = new QueryParser( "m", index.getAnalyzer() );
197 hits = searcher.search( parser.parse( "AE55D9B5720E11B6CF19FE1E31A42E51" ) );
199 assertEquals( "Total hits", 1, hits.length() );
201 assertEquals( "Check jar name", "maven-artifact-2.0.1.jar", doc.get( "j" ) );
203 parser = new QueryParser( "c", index.getAnalyzer() );
204 hits = searcher.search( parser.parse( "MavenXpp3Reader" ) );
206 assertEquals( "Total hits", 1, hits.length() );
208 assertEquals( "Check jar name", "maven-model-2.0.jar", doc.get( "j" ) );
210 parser = new QueryParser( "j", index.getAnalyzer() );
211 hits = searcher.search( parser.parse( "maven" ) );
213 assertEquals( "Total hits", 2, hits.length() );
222 * Method for creating artifact object
224 * @param groupId the groupId of the artifact to be created
225 * @param artifactId the artifactId of the artifact to be created
226 * @param version the version of the artifact to be created
227 * @return Artifact object
230 private Artifact getArtifact( String groupId, String artifactId, String version )
233 if ( artifactFactory == null )
235 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
238 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
241 protected void tearDown()