From: Maria Odea B. Ching Date: Thu, 29 Jan 2009 06:14:42 +0000 (+0000) Subject: [MRM-749] X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4d70f30499df4159aa47d51d971d7fe810f4c8b9;p=archiva.git [MRM-749] 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 --- diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java index c5172e0ef..8a34836de 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java @@ -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 { diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java index 330b8b976..87bf9cad0 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java @@ -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 artifactInfos = response.getResults(); for ( ArtifactInfo artifactInfo : artifactInfos ) { - String id = artifactInfo.groupId + ":" + artifactInfo.artifactId; + String id = artifactInfo.groupId + ":" + artifactInfo.artifactId; Map 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; } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java index b1e707e54..590280b2d 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/SearchResultHit.java @@ -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 index 000000000..1befe9479 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java index cc08bc834..2c6bc3c93 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java @@ -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 selectedRepos = new ArrayList(); + 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 index 000000000..9c16c3ced 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 index 000000000..b0d983fde --- /dev/null +++ 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 @@ -0,0 +1,28 @@ + + 4.0.0 + org.apache.archiva + archiva-search + jar + 1.0 + Archiva Search + http://archiva.apache.org + + + junit + junit + 3.8.1 + test + + + commons-lang + commons-lang + 2.3 + + + log4j + log4j + 1.2.8 + + + 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 index 000000000..843ee45bc 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 index 000000000..0b037a3a5 --- /dev/null +++ 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 @@ -0,0 +1,23 @@ + + 4.0.0 + org.apache.archiva + archiva-test + jar + 1.0 + archiva-test + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + commons-lang + commons-lang + 2.3 + + + 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 index 000000000..e904a17c4 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 index 000000000..04fdb4859 --- /dev/null +++ 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 @@ -0,0 +1,28 @@ + + 4.0.0 + org.apache.archiva + archiva-test + jar + 2.0 + Archiva Test + http://archiva.apache.org + + + junit + junit + 3.8.1 + test + + + commons-lang + commons-lang + 2.3 + + + commons-io + commons-io + 1.4 + + +