diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java index 7419ec0d3..8fd396f8e 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java @@ -40,9 +40,9 @@ public abstract class AbstractRepositoryIndex private IndexWriter indexWriter; - private ArtifactRepository repository; + protected ArtifactRepository repository; - public AbstractRepositoryIndex( ArtifactRepository repository, String indexPath ) + public AbstractRepositoryIndex( String indexPath, ArtifactRepository repository ) throws RepositoryIndexException { this.repository = repository; diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java index 9467c04e1..bf6545c71 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java @@ -17,11 +17,13 @@ package org.apache.maven.repository.indexing; */ import org.apache.lucene.document.Document; +import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.TermQuery; import org.apache.maven.repository.indexing.query.AbstractCompoundQuery; import org.apache.maven.repository.indexing.query.OptionalQuery; import org.apache.maven.repository.indexing.query.Query; @@ -125,7 +127,6 @@ public abstract class AbstractRepositoryIndexSearcher } catch ( IOException ie ) { - ie.printStackTrace(); throw new RepositoryIndexSearchException( ie.getMessage(), ie ); } @@ -172,8 +173,17 @@ public abstract class AbstractRepositoryIndexSearcher private void addQuery( String field, String value, boolean required, boolean prohibited ) throws ParseException { - QueryParser parser = new QueryParser( field, index.getAnalyzer() ); - org.apache.lucene.search.Query qry = parser.parse( value ); + org.apache.lucene.search.Query qry; + if ( index.isKeywordField( field ) ) + { + Term term = new Term( field, value ); + qry = new TermQuery( term ); + } + else + { + QueryParser parser = new QueryParser( field, index.getAnalyzer() ); + qry = parser.parse( value ); + } bQry.add( qry, required, prohibited ); } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java index 581c5bc00..6e7e3262d 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java @@ -69,7 +69,7 @@ public class ArtifactRepositoryIndex public ArtifactRepositoryIndex( String indexPath, ArtifactRepository repository, Digester digester ) throws RepositoryIndexException { - super( repository, indexPath ); + super( indexPath, repository ); this.digester = digester; } @@ -192,6 +192,11 @@ public class ArtifactRepositoryIndex } } + public boolean isKeywordField( String field ) + { + return false; + } + private boolean addIfClassEntry( ZipEntry entry, StringBuffer classes ) { boolean isAdded = false; diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java index 1b7ddf999..e4487f066 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java @@ -48,4 +48,15 @@ public class DefaultRepositoryIndexingFactory { return new ArtifactRepositoryIndex( indexPath, repository, digester ); } + + public PomRepositoryIndex createPomRepositoryIndex( String indexPath, ArtifactRepository repository ) + throws RepositoryIndexException + { + return new PomRepositoryIndex( indexPath, repository, digester, artifactFactory ); + } + + public PomRepositoryIndexSearcher createPomRepositoryIndexSearcher( PomRepositoryIndex index ) + { + return new PomRepositoryIndexSearcher( index, artifactFactory ); + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java index ce415118d..200d31030 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java @@ -21,15 +21,22 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Dependency; import org.apache.maven.model.License; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; +import org.apache.maven.model.ReportPlugin; +import org.apache.maven.repository.digest.Digester; import org.codehaus.plexus.util.StringUtils; +import java.io.File; import java.io.IOException; +import java.security.NoSuchAlgorithmException; import java.util.Iterator; +import java.util.List; /** * @author Edwin Punzalan @@ -55,22 +62,33 @@ public class PomRepositoryIndex protected static final String FLD_PLUGINS_ALL = "plugins_all"; + protected static final String FLD_SHA1 = "sha1"; + + protected static final String FLD_MD5 = "md5"; + private static final String[] FIELDS = {FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL}; private Analyzer analyzer; - public PomRepositoryIndex( ArtifactRepository repository, String indexPath ) + private Digester digester; + + private ArtifactFactory artifactFactory; + + public PomRepositoryIndex( String indexPath, ArtifactRepository repository, Digester digester, + ArtifactFactory artifactFactory ) throws RepositoryIndexException { - super( repository, indexPath ); + super( indexPath, repository ); + this.digester = digester; + this.artifactFactory = artifactFactory; } public Analyzer getAnalyzer() { if ( analyzer == null ) { - analyzer = new SimpleAnalyzer(); + analyzer = new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() ); } return analyzer; @@ -81,7 +99,7 @@ public class PomRepositoryIndex { if ( obj instanceof Model ) { - indexModel( (Model) obj ); + indexPom( (Model) obj ); } else { @@ -95,7 +113,7 @@ public class PomRepositoryIndex return FIELDS; } - public void indexModel( Model pom ) + public void indexPom( Model pom ) throws RepositoryIndexException { if ( !isOpen() ) @@ -109,12 +127,43 @@ public class PomRepositoryIndex doc.add( Field.Text( FLD_VERSION, pom.getVersion() ) ); doc.add( Field.Keyword( FLD_PACKAGING, pom.getPackaging() ) ); - indexLicenseUrls( doc, pom.getLicenses().iterator() ); - indexDependencies( doc, pom.getDependencies().iterator() ); - indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); - indexPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); - indexPlugins( doc, FLD_PLUGINS_ALL, pom.getBuild().getPlugins().iterator() ); - indexPlugins( doc, FLD_PLUGINS_ALL, pom.getReporting().getPlugins().iterator() ); + Artifact artifact = + artifactFactory.createBuildArtifact( pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), "pom" ); + File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) ); + doc.add( Field.Text( FLD_SHA1, getChecksum( Digester.SHA1, pomFile.getAbsolutePath() ) ) ); + doc.add( Field.Text( FLD_MD5, getChecksum( Digester.MD5, pomFile.getAbsolutePath() ) ) ); + + indexLicenseUrls( doc, pom ); + indexDependencies( doc, pom ); + + boolean hasPlugins = false; + if ( pom.getBuild() != null && pom.getBuild().getPlugins() != null && pom.getBuild().getPlugins().size() > 0 ) + { + hasPlugins = true; + indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); + indexPlugins( doc, FLD_PLUGINS_ALL, pom.getBuild().getPlugins().iterator() ); + } + else + { + doc.add( Field.Text( FLD_PLUGINS_BUILD, "" ) ); + } + + if ( pom.getReporting() != null && pom.getReporting().getPlugins() != null && + pom.getReporting().getPlugins().size() > 0 ) + { + hasPlugins = true; + indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); + indexReportPlugins( doc, FLD_PLUGINS_ALL, pom.getReporting().getPlugins().iterator() ); + } + else + { + doc.add( Field.Text( FLD_PLUGINS_REPORT, "" ) ); + } + + if ( !hasPlugins ) + { + doc.add( Field.Text( FLD_PLUGINS_ALL, "" ) ); + } try { @@ -126,26 +175,76 @@ public class PomRepositoryIndex } } - private void indexLicenseUrls( Document doc, Iterator licenses ) + public boolean isKeywordField( String field ) { - while ( licenses.hasNext() ) + boolean keyword; + + if ( field.equals( PomRepositoryIndex.FLD_LICENSE_URLS ) ) { - License license = (License) licenses.next(); - String url = license.getUrl(); - if ( StringUtils.isNotEmpty( url ) ) + keyword = true; + } + else if ( field.equals( PomRepositoryIndex.FLD_DEPENDENCIES ) ) + { + keyword = true; + } + else if ( field.equals( PomRepositoryIndex.FLD_PLUGINS_BUILD ) ) + { + keyword = true; + } + else if ( field.equals( PomRepositoryIndex.FLD_PLUGINS_REPORT ) ) + { + keyword = true; + } + else if ( field.equals( PomRepositoryIndex.FLD_PLUGINS_ALL ) ) + { + keyword = true; + } + else + { + keyword = false; + } + + return keyword; + } + + private void indexLicenseUrls( Document doc, Model pom ) + { + List licenseList = pom.getLicenses(); + if ( licenseList != null && licenseList.size() > 0 ) + { + Iterator licenses = licenseList.iterator(); + while ( licenses.hasNext() ) { - doc.add( Field.Keyword( FLD_LICENSE_URLS, url ) ); + License license = (License) licenses.next(); + String url = license.getUrl(); + if ( StringUtils.isNotEmpty( url ) ) + { + doc.add( Field.Keyword( FLD_LICENSE_URLS, url ) ); + } } } + else + { + doc.add( Field.Keyword( FLD_LICENSE_URLS, "" ) ); + } } - private void indexDependencies( Document doc, Iterator dependencies ) + private void indexDependencies( Document doc, Model pom ) { - while ( dependencies.hasNext() ) + List dependencyList = pom.getDependencies(); + if ( dependencyList != null && dependencyList.size() > 0 ) { - Dependency dep = (Dependency) dependencies.next(); - String id = getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); - doc.add( Field.Keyword( FLD_DEPENDENCIES, id ) ); + Iterator dependencies = dependencyList.iterator(); + while ( dependencies.hasNext() ) + { + Dependency dep = (Dependency) dependencies.next(); + String id = getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); + doc.add( Field.Keyword( FLD_DEPENDENCIES, id ) ); + } + } + else + { + doc.add( Field.Keyword( FLD_DEPENDENCIES, "" ) ); } } @@ -159,6 +258,33 @@ public class PomRepositoryIndex } } + private void indexReportPlugins( Document doc, String field, Iterator plugins ) + { + while ( plugins.hasNext() ) + { + ReportPlugin plugin = (ReportPlugin) plugins.next(); + String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); + doc.add( Field.Keyword( field, id ) ); + } + } + + private String getChecksum( String algorithm, String file ) + throws RepositoryIndexException + { + try + { + return digester.createChecksum( new File( file ), algorithm ); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( e.getMessage(), e ); + } + catch ( NoSuchAlgorithmException e ) + { + throw new RepositoryIndexException( e.getMessage(), e ); + } + } + private String getId( String groupId, String artifactId, String version ) { return groupId + ":" + artifactId + ":" + version; diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java index 7becb79a7..b9051b592 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java @@ -44,4 +44,6 @@ public interface RepositoryIndex Analyzer getAnalyzer(); String getIndexPath(); + + boolean isKeywordField( String field ); } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java index 8cf5c6dfc..1d085b523 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java @@ -31,4 +31,9 @@ public interface RepositoryIndexingFactory ArtifactRepositoryIndex createArtifactRepositoryIndex( String indexPath, ArtifactRepository repository ) throws RepositoryIndexException; + + PomRepositoryIndex createPomRepositoryIndex( String indexPath, ArtifactRepository repository ) + throws RepositoryIndexException; + + public PomRepositoryIndexSearcher createPomRepositoryIndexSearcher( PomRepositoryIndex index ); } diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java index edef88a15..23b2ace10 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java @@ -78,7 +78,7 @@ public class ArtifactRepositoryIndexingTest repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); digester = new DefaultDigester(); - indexPath = "target/index"; + indexPath = "target/index/jar"; FileUtils.deleteDirectory( indexPath ); } @@ -91,7 +91,7 @@ public class ArtifactRepositoryIndexingTest try { String notIndexDir = new File( "pom.xml" ).getAbsolutePath(); - indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository ); + factory.createArtifactRepositoryIndex( notIndexDir, repository ); fail( "Must throw exception on non-directory index directory" ); } catch ( RepositoryIndexException e ) @@ -102,7 +102,7 @@ public class ArtifactRepositoryIndexingTest try { String notIndexDir = new File( "" ).getAbsolutePath(); - indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository ); + factory.createArtifactRepositoryIndex( notIndexDir, repository ); fail( "Must throw an exception on a non-index directory" ); } catch ( RepositoryIndexException e ) diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java new file mode 100644 index 000000000..c3f62bd37 --- /dev/null +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java @@ -0,0 +1,506 @@ +package org.apache.maven.repository.indexing; + +/** + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.License; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.ReportPlugin; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.repository.digest.DefaultDigester; +import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.indexing.query.OptionalQuery; +import org.apache.maven.repository.indexing.query.Query; +import org.apache.maven.repository.indexing.query.RequiredQuery; +import org.apache.maven.repository.indexing.query.SinglePhraseQuery; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.io.FileReader; +import java.util.Iterator; +import java.util.List; + +/** + * @author Edwin Punzalan + */ +public class PomRepositoryIndexingTest + extends PlexusTestCase +{ + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private String indexPath; + + private Digester digester; + + protected void setUp() + throws Exception + { + super.setUp(); + + File repositoryDirectory = getTestFile( "src/test/repository" ); + String repoDir = repositoryDirectory.toURL().toString(); + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); + digester = new DefaultDigester(); + + indexPath = "target/index/pom"; + FileUtils.deleteDirectory( indexPath ); + } + + public void testIndexerExceptions() + throws Exception + { + PomRepositoryIndex indexer; + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + + try + { + String notIndexDir = new File( "pom.xml" ).getAbsolutePath(); + factory.createPomRepositoryIndex( notIndexDir, repository ); + fail( "Must throw exception on non-directory index directory" ); + } + catch ( RepositoryIndexException e ) + { + // expected + } + + try + { + String notIndexDir = new File( "" ).getAbsolutePath(); + factory.createPomRepositoryIndex( notIndexDir, repository ); + fail( "Must throw an exception on a non-index directory" ); + } + catch ( RepositoryIndexException e ) + { + // expected + } + + Model pom = getPom( "test", "test-artifactId", "1.0" ); + + indexer = factory.createPomRepositoryIndex( indexPath, repository ); + indexer.close(); + + try + { + indexer.indexPom( pom ); + fail( "Must throw exception on add index with closed index." ); + } + catch ( RepositoryIndexException e ) + { + // expected + } + + try + { + indexer.optimize(); + fail( "Must throw exception on optimize index with closed index." ); + } + catch ( RepositoryIndexException e ) + { + // expected + } + + indexer = factory.createPomRepositoryIndex( indexPath, repository ); + + try + { + indexer.index( "should fail" ); + fail( "Must throw exception on add non-Artifact object." ); + } + catch ( RepositoryIndexException e ) + { + // expected + } + + indexer.close(); + } + + /** + * Test the PomRepositoryIndexSearcher using a single-phrase search. + * + * @throws Exception + */ + public void testSearchSingle() + throws Exception + { + createTestIndex(); + + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); + RepositoryIndexSearcher repoSearcher = factory.createPomRepositoryIndexSearcher( indexer ); + + // search version + Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "1.0" ); + List artifactList = repoSearcher.search( qry ); + assertEquals( 1, artifactList.size() ); + for ( Iterator iter = artifactList.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + assertEquals( "1.0", artifact.getVersion() ); + } + + // search group id + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 2, artifactList.size() ); + Iterator artifacts = artifactList.iterator(); + if ( artifacts.hasNext() ) + { + Artifact artifact = (Artifact) artifacts.next(); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + } + + // search artifact id + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 1, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + } + + // search version + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 2, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); + } + + // search packaging + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PACKAGING, "jar" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 3, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + assertEquals( "jar", artifact.getType() ); + } + + //search license url + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_LICENSE_URLS, + "http://www.apache.org/licenses/LICENSE-2.0.txt" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 2, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + License license = (License) getPom( artifact ).getLicenses().get( 0 ); + assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", license.getUrl() ); + } + + //search dependencies + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 2, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + Iterator dependencies = getPom( artifact ).getDependencies().iterator(); + boolean depFound = false; + while ( dependencies.hasNext() ) + { + Dependency dep = (Dependency) dependencies.next(); + if ( ( dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion() ).equals( + "org.codehaus.plexus:plexus-utils:1.0.5" ) ) + { + depFound = true; + break; + } + } + assertTrue( "Searched dependency not found.", depFound ); + } + + //search build plugin + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_BUILD, + "org.codehaus.modello:modello-maven-plugin:2.0" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 1, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + Iterator plugins = getPom( artifact ).getBuild().getPlugins().iterator(); + boolean found = false; + while ( plugins.hasNext() ) + { + Plugin plugin = (Plugin) plugins.next(); + if ( ( plugin.getKey() + ":" + plugin.getVersion() ).equals( + "org.codehaus.modello:modello-maven-plugin:2.0" ) ) + { + found = true; + break; + } + } + assertTrue( "Searched plugin not found.", found ); + } + + //search reporting plugin + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_REPORT, + "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ); + artifactList = repoSearcher.search( qry ); + assertEquals( 1, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact = (Artifact) artifacts.next(); + Iterator plugins = getPom( artifact ).getReporting().getPlugins().iterator(); + boolean found = false; + while ( plugins.hasNext() ) + { + ReportPlugin plugin = (ReportPlugin) plugins.next(); + if ( ( plugin.getKey() + ":" + plugin.getVersion() ).equals( + "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ) ) + { + found = true; + break; + } + } + assertTrue( "Searched report plugin not found.", found ); + } + + // search sha1 checksum + Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); + + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_SHA1, sha1.trim() ); + artifactList = repoSearcher.search( qry ); + assertEquals( 1, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact2 = (Artifact) artifacts.next(); + String sha1Tmp = digester.createChecksum( getPomFile( artifact ), Digester.SHA1 ); + assertEquals( sha1, sha1Tmp ); + } + + // search md5 checksum + String md5 = digester.createChecksum( getPomFile( artifact ), Digester.MD5 ); + qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_MD5, md5.trim() ); + artifactList = repoSearcher.search( qry ); + assertEquals( 1, artifactList.size() ); + for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) + { + Artifact artifact2 = (Artifact) artifacts.next(); + String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 ); + assertEquals( md5, md5Tmp ); + } + + indexer.close(); + } + + /** + * Test the ArtifactRepositoryIndexSearcher using compound search (AND, OR). + * + * @throws Exception + */ + public void testSearchCompound() + throws Exception + { + createTestIndex(); + + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); + RepositoryIndexSearcher repoSearcher = factory.createPomRepositoryIndexSearcher( indexer ); + + // Criteria 1: required query + // ex. artifactId=maven-artifact AND groupId=org.apache.maven + Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); + Query qry2 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" ); + RequiredQuery rQry = new RequiredQuery(); + rQry.add( qry1 ); + rQry.add( qry2 ); + + List artifacts = repoSearcher.search( rQry ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + } + + // Criteria 2: nested required query + // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR + // version=2.0.3 + Query qry3 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.3" ); + OptionalQuery oQry = new OptionalQuery(); + oQry.add( rQry ); + oQry.add( qry3 ); + + artifacts = repoSearcher.search( oQry ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + } + + // Criteria 3: nested required query + // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND + // (version=2.0.3 OR version=2.0.1) + // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact) + Query qry4 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.1" ); + oQry = new OptionalQuery(); + oQry.add( qry3 ); + oQry.add( qry4 ); + + OptionalQuery oQry5 = new OptionalQuery(); + Query qry9 = + new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); + Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, + "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" ); + oQry5.add( qry9 ); + oQry5.add( qry10 ); + + RequiredQuery rQry2 = new RequiredQuery(); + rQry2.add( oQry ); + rQry2.add( rQry ); + rQry2.add( oQry5 ); + + artifacts = repoSearcher.search( rQry2 ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + assertEquals( "2.0.1", artifact.getVersion() ); + } + + // Criteria 4: nested required query + // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND + // (version=2.0.3 OR version=2.0.1) + // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] + // OR [(artifactId=sample AND groupId=test)] + RequiredQuery rQry3 = new RequiredQuery(); + Query qry5 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample" ); + Query qry6 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" ); + rQry3.add( qry5 ); + rQry3.add( qry6 ); + OptionalQuery oQry2 = new OptionalQuery(); + oQry2.add( rQry2 ); + oQry2.add( rQry3 ); + + artifacts = repoSearcher.search( oQry2 ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + assertEquals( "2.0.1", artifact.getVersion() ); + } + + // Criteria 4: nested required query + // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND + // (version=2.0.3 OR version=2.0.1) + // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR + // [(artifactId=sample AND groupId=test)] OR + // [(artifactId=sample2 AND groupId=test)] + RequiredQuery rQry4 = new RequiredQuery(); + Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" ); + Query qry8 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" ); + rQry4.add( qry7 ); + rQry4.add( qry8 ); + oQry2.add( rQry4 ); + + artifacts = repoSearcher.search( oQry2 ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + } + + indexer.close(); + } + + /** + * Create an index that will be used for testing. + * + * @throws Exception + */ + private void createTestIndex() + throws Exception + { + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); + + Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); + indexer.indexPom( pom ); + + pom = getPom( "org.apache.maven", "maven-model", "2.0" ); + indexer.indexPom( pom ); + + pom = getPom( "test", "test-artifactId", "1.0" ); + indexer.index( pom ); + + indexer.optimize(); + indexer.close(); + } + + private Model getPom( String groupId, String artifactId, String version ) + throws Exception + { + Artifact artifact = getArtifact( groupId, artifactId, version ); + + return getPom( artifact ); + } + + private Model getPom( Artifact artifact ) + throws Exception + { + File pomFile = getPomFile( artifact ); + + MavenXpp3Reader pomReader = new MavenXpp3Reader(); + return pomReader.read( new FileReader( pomFile ) ); + } + + private File getPomFile( Artifact artifact ) + { + String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath(); + return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" ); + } + + private Artifact getArtifact( String groupId, String artifactId, String version ) + throws Exception + { + if ( artifactFactory == null ) + { + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + } + + return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); + } + + protected void tearDown() + throws Exception + { + repository = null; + + super.tearDown(); + } +} diff --git a/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom new file mode 100644 index 000000000..9cebfecc7 --- /dev/null +++ b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom @@ -0,0 +1,44 @@ + + + maven + org.apache.maven + 2.0.1 + + 4.0.0 + org.apache.maven + maven-artifact + Maven Artifact + 2.0.1 + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + org.codehaus.plexus + plexus-utils + 1.0.5 + + + org.codehaus.plexus + plexus-container-default + 1.0-alpha-9 + test + + + + deployed + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.0 + + + + \ No newline at end of file diff --git a/maven-repository-indexer/src/test/repository/org/apache/maven/maven-model/2.0/maven-model-2.0.pom b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-model/2.0/maven-model-2.0.pom new file mode 100644 index 000000000..2abd766dc --- /dev/null +++ b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-model/2.0/maven-model-2.0.pom @@ -0,0 +1,95 @@ + + + maven + org.apache.maven + 2.0 + + 4.0.0 + org.apache.maven + maven-model + Maven Model + 2.0 + Maven Model + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + org.codehaus.modello + modello-maven-plugin + 2.0 + + + + xpp3-writer + java + xpp3-reader + xsd + + + + + 4.0.0 + maven.mdo + + + + + + + all-models + + + + org.codehaus.modello + modello-maven-plugin + + + v3 + + xpp3-writer + java + xpp3-reader + xsd + + + 3.0.0 + true + + + + + + maven-jar-plugin + + + package + + jar + + + all + + + + + + + + + + + org.codehaus.plexus + plexus-utils + 1.0.5 + + + + deployed + + \ No newline at end of file diff --git a/maven-repository-indexer/src/test/repository/test/test-artifactId/1.0/test-artifactId-1.0.pom b/maven-repository-indexer/src/test/repository/test/test-artifactId/1.0/test-artifactId-1.0.pom new file mode 100644 index 000000000..157d74f41 --- /dev/null +++ b/maven-repository-indexer/src/test/repository/test/test-artifactId/1.0/test-artifactId-1.0.pom @@ -0,0 +1,6 @@ + + 4.0.0 + test + test-artifactId + 1.0 + \ No newline at end of file