Adding new tests, merging snapshot versions on display

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-search-improvements@726573 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James William Dumay 2008-12-14 23:13:15 +00:00
parent 1d3352a6a4
commit fabbab74b2
9 changed files with 175 additions and 71 deletions

View File

@ -54,8 +54,8 @@ public Document convert( LuceneRepositoryContentRecord record )
// Artifact Reference
doc.addFieldTokenized( ArtifactKeys.GROUPID, filecontent.getArtifact().getGroupId() );
doc.addFieldExact( ArtifactKeys.GROUPID_EXACT, filecontent.getArtifact().getGroupId() );
doc.addFieldTokenized( ArtifactKeys.ARTIFACTID, filecontent.getArtifact().getArtifactId(), 1.5f );
doc.addFieldExact( ArtifactKeys.ARTIFACTID_EXACT, filecontent.getArtifact().getArtifactId(), 1.5f );
doc.addFieldTokenized( ArtifactKeys.ARTIFACTID, filecontent.getArtifact().getArtifactId()); //, 2.0f);
doc.addFieldExact( ArtifactKeys.ARTIFACTID_EXACT, filecontent.getArtifact().getArtifactId(), 2.0f);
doc.addFieldTokenized( ArtifactKeys.VERSION, filecontent.getArtifact().getVersion() );
doc.addFieldExact( ArtifactKeys.VERSION_EXACT, filecontent.getArtifact().getVersion() );
doc.addFieldTokenized( ArtifactKeys.TYPE, filecontent.getArtifact().getType() );

View File

