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
This commit is contained in:
Olivier Lamy 2011-05-27 09:34:43 +00:00
parent 63af39fba0
commit 7eebed1423
5 changed files with 387 additions and 260 deletions

View File

@ -66,6 +66,18 @@
<groupId>org.sonatype.nexus</groupId> <groupId>org.sonatype.nexus</groupId>
<artifactId>nexus-indexer</artifactId> <artifactId>nexus-indexer</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-plexus</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-guice</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-plexus-bridge</artifactId>
</dependency>
<dependency> <dependency>
<groupId>javax.inject</groupId> <groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId> <artifactId>javax.inject</artifactId>

View File

@ -19,15 +19,11 @@ package org.apache.archiva.indexer.search;
* under the License. * under the License.
*/ */
import java.io.File; import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import java.io.IOException; import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.archiva.indexer.util.SearchUtil; 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.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil; import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil;
import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration; 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.NexusIndexer;
import org.sonatype.nexus.index.context.IndexingContext; import org.sonatype.nexus.index.context.IndexingContext;
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; 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. * RepositorySearch implementation which uses the Nexus Indexer for searching.
*/ */
@Service( "nexusSearch" )
public class NexusRepositorySearch public class NexusRepositorySearch
implements RepositorySearch implements RepositorySearch
{ {
private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class ); private Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
private NexusIndexer indexer; private NexusIndexer indexer;
private ArchivaConfiguration archivaConfig; 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; this.archivaConfig = archivaConfig;
} }
@ -65,81 +72,81 @@ public class NexusRepositorySearch
public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits, public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
List<String> previousSearchTerms ) List<String> previousSearchTerms )
throws RepositorySearchException throws RepositorySearchException
{ {
addIndexingContexts( selectedRepos ); addIndexingContexts( selectedRepos );
// since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]* // 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 // resulting to more wildcard searches so we need to increase max clause count
BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE ); BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
BooleanQuery q = new BooleanQuery(); BooleanQuery q = new BooleanQuery();
if( previousSearchTerms == null || previousSearchTerms.isEmpty() ) if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
{ {
constructQuery( term, q ); constructQuery( term, q );
} }
else else
{ {
for( String previousTerm : previousSearchTerms ) for ( String previousTerm : previousSearchTerms )
{ {
BooleanQuery iQuery = new BooleanQuery(); BooleanQuery iQuery = new BooleanQuery();
constructQuery( previousTerm, iQuery ); constructQuery( previousTerm, iQuery );
q.add( iQuery, Occur.MUST ); q.add( iQuery, Occur.MUST );
} }
BooleanQuery iQuery = new BooleanQuery(); BooleanQuery iQuery = new BooleanQuery();
constructQuery( term, iQuery ); constructQuery( term, iQuery );
q.add( iQuery, Occur.MUST ); q.add( iQuery, Occur.MUST );
} }
return search( limits, q ); return search( limits, q );
} }
/** /**
* @see RepositorySearch#search(String, SearchFields, SearchResultLimits) * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
*/ */
public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits ) public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
throws RepositorySearchException throws RepositorySearchException
{ {
if( searchFields.getRepositories() == null ) if ( searchFields.getRepositories() == null )
{ {
throw new RepositorySearchException( "Repositories cannot be null." ); throw new RepositorySearchException( "Repositories cannot be null." );
} }
addIndexingContexts( searchFields.getRepositories() ); addIndexingContexts( searchFields.getRepositories() );
BooleanQuery q = new BooleanQuery(); 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 ); 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 ); 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 ); 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 ); 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 ); 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." ); throw new RepositorySearchException( "No search fields set." );
} }
return search( limits, q ); return search( limits, q );
} }
private SearchResults search( SearchResultLimits limits, BooleanQuery q ) private SearchResults search( SearchResultLimits limits, BooleanQuery q )
@ -149,14 +156,14 @@ public class NexusRepositorySearch
{ {
FlatSearchRequest request = new FlatSearchRequest( q ); FlatSearchRequest request = new FlatSearchRequest( q );
FlatSearchResponse response = indexer.searchFlat( request ); FlatSearchResponse response = indexer.searchFlat( request );
if( response == null || response.getTotalHits() == 0 ) if ( response == null || response.getTotalHits() == 0 )
{ {
SearchResults results = new SearchResults(); SearchResults results = new SearchResults();
results.setLimits( limits ); results.setLimits( limits );
return results; return results;
} }
return convertToSearchResults( response, limits ); return convertToSearchResults( response, limits );
} }
catch ( IOException e ) catch ( IOException e )
@ -166,20 +173,20 @@ public class NexusRepositorySearch
finally finally
{ {
Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts(); Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
for ( Map.Entry<String, IndexingContext> entry : indexingContexts.entrySet() ) for ( Map.Entry<String, IndexingContext> entry : indexingContexts.entrySet() )
{ {
try try
{ {
indexer.removeIndexingContext( entry.getValue(), false ); indexer.removeIndexingContext( entry.getValue(), false );
log.debug( "Indexing context '" + entry.getKey() + "' removed from search." ); log.debug( "Indexing context '" + entry.getKey() + "' removed from search." );
} }
catch ( IOException e ) catch ( IOException e )
{ {
log.warn( "IOException occurred while removing indexing content '" + entry.getKey() + "'." ); log.warn( "IOException occurred while removing indexing content '" + entry.getKey() + "'." );
continue; continue;
} }
} }
} }
} }
@ -189,24 +196,24 @@ public class NexusRepositorySearch
q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_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.VERSION, term ), Occur.SHOULD );
q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, 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<String> selectedRepos ) private void addIndexingContexts( List<String> selectedRepos )
{ {
for( String repo : selectedRepos ) for ( String repo : selectedRepos )
{ {
try try
{ {
Configuration config = archivaConfig.getConfiguration(); Configuration config = archivaConfig.getConfiguration();
ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo ); ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
if( repoConfig != null ) if ( repoConfig != null )
{ {
String indexDir = repoConfig.getIndexDir(); String indexDir = repoConfig.getIndexDir();
File indexDirectory = null; File indexDirectory = null;
if( indexDir != null && !"".equals( indexDir ) ) if ( indexDir != null && !"".equals( indexDir ) )
{ {
indexDirectory = new File( repoConfig.getIndexDir() ); indexDirectory = new File( repoConfig.getIndexDir() );
} }
@ -214,10 +221,11 @@ public class NexusRepositorySearch
{ {
indexDirectory = new File( repoConfig.getLocation(), ".indexer" ); indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
} }
IndexingContext context = IndexingContext context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(),
indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ), new File( repoConfig.getLocation() ),
indexDirectory, null, null, ArchivaNexusIndexerUtil.FULL_INDEX ); indexDirectory, null, null,
ArchivaNexusIndexerUtil.FULL_INDEX );
context.setSearchable( repoConfig.isScanned() ); context.setSearchable( repoConfig.isScanned() );
} }
else else
@ -226,12 +234,12 @@ public class NexusRepositorySearch
} }
} }
catch ( UnsupportedExistingLuceneIndexException e ) catch ( UnsupportedExistingLuceneIndexException e )
{ {
log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() ); log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
continue; continue;
} }
catch ( IOException e ) catch ( IOException e )
{ {
log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() ); log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
continue; continue;
} }
@ -239,10 +247,10 @@ public class NexusRepositorySearch
} }
private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits ) private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
{ {
SearchResults results = new SearchResults(); SearchResults results = new SearchResults();
Set<ArtifactInfo> artifactInfos = response.getResults(); Set<ArtifactInfo> artifactInfos = response.getResults();
for ( ArtifactInfo artifactInfo : artifactInfos ) for ( ArtifactInfo artifactInfo : artifactInfos )
{ {
String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId ); 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? // do we still need to set the repository id even though we're merging everything?
//hit.setRepositoryId( artifactInfo.repository ); //hit.setRepositoryId( artifactInfo.repository );
hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname ); hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
if( !hit.getVersions().contains( artifactInfo.version ) ) if ( !hit.getVersions().contains( artifactInfo.version ) )
{ {
hit.addVersion( artifactInfo.version ); hit.addVersion( artifactInfo.version );
} }
@ -269,33 +277,33 @@ public class NexusRepositorySearch
results.addHit( id, hit ); results.addHit( id, hit );
} }
results.setTotalHits( results.getHitsMap().size() ); results.setTotalHits( results.getHitsMap().size() );
results.setLimits( limits ); results.setLimits( limits );
if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES ) if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
{ {
return results; return results;
} }
else else
{ {
return paginate( results ); return paginate( results );
} }
} }
private SearchResults paginate( SearchResults results ) private SearchResults paginate( SearchResults results )
{ {
SearchResultLimits limits = results.getLimits(); SearchResultLimits limits = results.getLimits();
SearchResults paginated = new SearchResults(); SearchResults paginated = new SearchResults();
int fetchCount = limits.getPageSize(); int fetchCount = limits.getPageSize();
int offset = ( limits.getSelectedPage() * limits.getPageSize() ); int offset = ( limits.getSelectedPage() * limits.getPageSize() );
if( fetchCount > results.getTotalHits() ) if ( fetchCount > results.getTotalHits() )
{ {
fetchCount = results.getTotalHits(); fetchCount = results.getTotalHits();
} }
// Goto offset. // Goto offset.
if ( offset < results.getTotalHits() ) if ( offset < results.getTotalHits() )
{ {
@ -307,9 +315,9 @@ public class NexusRepositorySearch
{ {
break; break;
} }
SearchResultHit hit = results.getHits().get( ( offset + i ) ); SearchResultHit hit = results.getHits().get( ( offset + i ) );
if( hit != null ) if ( hit != null )
{ {
String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() ); String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
paginated.addHit( id, hit ); paginated.addHit( id, hit );
@ -319,10 +327,10 @@ public class NexusRepositorySearch
break; break;
} }
} }
} }
paginated.setTotalHits( results.getTotalHits() ); paginated.setTotalHits( results.getTotalHits() );
paginated.setLimits( limits ); paginated.setLimits( limits );
return paginated; return paginated;
} }
} }

