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.License;
26 import org.apache.maven.model.Dependency;
27 import org.apache.maven.model.Plugin;
28 import org.apache.maven.model.ReportPlugin;
29 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
30 import org.apache.maven.repository.digest.DefaultDigester;
31 import org.apache.maven.repository.digest.Digester;
32 import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
33 import org.apache.maven.repository.indexing.query.Query;
34 import org.apache.maven.repository.indexing.query.CompoundQuery;
35 import org.codehaus.plexus.PlexusTestCase;
36 import org.codehaus.plexus.util.FileUtils;
39 import java.io.FileReader;
40 import java.util.List;
41 import java.util.Iterator;
44 * @author Edwin Punzalan
46 public class PomRepositoryIndexingTest
47 extends PlexusTestCase
49 private ArtifactRepository repository;
51 private ArtifactFactory artifactFactory;
53 private String indexPath;
55 private Digester digester;
57 protected void setUp()
62 File repositoryDirectory = getTestFile( "src/test/repository" );
63 String repoDir = repositoryDirectory.toURL().toString();
64 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
65 ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
66 repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
67 digester = new DefaultDigester();
69 indexPath = "target/index";
70 FileUtils.deleteDirectory( indexPath );
74 public void testIndexerExceptions()
77 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
78 Model pom = getPom( "test", "test-artifactId", "1.0" );
82 String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
83 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository );
84 indexer.indexPom( pom );
85 fail( "Must throw exception on non-directory index directory" );
87 catch ( RepositoryIndexException e )
94 String notIndexDir = new File( "" ).getAbsolutePath();
95 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository );
96 indexer.indexPom( pom );
97 fail( "Must throw an exception on a non-index directory" );
99 catch ( RepositoryIndexException e )
104 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
107 indexer.isIndexed( new Object() );
108 fail( "Must throw exception when the passed object is not of type model." );
110 catch ( RepositoryIndexException e )
117 * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using a single-phrase search.
121 public void testSearchSingle()
126 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
127 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
128 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
131 Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "1.0" );
132 List artifactList = repoSearcher.search( qry );
133 //assertEquals( 1, artifactList.size() );
134 for ( Iterator iter = artifactList.iterator(); iter.hasNext(); )
136 Artifact artifact = (Artifact) iter.next();
137 assertEquals( "1.0", artifact.getVersion() );
141 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
142 artifactList = repoSearcher.search( qry );
143 assertEquals( 2, artifactList.size() );
144 Iterator artifacts = artifactList.iterator();
145 if ( artifacts.hasNext() )
147 Artifact artifact = (Artifact) artifacts.next();
148 assertEquals( "org.apache.maven", artifact.getGroupId() );
151 // search artifact id
152 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
153 artifactList = repoSearcher.search( qry );
154 assertEquals( 1, artifactList.size() );
155 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
157 Artifact artifact = (Artifact) artifacts.next();
158 assertEquals( "maven-artifact", artifact.getArtifactId() );
162 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2" );
163 artifactList = repoSearcher.search( qry );
164 assertEquals( 2, artifactList.size() );
165 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
167 Artifact artifact = (Artifact) artifacts.next();
168 assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
172 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PACKAGING, "jar" );
173 artifactList = repoSearcher.search( qry );
174 assertEquals( 3, artifactList.size() );
175 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
177 Artifact artifact = (Artifact) artifacts.next();
178 assertEquals( "jar", artifact.getType() );
182 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_LICENSE_URLS,
183 "http://www.apache.org/licenses/LICENSE-2.0.txt" );
184 artifactList = repoSearcher.search( qry );
185 assertEquals( 2, artifactList.size() );
186 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
188 Artifact artifact = (Artifact) artifacts.next();
189 License license = (License) getPom( artifact ).getLicenses().get( 0 );
190 assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", license.getUrl() );
193 //search dependencies
194 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
195 artifactList = repoSearcher.search( qry );
196 assertEquals( 2, artifactList.size() );
197 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
199 Artifact artifact = (Artifact) artifacts.next();
200 Iterator dependencies = getPom( artifact ).getDependencies().iterator();
201 boolean depFound = false;
202 while ( dependencies.hasNext() )
204 Dependency dep = (Dependency) dependencies.next();
205 if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals(
206 dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion() ) )
212 assertTrue( "Searched dependency not found.", depFound );
215 //search build plugin
216 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_BUILD,
217 "org.codehaus.modello:modello-maven-plugin:2.0" );
218 artifactList = repoSearcher.search( qry );
219 assertEquals( 1, artifactList.size() );
220 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
222 Artifact artifact = (Artifact) artifacts.next();
223 Iterator plugins = getPom( artifact ).getBuild().getPlugins().iterator();
224 boolean found = false;
225 while ( plugins.hasNext() )
227 Plugin plugin = (Plugin) plugins.next();
228 if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals(
229 plugin.getKey() + ":" + plugin.getVersion() ) )
235 assertTrue( "Searched plugin not found.", found );
238 //search reporting plugin
239 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_REPORT,
240 "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );
241 artifactList = repoSearcher.search( qry );
242 assertEquals( 1, artifactList.size() );
243 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
245 Artifact artifact = (Artifact) artifacts.next();
246 Iterator plugins = getPom( artifact ).getReporting().getPlugins().iterator();
247 boolean found = false;
248 while ( plugins.hasNext() )
250 ReportPlugin plugin = (ReportPlugin) plugins.next();
251 if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals(
252 plugin.getKey() + ":" + plugin.getVersion() ) )
258 assertTrue( "Searched report plugin not found.", found );
261 // search sha1 checksum
262 Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
263 artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
264 String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
266 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_SHA1, sha1.trim() );
267 artifactList = repoSearcher.search( qry );
268 assertEquals( 1, artifactList.size() );
269 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
271 Artifact artifact2 = (Artifact) artifacts.next();
272 String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 );
273 assertEquals( sha1, sha1Tmp );
276 // search md5 checksum
277 String md5 = digester.createChecksum( getPomFile( artifact ), Digester.MD5 );
278 qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_MD5, md5.trim() );
279 artifactList = repoSearcher.search( qry );
280 assertEquals( 1, artifactList.size() );
281 for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
283 Artifact artifact2 = (Artifact) artifacts.next();
284 String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 );
285 assertEquals( md5, md5Tmp );
292 * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using compound search (AND, OR).
296 public void testSearchCompound()
301 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
302 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
303 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
305 // Criteria 1: required query
306 // ex. artifactId=maven-artifact AND groupId=org.apache.maven
307 Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
308 Query qry2 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
309 CompoundQuery rQry = new CompoundQuery();
313 List artifacts = repoSearcher.search( rQry );
314 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
316 Artifact artifact = (Artifact) iter.next();
317 assertEquals( "maven-artifact", artifact.getArtifactId() );
318 assertEquals( "org.apache.maven", artifact.getGroupId() );
321 // Criteria 2: nested required query
322 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
324 Query qry3 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.3" );
325 CompoundQuery oQry = new CompoundQuery();
329 artifacts = repoSearcher.search( oQry );
330 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
332 Artifact artifact = (Artifact) iter.next();
333 assertEquals( "maven-artifact", artifact.getArtifactId() );
334 assertEquals( "org.apache.maven", artifact.getGroupId() );
337 // Criteria 3: nested required query
338 // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
339 // (version=2.0.3 OR version=2.0.1)
340 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
341 Query qry4 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.1" );
342 oQry = new CompoundQuery();
346 CompoundQuery oQry5 = new CompoundQuery();
348 new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
349 Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES,
350 "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
354 CompoundQuery rQry2 = new CompoundQuery();
359 artifacts = repoSearcher.search( rQry2 );
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)]
372 // OR [(artifactId=sample AND groupId=test)]
373 CompoundQuery rQry3 = new CompoundQuery();
374 Query qry5 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample" );
375 Query qry6 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" );
378 CompoundQuery oQry2 = new CompoundQuery();
382 artifacts = repoSearcher.search( oQry2 );
383 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
385 Artifact artifact = (Artifact) iter.next();
386 assertEquals( "maven-artifact", artifact.getArtifactId() );
387 assertEquals( "org.apache.maven", artifact.getGroupId() );
388 assertEquals( "2.0.1", artifact.getVersion() );
391 // Criteria 4: nested required query
392 // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND
393 // (version=2.0.3 OR version=2.0.1)
394 // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR
395 // [(artifactId=sample AND groupId=test)] OR
396 // [(artifactId=sample2 AND groupId=test)]
397 CompoundQuery rQry4 = new CompoundQuery();
398 Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" );
399 Query qry8 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" );
404 artifacts = repoSearcher.search( oQry2 );
405 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
407 Artifact artifact = (Artifact) iter.next();
408 assertEquals( "maven-artifact", artifact.getArtifactId() );
409 assertEquals( "org.apache.maven", artifact.getGroupId() );
416 * Create an index that will be used for testing.
417 * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ],
418 * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ].
422 private void createTestIndex()
425 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
426 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
428 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
429 indexer.indexPom( pom );
433 pom = getPom( "org.apache.maven", "maven-model", "2.0" );
434 indexer.indexPom( pom );
438 pom = getPom( "test", "test-artifactId", "1.0" );
439 indexer.indexPom( pom );
443 pom = getPom( "test", "test-artifactId", "1.0" );
444 indexer.indexPom( pom );
450 * Test delete of pom document from index.
454 public void testDeletePomDocument()
459 RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
460 PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
461 Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
462 indexer.deleteDocument( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM + pom.getId() );
464 RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
465 Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM + pom.getId() );
466 List artifactList = repoSearcher.search( qry );
467 assertEquals( artifactList.size(), 0 );
470 private Model getPom( String groupId, String artifactId, String version )
473 Artifact artifact = getArtifact( groupId, artifactId, version );
475 return getPom( artifact );
478 private Model getPom( Artifact artifact )
481 File pomFile = getPomFile( artifact );
483 MavenXpp3Reader pomReader = new MavenXpp3Reader();
484 return pomReader.read( new FileReader( pomFile ) );
487 private File getPomFile( Artifact artifact )
489 String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();
490 return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );
493 private Artifact getArtifact( String groupId, String artifactId, String version )
496 if ( artifactFactory == null )
498 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
501 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" );
504 protected void tearDown()