From 7eebed1423c5a7364302900034cf38263f8be465 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 27 May 2011 09:34:43 +0000 Subject: [PATCH] fix unit tests spring configuration in archiva-indexer git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1128212 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva-base/archiva-indexer/pom.xml | 12 + .../indexer/search/NexusRepositorySearch.java | 170 ++++---- .../resources/META-INF/spring-context.xml | 43 +- .../search/NexusRepositorySearchTest.java | 390 ++++++++++-------- .../src/test/resources/spring-context.xml | 32 ++ 5 files changed, 387 insertions(+), 260 deletions(-) create mode 100755 archiva-modules/archiva-base/archiva-indexer/src/test/resources/spring-context.xml diff --git a/archiva-modules/archiva-base/archiva-indexer/pom.xml b/archiva-modules/archiva-base/archiva-indexer/pom.xml index cb1af1db9..4a421f63d 100644 --- a/archiva-modules/archiva-base/archiva-indexer/pom.xml +++ b/archiva-modules/archiva-base/archiva-indexer/pom.xml @@ -66,6 +66,18 @@ org.sonatype.nexus nexus-indexer + + org.sonatype.sisu + sisu-inject-plexus + + + org.sonatype.sisu + sisu-guice + + + org.apache.archiva + archiva-plexus-bridge + javax.inject javax.inject 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 53416912f..b58cfea17 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 @@ -19,15 +19,11 @@ package org.apache.archiva.indexer.search; * under the License. */ -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.Set; - +import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; +import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; import org.apache.archiva.indexer.util.SearchUtil; -import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanClause.Occur; +import org.apache.lucene.search.BooleanQuery; import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; @@ -40,22 +36,33 @@ import org.sonatype.nexus.index.FlatSearchResponse; import org.sonatype.nexus.index.NexusIndexer; import org.sonatype.nexus.index.context.IndexingContext; import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * RepositorySearch implementation which uses the Nexus Indexer for searching. */ +@Service( "nexusSearch" ) public class NexusRepositorySearch implements RepositorySearch { - private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class ); - + private Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class ); + private NexusIndexer indexer; - + private ArchivaConfiguration archivaConfig; - - public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig ) + + @Inject + public NexusRepositorySearch( PlexusSisuBridge plexusSisuBridge, ArchivaConfiguration archivaConfig ) + throws PlexusSisuBridgeException { - this.indexer = indexer; + this.indexer = plexusSisuBridge.lookup( NexusIndexer.class ); this.archivaConfig = archivaConfig; } @@ -65,81 +72,81 @@ public class NexusRepositorySearch public SearchResults search( String principal, List selectedRepos, String term, SearchResultLimits limits, List previousSearchTerms ) throws RepositorySearchException - { + { addIndexingContexts( selectedRepos ); - + // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]* // resulting to more wildcard searches so we need to increase max clause count BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE ); BooleanQuery q = new BooleanQuery(); - - if( previousSearchTerms == null || previousSearchTerms.isEmpty() ) - { + + if ( previousSearchTerms == null || previousSearchTerms.isEmpty() ) + { constructQuery( term, q ); } else - { - for( String previousTerm : previousSearchTerms ) + { + for ( String previousTerm : previousSearchTerms ) { BooleanQuery iQuery = new BooleanQuery(); constructQuery( previousTerm, iQuery ); - + q.add( iQuery, Occur.MUST ); } - + BooleanQuery iQuery = new BooleanQuery(); constructQuery( term, iQuery ); q.add( iQuery, Occur.MUST ); - } - + } + return search( limits, q ); } - + /** * @see RepositorySearch#search(String, SearchFields, SearchResultLimits) */ public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits ) throws RepositorySearchException { - if( searchFields.getRepositories() == null ) + if ( searchFields.getRepositories() == null ) { throw new RepositorySearchException( "Repositories cannot be null." ); } - + addIndexingContexts( searchFields.getRepositories() ); - + BooleanQuery q = new BooleanQuery(); - if( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) ) - { + if ( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) ) + { q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, searchFields.getGroupId() ), Occur.MUST ); } - - if( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) ) + + if ( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) ) { q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, searchFields.getArtifactId() ), Occur.MUST ); } - - if( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) ) + + if ( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) ) { q.add( indexer.constructQuery( ArtifactInfo.VERSION, searchFields.getVersion() ), Occur.MUST ); } - - if( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) ) + + if ( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) ) { q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, searchFields.getPackaging() ), Occur.MUST ); } - - if( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) ) + + if ( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) ) { q.add( indexer.constructQuery( ArtifactInfo.NAMES, searchFields.getClassName() ), Occur.MUST ); } - - if( q.getClauses() == null || q.getClauses().length <= 0 ) + + if ( q.getClauses() == null || q.getClauses().length <= 0 ) { throw new RepositorySearchException( "No search fields set." ); } - - return search( limits, q ); + + return search( limits, q ); } private SearchResults search( SearchResultLimits limits, BooleanQuery q ) @@ -149,14 +156,14 @@ public class NexusRepositorySearch { FlatSearchRequest request = new FlatSearchRequest( q ); FlatSearchResponse response = indexer.searchFlat( request ); - - if( response == null || response.getTotalHits() == 0 ) + + if ( response == null || response.getTotalHits() == 0 ) { SearchResults results = new SearchResults(); results.setLimits( limits ); return results; } - + return convertToSearchResults( response, limits ); } catch ( IOException e ) @@ -166,20 +173,20 @@ public class NexusRepositorySearch finally { Map indexingContexts = indexer.getIndexingContexts(); - + for ( Map.Entry entry : indexingContexts.entrySet() ) { - try - { + try + { indexer.removeIndexingContext( entry.getValue(), false ); log.debug( "Indexing context '" + entry.getKey() + "' removed from search." ); } catch ( IOException e ) { - log.warn( "IOException occurred while removing indexing content '" + entry.getKey() + "'." ); + log.warn( "IOException occurred while removing indexing content '" + entry.getKey() + "'." ); continue; } - } + } } } @@ -189,24 +196,24 @@ public class NexusRepositorySearch 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 ); - q.add( indexer.constructQuery( ArtifactInfo.NAMES, term ), Occur.SHOULD ); + q.add( indexer.constructQuery( ArtifactInfo.NAMES, term ), Occur.SHOULD ); } - - + + private void addIndexingContexts( List selectedRepos ) { - for( String repo : selectedRepos ) + for ( String repo : selectedRepos ) { try { Configuration config = archivaConfig.getConfiguration(); ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo ); - - if( repoConfig != null ) + + if ( repoConfig != null ) { String indexDir = repoConfig.getIndexDir(); File indexDirectory = null; - if( indexDir != null && !"".equals( indexDir ) ) + if ( indexDir != null && !"".equals( indexDir ) ) { indexDirectory = new File( repoConfig.getIndexDir() ); } @@ -214,10 +221,11 @@ public class NexusRepositorySearch { indexDirectory = new File( repoConfig.getLocation(), ".indexer" ); } - - IndexingContext context = - indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ), - indexDirectory, null, null, ArchivaNexusIndexerUtil.FULL_INDEX ); + + IndexingContext context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), + new File( repoConfig.getLocation() ), + indexDirectory, null, null, + ArchivaNexusIndexerUtil.FULL_INDEX ); context.setSearchable( repoConfig.isScanned() ); } else @@ -226,12 +234,12 @@ public class NexusRepositorySearch } } catch ( UnsupportedExistingLuceneIndexException e ) - { + { log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() ); continue; } catch ( IOException e ) - { + { log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() ); continue; } @@ -239,10 +247,10 @@ public class NexusRepositorySearch } private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits ) - { + { SearchResults results = new SearchResults(); Set artifactInfos = response.getResults(); - + for ( ArtifactInfo artifactInfo : artifactInfos ) { String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId ); @@ -261,7 +269,7 @@ public class NexusRepositorySearch // do we still need to set the repository id even though we're merging everything? //hit.setRepositoryId( artifactInfo.repository ); hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname ); - if( !hit.getVersions().contains( artifactInfo.version ) ) + if ( !hit.getVersions().contains( artifactInfo.version ) ) { hit.addVersion( artifactInfo.version ); } @@ -269,33 +277,33 @@ public class NexusRepositorySearch results.addHit( id, hit ); } - + results.setTotalHits( results.getHitsMap().size() ); results.setLimits( limits ); - - if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES ) - { + + if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES ) + { return results; } else { - return paginate( results ); - } + return paginate( results ); + } } private SearchResults paginate( SearchResults results ) { SearchResultLimits limits = results.getLimits(); - SearchResults paginated = new SearchResults(); - + SearchResults paginated = new SearchResults(); + int fetchCount = limits.getPageSize(); int offset = ( limits.getSelectedPage() * limits.getPageSize() ); - - if( fetchCount > results.getTotalHits() ) + + if ( fetchCount > results.getTotalHits() ) { fetchCount = results.getTotalHits(); } - + // Goto offset. if ( offset < results.getTotalHits() ) { @@ -307,9 +315,9 @@ public class NexusRepositorySearch { break; } - + SearchResultHit hit = results.getHits().get( ( offset + i ) ); - if( hit != null ) + if ( hit != null ) { String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() ); paginated.addHit( id, hit ); @@ -319,10 +327,10 @@ public class NexusRepositorySearch break; } } - } + } paginated.setTotalHits( results.getTotalHits() ); paginated.setLimits( limits ); - + return paginated; } } 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 index 7401aec3f..9775f6e53 100644 --- 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 @@ -1,16 +1,45 @@ - + + + + + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd" + default-lazy-init="true"> + + + + + + org.sonatype.nexus.index.DefaultNexusIndexer + \ 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 f180ab0d6..687a5fb20 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,29 +19,40 @@ 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 junit.framework.TestCase; +import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.commons.io.FileUtils; import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.easymock.MockControl; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; import org.sonatype.nexus.artifact.IllegalArtifactCoordinateException; import org.sonatype.nexus.index.ArtifactContext; import org.sonatype.nexus.index.ArtifactContextProducer; +import org.sonatype.nexus.index.IndexerEngine; import org.sonatype.nexus.index.NexusIndexer; import org.sonatype.nexus.index.context.DefaultIndexingContext; import org.sonatype.nexus.index.context.IndexingContext; import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; -import org.sonatype.nexus.index.IndexerEngine; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import javax.inject.Inject; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + +@RunWith( SpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } ) public class NexusRepositorySearchTest - extends PlexusInSpringTestCase + extends TestCase { private RepositorySearch search; @@ -63,62 +74,76 @@ public class NexusRepositorySearchTest private final static String TEST_REPO_2 = "nexus-search-test-repo-2"; - @Override - protected void setUp() + @Inject + PlexusSisuBridge plexusSisuBridge; + + @Before + public void setUp() throws Exception { super.setUp(); - indexer = (NexusIndexer) lookup( NexusIndexer.class ); + indexer = plexusSisuBridge.lookup( NexusIndexer.class ); archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class ); archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock(); - search = new NexusRepositorySearch( indexer, archivaConfig ); + search = new NexusRepositorySearch( plexusSisuBridge, archivaConfig ); - indexerEngine = (IndexerEngine) lookup( IndexerEngine.class ); + indexerEngine = plexusSisuBridge.lookup( IndexerEngine.class ); - artifactContextProducer = (ArtifactContextProducer) lookup( ArtifactContextProducer.class ); + artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class ); config = new Configuration(); config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) ); config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) ); } + public static String getBasedir() + { + String basedir = System.getProperty( "basedir" ); + if ( basedir == null ) + { + basedir = new File( "" ).getAbsolutePath(); + } + + return basedir; + } + private void createSimpleIndex() throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException { List files = new ArrayList(); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); createIndex( TEST_REPO_1, files ); } - + private void createIndexContainingMoreArtifacts() throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException { List files = new ArrayList(); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/com/classname-search/1.0/classname-search-1.0.jar" ) ); - + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/classname-search/1.0/classname-search-1.0.jar" ) ); + createIndex( TEST_REPO_1, files ); } @@ -136,13 +161,13 @@ public class NexusRepositorySearchTest return repositoryConfig; } - @Override - protected void tearDown() + @After + public void tearDown() throws Exception { FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() ); - + FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); @@ -152,12 +177,14 @@ public class NexusRepositorySearchTest private void createIndex( String repository, List filesToBeIndexed ) throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException { - context = new DefaultIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" + - repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, - ArchivaNexusIndexerUtil.FULL_INDEX, false ); - //indexer.addIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" + - // repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, - // NexusIndexer.FULL_INDEX ); + context = new DefaultIndexingContext( repository, repository, + new File( getBasedir(), "/target/test-classes/" + repository ), + new File( getBasedir(), + "/target/test-classes/" + repository + "/.indexer" ), null, + null, ArchivaNexusIndexerUtil.FULL_INDEX, false ); + //indexer.addIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" + + // repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, + // NexusIndexer.FULL_INDEX ); context.setSearchable( true ); //indexerEngine.beginIndexing( context ); @@ -171,15 +198,16 @@ public class NexusRepositorySearchTest context.close( false ); //indexerEngine.endIndexing( context ); //indexer.removeIndexingContext( context, false ); - + assertTrue( new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() ); } + @Test public void testQuickSearch() throws Exception - { + { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); @@ -199,7 +227,7 @@ public class NexusRepositorySearchTest assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "archiva-search", hit.getArtifactId() ); assertEquals( "1.0", hit.getVersions().get( 0 ) ); - + archivaConfigControl.reset(); // search groupId @@ -216,32 +244,34 @@ public class NexusRepositorySearchTest //TODO: search for class & package names } - + // search for existing artifact using multiple keywords + @Test public void testQuickSearchWithMultipleKeywords() throws Exception - { + { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); - + SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null ); - + archivaConfigControl.verify(); - + assertNotNull( results ); assertEquals( 0, results.getTotalHits() ); } - + + @Test public void testQuickSearchWithPagination() throws Exception - { + { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); @@ -282,16 +312,17 @@ public class NexusRepositorySearchTest assertEquals( limits, results.getLimits() ); } + @Test public void testArtifactFoundInMultipleRepositories() throws Exception { createSimpleIndex(); - + List files = new ArrayList(); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + - "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + - "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); createIndex( TEST_REPO_2, files ); List selectedRepos = new ArrayList(); @@ -323,14 +354,15 @@ public class NexusRepositorySearchTest // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found } + @Test public void testNoMatchFound() throws Exception { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -343,12 +375,13 @@ public class NexusRepositorySearchTest assertEquals( 0, results.getTotalHits() ); } + @Test public void testNoIndexFound() throws Exception { List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -356,16 +389,17 @@ public class NexusRepositorySearchTest SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null ); assertNotNull( results ); assertEquals( 0, results.getTotalHits() ); - - archivaConfigControl.verify(); + + archivaConfigControl.verify(); } + @Test public void testRepositoryNotFound() throws Exception { List selectedRepos = new ArrayList(); selectedRepos.add( "non-existing-repo" ); - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -373,21 +407,22 @@ public class NexusRepositorySearchTest SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null ); assertNotNull( results ); assertEquals( 0, results.getTotalHits() ); - - archivaConfigControl.verify(); + + archivaConfigControl.verify(); } - + + @Test public void testSearchWithinSearchResults() throws Exception { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + List previousSearchTerms = new ArrayList(); previousSearchTerms.add( "archiva-test" ); - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -398,34 +433,34 @@ public class NexusRepositorySearchTest assertNotNull( results ); assertEquals( 1, results.getTotalHits() ); - + SearchResultHit hit = results.getHits().get( 0 ); assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "archiva-test", hit.getArtifactId() ); assertEquals( 1, hit.getVersions().size() ); assertEquals( "1.0", hit.getVersions().get( 0 ) ); } - + // tests for advanced search - + @Test public void testAdvancedSearch() throws Exception { List files = new ArrayList(); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + - "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + - "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); createIndex( TEST_REPO_2, files ); - List selectedRepos = new ArrayList(); + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_2 ); - + SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); searchFields.setVersion( "1.0" ); - searchFields.setRepositories( selectedRepos ); - + searchFields.setRepositories( selectedRepos ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -436,13 +471,14 @@ public class NexusRepositorySearchTest 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( "1.0", hit.getVersions().get( 0 ) ); } - + + @Test public void testAdvancedSearchWithPagination() throws Exception { @@ -450,16 +486,16 @@ public class NexusRepositorySearchTest List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); - searchFields.setRepositories( selectedRepos ); - + searchFields.setRepositories( selectedRepos ); + // page 1 - + SearchResultLimits limits = new SearchResultLimits( 0 ); limits.setPageSize( 1 ); - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -471,13 +507,13 @@ public class NexusRepositorySearchTest assertNotNull( results ); assertEquals( 3, results.getTotalHits() ); assertEquals( 1, results.getHits().size() ); - + // page 2 archivaConfigControl.reset(); - + limits = new SearchResultLimits( 1 ); limits.setPageSize( 1 ); - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); @@ -488,27 +524,28 @@ public class NexusRepositorySearchTest assertNotNull( results ); assertEquals( 3, results.getTotalHits() ); - assertEquals( 1, results.getHits().size() ); + assertEquals( 1, results.getHits().size() ); } - + // MRM-981 - artifactIds with numeric characters aren't found in advanced search + @Test public void testAdvancedSearchArtifactIdHasNumericChar() throws Exception { List files = new ArrayList(); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); - files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + - "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); createIndex( TEST_REPO_1, files ); List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setArtifactId( "artifactid-numeric" ); - searchFields.setRepositories( selectedRepos ); - + searchFields.setRepositories( selectedRepos ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 ); archivaConfigControl.replay(); @@ -518,35 +555,37 @@ public class NexusRepositorySearchTest archivaConfigControl.verify(); assertNotNull( results ); - assertEquals( 2, results.getTotalHits() ); + assertEquals( 2, results.getTotalHits() ); } - + + @Test public void testAdvancedSearchNoRepositoriesConfigured() throws Exception { SearchFields searchFields = new SearchFields(); searchFields.setArtifactId( "archiva" ); - searchFields.setRepositories( null ); + searchFields.setRepositories( null ); try { search.search( "user", searchFields, null ); fail( "A RepositorySearchExcecption should have been thrown." ); } - catch( RepositorySearchException e ) + catch ( RepositorySearchException e ) { assertEquals( "Repositories cannot be null.", e.getMessage() ); } } - + + @Test public void testAdvancedSearchSearchFieldsAreNull() throws Exception { List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); - searchFields.setRepositories( selectedRepos ); + searchFields.setRepositories( selectedRepos ); try { @@ -555,58 +594,60 @@ public class NexusRepositorySearchTest archivaConfigControl.replay(); search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - + fail( "A RepositorySearchExcecption should have been thrown." ); } - catch( RepositorySearchException e ) + catch ( RepositorySearchException e ) { assertEquals( "No search fields set.", e.getMessage() ); } } - + + @Test public void testAdvancedSearchSearchFieldsAreBlank() throws Exception { List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "" ); searchFields.setArtifactId( "" ); searchFields.setVersion( "" ); searchFields.setPackaging( "" ); searchFields.setClassName( "" ); - - searchFields.setRepositories( selectedRepos ); - + + searchFields.setRepositories( selectedRepos ); + try { archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 ); - + archivaConfigControl.replay(); - + search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - + fail( "A RepositorySearchExcecption should have been thrown." ); } - catch( RepositorySearchException e ) + catch ( RepositorySearchException e ) { assertEquals( "No search fields set.", e.getMessage() ); } } - + + @Test public void testAdvancedSearchAllSearchCriteriaSpecified() throws Exception { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); searchFields.setArtifactId( "archiva-test" ); @@ -614,122 +655,127 @@ public class NexusRepositorySearchTest searchFields.setPackaging( "jar" ); searchFields.setClassName( "org.apache.archiva.test.App" ); searchFields.setRepositories( selectedRepos ); - + archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config ); - + archivaConfigControl.replay(); - + SearchResults results = search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - + assertNotNull( results ); - + assertEquals( 1, results.getTotalHits() ); - + SearchResultHit hit = results.getHits().get( 0 ); assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "archiva-test", hit.getArtifactId() ); assertEquals( "2.0", hit.getVersions().get( 0 ) ); } - + + @Test public void testAdvancedSearchJarArtifacts() throws Exception { createIndexContainingMoreArtifacts(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setPackaging( "jar" ); - searchFields.setRepositories( selectedRepos ); - + searchFields.setRepositories( selectedRepos ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); - + archivaConfigControl.replay(); - + SearchResults results = search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - + assertNotNull( results ); - assertEquals( 5, results.getTotalHits() ); + assertEquals( 5, results.getTotalHits() ); } - + + @Test public void testAdvancedSearchWithIncorrectPackaging() throws Exception { createSimpleIndex(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); searchFields.setArtifactId( "archiva-test" ); searchFields.setVersion( "2.0" ); searchFields.setPackaging( "war" ); searchFields.setRepositories( selectedRepos ); - + archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.replay(); - + SearchResults results = search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - + assertNotNull( results ); assertEquals( 0, results.getTotalHits() ); } - + + @Test public void testAdvancedSearchClassname() throws Exception { createIndexContainingMoreArtifacts(); - + List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setClassName( "com.classname.search.App" ); - searchFields.setRepositories( selectedRepos ); - + searchFields.setRepositories( selectedRepos ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); - + archivaConfigControl.replay(); - + SearchResults results = search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - + assertNotNull( results ); assertEquals( 1, results.getTotalHits() ); - + SearchResultHit hit = results.getHits().get( 0 ); assertEquals( "com", hit.getGroupId() ); assertEquals( "classname-search", hit.getArtifactId() ); assertEquals( "1.0", hit.getVersions().get( 0 ) ); } - + + @Test public void testAdvancedSearchNoIndexFound() throws Exception { List selectedRepos = new ArrayList(); selectedRepos.add( TEST_REPO_1 ); - + SearchFields searchFields = new SearchFields(); searchFields.setGroupId( "org.apache.archiva" ); - searchFields.setRepositories( selectedRepos ); - + searchFields.setRepositories( selectedRepos ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); - + archivaConfigControl.replay(); - + SearchResults results = search.search( "user", searchFields, null ); - + archivaConfigControl.verify(); - - assertNotNull( results );assertEquals( 0, results.getTotalHits() ); + + assertNotNull( results ); + assertEquals( 0, results.getTotalHits() ); } } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/resources/spring-context.xml b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/spring-context.xml new file mode 100755 index 000000000..d2cff166d --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/resources/spring-context.xml @@ -0,0 +1,32 @@ + + + + + + + + \ No newline at end of file