View File

@ -1,16 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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">
<context:annotation-config/>
<context:component-scan base-package="org.apache.archiva.indexer.search"/>
<!--
<bean id="nexusSearch" class="org.apache.archiva.indexer.search.NexusRepositorySearch"> <bean id="nexusSearch" class="org.apache.archiva.indexer.search.NexusRepositorySearch">
<constructor-arg ref="nexusIndexer"/> <constructor-arg ref="nexusIndexer"/>
<constructor-arg ref="archivaConfiguration"/> <constructor-arg ref="archivaConfiguration#default"/>
</bean>
<bean id="logger" class="org.apache.maven.archiva.common.utils.Slf4JPlexusLogger">
<constructor-arg type="java.lang.Class"><value>org.sonatype.nexus.index.DefaultNexusIndexer</value></constructor-arg>
</bean> </bean>
-->
<bean id="logger" class="org.apache.maven.archiva.common.utils.Slf4JPlexusLogger">
<constructor-arg type="java.lang.Class"><value>org.sonatype.nexus.index.DefaultNexusIndexer</value></constructor-arg>
</bean>
<!-- <bean id="indexingContextMap" class="org.apache.archiva.indexer.IndexingContextMap"/> --> <!-- <bean id="indexingContextMap" class="org.apache.archiva.indexer.IndexingContextMap"/> -->
</beans> </beans>

