]> source.dussan.org Git - archiva.git/commitdiff
[MRM-749]
authorMaria Odea B. Ching <oching@apache.org>
Thu, 29 Jan 2009 06:14:42 +0000 (06:14 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Thu, 29 Jan 2009 06:14:42 +0000 (06:14 +0000)
o added quick search implementation in NexusRepositorySearch
o added tests

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@738766 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java
archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java
archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.pom [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.pom [new file with mode: 0644]

index c5172e0ef74a127bebd56fa1a52436411bed57fe..8a34836de8fab28356cea63964afc035c3e5c2ba 100644 (file)
@@ -105,7 +105,8 @@ public class NexusIndexerConsumer
         File indexDirectory = null;
         if( indexDir != null && !"".equals( indexDir ) )
         {
-            indexDirectory = new File( managedRepository, repository.getIndexDir() );
+            //indexDirectory = new File( managedRepository, repository.getIndexDir() );
+            indexDirectory = new File( repository.getIndexDir() );
         }
         else
         {
index 330b8b976592e7c8b833b15b142fdcb3620cea23..87bf9cad09cfad409f5738e4a9add5bb40d0acb2 100644 (file)
@@ -26,7 +26,9 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.Configuration;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.indexer.search.SearchResultHit;
 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
@@ -69,15 +71,25 @@ public class NexusRepositorySearch
         //    - regular search
         //    - searching within search results
         // 2. consider pagination
-        
+        // 3. multiple repositories
+                        
         BooleanQuery q = new BooleanQuery();
-        // q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
-        // q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
-
+        q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, term ), Occur.SHOULD );
+        q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, term ), Occur.SHOULD );
+        q.add( indexer.constructQuery( ArtifactInfo.VERSION, term ), Occur.SHOULD );
+        q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, term ), Occur.SHOULD );
+                
+        // TODO: what about class & package?        
+        
         try
         {
             FlatSearchRequest request = new FlatSearchRequest( q );
             FlatSearchResponse response = indexer.searchFlat( request );
+            
+            if( response == null )
+            {
+                return new SearchResults();
+            }
 
             return convertToSearchResults( response );
         }
@@ -104,22 +116,31 @@ public class NexusRepositorySearch
         {
             try
             {
-                ManagedRepositoryConfiguration repoConfig = archivaConfig.getConfiguration().findManagedRepositoryById( repo );
-                String indexDir = repoConfig.getIndexDir();
-                File indexDirectory = null;
-                if( indexDir != null && !"".equals( indexDir ) )
+                Configuration config = archivaConfig.getConfiguration();
+                ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
+                
+                if( repoConfig != null )
                 {
-                    indexDirectory = new File( repoConfig.getLocation(), repoConfig.getIndexDir() );
+                    String indexDir = repoConfig.getIndexDir();
+                    File indexDirectory = null;
+                    if( indexDir != null && !"".equals( indexDir ) )
+                    {
+                        indexDirectory = new File( repoConfig.getIndexDir() );
+                    }
+                    else
+                    {
+                        indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
+                    }
+                    
+                    IndexingContext context =
+                        indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
+                                                    indexDirectory, null, null, NexusIndexer.FULL_INDEX );
+                    context.setSearchable( repoConfig.isScanned() );
                 }
                 else
                 {
-                    indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
+                    log.warn( "Repository '" + repo + "' not found in configuration." );
                 }
-                
-                IndexingContext context =
-                    indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
-                                                indexDirectory, null, null, NexusIndexer.FULL_INDEX );
-                context.setSearchable( repoConfig.isScanned() );
             }
             catch ( UnsupportedExistingLuceneIndexException e )
             {
@@ -138,12 +159,14 @@ public class NexusRepositorySearch
 
     private SearchResults convertToSearchResults( FlatSearchResponse response )
     {
+        // TODO: paginate!
+        
         SearchResults results = new SearchResults();
         Set<ArtifactInfo> artifactInfos = response.getResults();
 
         for ( ArtifactInfo artifactInfo : artifactInfos )
         {
-            String id = artifactInfo.groupId + ":" + artifactInfo.artifactId;
+            String id = artifactInfo.groupId + ":" + artifactInfo.artifactId;            
             Map<String, SearchResultHit> hitsMap = results.getHitsMap();
 
             SearchResultHit hit = hitsMap.get( id );
@@ -158,12 +181,17 @@ public class NexusRepositorySearch
                 hit.setGroupId( artifactInfo.groupId );
                 hit.setRepositoryId( artifactInfo.repository );
                 hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
-                hit.addVersion( artifactInfo.version );
+                if( !hit.getVersions().contains( artifactInfo.version ) )
+                {
+                    hit.addVersion( artifactInfo.version );
+                }
             }
 
             results.addHit( id, hit );
         }
-
+        
+        results.setTotalHits( results.getHitsMap().size() );
+        
         return results;
     }
 
