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.deleteIfIndexed( 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 RepositoryIndexSearchLayer repoSearchLayer =
165 (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
167 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
170 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
171 List artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
172 assertEquals( 1, artifacts.size() );
173 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
175 SearchResult result = (SearchResult) iter.next();
176 Artifact artifact = result.getArtifact();
177 assertEquals( "1.0", artifact.getVersion() );
181 qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" );
182 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
183 assertEquals( 1, artifacts.size() );
184 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
186 SearchResult result = (SearchResult) iter.next();
187 Artifact artifact = result.getArtifact();
188 assertEquals( "test-artifactId", artifact.getArtifactId() );
192 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" );
193 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
194 assertEquals( 1, artifacts.size() );
195 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
197 SearchResult result = (SearchResult) iter.next();
198 Artifact artifact = result.getArtifact();
199 assertEquals( "test-artifactId", artifact.getArtifactId() );
203 qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" );
204 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
205 assertEquals( 3, artifacts.size() );
206 Iterator iter = artifacts.iterator();
207 if ( iter.hasNext() )
209 SearchResult result = (SearchResult) iter.next();
210 Artifact artifact = result.getArtifact();
211 assertEquals( "test-artifactId", artifact.getArtifactId() );
215 qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
216 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
217 assertEquals( 2, artifacts.size() );
218 iter = artifacts.iterator();
219 if ( iter.hasNext() )
221 SearchResult result = (SearchResult) iter.next();
222 Artifact artifact = result.getArtifact();
223 assertEquals( "org.apache.maven", artifact.getGroupId() );
226 // search artifact id
227 qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
228 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
229 assertEquals( 1, artifacts.size() );
230 for ( iter = artifacts.iterator(); iter.hasNext(); )
232 SearchResult result = (SearchResult) iter.next();
233 Artifact artifact = result.getArtifact();
234 assertEquals( "maven-artifact", artifact.getArtifactId() );
238 qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
239 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
240 assertEquals( 2, artifacts.size() );
241 for ( iter = artifacts.iterator(); iter.hasNext(); )
243 SearchResult result = (SearchResult) iter.next();
244 Artifact artifact = result.getArtifact();
245 assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
248 // search sha1 checksum
249 Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
250 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
252 String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
254 qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
255 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
256 assertEquals( 1, artifacts.size() );
257 for ( iter = artifacts.iterator(); iter.hasNext(); )
259 SearchResult result = (SearchResult) iter.next();
260 Artifact artifact2 = result.getArtifact();
261 String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 );
262 assertEquals( sha1, sha1Tmp );
265 // search md5 checksum
266 String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 );
267 qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
268 artifacts = repoSearchLayer.searchAdvanced( qry, indexer );
269 assertEquals( 1, artifacts.size() );
270 for ( iter = artifacts.iterator(); iter.hasNext(); )
272 SearchResult result = (SearchResult) iter.next();
273 Artifact artifact2 = result.getArtifact();
274 String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 );
275 assertEquals( md5, md5Tmp );
282 * Test the ArtifactRepositoryIndex using compound search (AND, OR).
286 public void testSearchCompound()
291 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
292 RepositoryIndexSearchLayer repoSearchLayer =
293 (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
294 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
296 // Criteria 1: required query
297 // ex. artifactId=maven-artifact AND groupId=org.apache.maven
298 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
299 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
300 CompoundQuery rQry = new CompoundQuery();
304 List artifacts = repoSearchLayer.searchAdvanced( rQry, indexer );
305 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
307 SearchResult result = (SearchResult) iter.next();
308 Artifact artifact = result.getArtifact();
309 assertEquals( "maven-artifact", artifact.getArtifactId() );
310 assertEquals( "org.apache.maven", artifact.getGroupId() );
313 // Criteria 2: nested required query
314 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
316 Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
317 CompoundQuery oQry = new CompoundQuery();
321 artifacts = repoSearchLayer.searchAdvanced( oQry, indexer );
322 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
324 SearchResult result = (SearchResult) iter.next();
325 Artifact artifact = result.getArtifact();
326 assertEquals( "maven-artifact", artifact.getArtifactId() );
327 assertEquals( "org.apache.maven", artifact.getGroupId() );
330 // Criteria 3: nested required query
331 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
332 // (version=2.0.3 OR version=2.0.1)
333 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
334 Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
335 oQry = new CompoundQuery();
339 CompoundQuery oQry5 = new CompoundQuery();
340 Query qry9 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact-2.0.1.jar" );
341 Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_NAME, "maven-artifact" );
345 CompoundQuery rQry2 = new CompoundQuery();
350 artifacts = repoSearchLayer.searchAdvanced( rQry2, indexer );
351 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
353 SearchResult result = (SearchResult) iter.next();
354 Artifact artifact = result.getArtifact();
355 assertEquals( "maven-artifact", artifact.getArtifactId() );
356 assertEquals( "org.apache.maven", artifact.getGroupId() );
357 assertEquals( "2.0.1", artifact.getVersion() );
360 // Criteria 4: nested required query
361 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
362 // (version=2.0.3 OR version=2.0.1)
363 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
364 // OR [(artifactId=sample AND groupId=test)]
365 CompoundQuery rQry3 = new CompoundQuery();
366 Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
367 Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
370 CompoundQuery oQry2 = new CompoundQuery();
374 artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer );
375 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
377 SearchResult result = (SearchResult) iter.next();
378 Artifact artifact = result.getArtifact();
379 assertEquals( "maven-artifact", artifact.getArtifactId() );
380 assertEquals( "org.apache.maven", artifact.getGroupId() );
381 assertEquals( "2.0.1", artifact.getVersion() );
384 // Criteria 4: nested required query
385 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
386 // (version=2.0.3 OR version=2.0.1)
387 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
388 // [(artifactId=sample AND groupId=test)] OR
389 // [(artifactId=sample2 AND groupId=test)]
390 CompoundQuery rQry4 = new CompoundQuery();
391 Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
392 Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
397 artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer );
398 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
400 SearchResult result = (SearchResult) iter.next();
401 Artifact artifact = result.getArtifact();
402 assertEquals( "maven-artifact", artifact.getArtifactId() );
403 assertEquals( "org.apache.maven", artifact.getGroupId() );
410 * Test the exceptions thrown by DefaultRepositoryIndexSearcher
414 public void testSearchExceptions()
419 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
420 RepositoryIndexSearchLayer repoSearchLayer =
421 (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
423 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
427 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "~~~~~" );
428 repoSearchLayer.searchAdvanced( qry, indexer );
429 fail( "Must throw an exception on unparseable query." );
431 catch ( RepositoryIndexSearchException re )
436 indexer = factory.createArtifactRepositoryIndex( "target/index/sample", repository );
440 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
441 repoSearchLayer.searchAdvanced( qry, indexer );
442 fail( "Must throw an exception on invalid index location." );
444 catch ( RepositoryIndexSearchException re )
452 * Test delete of document from the artifact index.
456 public void testDeleteArtifactDocument()
461 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
462 RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE );
464 ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
466 Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
467 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
468 indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
470 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() );
471 List artifacts = repoSearcher.search( qry, indexer );
472 assertEquals( 0, artifacts.size() );
476 * Method for creating artifact object
478 * @param groupId the groupId of the artifact to be created
479 * @param artifactId the artifactId of the artifact to be created
480 * @param version the version of the artifact to be created
481 * @return Artifact object
484 private Artifact getArtifact( String groupId, String artifactId, String version )
487 if ( artifactFactory == null )
489 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
492 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
495 protected void tearDown()