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.SinglePhraseQuery;
27 import org.apache.maven.repository.indexing.query.Query;
28 import org.apache.maven.repository.indexing.query.CompoundQuery;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.util.FileUtils;
33 import java.util.List;
34 import java.util.Iterator;
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 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
168 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
169 List artifacts = repoSearcher.search( qry );
170 assertEquals( 1, artifacts.size() );
171 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
173 Artifact artifact = (Artifact) iter.next();
174 assertEquals( "1.0", artifact.getVersion() );
178 qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" );
179 artifacts = repoSearcher.search( qry );
180 assertEquals( 1, artifacts.size() );
181 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
183 Artifact artifact = (Artifact) iter.next();
184 assertEquals( "test-artifactId", artifact.getArtifactId() );
188 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" );
189 artifacts = repoSearcher.search( qry );
190 assertEquals( 1, artifacts.size() );
191 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
193 Artifact artifact = (Artifact) iter.next();
194 assertEquals( "test-artifactId", artifact.getArtifactId() );
198 qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" );
199 artifacts = repoSearcher.search( qry );
200 assertEquals( 3, artifacts.size() );
201 Iterator iter = artifacts.iterator();
202 if ( iter.hasNext() )
204 Artifact artifact = (Artifact) iter.next();
205 assertEquals( "test-artifactId", artifact.getArtifactId() );
209 qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
210 artifacts = repoSearcher.search( qry );
211 assertEquals( 2, artifacts.size() );
212 iter = artifacts.iterator();
213 if ( iter.hasNext() )
215 Artifact artifact = (Artifact) iter.next();
216 assertEquals( "org.apache.maven", artifact.getGroupId() );
219 // search artifact id
220 qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
221 artifacts = repoSearcher.search( qry );
222 assertEquals( 1, artifacts.size() );
223 for ( iter = artifacts.iterator(); iter.hasNext(); )
225 Artifact artifact = (Artifact) iter.next();
226 assertEquals( "maven-artifact", artifact.getArtifactId() );
230 qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
231 artifacts = repoSearcher.search( qry );
232 assertEquals( 2, artifacts.size() );
233 for ( iter = artifacts.iterator(); iter.hasNext(); )
235 Artifact artifact = (Artifact) iter.next();
236 assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
239 // search sha1 checksum
240 Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
241 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
243 String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
245 qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
246 artifacts = repoSearcher.search( qry );
247 assertEquals( 1, artifacts.size() );
248 for ( iter = artifacts.iterator(); iter.hasNext(); )
250 Artifact artifact2 = (Artifact) iter.next();
251 String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 );
252 assertEquals( sha1, sha1Tmp );
255 // search md5 checksum
256 String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 );
257 qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
258 artifacts = repoSearcher.search( qry );
259 assertEquals( 1, artifacts.size() );
260 for ( iter = artifacts.iterator(); iter.hasNext(); )
262 Artifact artifact2 = (Artifact) iter.next();
263 String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 );
264 assertEquals( md5, md5Tmp );
271 * Test the ArtifactRepositoryIndex using compound search (AND, OR).
275 public void testSearchCompound()
280 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
281 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
282 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
284 // Criteria 1: required query
285 // ex. artifactId=maven-artifact AND groupId=org.apache.maven
286 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
287 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
288 CompoundQuery rQry = new CompoundQuery();
292 List artifacts = repoSearcher.search( rQry );
293 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
295 Artifact artifact = (Artifact) iter.next();
296 assertEquals( "maven-artifact", artifact.getArtifactId() );
297 assertEquals( "org.apache.maven", artifact.getGroupId() );
300 // Criteria 2: nested required query
301 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
303 Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
304 CompoundQuery oQry = new CompoundQuery();
308 artifacts = repoSearcher.search( oQry );
309 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
311 Artifact artifact = (Artifact) iter.next();
312 assertEquals( "maven-artifact", artifact.getArtifactId() );
313 assertEquals( "org.apache.maven", artifact.getGroupId() );
316 // Criteria 3: nested required query
317 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
318 // (version=2.0.3 OR version=2.0.1)
319 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
320 Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
321 oQry = new CompoundQuery();
325 CompoundQuery oQry5 = new CompoundQuery();
326 Query qry9 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" );
327 Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact" );
331 CompoundQuery rQry2 = new CompoundQuery();
336 artifacts = repoSearcher.search( rQry2 );
337 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
339 Artifact artifact = (Artifact) iter.next();
340 assertEquals( "maven-artifact", artifact.getArtifactId() );
341 assertEquals( "org.apache.maven", artifact.getGroupId() );
342 assertEquals( "2.0.1", artifact.getVersion() );
345 // Criteria 4: nested required query
346 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
347 // (version=2.0.3 OR version=2.0.1)
348 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
349 // OR [(artifactId=sample AND groupId=test)]
350 CompoundQuery rQry3 = new CompoundQuery();
351 Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
352 Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
355 CompoundQuery oQry2 = new CompoundQuery();
359 artifacts = repoSearcher.search( oQry2 );
360 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
362 Artifact artifact = (Artifact) iter.next();
363 assertEquals( "maven-artifact", artifact.getArtifactId() );
364 assertEquals( "org.apache.maven", artifact.getGroupId() );
365 assertEquals( "2.0.1", artifact.getVersion() );
368 // Criteria 4: nested required query
369 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
370 // (version=2.0.3 OR version=2.0.1)
371 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
372 // [(artifactId=sample AND groupId=test)] OR
373 // [(artifactId=sample2 AND groupId=test)]
374 CompoundQuery rQry4 = new CompoundQuery();
375 Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
376 Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
381 artifacts = repoSearcher.search( oQry2 );
382 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
384 Artifact artifact = (Artifact) iter.next();
385 assertEquals( "maven-artifact", artifact.getArtifactId() );
386 assertEquals( "org.apache.maven", artifact.getGroupId() );
393 * Test the exceptions thrown by DefaultRepositoryIndexSearcher
397 public void testSearchExceptions()
402 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
403 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
404 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
408 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "~~~~~" );
409 List artifacts = repoSearcher.search( qry );
410 fail( "Must throw an exception on unparseable query." );
412 catch ( RepositoryIndexSearchException re )
417 indexer = factory.createArtifactRepositoryIndex( "target/index/sample", repository );
418 repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
422 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
423 List artifacts = repoSearcher.search( qry );
424 fail( "Must throw an exception on invalid index location." );
426 catch ( RepositoryIndexSearchException re )
434 * Test delete of document from the artifact index.
438 public void testDeleteArtifactDocument()
443 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
444 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
446 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
447 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
448 indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
450 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
451 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
452 List artifacts = repoSearcher.search( qry );
453 assertEquals( artifacts.size(), 0 );
457 * Method for creating artifact object
459 * @param groupId the groupId of the artifact to be created
460 * @param artifactId the artifactId of the artifact to be created
461 * @param version the version of the artifact to be created
462 * @return Artifact object
465 private Artifact getArtifact( String groupId, String artifactId, String version )
468 if ( artifactFactory == null )
470 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
473 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
476 protected void tearDown()