diff options
author | Maria Odea B. Ching <oching@apache.org> | 2009-10-08 10:01:32 +0000 |
---|---|---|
committer | Maria Odea B. Ching <oching@apache.org> | 2009-10-08 10:01:32 +0000 |
commit | 3390708c18565248333167c0282e0346761d01a4 (patch) | |
tree | 2e1738b8f9e38d84cc4c6973846043d46f98a177 /archiva-modules | |
parent | 8a116d5a94a76c55818ec4d98e7c75d45763e742 (diff) | |
download | archiva-3390708c18565248333167c0282e0346761d01a4.tar.gz archiva-3390708c18565248333167c0282e0346761d01a4.zip |
[MRM-1230] Type of artifact is allways "pom"
o for each search hit, get project model thru repositorybrowse#selectVersion(..) where complete project model is retrieved instead of just the bare artifact obj
o added tests
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@823109 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-modules')
2 files changed, 189 insertions, 47 deletions
diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java index 9cd26bd18..16974f12e 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImpl.java @@ -31,13 +31,17 @@ import org.apache.archiva.web.xmlrpc.api.SearchService; import org.apache.archiva.web.xmlrpc.api.beans.Artifact; import org.apache.archiva.web.xmlrpc.api.beans.Dependency; import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories; +import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.database.ArchivaDAO; import org.apache.maven.archiva.database.ArchivaDatabaseException; import org.apache.maven.archiva.database.ArtifactDAO; import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.ProjectModelDAO; import org.apache.maven.archiva.database.browsing.BrowsingResults; import org.apache.maven.archiva.database.browsing.RepositoryBrowsing; import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint; +import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint; +import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; import org.apache.maven.archiva.model.ArchivaArtifact; import org.apache.maven.archiva.model.ArchivaProjectModel; import org.slf4j.Logger; @@ -46,6 +50,14 @@ import org.slf4j.LoggerFactory; /** * SearchServiceImpl * + * quick/general text search which returns a list of artifacts + * query for an artifact based on a checksum + * query for all available versions of an artifact, sorted in version significance order + * query for all available versions of an artifact since a given date + * query for an artifact's direct dependencies + * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included) + * query for all artifacts that depend on a given artifact + * * @version $Id: SearchServiceImpl.java */ public class SearchServiceImpl @@ -69,24 +81,11 @@ public class SearchServiceImpl this.repoBrowsing = repoBrowsing; this.search = search; } - - /* - * quick/general text search which returns a list of artifacts - * query for an artifact based on a checksum - * query for all available versions of an artifact, sorted in version significance order - * query for all available versions of an artifact since a given date - * query for an artifact's direct dependencies - * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included) - * query for all artifacts that depend on a given artifact - */ - + + @SuppressWarnings( "unchecked" ) public List<Artifact> quickSearch( String queryString ) throws Exception { - // 1. check whether bytecode search or ordinary search - // 2. get observable repos - // 3. convert results to a list of Artifact objects - List<Artifact> artifacts = new ArrayList<Artifact>(); List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories(); SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); @@ -94,48 +93,70 @@ public class SearchServiceImpl results = search.search( "", observableRepos, queryString, limits, null ); - List<SearchResultHit> hits = results.getHits(); - - for( SearchResultHit hit : hits ) - { - ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO(); - - List<String> versions = hit.getVersions(); - if( versions != null ) + for ( SearchResultHit resultHit : results.getHits() ) + { + // double-check all versions as done in SearchAction + final List<String> versions = + (List<String>) archivaDAO.query( new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), + resultHit.getArtifactId() ) ); + if ( versions != null && !versions.isEmpty() ) { - for( String version : versions ) - { - for( String repo : observableRepos ) + resultHit.setVersion( null ); + resultHit.setVersions( filterTimestampedSnapshots( versions ) ); + } + + List<String> resultHitVersions = resultHit.getVersions(); + if( resultHitVersions != null ) + { + for( String version : resultHitVersions ) + { + try { - try - { - ArchivaArtifact pomArtifact = artifactDAO.getArtifact( - hit.getGroupId(), hit.getArtifactId(), version, null, "pom", repo ); - if( pomArtifact != null ) - { - Artifact artifact = new Artifact( pomArtifact.getModel().getRepositoryId(), pomArtifact.getGroupId(), pomArtifact.getArtifactId(), pomArtifact.getVersion(), - pomArtifact.getType() ); - //pomArtifact.getType(), pomArtifact.getModel().getWhenGathered() ); - artifacts.add( artifact ); - break; - } - } - catch( ObjectNotFoundException e ) + ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version ); + + Artifact artifact = null; + if( model == null ) { - log.debug( "Unable to find pom artifact : " + e.getMessage() ); + artifact = new Artifact( resultHit.getRepositoryId(), resultHit.getGroupId(), resultHit.getArtifactId(), version, "jar" ); } - catch( ArchivaDatabaseException e ) - { - log.debug( "Error occurred while getting pom artifact from database : " + e.getMessage() ); + else + { + artifact = new Artifact( resultHit.getRepositoryId(), model.getGroupId(), model.getArtifactId(), version, model.getPackaging() ); } - } + artifacts.add( artifact ); + } + catch( ObjectNotFoundException e ) + { + log.debug( "Unable to find pom artifact : " + e.getMessage() ); + } + catch( ArchivaDatabaseException e ) + { + log.debug( "Error occurred while getting pom artifact from database : " + e.getMessage() ); + } } } - } + } return artifacts; } + /** + * Remove timestamped snapshots from versions + */ + private static List<String> filterTimestampedSnapshots(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 List<Artifact> getArtifactByChecksum( String checksum ) throws Exception { diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java index 20170a6e6..e83c685cf 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/SearchServiceImplTest.java @@ -25,6 +25,10 @@ import java.util.Date; import java.util.List; import org.apache.archiva.indexer.search.RepositorySearch; +import org.apache.archiva.indexer.search.SearchResultHit; +import org.apache.archiva.indexer.search.SearchResultLimits; +import org.apache.archiva.indexer.search.SearchResults; +import org.apache.archiva.indexer.util.SearchUtil; import org.apache.archiva.web.xmlrpc.api.SearchService; import org.apache.archiva.web.xmlrpc.api.beans.Artifact; import org.apache.archiva.web.xmlrpc.api.beans.Dependency; @@ -35,6 +39,7 @@ import org.apache.maven.archiva.database.ObjectNotFoundException; import org.apache.maven.archiva.database.browsing.BrowsingResults; import org.apache.maven.archiva.database.browsing.RepositoryBrowsing; import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint; +import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; import org.apache.maven.archiva.model.ArchivaArtifact; import org.apache.maven.archiva.model.ArchivaProjectModel; import org.codehaus.plexus.spring.PlexusInSpringTestCase; @@ -79,20 +84,136 @@ public class SearchServiceImplTest userRepos = ( XmlRpcUserRepositories ) userReposControl.getMock(); archivaDAOControl = MockControl.createControl( ArchivaDAO.class ); + archivaDAOControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); archivaDAO = ( ArchivaDAO ) archivaDAOControl.getMock(); repoBrowsingControl = MockControl.createControl( RepositoryBrowsing.class ); repoBrowsing = ( RepositoryBrowsing ) repoBrowsingControl.getMock(); searchControl = MockControl.createControl( RepositorySearch.class ); + searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); search = ( RepositorySearch ) searchControl.getMock(); searchService = new SearchServiceImpl( userRepos, archivaDAO, repoBrowsing, search ); - artifactDAOControl = MockControl.createControl( ArtifactDAO.class ); + artifactDAOControl = MockControl.createControl( ArtifactDAO.class ); artifactDAO = ( ArtifactDAO ) artifactDAOControl.getMock(); } + // MRM-1230 + public void testQuickSearchModelPackagingIsUsed() + throws Exception + { + List<String> observableRepoIds = new ArrayList<String>(); + observableRepoIds.add( "repo1.mirror" ); + observableRepoIds.add( "public.releases" ); + + userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds ); + + SearchResults results = new SearchResults(); + List<String> versions = new ArrayList<String>(); + versions.add( "1.0" ); + + SearchResultHit resultHit = new SearchResultHit(); + resultHit.setGroupId( "org.apache.archiva" ); + resultHit.setArtifactId( "archiva-webapp" ); + resultHit.setRepositoryId("repo1.mirror"); + resultHit.setVersions( versions ); + + results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-webapp" ), resultHit ); + + SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); + + searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), results ); + + archivaDAOControl.expectAndReturn( archivaDAO.query( new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), + resultHit.getArtifactId() ) ), null ); + + ArchivaProjectModel model = new ArchivaProjectModel(); + model.setGroupId( "org.apache.archiva" ); + model.setArtifactId( "archiva-webapp" ); + model.setVersion( "1.0" ); + model.setPackaging( "war" ); + + repoBrowsingControl.expectAndReturn( repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-webapp", "1.0" ), model ); + + userReposControl.replay(); + searchControl.replay(); + repoBrowsingControl.replay(); + archivaDAOControl.replay(); + + List<Artifact> artifacts = searchService.quickSearch( "archiva" ); + + userReposControl.verify(); + searchControl.verify(); + repoBrowsingControl.verify(); + archivaDAOControl.verify(); + + assertNotNull( artifacts ); + assertEquals( 1, artifacts.size() ); + + Artifact hit = artifacts.get( 0 ); + assertEquals( "org.apache.archiva", hit.getGroupId() ); + assertEquals( "archiva-webapp", hit.getArtifactId() ); + assertEquals( "1.0", hit.getVersion() ); + assertEquals( "war", hit.getType() ); + assertEquals( "repo1.mirror", hit.getRepositoryId() ); + } + + // returned model is null! + public void testQuickSearchDefaultPackagingIsUsed() + throws Exception + { + List<String> observableRepoIds = new ArrayList<String>(); + observableRepoIds.add( "repo1.mirror" ); + observableRepoIds.add( "public.releases" ); + + userReposControl.expectAndReturn( userRepos.getObservableRepositories(), observableRepoIds ); + + SearchResults results = new SearchResults(); + List<String> versions = new ArrayList<String>(); + versions.add( "1.0" ); + + SearchResultHit resultHit = new SearchResultHit(); + resultHit.setRepositoryId( "repo1.mirror" ); + resultHit.setGroupId( "org.apache.archiva" ); + resultHit.setArtifactId( "archiva-test" ); + resultHit.setVersions( versions ); + + results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-test" ), resultHit ); + + SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); + + searchControl.expectAndDefaultReturn( search.search( "", observableRepoIds, "archiva", limits, null ), results ); + + archivaDAOControl.expectAndReturn( archivaDAO.query( new UniqueVersionConstraint( observableRepoIds, resultHit.getGroupId(), + resultHit.getArtifactId() ) ), null ); + + repoBrowsingControl.expectAndReturn( repoBrowsing.selectVersion( "", observableRepoIds, "org.apache.archiva", "archiva-test", "1.0" ), null ); + + userReposControl.replay(); + searchControl.replay(); + repoBrowsingControl.replay(); + archivaDAOControl.replay(); + + List<Artifact> artifacts = searchService.quickSearch( "archiva" ); + + userReposControl.verify(); + searchControl.verify(); + repoBrowsingControl.verify(); + archivaDAOControl.verify(); + + assertNotNull( artifacts ); + assertEquals( 1, artifacts.size() ); + + Artifact hit = artifacts.get( 0 ); + assertEquals( "org.apache.archiva", hit.getGroupId() ); + assertEquals( "archiva-test", hit.getArtifactId() ); + assertEquals( "1.0", hit.getVersion() ); + assertEquals( "jar", hit.getType() ); + assertEquals( "repo1.mirror", hit.getRepositoryId() ); + } + /* * quick/general text search which returns a list of artifacts * query for an artifact based on a checksum |