@ -212,10 +212,17 @@ public SearchResults searchForTerm( String principal, List<String> selectedRepos
QueryParser parser = new FileContentHandlers().getQueryParser();
LuceneQuery query = null;
SearchResults results = null;
BooleanFilter duplicateFilter = new BooleanFilter();
DuplicateFilter artifactIdDuplicateFilter = new DuplicateFilter(FileContentKeys.ARTIFACTID_EXACT);
duplicateFilter.add(new FilterClause(artifactIdDuplicateFilter, BooleanClause.Occur.SHOULD));
DuplicateFilter groupIdDuplicateFilter = new DuplicateFilter(FileContentKeys.GROUPID_EXACT);
duplicateFilter.add(new FilterClause(groupIdDuplicateFilter, BooleanClause.Occur.SHOULD));
if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
{
query = new LuceneQuery( parser.parse( term ) );
results = searchAll( query, limits, indexes, null );
results = searchAll( query, limits, indexes, duplicateFilter );
}
else
{
@ -228,7 +235,8 @@ public SearchResults searchForTerm( String principal, List<String> selectedRepos
query = new LuceneQuery( booleanQuery );
Filter filter = new QueryWrapperFilter( parser.parse( term ) );
results = searchAll( query, limits, indexes, filter );
duplicateFilter.add(new FilterClause(filter, BooleanClause.Occur.SHOULD));
results = searchAll( query, limits, indexes, duplicateFilter );
}
results.getRepositories().addAll( this.localIndexedRepositories );
@ -272,23 +280,16 @@ private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits lim
{
// Create a multi-searcher for looking up the information.
searcher = new MultiSearcher( searchables );
BooleanFilter booleanFilter = new BooleanFilter();
DuplicateFilter artifactIdDuplicateFilter = new DuplicateFilter(FileContentKeys.ARTIFACTID_EXACT);
booleanFilter.add(new FilterClause(artifactIdDuplicateFilter, BooleanClause.Occur.MUST));
DuplicateFilter groupIdDuplicateFilter = new DuplicateFilter(FileContentKeys.GROUPID_EXACT);
booleanFilter.add(new FilterClause(groupIdDuplicateFilter, BooleanClause.Occur.MUST));
// Perform the search.
Hits hits = null;
if ( filter != null )
{
booleanFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));
hits = searcher.search( specificQuery, booleanFilter );
hits = searcher.search( specificQuery, filter );
}
else
{
hits = searcher.search( specificQuery, booleanFilter );
hits = searcher.search( specificQuery );
}
int hitCount = hits.length();

View File

@ -128,64 +128,95 @@ private CrossRepositorySearch lookupCrossRepositorySearch()
public void testSearchTerm_Org()
throws Exception
{
{
CrossRepositorySearch search = lookupCrossRepositorySearch();
SearchResults results = search.searchForTerm( "guest", Arrays.asList(TEST_DEFAULT_REPO_ID), "org", new SearchResultLimits(1) );
assertNotNull(results);
assertEquals(7, results.getHits().size());
}
public void testSearchArtifactIdHasMoreWieghtThanGroupId() throws Exception
{
CrossRepositorySearch search = lookupCrossRepositorySearch();
String expectedRepos[] = new String[] {
TEST_DEFAULT_REPO_ID
};
String expectedResults[] = new String[] {
"org","org2","org3","org4","org5","org6","org7"
};
assertSearchResults( expectedRepos, expectedResults, search, "org", null, false );
List<SearchResultHit> expectedHits = new ArrayList<SearchResultHit>();
SearchResultHit hit = new SearchResultHit();
hit.setGroupId("ant");
hit.setArtifactId("ant");
hit.setVersion("1.5.1");
expectedHits.add(hit);
hit = new SearchResultHit();
hit.setGroupId("ant");
hit.setArtifactId("ant");
hit.setVersion("1.5");
expectedHits.add(hit);
hit = new SearchResultHit();
hit.setGroupId("ant");
hit.setArtifactId("ant-optional");
hit.setVersion("1.5.1");
expectedHits.add(hit);
hit = new SearchResultHit();
hit.setGroupId("ant");
hit.setArtifactId("ant-junit");
hit.setVersion("1.6.5");
expectedHits.add(hit);
assertSearchResults( expectedRepos, expectedHits, search, "ant", null, false );
}
public void testSearchInvalidTerm()
throws Exception
{
{
CrossRepositorySearch search = lookupCrossRepositorySearch();
String expectedRepos[] = new String[] {
TEST_DEFAULT_REPO_ID
};
String expectedResults[] = new String[] {
// Nothing.
};
assertSearchResults( expectedRepos, expectedResults, search, "monosodium", null, false );
}
public void testSearchForClassesAndPackages()
throws Exception
{
CrossRepositorySearch search = lookupCrossRepositorySearch();
String expectedRepos[] = new String[] {
TEST_DEFAULT_REPO_ID
};
String expectedResults[] = new String[] {
"archiva-common-1.0.jar"
};
// class with packagename search
assertSearchResults( expectedRepos, expectedResults, search,
"org.apache.maven.archiva.common.utils.BaseFile", null, true );
// class name search
assertSearchResults( expectedRepos, expectedResults, search,
"BaseFile", null, true );
String expectedMethodSearchResults[] = new String[] {
"continuum-webapp-1.0.3-SNAPSHOT.war"
};
// method search
assertSearchResults( expectedRepos, expectedMethodSearchResults, search,
"org.apache.maven.continuum.web.action.BuildDefinitionAction.isBuildFresh", null, true );
assertSearchResults( expectedRepos, new ArrayList<SearchResultHit>(), search, "monosodium", null, false );
}
// public void testSearchForClassesAndPackages()
// throws Exception
// {
// CrossRepositorySearch search = lookupCrossRepositorySearch();
//
// String expectedRepos[] = new String[] {
// TEST_DEFAULT_REPO_ID
// };
//
//// String expectedResults[] = new String[] {
//// "archiva-common-1.0.jar"
//// };
//
// ArrayList<SearchResultHit> expectedHits = new ArrayList<SearchResultHit>();
//
// // class with packagename search
// assertSearchResults( expectedRepos, expectedHits, search,
// "org.apache.maven.archiva.common.utils.BaseFile", null, true );
// // class name search
// assertSearchResults( expectedRepos, expectedHits, search,
// "BaseFile", null, true );
//
//// String expectedMethodSearchResults[] = new String[] {
//// "continuum-webapp-1.0.3-SNAPSHOT.war"
//// };
//
// ArrayList<SearchResultHit> expectedMethodSearchResults = new ArrayList<SearchResultHit>();
//
// // method search
// assertSearchResults( expectedRepos, expectedMethodSearchResults, search,
// "org.apache.maven.continuum.web.action.BuildDefinitionAction.isBuildFresh", null, true );
// }
public void testExecuteFilteredSearch()
throws Exception
@ -258,7 +289,7 @@ private void assertFilteredSearchResults ( String expectedRepos[], String expect
assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
}
private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
private void assertSearchResults( String expectedRepos[], List<SearchResultHit> expectedResults, CrossRepositorySearch search,
String term, List<String> previousSearchTerms, boolean bytecode )
throws Exception
{
@ -271,8 +302,8 @@ private void assertSearchResults( String expectedRepos[], String expectedResults
SearchResults results = null;
if( previousSearchTerms == null )
{
if( bytecode )
{
if( bytecode )
{
results = search.searchForBytecode( "guest", selectedRepos, term, limits );
}
@ -292,9 +323,16 @@ private void assertSearchResults( String expectedRepos[], String expectedResults
// TODO: test the repository ids returned.
assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() );
// TODO: test the order of hits.
// TODO: test the value of the hits.
assertEquals( "Search Result Hits", expectedResults.size(), results.getHits().size() );
for (int i = 0; i < expectedResults.size(); i++)
{
final SearchResultHit expectedResult = expectedResults.get(i);
final SearchResultHit hit = results.getHits().get(i);
assertEquals("artifactid", expectedResult.getArtifactId(), hit.getArtifactId());
assertEquals("groupid", expectedResult.getGroupId(), hit.getGroupId());
assertEquals("version", expectedResult.getVersion(), hit.getVersion());
}
}
protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )

View File

@ -19,16 +19,18 @@
* under the License.
*/
import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
import org.apache.maven.archiva.model.ArchivaArtifact;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import junit.framework.AssertionFailedError;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.repository.content.DefaultPathParser;
import org.apache.maven.archiva.repository.content.PathParser;
import org.apache.maven.archiva.repository.layout.LayoutException;
/**
* FileContentIndexPopulator
@ -62,6 +64,11 @@ public Map<String, FileContentRecord> populate( File basedir )
map.put( "test-pom-1.0", createFileContentRecord( repoDir, prefix + "test-pom/1.0/test-pom-1.0.pom" ) );
map.put( "test-skin-1.0", createFileContentRecord( repoDir, prefix + "test-skin/1.0/test-skin-1.0.pom" ) );
map.put("ant-1.5.pom", createFileContentRecord(repoDir, "ant/ant/1.5/ant-1.5.pom"));
map.put("ant-1.5.1.pom", createFileContentRecord(repoDir, "ant/ant/1.5.1/ant-1.5.1.pom"));
map.put("ant-junit-1.6.5.pom", createFileContentRecord(repoDir, "ant/ant-junit/1.6.5/ant-junit-1.6.5.pom"));
map.put("ant-optional-1.5.1.pom", createFileContentRecord(repoDir, "ant/ant-optional/1.5.1/ant-optional-1.5.1.pom"));
return map;
}
@ -78,6 +85,18 @@ private FileContentRecord createFileContentRecord( File repoDir, String path )
record.setRepositoryId( "test-repo" );
record.setFilename( path );
PathParser pathParser = new DefaultPathParser();
try
{
ArtifactReference reference = pathParser.toArtifactReference(path);
ArchivaArtifact artifact = new ArchivaArtifact( reference );
record.setArtifact(artifact);
}
catch (LayoutException e)
{
throw new RuntimeException(e);
}
return record;
}
}

View File

@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.6.5</version>
</project>

View File

@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>ant</groupId>
<artifactId>ant-optional</artifactId>
<version>1.5.1</version>
</project>

View File

@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.5.1</version>
</project>

View File

@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.5</version>
</project>

View File

@ -46,6 +46,7 @@
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;
import org.apache.maven.archiva.common.utils.VersionUtil;
import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
import org.apache.maven.archiva.indexer.search.SearchResultHit;
@ -231,7 +232,8 @@ public String quickSearch()
return GlobalResults.ACCESS_TO_NO_REPOS;
}
if( SearchUtil.isBytecodeSearch( q ) )
final boolean isbytecodeSearch = SearchUtil.isBytecodeSearch( q );
if( isbytecodeSearch )
{
results = crossRepoSearch.searchForBytecode( getPrincipal(), selectedRepos, SearchUtil.removeBytecodeKeyword( q ), limits );
}
@ -274,21 +276,41 @@ public String quickSearch()
buildCompleteQueryString( q );
}
//Lets get the versions for the artifact we just found and display them
//Yes, this is in the lucene index but its more challenging to get them out when we are searching by project
for (SearchResultHit resultHit : results.getHits())
if (!isbytecodeSearch)
{
final List<String> versions = dao.query(new UniqueVersionConstraint(getObservableRepos(), resultHit.getGroupId(), resultHit.getArtifactId()));
if (versions != null && !versions.isEmpty())
//Lets get the versions for the artifact we just found and display them
//Yes, this is in the lucene index but its more challenging to get them out when we are searching by project
for (SearchResultHit resultHit : results.getHits())
{
resultHit.setVersion(null);
resultHit.setVersions(versions);
final List<String> versions = dao.query(new UniqueVersionConstraint(getObservableRepos(), resultHit.getGroupId(), resultHit.getArtifactId()));
if (versions != null && !versions.isEmpty())
{
resultHit.setVersion(null);
resultHit.setVersions(filterOutTimestampedSnapshots(versions));
}
}
}
return SUCCESS;
}
/**
* Remove timestamped snapshots from versions
*/
private static List<String> filterOutTimestampedSnapshots(List<String> versions)
{
final List<String> filtered = new ArrayList<String>();
for (final String version : versions)
{
final String baseVersion = VersionUtil.getBaseVersion(version);
if (!filtered.contains(baseVersion))
{
filtered.add(baseVersion);
}
}
return filtered;
}
public String findArtifact()
throws Exception
{