]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-34
authorEdwin L. Punzalan <epunzalan@apache.org>
Fri, 6 Jan 2006 07:19:12 +0000 (07:19 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Fri, 6 Jan 2006 07:19:12 +0000 (07:19 +0000)
Added unit tests and refactored some common code

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@366445 13f79535-47bb-0310-9956-ffa450edef68

12 files changed:
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java
maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java [new file with mode: 0644]
maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom [new file with mode: 0644]
maven-repository-indexer/src/test/repository/org/apache/maven/maven-model/2.0/maven-model-2.0.pom [new file with mode: 0644]
maven-repository-indexer/src/test/repository/test/test-artifactId/1.0/test-artifactId-1.0.pom [new file with mode: 0644]

index 7419ec0d3546bd7086dbf189af761f07df672a25..8fd396f8e4b17bc719ff82937fa323327b578b88 100644 (file)
@@ -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;
index 9467c04e15550a207dda8fe6135bfe6e41aec197..bf6545c7197628c2f38392f7c3c245495a685715 100644 (file)
@@ -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 );
     }
 
index 581c5bc00def95da87ddbebfd5963b74514c8d92..6e7e3262d4282e4d03adde46e28580f2d1c2d29b 100644 (file)
@@ -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;
index 1b7ddf99980f9f2c36a747bc2e3375b2a5192c67..e4487f066e586a32ee95b874bd26e2f07baa1245 100644 (file)
@@ -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 );
+    }
 }
index ce415118d41d9b81923ae9be4f5f4c5b26946eb9..200d3103093494d43ea6ea178a4fbc4954915662 100644 (file)
@@ -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 )
+    {
+        boolean keyword;
+
+        if ( field.equals( PomRepositoryIndex.FLD_LICENSE_URLS ) )
+        {
+            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 )
     {
-        while ( licenses.hasNext() )
+        List licenseList = pom.getLicenses();
+        if ( licenseList != null && licenseList.size() > 0 )
         {
-            License license = (License) licenses.next();
-            String url = license.getUrl();
-            if ( StringUtils.isNotEmpty( url ) )
+            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;
index 7becb79a7ed93fe2edee056bd085e620913ac7e5..b9051b592f31d196084e3628a766506c021ef228 100644 (file)
@@ -44,4 +44,6 @@ public interface RepositoryIndex
     Analyzer getAnalyzer();
 
     String getIndexPath();
+
+    boolean isKeywordField( String field );
 }
index 8cf5c6dfc8e91ea1e5189d42a0394f60f0eaf651..1d085b523196dc559f63ebcd178331fc9b34af78 100644 (file)
@@ -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 );
 }
index edef88a150f6a0b22580c66af239ff9c7e9ede6b..23b2ace10d6068dc571ae8665b86e55e4f666519 100644 (file)
@@ -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 (file)
index 0000000..c3f62bd
--- /dev/null
@@ -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 (file)
index 0000000..9cebfec
--- /dev/null
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?><project>
+  <parent>
+    <artifactId>maven</artifactId>
+    <groupId>org.apache.maven</groupId>
+    <version>2.0.1</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven</groupId>
+  <artifactId>maven-artifact</artifactId>
+  <name>Maven Artifact</name>
+  <version>2.0.1</version>
+  <licenses>
+    <license>
+      <name>The Apache Software License, Version 2.0</name>
+      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+      <distribution>repo</distribution>
+    </license>
+  </licenses>
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>1.0.5</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-container-default</artifactId>
+      <version>1.0-alpha-9</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <distributionManagement>
+    <status>deployed</status>
+  </distributionManagement>
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <version>2.0</version>
+      </plugin>
+    </plugins>
+  </reporting>
+</project>
\ 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 (file)
index 0000000..2abd766
--- /dev/null
@@ -0,0 +1,95 @@
+<project>
+  <parent>
+    <artifactId>maven</artifactId>
+    <groupId>org.apache.maven</groupId>
+    <version>2.0</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven</groupId>
+  <artifactId>maven-model</artifactId>
+  <name>Maven Model</name>
+  <version>2.0</version>
+  <description>Maven Model</description>
+  <licenses>
+    <license>
+      <name>The Apache Software License, Version 2.0</name>
+      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+      <distribution>repo</distribution>
+    </license>
+  </licenses>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.modello</groupId>
+        <artifactId>modello-maven-plugin</artifactId>
+        <version>2.0</version>
+        <executions>
+          <execution>
+            <goals>
+              <goal>xpp3-writer</goal>
+              <goal>java</goal>
+              <goal>xpp3-reader</goal>
+              <goal>xsd</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <version>4.0.0</version>
+          <model>maven.mdo</model>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <profiles>
+    <profile>
+      <id>all-models</id>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.codehaus.modello</groupId>
+            <artifactId>modello-maven-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>v3</id>
+                <goals>
+                  <goal>xpp3-writer</goal>
+                  <goal>java</goal>
+                  <goal>xpp3-reader</goal>
+                  <goal>xsd</goal>
+                </goals>
+                <configuration>
+                  <version>3.0.0</version>
+                  <packageWithVersion>true</packageWithVersion>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+          <plugin>
+            <artifactId>maven-jar-plugin</artifactId>
+            <executions>
+              <execution>
+                <phase>package</phase>
+                <goals>
+                  <goal>jar</goal>
+                </goals>
+                <configuration>
+                  <classifier>all</classifier>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+  </profiles>
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>1.0.5</version>
+    </dependency>
+  </dependencies>
+  <distributionManagement>
+    <status>deployed</status>
+  </distributionManagement>
+</project>
\ 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 (file)
index 0000000..157d74f
--- /dev/null
@@ -0,0 +1,6 @@
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>test</groupId>
+  <artifactId>test-artifactId</artifactId>
+  <version>1.0</version>
+</project>
\ No newline at end of file