index b1e707e5494c800f7d7809b6178eacaa28c6223a..590280b2def10d436d87d3c4bb9a6a467281c228 100644 (file)
@@ -44,6 +44,7 @@ public class SearchResultHit
     //  Advanced hit, reference to artifactId.
     private String artifactId;
 
+    // TODO: remove/deprecate this field!
     private String version = "";
     
     private String repositoryId = "";
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml
new file mode 100644 (file)
index 0000000..1befe94
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+    <bean id="nexusSearch" class="org.apache.archiva.indexer.search.NexusRepositorySearch">
+        <constructor-arg ref="nexusIndexer"/>
+        <constructor-arg ref="archivaConfiguration"/>
+    </bean>
+</beans>
\ No newline at end of file
index cc08bc834f328682fd9a895201cde5d9c2309738..2c6bc3c9338a91b1e07ee7d0f3980b7335806e68 100644 (file)
@@ -19,9 +19,25 @@ package org.apache.archiva.indexer.search;
  * under the License.
  */
 
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.Configuration;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
+import org.apache.maven.archiva.indexer.search.SearchResultHit;
+import org.apache.maven.archiva.indexer.search.SearchResults;
 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import org.easymock.MockControl;
+import org.sonatype.nexus.index.ArtifactContext;
+import org.sonatype.nexus.index.ArtifactContextProducer;
 import org.sonatype.nexus.index.NexusIndexer;
+import org.sonatype.nexus.index.context.IndexingContext;
+import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
+import org.sonatype.nexus.index.creator.IndexerEngine;
 
 public class NexusRepositorySearchTest
     extends PlexusInSpringTestCase
@@ -32,20 +48,144 @@ public class NexusRepositorySearchTest
     
     private NexusIndexer indexer;
     
