From: James William Dumay Date: Sun, 14 Dec 2008 23:13:15 +0000 (+0000) Subject: Adding new tests, merging snapshot versions on display X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fabbab74b2bb33516f0cdf3f0f60f6b467d2b9e9;p=archiva.git 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 --- diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/filecontent/FileContentConverter.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/filecontent/FileContentConverter.java index 96c8295dd..cefc5e9ad 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/filecontent/FileContentConverter.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/filecontent/FileContentConverter.java @@ -54,8 +54,8 @@ public class FileContentConverter // 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() ); diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java index b9f35d7f2..21deef954 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java @@ -212,10 +212,17 @@ public class DefaultCrossRepositorySearch 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 class DefaultCrossRepositorySearch 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 @@ public class DefaultCrossRepositorySearch { // 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(); diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java index 3b411d15e..27c840b73 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java @@ -128,64 +128,95 @@ public class DefaultCrossRepositorySearchTest public void testSearchTerm_Org() 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 ); + SearchResults results = search.searchForTerm( "guest", Arrays.asList(TEST_DEFAULT_REPO_ID), "org", new SearchResultLimits(1) ); + + assertNotNull(results); + assertEquals(7, results.getHits().size()); } - public void testSearchInvalidTerm() - throws Exception - { + public void testSearchArtifactIdHasMoreWieghtThanGroupId() 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 ); + + List expectedHits = new ArrayList(); + 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 testSearchForClassesAndPackages() + + public void testSearchInvalidTerm() 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(), 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 expectedHits = new ArrayList(); +// +// // 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 expectedMethodSearchResults = new ArrayList(); +// +// // 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 @@ public class DefaultCrossRepositorySearchTest assertEquals( "Search Result Hits", expectedResults.length, results.getHits().size() ); } - private void assertSearchResults( String expectedRepos[], String expectedResults[], CrossRepositorySearch search, + private void assertSearchResults( String expectedRepos[], List expectedResults, CrossRepositorySearch search, String term, List previousSearchTerms, boolean bytecode ) throws Exception { @@ -271,8 +302,8 @@ public class DefaultCrossRepositorySearchTest SearchResults results = null; if( previousSearchTerms == null ) - { - if( bytecode ) + { + if( bytecode ) { results = search.searchForBytecode( "guest", selectedRepos, term, limits ); } @@ -292,9 +323,16 @@ public class DefaultCrossRepositorySearchTest // 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 ) diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/FileContentIndexPopulator.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/FileContentIndexPopulator.java index 12e2f4e6b..0bc1b3d64 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/FileContentIndexPopulator.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/FileContentIndexPopulator.java @@ -19,16 +19,18 @@ package org.apache.maven.archiva.indexer.search; * 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 class FileContentIndexPopulator 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 @@ public class FileContentIndexPopulator 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; } } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant-junit/1.6.5/ant-junit-1.6.5.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant-junit/1.6.5/ant-junit-1.6.5.pom new file mode 100644 index 000000000..c3710e60b --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant-junit/1.6.5/ant-junit-1.6.5.pom @@ -0,0 +1,6 @@ + + 4.0.0 + ant + ant-junit + 1.6.5 + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant-optional/1.5.1/ant-optional-1.5.1.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant-optional/1.5.1/ant-optional-1.5.1.pom new file mode 100644 index 000000000..9f34b9e3e --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant-optional/1.5.1/ant-optional-1.5.1.pom @@ -0,0 +1,6 @@ + + 4.0.0 + ant + ant-optional + 1.5.1 + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant/1.5.1/ant-1.5.1.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant/1.5.1/ant-1.5.1.pom new file mode 100644 index 000000000..5ef748993 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant/1.5.1/ant-1.5.1.pom @@ -0,0 +1,6 @@ + + 4.0.0 + ant + ant + 1.5.1 + \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant/1.5/ant-1.5.pom b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant/1.5/ant-1.5.pom new file mode 100644 index 000000000..b634f148d --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/managed-repository/ant/ant/1.5/ant-1.5.pom @@ -0,0 +1,6 @@ + + 4.0.0 + ant + ant + 1.5 + \ No newline at end of file diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java index 4965ff45a..a191faea2 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java @@ -46,6 +46,7 @@ import org.apache.maven.archiva.security.UserRepositories; 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 class SearchAction 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 class SearchAction 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 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 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 filterOutTimestampedSnapshots(List versions) + { + final List filtered = new ArrayList(); + 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 {