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.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.apache.maven.repository.digest.DefaultDigester;
25 import org.apache.maven.repository.digest.Digester;
26 import org.apache.maven.repository.indexing.query.CompoundQuery;
27 import org.apache.maven.repository.indexing.query.Query;
28 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.util.FileUtils;
33 import java.util.Iterator;
34 import java.util.List;
37 * @author Edwin Punzalan
39 public class ArtifactRepositoryIndexingTest
40 extends PlexusTestCase
42 private ArtifactFactory artifactFactory;
44 private ArtifactRepository repository;
46 private String indexPath;
48 private Digester digester;
50 protected void setUp()
55 File repositoryDirectory = getTestFile( "src/test/repository" );
56 String repoDir = repositoryDirectory.toURL().toString();
57 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
58 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
59 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
60 digester = new DefaultDigester();
62 indexPath = "target/index";
63 FileUtils.deleteDirectory( indexPath );
67 * Method for testing the exceptions thrown by ArtifactRepositoryIndex
71 public void testIndexerExceptions()
74 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
75 Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
76 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
80 String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
81 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository );
82 indexer.indexArtifact( artifact );
83 fail( "Must throw exception on non-directory index directory" );
85 catch ( RepositoryIndexException e )
92 String notIndexDir = new File( "" ).getAbsolutePath();
93 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository );
94 indexer.indexArtifact( artifact );
95 fail( "Must throw an exception on a non-index directory" );
97 catch ( RepositoryIndexException e )
102 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
105 indexer.isIndexed( new Object() );
106 fail( "Must throw exception on object not of type artifact." );
108 catch ( RepositoryIndexException e )
115 * Create an index that will be used for testing.
116 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
117 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
121 private void createTestIndex()
124 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
125 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
127 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
128 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
129 indexer.indexArtifact( artifact );
133 artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
134 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
135 indexer.indexArtifact( artifact );
139 artifact = getArtifact( "test", "test-artifactId", "1.0" );
140 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
141 indexer.indexArtifact( artifact );
145 artifact = getArtifact( "test", "test-artifactId", "1.0" );
146 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
147 indexer.indexArtifact( artifact );
154 * Test the ArtifactRepositoryIndex using a single-phrase search.
158 public void testSearchSingle()
163 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
164 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
165 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
168 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
169 List artifacts = repoSearchLayer.searchAdvanced( qry );
170 assertEquals( 1, artifacts.size() );
171 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
173 SearchResult result = (SearchResult) iter.next();
174 Artifact artifact = result.getArtifact();
175 assertEquals( "1.0", artifact.getVersion() );
179 qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" );
180 artifacts = repoSearchLayer.searchAdvanced( qry );
181 assertEquals( 1, artifacts.size() );
182 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
184 SearchResult result = (SearchResult) iter.next();
185 Artifact artifact = result.getArtifact();
186 assertEquals( "test-artifactId", artifact.getArtifactId() );
190 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" );
191 artifacts = repoSearchLayer.searchAdvanced( qry );
192 assertEquals( 1, artifacts.size() );
193 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
195 SearchResult result = (SearchResult) iter.next();
196 Artifact artifact = result.getArtifact();
197 assertEquals( "test-artifactId", artifact.getArtifactId() );
201 qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" );
202 artifacts = repoSearchLayer.searchAdvanced( qry );
203 assertEquals( 3, artifacts.size() );
204 Iterator iter = artifacts.iterator();
205 if ( iter.hasNext() )
207 SearchResult result = (SearchResult) iter.next();
208 Artifact artifact = result.getArtifact();
209 assertEquals( "test-artifactId", artifact.getArtifactId() );
213 qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
214 artifacts = repoSearchLayer.searchAdvanced( qry );
215 assertEquals( 2, artifacts.size() );
216 iter = artifacts.iterator();
217 if ( iter.hasNext() )
219 SearchResult result = (SearchResult) iter.next();
220 Artifact artifact = result.getArtifact();
221 assertEquals( "org.apache.maven", artifact.getGroupId() );
224 // search artifact id
225 qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
226 artifacts = repoSearchLayer.searchAdvanced( qry );
227 assertEquals( 1, artifacts.size() );
228 for ( iter = artifacts.iterator(); iter.hasNext(); )
230 SearchResult result = (SearchResult) iter.next();
231 Artifact artifact = result.getArtifact();
232 assertEquals( "maven-artifact", artifact.getArtifactId() );
236 qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
237 artifacts = repoSearchLayer.searchAdvanced( qry );
238 assertEquals( 2, artifacts.size() );
239 for ( iter = artifacts.iterator(); iter.hasNext(); )
241 SearchResult result = (SearchResult) iter.next();
242 Artifact artifact = result.getArtifact();
243 assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
246 // search sha1 checksum
247 Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
248 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
250 String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
252 qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
253 artifacts = repoSearchLayer.searchAdvanced( qry );
254 assertEquals( 1, artifacts.size() );
255 for ( iter = artifacts.iterator(); iter.hasNext(); )
257 SearchResult result = (SearchResult) iter.next();
258 Artifact artifact2 = result.getArtifact();
259 String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 );
260 assertEquals( sha1, sha1Tmp );
263 // search md5 checksum
264 String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 );
265 qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
266 artifacts = repoSearchLayer.searchAdvanced( qry );
267 assertEquals( 1, artifacts.size() );
268 for ( iter = artifacts.iterator(); iter.hasNext(); )
270 SearchResult result = (SearchResult) iter.next();
271 Artifact artifact2 = result.getArtifact();
272 String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 );
273 assertEquals( md5, md5Tmp );
280 * Test the ArtifactRepositoryIndex using compound search (AND, OR).
284 public void testSearchCompound()
289 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
290 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
291 //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
292 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
294 // Criteria 1: required query
295 // ex. artifactId=maven-artifact AND groupId=org.apache.maven
296 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
297 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
298 CompoundQuery rQry = new CompoundQuery();
302 List artifacts = repoSearchLayer.searchAdvanced( rQry );
303 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
305 SearchResult result = (SearchResult) iter.next();
306 Artifact artifact = result.getArtifact();
307 assertEquals( "maven-artifact", artifact.getArtifactId() );
308 assertEquals( "org.apache.maven", artifact.getGroupId() );
311 // Criteria 2: nested required query
312 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
314 Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
315 CompoundQuery oQry = new CompoundQuery();
319 artifacts = repoSearchLayer.searchAdvanced( oQry );
320 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
322 SearchResult result = (SearchResult) iter.next();
323 Artifact artifact = result.getArtifact();
324 assertEquals( "maven-artifact", artifact.getArtifactId() );
325 assertEquals( "org.apache.maven", artifact.getGroupId() );
328 // Criteria 3: nested required query
329 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
330 // (version=2.0.3 OR version=2.0.1)
331 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
332 Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
333 oQry = new CompoundQuery();
337 CompoundQuery oQry5 = new CompoundQuery();
338 Query qry9 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" );
339 Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact" );
343 CompoundQuery rQry2 = new CompoundQuery();
348 artifacts = repoSearchLayer.searchAdvanced( rQry2 );
349 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
351 SearchResult result = (SearchResult) iter.next();
352 Artifact artifact = result.getArtifact();
353 assertEquals( "maven-artifact", artifact.getArtifactId() );
354 assertEquals( "org.apache.maven", artifact.getGroupId() );
355 assertEquals( "2.0.1", artifact.getVersion() );
358 // Criteria 4: nested required query
359 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
360 // (version=2.0.3 OR version=2.0.1)
361 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
362 // OR [(artifactId=sample AND groupId=test)]
363 CompoundQuery rQry3 = new CompoundQuery();
364 Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
365 Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
368 CompoundQuery oQry2 = new CompoundQuery();
372 artifacts = repoSearchLayer.searchAdvanced( oQry2 );
373 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
375 SearchResult result = (SearchResult) iter.next();
376 Artifact artifact = result.getArtifact();
377 assertEquals( "maven-artifact", artifact.getArtifactId() );
378 assertEquals( "org.apache.maven", artifact.getGroupId() );
379 assertEquals( "2.0.1", artifact.getVersion() );
382 // Criteria 4: nested required query
383 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
384 // (version=2.0.3 OR version=2.0.1)
385 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
386 // [(artifactId=sample AND groupId=test)] OR
387 // [(artifactId=sample2 AND groupId=test)]
388 CompoundQuery rQry4 = new CompoundQuery();
389 Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
390 Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
395 artifacts = repoSearchLayer.searchAdvanced( oQry2 );
396 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
398 SearchResult result = (SearchResult) iter.next();
399 Artifact artifact = result.getArtifact();
400 assertEquals( "maven-artifact", artifact.getArtifactId() );
401 assertEquals( "org.apache.maven", artifact.getGroupId() );
408 * Test the exceptions thrown by DefaultRepositoryIndexSearcher
412 public void testSearchExceptions()
417 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
418 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
419 // RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
420 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
424 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "~~~~~" );
425 List artifacts = repoSearchLayer.searchAdvanced( qry );
426 fail( "Must throw an exception on unparseable query." );
428 catch ( RepositoryIndexSearchException re )
433 indexer = factory.createArtifactRepositoryIndex( "target/index/sample", repository );
434 repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
438 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
439 List artifacts = repoSearchLayer.searchAdvanced( qry );
440 fail( "Must throw an exception on invalid index location." );
442 catch ( RepositoryIndexSearchException re )
450 * Test delete of document from the artifact index.
454 public void testDeleteArtifactDocument()
459 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
460 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
462 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
463 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
464 indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
466 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
467 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
468 List artifacts = repoSearcher.search( qry );
469 assertEquals( artifacts.size(), 0 );
473 * Method for creating artifact object
475 * @param groupId the groupId of the artifact to be created
476 * @param artifactId the artifactId of the artifact to be created
477 * @param version the version of the artifact to be created
478 * @return Artifact object
481 private Artifact getArtifact( String groupId, String artifactId, String version )
484 if ( artifactFactory == null )
486 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
489 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
492 protected void tearDown()