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.model.Model;
25 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
26 import org.apache.maven.repository.digest.DefaultDigester;
27 import org.apache.maven.repository.digest.Digester;
28 import org.apache.maven.repository.indexing.query.CompoundQuery;
29 import org.apache.maven.repository.indexing.query.Query;
30 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
31 import org.codehaus.plexus.PlexusTestCase;
32 import org.codehaus.plexus.util.FileUtils;
35 import java.io.FileReader;
36 import java.util.Iterator;
37 import java.util.List;
42 * @author Edwin Punzalan
44 public class PomRepositoryIndexingTest
45 extends PlexusTestCase
47 private ArtifactRepository repository;
49 private ArtifactFactory artifactFactory;
51 private String indexPath;
53 private Digester digester;
55 protected void setUp()
60 File repositoryDirectory = getTestFile( "src/test/repository" );
61 String repoDir = repositoryDirectory.toURL().toString();
62 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
63 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
64 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
65 digester = new DefaultDigester();
67 indexPath = "target/index";
68 FileUtils.deleteDirectory( indexPath );
72 public void testIndexerExceptions()
75 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
76 Model pom = getPom( "test", "test-artifactId", "1.0" );
80 String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
81 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository );
82 indexer.indexPom( pom );
83 fail( "Must throw exception on non-directory index directory" );
85 catch ( RepositoryIndexException e )
92 String notIndexDir = new File( "" ).getAbsolutePath();
93 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository );
94 indexer.indexPom( pom );
95 fail( "Must throw an exception on a non-index directory" );
97 catch ( RepositoryIndexException e )
102 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
105 indexer.isIndexed( new Object() );
106 fail( "Must throw exception when the passed object is not of type model." );
108 catch ( RepositoryIndexException e )
115 * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using a single-phrase search.
119 public void testSearchSingle()
124 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
125 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
126 //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
127 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
130 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
131 List artifactList = repoSearchLayer.searchAdvanced( qry );
132 assertEquals( 1, artifactList.size() );
133 for ( Iterator iter = artifactList.iterator(); iter.hasNext(); )
135 SearchResult result = (SearchResult) iter.next();
136 Artifact artifact = result.getArtifact();
137 assertEquals( "1.0", artifact.getVersion() );
141 qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
142 artifactList = repoSearchLayer.searchAdvanced( qry );
143 assertEquals( 2, artifactList.size() );
144 Iterator artifacts = artifactList.iterator();
145 if ( artifacts.hasNext() )
147 SearchResult result = (SearchResult) artifacts.next();
148 Artifact artifact = result.getArtifact();
149 assertEquals( "org.apache.maven", artifact.getGroupId() );
152 // search artifact id
153 qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
154 artifactList = repoSearchLayer.searchAdvanced( qry );
155 assertEquals( 1, artifactList.size() );
156 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
158 SearchResult result = (SearchResult) artifacts.next();
159 Artifact artifact = result.getArtifact();
160 assertEquals( "maven-artifact", artifact.getArtifactId() );
164 qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
165 artifactList = repoSearchLayer.searchAdvanced( qry );
166 assertEquals( 2, artifactList.size() );
167 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
169 SearchResult result = (SearchResult) artifacts.next();
170 Artifact artifact = result.getArtifact();
171 assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
175 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" );
176 artifactList = repoSearchLayer.searchAdvanced( qry );
177 assertEquals( 3, artifactList.size() );
178 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
180 SearchResult result = (SearchResult) artifacts.next();
181 Map map = result.getFieldMatches();
182 Set mapEntry = map.entrySet();
183 for ( Iterator it = mapEntry.iterator(); it.hasNext(); )
185 Map.Entry entry = (Map.Entry) it.next();
187 assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) );
192 new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" );
193 artifactList = repoSearchLayer.searchAdvanced( qry );
194 assertEquals( 2, artifactList.size() );
195 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
197 SearchResult result = (SearchResult) artifacts.next();
198 Map map = result.getFieldMatches();
199 List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS );
200 for ( Iterator it = matches.iterator(); it.hasNext(); )
202 assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() );
206 //search dependencies
207 qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
208 artifactList = repoSearchLayer.searchAdvanced( qry );
209 assertEquals( 2, artifactList.size() );
210 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
212 SearchResult result = (SearchResult) artifacts.next();
213 Map map = result.getFieldMatches();
214 boolean depFound = false;
215 Iterator dependencies = ( (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ) ).iterator();
216 while ( dependencies.hasNext() )
218 String dep = (String) dependencies.next();
219 if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) )
225 assertTrue( "Searched dependency not found.", depFound );
228 //search build plugin
230 new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" );
231 artifactList = repoSearchLayer.searchAdvanced( qry );
232 assertEquals( 1, artifactList.size() );
233 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
235 SearchResult result = (SearchResult) artifacts.next();
236 Map map = result.getFieldMatches();
237 Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ) ).iterator();
238 boolean found = false;
239 while ( plugins.hasNext() )
241 String plugin = (String) plugins.next();
242 if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) )
248 assertTrue( "Searched plugin not found.", found );
251 //search reporting plugin
252 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT,
253 "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );
254 artifactList = repoSearchLayer.searchAdvanced( qry );
255 assertEquals( 1, artifactList.size() );
256 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
258 SearchResult result = (SearchResult) artifacts.next();
259 Map map = result.getFieldMatches();
260 Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ) ).iterator();
261 boolean found = false;
262 while ( plugins.hasNext() )
264 String plugin = (String) plugins.next();
265 if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) )
271 assertTrue( "Searched report plugin not found.", found );
274 // search sha1 checksum
275 Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
276 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
277 String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
279 qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
280 artifactList = repoSearchLayer.searchAdvanced( qry );
281 assertEquals( 1, artifactList.size() );
282 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
284 SearchResult result = (SearchResult) artifacts.next();
285 Artifact artifact2 = result.getArtifact();
286 String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 );
287 assertEquals( sha1, sha1Tmp );
290 // search md5 checksum
291 String md5 = digester.createChecksum( getPomFile( artifact ), Digester.MD5 );
292 qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
293 artifactList = repoSearchLayer.searchAdvanced( qry );
294 assertEquals( 1, artifactList.size() );
295 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
297 SearchResult result = (SearchResult) artifacts.next();
298 Artifact artifact2 = result.getArtifact();
299 String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 );
300 assertEquals( md5, md5Tmp );
307 * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using compound search (AND, OR).
311 public void testSearchCompound()
316 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
317 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
318 //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
319 RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
321 // Criteria 1: required query
322 // ex. artifactId=maven-artifact AND groupId=org.apache.maven
323 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
324 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
325 CompoundQuery rQry = new CompoundQuery();
329 List artifacts = repoSearchLayer.searchAdvanced( rQry );
330 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
332 SearchResult result = (SearchResult) iter.next();
333 Artifact artifact = result.getArtifact();
334 assertEquals( "maven-artifact", artifact.getArtifactId() );
335 assertEquals( "org.apache.maven", artifact.getGroupId() );
338 // Criteria 2: nested required query
339 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
341 Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
342 CompoundQuery oQry = new CompoundQuery();
346 artifacts = repoSearchLayer.searchAdvanced( oQry );
347 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
349 SearchResult result = (SearchResult) iter.next();
350 Artifact artifact = result.getArtifact();
351 assertEquals( "maven-artifact", artifact.getArtifactId() );
352 assertEquals( "org.apache.maven", artifact.getGroupId() );
355 // Criteria 3: nested required query
356 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
357 // (version=2.0.3 OR version=2.0.1)
358 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
359 Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
360 oQry = new CompoundQuery();
364 CompoundQuery oQry5 = new CompoundQuery();
366 new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
367 Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES,
368 "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
372 CompoundQuery rQry2 = new CompoundQuery();
377 artifacts = repoSearchLayer.searchAdvanced( rQry2 );
378 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
380 SearchResult result = (SearchResult) iter.next();
381 Artifact artifact = result.getArtifact();
382 assertEquals( "maven-artifact", artifact.getArtifactId() );
383 assertEquals( "org.apache.maven", artifact.getGroupId() );
384 assertEquals( "2.0.1", artifact.getVersion() );
387 // Criteria 4: nested required query
388 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
389 // (version=2.0.3 OR version=2.0.1)
390 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
391 // OR [(artifactId=sample AND groupId=test)]
392 CompoundQuery rQry3 = new CompoundQuery();
393 Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
394 Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
397 CompoundQuery oQry2 = new CompoundQuery();
401 artifacts = repoSearchLayer.searchAdvanced( oQry2 );
402 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
404 SearchResult result = (SearchResult) iter.next();
405 Artifact artifact = result.getArtifact();
406 assertEquals( "maven-artifact", artifact.getArtifactId() );
407 assertEquals( "org.apache.maven", artifact.getGroupId() );
408 assertEquals( "2.0.1", artifact.getVersion() );
411 // Criteria 4: nested required query
412 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
413 // (version=2.0.3 OR version=2.0.1)
414 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
415 // [(artifactId=sample AND groupId=test)] OR
416 // [(artifactId=sample2 AND groupId=test)]
417 CompoundQuery rQry4 = new CompoundQuery();
418 Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
419 Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
424 artifacts = repoSearchLayer.searchAdvanced( oQry2 );
425 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
427 SearchResult result = (SearchResult) iter.next();
428 Artifact artifact = result.getArtifact();
429 assertEquals( "maven-artifact", artifact.getArtifactId() );
430 assertEquals( "org.apache.maven", artifact.getGroupId() );
437 * Create an index that will be used for testing.
438 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
439 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
443 private void createTestIndex()
446 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
447 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
449 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
450 indexer.indexPom( pom );
454 pom = getPom( "org.apache.maven", "maven-model", "2.0" );
455 indexer.indexPom( pom );
459 pom = getPom( "test", "test-artifactId", "1.0" );
460 indexer.indexPom( pom );
464 pom = getPom( "test", "test-artifactId", "1.0" );
465 indexer.indexPom( pom );
471 * Test delete of pom document from index.
475 public void testDeletePomDocument()
480 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
481 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
482 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
483 indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
485 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
486 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
487 List artifactList = repoSearcher.search( qry );
488 assertEquals( artifactList.size(), 0 );
491 private Model getPom( String groupId, String artifactId, String version )
494 Artifact artifact = getArtifact( groupId, artifactId, version );
496 return getPom( artifact );
499 private Model getPom( Artifact artifact )
502 File pomFile = getPomFile( artifact );
504 MavenXpp3Reader pomReader = new MavenXpp3Reader();
505 return pomReader.read( new FileReader( pomFile ) );
508 private File getPomFile( Artifact artifact )
510 String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();
511 return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
514 private Artifact getArtifact( String groupId, String artifactId, String version )
517 if ( artifactFactory == null )
519 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
522 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
525 protected void tearDown()