* 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;
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;
}
public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
List<String> 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 )
{
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 )
finally
{
Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
-
+
for ( Map.Entry<String, IndexingContext> 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;
}
- }
+ }
}
}
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<String> 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() );
}
{
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
}
}
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;
}
}
private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
- {
+ {
SearchResults results = new SearchResults();
Set<ArtifactInfo> artifactInfos = response.getResults();
-
+
for ( ArtifactInfo artifactInfo : artifactInfos )
{
String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
// 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 );
}
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() )
{
{
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 );
break;
}
}
- }
+ }
paginated.setTotalHits( results.getTotalHits() );
paginated.setLimits( limits );
-
+
return paginated;
}
}
* 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;
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<File> files = new ArrayList<File>();
- 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<File> files = new ArrayList<File>();
- 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 );
}
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() );
private void createIndex( String repository, List<File> 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 );
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<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
assertEquals( "org.apache.archiva", hit.getGroupId() );
assertEquals( "archiva-search", hit.getArtifactId() );
assertEquals( "1.0", hit.getVersions().get( 0 ) );
-
+
archivaConfigControl.reset();
// search groupId
//TODO: search for class & package names
}
-
+
// search for existing artifact using multiple keywords
+ @Test
public void testQuickSearchWithMultipleKeywords()
throws Exception
- {
+ {
createSimpleIndex();
-
+
List<String> selectedRepos = new ArrayList<String>();
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<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
assertEquals( limits, results.getLimits() );
}
+ @Test
public void testArtifactFoundInMultipleRepositories()
throws Exception
{
createSimpleIndex();
-
+
List<File> files = new ArrayList<File>();
- 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<String> selectedRepos = new ArrayList<String>();
// TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found
}
+ @Test
public void testNoMatchFound()
throws Exception
{
createSimpleIndex();
-
+
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
-
+
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay();
assertEquals( 0, results.getTotalHits() );
}
+ @Test
public void testNoIndexFound()
throws Exception
{
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
-
+
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay();
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<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( "non-existing-repo" );
-
+
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay();
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<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
-
+
List<String> previousSearchTerms = new ArrayList<String>();
previousSearchTerms.add( "archiva-test" );
-
+
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.replay();
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<File> files = new ArrayList<File>();
- 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<String> selectedRepos = new ArrayList<String>();
+ List<String> selectedRepos = new ArrayList<String>();
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();
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
{
List<String> selectedRepos = new ArrayList<String>();
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();
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();
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<File> files = new ArrayList<File>();
- 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<String> selectedRepos = new ArrayList<String>();
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();
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<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
-
+
SearchFields searchFields = new SearchFields();
- searchFields.setRepositories( selectedRepos );
+ searchFields.setRepositories( selectedRepos );
try
{
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<String> selectedRepos = new ArrayList<String>();
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<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
-
+
SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" );
searchFields.setArtifactId( "archiva-test" );
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<String> selectedRepos = new ArrayList<String>();
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<String> selectedRepos = new ArrayList<String>();
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<String> selectedRepos = new ArrayList<String>();
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<String> selectedRepos = new ArrayList<String>();
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() );
}
}