+    private IndexingContext context;
+    
+    private IndexerEngine indexerEngine;
+    
+    private ArtifactContextProducer artifactContextProducer;
+    
+    private MockControl archivaConfigControl;
+    
+    private Configuration config;
+    
+    private final static String TEST_REPO = "nexus-search-test-repo"; 
+    
     @Override
     protected void setUp()
         throws Exception
     {
         super.setUp();
         
-        indexer = ( NexusIndexer )lookup( NexusIndexer.class );
+        indexer = ( NexusIndexer ) lookup( NexusIndexer.class );
         
+        archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
+        
+        archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
+                
         search = new NexusRepositorySearch( indexer, archivaConfig );
+        
+        indexerEngine = ( IndexerEngine ) lookup( IndexerEngine.class );
+        
+        artifactContextProducer = ( ArtifactContextProducer ) lookup( ArtifactContextProducer.class );
+        
+        config = new Configuration();
+        
+        ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration();
+        repositoryConfig.setId( TEST_REPO );
+        repositoryConfig.setLocation( getBasedir() + "/target/test-classes/" + TEST_REPO );
+        repositoryConfig.setLayout( "default" );
+        repositoryConfig.setName( "Nexus Search Test Repository" );
+        repositoryConfig.setScanned( true );
+        repositoryConfig.setSnapshots( false );
+        repositoryConfig.setReleases( true );
+        
+        config.addManagedRepository( repositoryConfig );
+        
+        createIndex();
+    }
+    
+    @Override
+    protected void tearDown()
+        throws Exception
+    {
+        FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/"+ TEST_REPO + "/.indexer" ) );
+        assertFalse( new File( getBasedir(), "/target/test-classes/"+ TEST_REPO + "/.indexer" ).exists() );
+        
+        super.tearDown();
+    }
+    
+    private void createIndex()
+        throws IOException, UnsupportedExistingLuceneIndexException
+    {
+        context =
+            indexer.addIndexingContext( TEST_REPO, TEST_REPO, new File( getBasedir(), "/target/test-classes/" + TEST_REPO ),
+                                    new File( getBasedir(), "/target/test-classes/" + TEST_REPO + "/.indexer"), null, null, NexusIndexer.FULL_INDEX );
+        context.setSearchable( true );
+        
+        indexerEngine.beginIndexing( context );
+        
+        File artifactFile =
+            new File( getBasedir(),
+                      "/target/test-classes/" + TEST_REPO + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" );
+        ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
+        indexerEngine.index( context, ac );
+        
+        artifactFile =
+            new File( getBasedir(),
+                      "/target/test-classes/" + TEST_REPO + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" );
+        ac = artifactContextProducer.getArtifactContext( context, artifactFile );
+        indexerEngine.index( context, ac );
+        
+        artifactFile =
+            new File( getBasedir(),
+                      "/target/test-classes/" + TEST_REPO + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" );
+        ac = artifactContextProducer.getArtifactContext( context, artifactFile );
+        indexerEngine.index( context, ac );
+        
+        indexerEngine.endIndexing( context );     
+        
+        assertTrue( new File( getBasedir(), "/target/test-classes/"+ TEST_REPO + "/.indexer" ).exists() );
     }
     
     public void testQuickSearch()
         throws Exception
     {
+        List<String> selectedRepos = new ArrayList<String>();
+        selectedRepos.add( TEST_REPO );
+        
+        archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
+        
+        archivaConfigControl.replay();
+        
+        SearchResults results = search.search( "user", selectedRepos, "archiva-search", null );
+        
+        archivaConfigControl.verify();
+        
+        assertNotNull( results );
+        assertEquals( 1, results.getTotalHits() );
+        
+        SearchResultHit hit = results.getHits().get( 0 );
+        assertEquals( "org.apache.archiva", hit.getGroupId() );
+        assertEquals( "archiva-search", hit.getArtifactId() );
+        assertEquals( "1.0", hit.getVersions().get( 0 ) );
+        assertEquals( "nexus-search-test-repo", hit.getRepositoryId() );
+        
+        archivaConfigControl.reset();
+        
+        archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
+        
+        archivaConfigControl.replay();
+        
+        results = search.search( "user", selectedRepos, "org.apache.archiva", null );
+        
+        archivaConfigControl.verify();
+        
+        assertNotNull( results );
+        assertEquals( 2, results.getTotalHits() );
+        
+        //TODO: search for class & package names
+    }
+    
+    public void testArtifactFoundInMultipleRepositories()
+        throws Exception
+    {
+        // there should be no duplicates in the search result hit
+        // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
+    }
+    
+    public void testNoMatchFound()
+        throws Exception 
+    {
     
     }
     
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar
new file mode 100644 (file)
index 0000000..9c16c3c
Binary files /dev/null and b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar differ
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.pom
new file mode 100644 (file)
index 0000000..b0d983f
--- /dev/null
@@ -0,0 +1,28 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.archiva</groupId>
+  <artifactId>archiva-search</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0</version>
+  <name>Archiva Search</name>
+  <url>http://archiva.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.3</version>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <version>1.2.8</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar
new file mode 100644 (file)
index 0000000..843ee45
Binary files /dev/null and b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar differ
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.pom
new file mode 100644 (file)
index 0000000..0b037a3
--- /dev/null
@@ -0,0 +1,23 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.archiva</groupId>
+  <artifactId>archiva-test</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0</version>
+  <name>archiva-test</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.3</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar
new file mode 100644 (file)
index 0000000..e904a17
Binary files /dev/null and b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar differ
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/nexus-search-test-repo/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.pom
new file mode 100644 (file)
index 0000000..04fdb48
--- /dev/null
@@ -0,0 +1,28 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.archiva</groupId>
+  <artifactId>archiva-test</artifactId>
+  <packaging>jar</packaging>
+  <version>2.0</version>
+  <name>Archiva Test</name>
+  <url>http://archiva.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.3</version>
+    </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>1.4</version>
+    </dependency>
+  </dependencies>
+</project>