View File

@ -19,29 +19,40 @@ package org.apache.archiva.indexer.search;
* under the License. * under the License.
*/ */
import java.io.File; import junit.framework.TestCase;
import java.io.IOException; import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil; import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil;
import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.MockControl; 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.artifact.IllegalArtifactCoordinateException;
import org.sonatype.nexus.index.ArtifactContext; import org.sonatype.nexus.index.ArtifactContext;
import org.sonatype.nexus.index.ArtifactContextProducer; import org.sonatype.nexus.index.ArtifactContextProducer;
import org.sonatype.nexus.index.IndexerEngine;
import org.sonatype.nexus.index.NexusIndexer; import org.sonatype.nexus.index.NexusIndexer;
import org.sonatype.nexus.index.context.DefaultIndexingContext; import org.sonatype.nexus.index.context.DefaultIndexingContext;
import org.sonatype.nexus.index.context.IndexingContext; import org.sonatype.nexus.index.context.IndexingContext;
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; 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 public class NexusRepositorySearchTest
extends PlexusInSpringTestCase extends TestCase
{ {
private RepositorySearch search; private RepositorySearch search;
@ -63,62 +74,76 @@ public class NexusRepositorySearchTest
private final static String TEST_REPO_2 = "nexus-search-test-repo-2"; private final static String TEST_REPO_2 = "nexus-search-test-repo-2";
@Override @Inject
protected void setUp() PlexusSisuBridge plexusSisuBridge;
@Before
public void setUp()
throws Exception throws Exception
{ {
super.setUp(); super.setUp();
indexer = (NexusIndexer) lookup( NexusIndexer.class ); indexer = plexusSisuBridge.lookup( NexusIndexer.class );
archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class ); archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock(); 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 = new Configuration();
config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) ); config.addManagedRepository( createRepositoryConfig( TEST_REPO_1 ) );
config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) ); 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() private void createSimpleIndex()
throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException
{ {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
createIndex( TEST_REPO_1, files ); createIndex( TEST_REPO_1, files );
} }
private void createIndexContainingMoreArtifacts() private void createIndexContainingMoreArtifacts()
throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException
{ {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ); + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ); + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) ); + "/org/apache/archiva/archiva-webapp/1.0/archiva-webapp-1.0.war" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/com/classname-search/1.0/classname-search-1.0.jar" ) ); + "/com/classname-search/1.0/classname-search-1.0.jar" ) );
createIndex( TEST_REPO_1, files ); createIndex( TEST_REPO_1, files );
} }
@ -136,13 +161,13 @@ public class NexusRepositorySearchTest
return repositoryConfig; return repositoryConfig;
} }
@Override @After
protected void tearDown() public void tearDown()
throws Exception throws Exception
{ {
FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) );
assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() ); assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() );
FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); 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<File> filesToBeIndexed ) private void createIndex( String repository, List<File> filesToBeIndexed )
throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException
{ {
context = new DefaultIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" + context = new DefaultIndexingContext( repository, repository,
repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, new File( getBasedir(), "/target/test-classes/" + repository ),
ArchivaNexusIndexerUtil.FULL_INDEX, false ); new File( getBasedir(),
//indexer.addIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" + "/target/test-classes/" + repository + "/.indexer" ), null,
// repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, null, ArchivaNexusIndexerUtil.FULL_INDEX, false );
// NexusIndexer.FULL_INDEX ); //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 ); context.setSearchable( true );
//indexerEngine.beginIndexing( context ); //indexerEngine.beginIndexing( context );
@ -171,15 +198,16 @@ public class NexusRepositorySearchTest
context.close( false ); context.close( false );
//indexerEngine.endIndexing( context ); //indexerEngine.endIndexing( context );
//indexer.removeIndexingContext( context, false ); //indexer.removeIndexingContext( context, false );
assertTrue( new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() ); assertTrue( new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() );
} }
@Test
public void testQuickSearch() public void testQuickSearch()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
@ -199,7 +227,7 @@ public class NexusRepositorySearchTest
assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-search", hit.getArtifactId() ); assertEquals( "archiva-search", hit.getArtifactId() );
assertEquals( "1.0", hit.getVersions().get( 0 ) ); assertEquals( "1.0", hit.getVersions().get( 0 ) );
archivaConfigControl.reset(); archivaConfigControl.reset();
// search groupId // search groupId
@ -216,32 +244,34 @@ public class NexusRepositorySearchTest
//TODO: search for class & package names //TODO: search for class & package names
} }
// search for existing artifact using multiple keywords // search for existing artifact using multiple keywords
@Test
public void testQuickSearchWithMultipleKeywords() public void testQuickSearchWithMultipleKeywords()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null ); SearchResults results = search.search( "user", selectedRepos, "archiva search", null, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 0, results.getTotalHits() ); assertEquals( 0, results.getTotalHits() );
} }
@Test
public void testQuickSearchWithPagination() public void testQuickSearchWithPagination()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
@ -282,16 +312,17 @@ public class NexusRepositorySearchTest
assertEquals( limits, results.getLimits() ); assertEquals( limits, results.getLimits() );
} }
@Test
public void testArtifactFoundInMultipleRepositories() public void testArtifactFoundInMultipleRepositories()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
"/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
createIndex( TEST_REPO_2, files ); createIndex( TEST_REPO_2, files );
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
@ -323,14 +354,15 @@ public class NexusRepositorySearchTest
// TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found // TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
} }
@Test
public void testNoMatchFound() public void testNoMatchFound()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -343,12 +375,13 @@ public class NexusRepositorySearchTest
assertEquals( 0, results.getTotalHits() ); assertEquals( 0, results.getTotalHits() );
} }
@Test
public void testNoIndexFound() public void testNoIndexFound()
throws Exception throws Exception
{ {
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -356,16 +389,17 @@ public class NexusRepositorySearchTest
SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null ); SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
assertNotNull( results ); assertNotNull( results );
assertEquals( 0, results.getTotalHits() ); assertEquals( 0, results.getTotalHits() );
archivaConfigControl.verify(); archivaConfigControl.verify();
} }
@Test
public void testRepositoryNotFound() public void testRepositoryNotFound()
throws Exception throws Exception
{ {
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( "non-existing-repo" ); selectedRepos.add( "non-existing-repo" );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -373,21 +407,22 @@ public class NexusRepositorySearchTest
SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null ); SearchResults results = search.search( "user", selectedRepos, "org.apache.archiva", null, null );
assertNotNull( results ); assertNotNull( results );
assertEquals( 0, results.getTotalHits() ); assertEquals( 0, results.getTotalHits() );
archivaConfigControl.verify(); archivaConfigControl.verify();
} }
@Test
public void testSearchWithinSearchResults() public void testSearchWithinSearchResults()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
List<String> previousSearchTerms = new ArrayList<String>(); List<String> previousSearchTerms = new ArrayList<String>();
previousSearchTerms.add( "archiva-test" ); previousSearchTerms.add( "archiva-test" );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -398,34 +433,34 @@ public class NexusRepositorySearchTest
assertNotNull( results ); assertNotNull( results );
assertEquals( 1, results.getTotalHits() ); assertEquals( 1, results.getTotalHits() );
SearchResultHit hit = results.getHits().get( 0 ); SearchResultHit hit = results.getHits().get( 0 );
assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-test", hit.getArtifactId() ); assertEquals( "archiva-test", hit.getArtifactId() );
assertEquals( 1, hit.getVersions().size() ); assertEquals( 1, hit.getVersions().size() );
assertEquals( "1.0", hit.getVersions().get( 0 ) ); assertEquals( "1.0", hit.getVersions().get( 0 ) );
} }
// tests for advanced search // tests for advanced search
@Test
public void testAdvancedSearch() public void testAdvancedSearch()
throws Exception throws Exception
{ {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) ); + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2
"/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) ); + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
createIndex( TEST_REPO_2, files ); createIndex( TEST_REPO_2, files );
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_2 ); selectedRepos.add( TEST_REPO_2 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" ); searchFields.setGroupId( "org.apache.archiva" );
searchFields.setVersion( "1.0" ); searchFields.setVersion( "1.0" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -436,13 +471,14 @@ public class NexusRepositorySearchTest
assertNotNull( results ); assertNotNull( results );
assertEquals( 1, results.getTotalHits() ); assertEquals( 1, results.getTotalHits() );
SearchResultHit hit = results.getHits().get( 0 ); SearchResultHit hit = results.getHits().get( 0 );
assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-search", hit.getArtifactId() ); assertEquals( "archiva-search", hit.getArtifactId() );
assertEquals( "1.0", hit.getVersions().get( 0 ) ); assertEquals( "1.0", hit.getVersions().get( 0 ) );
} }
@Test
public void testAdvancedSearchWithPagination() public void testAdvancedSearchWithPagination()
throws Exception throws Exception
{ {
@ -450,16 +486,16 @@ public class NexusRepositorySearchTest
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" ); searchFields.setGroupId( "org.apache.archiva" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
// page 1 // page 1
SearchResultLimits limits = new SearchResultLimits( 0 ); SearchResultLimits limits = new SearchResultLimits( 0 );
limits.setPageSize( 1 ); limits.setPageSize( 1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -471,13 +507,13 @@ public class NexusRepositorySearchTest
assertNotNull( results ); assertNotNull( results );
assertEquals( 3, results.getTotalHits() ); assertEquals( 3, results.getTotalHits() );
assertEquals( 1, results.getHits().size() ); assertEquals( 1, results.getHits().size() );
// page 2 // page 2
archivaConfigControl.reset(); archivaConfigControl.reset();
limits = new SearchResultLimits( 1 ); limits = new SearchResultLimits( 1 );
limits.setPageSize( 1 ); limits.setPageSize( 1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -488,27 +524,28 @@ public class NexusRepositorySearchTest
assertNotNull( results ); assertNotNull( results );
assertEquals( 3, results.getTotalHits() ); 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 // MRM-981 - artifactIds with numeric characters aren't found in advanced search
@Test
public void testAdvancedSearchArtifactIdHasNumericChar() public void testAdvancedSearchArtifactIdHasNumericChar()
throws Exception throws Exception
{ {
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) ); + "/com/artifactid-numeric/1.0/artifactid-numeric-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1 + files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_1
"/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) ); + "/com/artifactid-numeric123/1.0/artifactid-numeric123-1.0.jar" ) );
createIndex( TEST_REPO_1, files ); createIndex( TEST_REPO_1, files );
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setArtifactId( "artifactid-numeric" ); searchFields.setArtifactId( "artifactid-numeric" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 );
archivaConfigControl.replay(); archivaConfigControl.replay();
@ -518,35 +555,37 @@ public class NexusRepositorySearchTest
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 2, results.getTotalHits() ); assertEquals( 2, results.getTotalHits() );
} }
@Test
public void testAdvancedSearchNoRepositoriesConfigured() public void testAdvancedSearchNoRepositoriesConfigured()
throws Exception throws Exception
{ {
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setArtifactId( "archiva" ); searchFields.setArtifactId( "archiva" );
searchFields.setRepositories( null ); searchFields.setRepositories( null );
try try
{ {
search.search( "user", searchFields, null ); search.search( "user", searchFields, null );
fail( "A RepositorySearchExcecption should have been thrown." ); fail( "A RepositorySearchExcecption should have been thrown." );
} }
catch( RepositorySearchException e ) catch ( RepositorySearchException e )
{ {
assertEquals( "Repositories cannot be null.", e.getMessage() ); assertEquals( "Repositories cannot be null.", e.getMessage() );
} }
} }
@Test
public void testAdvancedSearchSearchFieldsAreNull() public void testAdvancedSearchSearchFieldsAreNull()
throws Exception throws Exception
{ {
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
try try
{ {
@ -555,58 +594,60 @@ public class NexusRepositorySearchTest
archivaConfigControl.replay(); archivaConfigControl.replay();
search.search( "user", searchFields, null ); search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
fail( "A RepositorySearchExcecption should have been thrown." ); fail( "A RepositorySearchExcecption should have been thrown." );
} }
catch( RepositorySearchException e ) catch ( RepositorySearchException e )
{ {
assertEquals( "No search fields set.", e.getMessage() ); assertEquals( "No search fields set.", e.getMessage() );
} }
} }
@Test
public void testAdvancedSearchSearchFieldsAreBlank() public void testAdvancedSearchSearchFieldsAreBlank()
throws Exception throws Exception
{ {
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "" ); searchFields.setGroupId( "" );
searchFields.setArtifactId( "" ); searchFields.setArtifactId( "" );
searchFields.setVersion( "" ); searchFields.setVersion( "" );
searchFields.setPackaging( "" ); searchFields.setPackaging( "" );
searchFields.setClassName( "" ); searchFields.setClassName( "" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
try try
{ {
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 1 );
archivaConfigControl.replay(); archivaConfigControl.replay();
search.search( "user", searchFields, null ); search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
fail( "A RepositorySearchExcecption should have been thrown." ); fail( "A RepositorySearchExcecption should have been thrown." );
} }
catch( RepositorySearchException e ) catch ( RepositorySearchException e )
{ {
assertEquals( "No search fields set.", e.getMessage() ); assertEquals( "No search fields set.", e.getMessage() );
} }
} }
@Test
public void testAdvancedSearchAllSearchCriteriaSpecified() public void testAdvancedSearchAllSearchCriteriaSpecified()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" ); searchFields.setGroupId( "org.apache.archiva" );
searchFields.setArtifactId( "archiva-test" ); searchFields.setArtifactId( "archiva-test" );
@ -614,122 +655,127 @@ public class NexusRepositorySearchTest
searchFields.setPackaging( "jar" ); searchFields.setPackaging( "jar" );
searchFields.setClassName( "org.apache.archiva.test.App" ); searchFields.setClassName( "org.apache.archiva.test.App" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, null ); SearchResults results = search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 1, results.getTotalHits() ); assertEquals( 1, results.getTotalHits() );
SearchResultHit hit = results.getHits().get( 0 ); SearchResultHit hit = results.getHits().get( 0 );
assertEquals( "org.apache.archiva", hit.getGroupId() ); assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-test", hit.getArtifactId() ); assertEquals( "archiva-test", hit.getArtifactId() );
assertEquals( "2.0", hit.getVersions().get( 0 ) ); assertEquals( "2.0", hit.getVersions().get( 0 ) );
} }
@Test
public void testAdvancedSearchJarArtifacts() public void testAdvancedSearchJarArtifacts()
throws Exception throws Exception
{ {
createIndexContainingMoreArtifacts(); createIndexContainingMoreArtifacts();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setPackaging( "jar" ); searchFields.setPackaging( "jar" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, null ); SearchResults results = search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 5, results.getTotalHits() ); assertEquals( 5, results.getTotalHits() );
} }
@Test
public void testAdvancedSearchWithIncorrectPackaging() public void testAdvancedSearchWithIncorrectPackaging()
throws Exception throws Exception
{ {
createSimpleIndex(); createSimpleIndex();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" ); searchFields.setGroupId( "org.apache.archiva" );
searchFields.setArtifactId( "archiva-test" ); searchFields.setArtifactId( "archiva-test" );
searchFields.setVersion( "2.0" ); searchFields.setVersion( "2.0" );
searchFields.setPackaging( "war" ); searchFields.setPackaging( "war" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndDefaultReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, null ); SearchResults results = search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 0, results.getTotalHits() ); assertEquals( 0, results.getTotalHits() );
} }
@Test
public void testAdvancedSearchClassname() public void testAdvancedSearchClassname()
throws Exception throws Exception
{ {
createIndexContainingMoreArtifacts(); createIndexContainingMoreArtifacts();
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setClassName( "com.classname.search.App" ); searchFields.setClassName( "com.classname.search.App" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, null ); SearchResults results = search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results ); assertNotNull( results );
assertEquals( 1, results.getTotalHits() ); assertEquals( 1, results.getTotalHits() );
SearchResultHit hit = results.getHits().get( 0 ); SearchResultHit hit = results.getHits().get( 0 );
assertEquals( "com", hit.getGroupId() ); assertEquals( "com", hit.getGroupId() );
assertEquals( "classname-search", hit.getArtifactId() ); assertEquals( "classname-search", hit.getArtifactId() );
assertEquals( "1.0", hit.getVersions().get( 0 ) ); assertEquals( "1.0", hit.getVersions().get( 0 ) );
} }
@Test
public void testAdvancedSearchNoIndexFound() public void testAdvancedSearchNoIndexFound()
throws Exception throws Exception
{ {
List<String> selectedRepos = new ArrayList<String>(); List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 ); selectedRepos.add( TEST_REPO_1 );
SearchFields searchFields = new SearchFields(); SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" ); searchFields.setGroupId( "org.apache.archiva" );
searchFields.setRepositories( selectedRepos ); searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay(); archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, null ); SearchResults results = search.search( "user", searchFields, null );
archivaConfigControl.verify(); archivaConfigControl.verify();
assertNotNull( results );assertEquals( 0, results.getTotalHits() ); assertNotNull( results );
assertEquals( 0, results.getTotalHits() );
} }
} }

View File

@ -0,0 +1,32 @@
<?xml version="1.0"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
</beans>