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 File 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 = getTestFile( "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 File notIndexDir = new File( "pom.xml" );
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 File notIndexDir = new File( "" );
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.deleteIfIndexed( 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 RepositoryIndexSearchLayer repoSearchLayer =
126 (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
128 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
131 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
132 List artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
133 assertEquals( 1, artifactList.size() );
134 for ( Iterator iter = artifactList.iterator(); iter.hasNext(); )
136 SearchResult result = (SearchResult) iter.next();
137 Artifact artifact = result.getArtifact();
138 assertEquals( "1.0", artifact.getVersion() );
142 qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
143 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
144 assertEquals( 2, artifactList.size() );
145 Iterator artifacts = artifactList.iterator();
146 if ( artifacts.hasNext() )
148 SearchResult result = (SearchResult) artifacts.next();
149 Artifact artifact = result.getArtifact();
150 assertEquals( "org.apache.maven", artifact.getGroupId() );
153 // search artifact id
154 qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
155 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
156 assertEquals( 1, artifactList.size() );
157 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
159 SearchResult result = (SearchResult) artifacts.next();
160 Artifact artifact = result.getArtifact();
161 assertEquals( "maven-artifact", artifact.getArtifactId() );
165 qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
166 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
167 assertEquals( 2, artifactList.size() );
168 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
170 SearchResult result = (SearchResult) artifacts.next();
171 Artifact artifact = result.getArtifact();
172 assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
176 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" );
177 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
178 assertEquals( 3, artifactList.size() );
179 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
181 SearchResult result = (SearchResult) artifacts.next();
182 Map map = result.getFieldMatches();
183 Set mapEntry = map.entrySet();
184 assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) );
189 new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" );
190 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
191 assertEquals( 2, artifactList.size() );
192 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
194 SearchResult result = (SearchResult) artifacts.next();
195 Map map = result.getFieldMatches();
196 List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS );
197 for ( Iterator it = matches.iterator(); it.hasNext(); )
199 assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() );
203 //search dependencies
204 qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
205 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
206 assertEquals( 2, artifactList.size() );
207 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
209 SearchResult result = (SearchResult) artifacts.next();
210 Map map = result.getFieldMatches();
211 boolean depFound = false;
212 List list = (List) map.get( RepositoryIndex.FLD_DEPENDENCIES );
213 Iterator dependencies = list.iterator();
214 while ( dependencies.hasNext() )
216 String dep = (String) dependencies.next();
217 if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) )
223 assertTrue( "Searched dependency not found.", depFound );
226 //search build plugin
228 new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" );
229 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
230 assertEquals( 1, artifactList.size() );
231 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
233 SearchResult result = (SearchResult) artifacts.next();
234 Map map = result.getFieldMatches();
235 List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD );
236 Iterator plugins = list.iterator();
237 boolean found = false;
238 while ( plugins.hasNext() )
240 String plugin = (String) plugins.next();
241 if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) )
247 assertTrue( "Searched plugin not found.", found );
250 //search reporting plugin
251 qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT,
252 "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );
253 artifactList = repoSearchLayer.searchAdvanced( qry, indexer );
254 assertEquals( 1, artifactList.size() );
255 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
257 SearchResult result = (SearchResult) artifacts.next();
258 Map map = result.getFieldMatches();
259 List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT );
260 Iterator plugins = list.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, indexer );
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, indexer );
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 RepositoryIndexSearchLayer repoSearchLayer =
318 (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE );
320 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
322 // Criteria 1: required query
323 // ex. artifactId=maven-artifact AND groupId=org.apache.maven
324 Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
325 Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
326 CompoundQuery rQry = new CompoundQuery();
330 List artifacts = repoSearchLayer.searchAdvanced( rQry, indexer );
331 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
333 SearchResult result = (SearchResult) iter.next();
334 Artifact artifact = result.getArtifact();
335 assertEquals( "maven-artifact", artifact.getArtifactId() );
336 assertEquals( "org.apache.maven", artifact.getGroupId() );
339 // Criteria 2: nested required query
340 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
342 Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
343 CompoundQuery oQry = new CompoundQuery();
347 artifacts = repoSearchLayer.searchAdvanced( oQry, indexer );
348 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
350 SearchResult result = (SearchResult) iter.next();
351 Artifact artifact = result.getArtifact();
352 assertEquals( "maven-artifact", artifact.getArtifactId() );
353 assertEquals( "org.apache.maven", artifact.getGroupId() );
356 // Criteria 3: nested required query
357 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
358 // (version=2.0.3 OR version=2.0.1)
359 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
360 Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
361 oQry = new CompoundQuery();
365 CompoundQuery oQry5 = new CompoundQuery();
367 new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
368 Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES,
369 "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
373 CompoundQuery rQry2 = new CompoundQuery();
378 artifacts = repoSearchLayer.searchAdvanced( rQry2, indexer );
379 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
381 SearchResult result = (SearchResult) iter.next();
382 Artifact artifact = result.getArtifact();
383 assertEquals( "maven-artifact", artifact.getArtifactId() );
384 assertEquals( "org.apache.maven", artifact.getGroupId() );
385 assertEquals( "2.0.1", artifact.getVersion() );
388 // Criteria 4: nested required query
389 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
390 // (version=2.0.3 OR version=2.0.1)
391 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
392 // OR [(artifactId=sample AND groupId=test)]
393 CompoundQuery rQry3 = new CompoundQuery();
394 Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
395 Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
398 CompoundQuery oQry2 = new CompoundQuery();
402 artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer );
403 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
405 SearchResult result = (SearchResult) iter.next();
406 Artifact artifact = result.getArtifact();
407 assertEquals( "maven-artifact", artifact.getArtifactId() );
408 assertEquals( "org.apache.maven", artifact.getGroupId() );
409 assertEquals( "2.0.1", artifact.getVersion() );
412 // Criteria 4: nested required query
413 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
414 // (version=2.0.3 OR version=2.0.1)
415 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
416 // [(artifactId=sample AND groupId=test)] OR
417 // [(artifactId=sample2 AND groupId=test)]
418 CompoundQuery rQry4 = new CompoundQuery();
419 Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
420 Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
425 artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer );
426 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
428 SearchResult result = (SearchResult) iter.next();
429 Artifact artifact = result.getArtifact();
430 assertEquals( "maven-artifact", artifact.getArtifactId() );
431 assertEquals( "org.apache.maven", artifact.getGroupId() );
438 * Create an index that will be used for testing.
439 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
440 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
444 private void createTestIndex()
447 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
448 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
450 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
451 indexer.indexPom( pom );
455 pom = getPom( "org.apache.maven", "maven-model", "2.0" );
456 indexer.indexPom( pom );
460 pom = getPom( "test", "test-artifactId", "1.0" );
461 indexer.indexPom( pom );
465 pom = getPom( "test", "test-artifactId", "1.0" );
466 indexer.indexPom( pom );
472 * Test delete of pom document from index.
476 public void testDeletePomDocument()
481 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
482 RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE );
484 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
486 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
487 indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
489 Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
490 List artifactList = repoSearcher.search( qry, indexer );
491 assertEquals( 0, artifactList.size() );
494 private Model getPom( String groupId, String artifactId, String version )
497 Artifact artifact = getArtifact( groupId, artifactId, version );
499 return getPom( artifact );
502 private Model getPom( Artifact artifact )
505 File pomFile = getPomFile( artifact );
507 MavenXpp3Reader pomReader = new MavenXpp3Reader();
508 return pomReader.read( new FileReader( pomFile ) );
511 private File getPomFile( Artifact artifact )
513 String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();
514 return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
517 private Artifact getArtifact( String groupId, String artifactId, String version )
520 if ( artifactFactory == null )
522 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
525 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
528 protected void tearDown()