From: Brett Porter Date: Thu, 10 Dec 2009 08:22:22 +0000 (+0000) Subject: [MRM-1300] move search and find artifact action over to metadata repository X-Git-Tag: archiva-1.4-M1~1017^2~78 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a08431ea1a03fcbaed17f2621f2a959cb281366b;p=archiva.git [MRM-1300] move search and find artifact action over to metadata repository git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@889130 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java index 99571538b..ec05ceb5e 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java @@ -136,6 +136,11 @@ public class TestMetadataRepository return null; //To change body of implemented methods use File | Settings | File Templates. } + public List getArtifactsByChecksum( String repoId, String checksum ) + { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + public Collection getArtifacts( String repoId, String namespace, String projectId, String projectVersion ) { 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 ac91cd9e9..27f046e50 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 @@ -26,23 +26,20 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import com.opensymphony.xwork2.Preparable; import org.apache.archiva.indexer.search.RepositorySearch; import org.apache.archiva.indexer.search.RepositorySearchException; import org.apache.archiva.indexer.search.SearchFields; 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.metadata.model.ArtifactMetadata; +import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.database.ArchivaDAO; -import org.apache.maven.archiva.database.ArtifactDAO; -import org.apache.maven.archiva.database.Constraint; -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.security.AccessDeniedException; import org.apache.maven.archiva.security.ArchivaSecurityException; import org.apache.maven.archiva.security.PrincipalNotFoundException; @@ -51,8 +48,6 @@ import org.apache.struts2.ServletActionContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; -import com.opensymphony.xwork2.Preparable; - /** * Search all indexed fields by the given criteria. * @@ -70,11 +65,6 @@ public class SearchAction private String q; - /** - * @plexus.requirement role-hint="jdo" - */ - private ArchivaDAO dao; - /** * The Search Results. */ @@ -89,7 +79,7 @@ public class SearchAction private static final String ARTIFACT = "artifact"; - private List databaseResults; + private List databaseResults; private int currentPage = 0; @@ -126,7 +116,12 @@ public class SearchAction private Map searchFields; private String infoMessage; - + + /** + * @plexus.requirement + */ + private MetadataRepository metadataRepository; + public boolean isFromResultsPage() { return fromResultsPage; @@ -322,43 +317,9 @@ 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 - - // TODO: do we still need to do this? all hits are already filtered in the NexusRepositorySearch - // before being returned as search results - for ( SearchResultHit resultHit : results.getHits() ) - { - final List versions = - (List) dao.query( new UniqueVersionConstraint( getObservableRepos(), resultHit.getGroupId(), - resultHit.getArtifactId() ) ); - if ( versions != null && !versions.isEmpty() ) - { - resultHit.setVersion( null ); - resultHit.setVersions( filterTimestampedSnapshots( versions ) ); - } - } - return SUCCESS; } - /** - * Remove timestamped snapshots from versions - */ - private static List filterTimestampedSnapshots(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 { @@ -370,10 +331,11 @@ public class SearchAction return INPUT; } - Constraint constraint = new ArtifactsByChecksumConstraint( q ); - - ArtifactDAO artifactDao = dao.getArtifactDAO(); - databaseResults = artifactDao.queryArtifacts( constraint ); + databaseResults = new ArrayList(); + for ( String repoId : getObservableRepos() ) + { + databaseResults.addAll( metadataRepository.getArtifactsByChecksum( repoId, q ) ); + } if ( databaseResults.isEmpty() ) { @@ -471,7 +433,7 @@ public class SearchAction return results; } - public List getDatabaseResults() + public List getDatabaseResults() { return databaseResults; } @@ -632,16 +594,6 @@ public class SearchAction this.nexusSearch = nexusSearch; } - public ArchivaDAO getDao() - { - return dao; - } - - public void setDao( ArchivaDAO dao ) - { - this.dao = dao; - } - public UserRepositories getUserRepositories() { return userRepositories; @@ -671,4 +623,9 @@ public class SearchAction { this.infoMessage = infoMessage; } + + public void setMetadataRepository( MetadataRepository metadataRepository ) + { + this.metadataRepository = metadataRepository; + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/resources/struts.xml b/archiva-modules/archiva-web/archiva-webapp/src/main/resources/struts.xml index b147fd6e5..3c9a6b5fd 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/resources/struts.xml +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/resources/struts.xml @@ -177,7 +177,7 @@ /WEB-INF/jsp/results.jsp /WEB-INF/jsp/findArtifact.jsp - /browse/${databaseResults.get(0).getGroupId()}/${databaseResults.get(0).getArtifactId()}/${databaseResults.get(0).getVersion()} + /browse/${databaseResults.get(0).getNamespace()}/${databaseResults.get(0).getProject()}/${databaseResults.get(0).getVersion()} diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp index 5a92d85ab..5b301f950 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp @@ -332,16 +332,16 @@ - +

-

-

diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java index f12ad2a83..11529eba2 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java @@ -176,6 +176,11 @@ public class TestMetadataRepository return Collections.singletonList( TEST_REPO ); } + public List getArtifactsByChecksum( String repoId, String checksum ) + { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + public Collection getArtifacts( String repoId, String namespace, String projectId, String projectVersion ) { diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/SearchActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/SearchActionTest.java index 87dff73e1..6cfd42d5f 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/SearchActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/SearchActionTest.java @@ -20,146 +20,121 @@ package org.apache.maven.archiva.web.action; */ import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import com.opensymphony.xwork2.Action; import org.apache.archiva.indexer.search.RepositorySearch; import org.apache.archiva.indexer.search.SearchFields; 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.metadata.model.ArtifactMetadata; +import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.database.ArchivaDAO; -import org.apache.maven.archiva.database.ArtifactDAO; -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.security.UserRepositories; import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.easymock.MockControl; -import com.opensymphony.xwork2.Action; - /** - * + * */ public class SearchActionTest - extends PlexusInSpringTestCase + extends PlexusInSpringTestCase { private SearchAction action; - - private MockControl archivaConfigControl; - - private ArchivaConfiguration archivaConfig; - - private MockControl daoControl; - - private ArchivaDAO dao; - + private MockControl userReposControl; - + private UserRepositories userRepos; - + private MockControl searchControl; - + private RepositorySearch search; - - private MockControl artifactDaoControl; - - private ArtifactDAO artifactDao; - + + private static final String TEST_CHECKSUM = "afbcdeaadbcffceabbba1"; + + private static final String TEST_REPO = "test-repo"; + + private static final String GUEST = "guest"; + @Override - protected void setUp() + protected void setUp() throws Exception { super.setUp(); - + action = new SearchAction(); - - archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class ); - archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock(); - - daoControl = MockControl.createControl( ArchivaDAO.class ); - daoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); - dao = ( ArchivaDAO ) daoControl.getMock(); - - userReposControl = MockControl.createControl( UserRepositories.class ); + + MockControl archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class ); + ArchivaConfiguration archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock(); + + userReposControl = MockControl.createControl( UserRepositories.class ); userRepos = ( UserRepositories ) userReposControl.getMock(); - + searchControl = MockControl.createControl( RepositorySearch.class ); searchControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); search = ( RepositorySearch ) searchControl.getMock(); - - artifactDaoControl = MockControl.createControl( ArtifactDAO.class ); - artifactDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER ); - artifactDao = ( ArtifactDAO ) artifactDaoControl.getMock(); - + action.setArchivaConfiguration( archivaConfig ); action.setUserRepositories( userRepos ); - action.setDao( dao ); action.setNexusSearch( search ); } - + @Override protected void tearDown() throws Exception { super.tearDown(); } - + // quick search... - + public void testQuickSearch() throws Exception - { + { action.setQ( "archiva" ); action.setCurrentPage( 0 ); action.setSearchResultsOnly( false ); action.setCompleteQueryString( "" ); - + List selectedRepos = new ArrayList(); selectedRepos.add( "internal" ); selectedRepos.add( "snapshots" ); - + SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() ); limits.setPageSize( 30 ); - + SearchResultHit hit = new SearchResultHit(); hit.setGroupId( "org.apache.archiva" ); hit.setArtifactId( "archiva-configuration" ); hit.setUrl( "url" ); hit.addVersion( "1.0" ); - hit.addVersion( "1.1" ); - + hit.addVersion( "1.1" ); + SearchResults results = new SearchResults(); results.setLimits( limits ); results.setTotalHits( 1 ); results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit ); - - List versions = new ArrayList(); - versions.add( "1.0" ); - versions.add( "1.1" ); - - userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 ); - + + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos ); + searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results ); - - daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions ); - + userReposControl.replay(); searchControl.replay(); - daoControl.replay(); - + action.setPrincipal( "user" ); String result = action.quickSearch(); - - assertEquals( Action.SUCCESS, result ); + + assertEquals( Action.SUCCESS, result ); assertEquals( 1, action.getTotalPages() ); assertEquals( 1, action.getResults().getTotalHits() ); - + userReposControl.verify(); searchControl.verify(); - daoControl.verify(); } public void testSearchWithinSearchResults() @@ -169,77 +144,69 @@ public class SearchActionTest action.setCurrentPage( 0 ); action.setSearchResultsOnly( true ); action.setCompleteQueryString( "org;apache" ); - + List parsed = new ArrayList(); parsed.add( "org" ); parsed.add( "apache" ); - + List selectedRepos = new ArrayList(); selectedRepos.add( "internal" ); selectedRepos.add( "snapshots" ); - + SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() ); limits.setPageSize( 30 ); - + SearchResultHit hit = new SearchResultHit(); hit.setGroupId( "org.apache.archiva" ); hit.setArtifactId( "archiva-configuration" ); hit.setUrl( "url" ); hit.addVersion( "1.0" ); - hit.addVersion( "1.1" ); - + hit.addVersion( "1.1" ); + SearchResults results = new SearchResults(); results.setLimits( limits ); results.setTotalHits( 1 ); results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit ); - - List versions = new ArrayList(); - versions.add( "1.0" ); - versions.add( "1.1" ); - - userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos, 2 ); - + + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos ); + searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, parsed ), results ); - - daoControl.expectAndReturn( dao.query( new UniqueVersionConstraint( selectedRepos, hit.getGroupId(), hit.getArtifactId() ) ), versions ); - + userReposControl.replay(); searchControl.replay(); - daoControl.replay(); - + action.setPrincipal( "user" ); String result = action.quickSearch(); - + assertEquals( Action.SUCCESS, result ); assertEquals( "org;apache;archiva", action.getCompleteQueryString() ); assertEquals( 1, action.getTotalPages() ); assertEquals( 1, action.getResults().getTotalHits() ); - + userReposControl.verify(); searchControl.verify(); - daoControl.verify(); - } - + } + public void testQuickSearchUserHasNoAccessToAnyRepository() throws Exception { action.setQ( "archiva" ); action.setCurrentPage( 0 ); - + List selectedRepos = new ArrayList(); - + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos ); - + userReposControl.replay(); - + action.setPrincipal( "user" ); String result = action.quickSearch(); - - assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result ); - - userReposControl.verify(); + + assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result ); + + userReposControl.verify(); } - + public void testQuickSearchNoSearchHits() throws Exception { @@ -247,281 +214,299 @@ public class SearchActionTest action.setCurrentPage( 0 ); action.setSearchResultsOnly( false ); action.setCompleteQueryString( "" ); - + List selectedRepos = new ArrayList(); selectedRepos.add( "internal" ); selectedRepos.add( "snapshots" ); - + SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() ); limits.setPageSize( 30 ); - + SearchResults results = new SearchResults(); - + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos ); - + searchControl.expectAndReturn( search.search( "user", selectedRepos, "archiva", limits, null ), results ); - + userReposControl.replay(); searchControl.replay(); - + action.setPrincipal( "user" ); String result = action.quickSearch(); - - assertEquals( Action.INPUT, result ); - + + assertEquals( Action.INPUT, result ); + userReposControl.verify(); searchControl.verify(); } - + // advanced/filtered search... - + public void testAdvancedSearchOneRepository() throws Exception { List managedRepos = new ArrayList(); managedRepos.add( "internal" ); managedRepos.add( "snapshots" ); - + action.setRepositoryId( "internal" ); action.setManagedRepositoryList( managedRepos ); action.setCurrentPage( 0 ); action.setRowCount( 30 ); action.setGroupId( "org" ); - + SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() ); limits.setPageSize( 30 ); - + SearchResultHit hit = new SearchResultHit(); hit.setGroupId( "org.apache.archiva" ); hit.setArtifactId( "archiva-configuration" ); hit.setUrl( "url" ); hit.addVersion( "1.0" ); - hit.addVersion( "1.1" ); - + hit.addVersion( "1.1" ); + SearchResults results = new SearchResults(); results.setLimits( limits ); results.setTotalHits( 1 ); results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit ); - + List selectedRepos = new ArrayList(); selectedRepos.add( "internal" ); selectedRepos.add( "snapshots" ); - + SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos ); - + searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results ); - + searchControl.replay(); - + String result = action.filteredSearch(); - + assertEquals( Action.SUCCESS, result ); assertEquals( 1, action.getTotalPages() ); assertEquals( 1, action.getResults().getTotalHits() ); - + searchControl.verify(); } - + public void testAdvancedSearchAllRepositories() throws Exception - { + { List managedRepos = new ArrayList(); managedRepos.add( "internal" ); managedRepos.add( "snapshots" ); - + action.setRepositoryId( "all" ); action.setManagedRepositoryList( managedRepos ); action.setCurrentPage( 0 ); action.setRowCount( 30 ); action.setGroupId( "org" ); - + SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() ); limits.setPageSize( 30 ); - + SearchResultHit hit = new SearchResultHit(); hit.setGroupId( "org.apache.archiva" ); hit.setArtifactId( "archiva-configuration" ); hit.setUrl( "url" ); hit.addVersion( "1.0" ); - hit.addVersion( "1.1" ); - + hit.addVersion( "1.1" ); + SearchResults results = new SearchResults(); results.setLimits( limits ); results.setTotalHits( 1 ); results.addHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-configuration" ), hit ); - + List selectedRepos = new ArrayList(); selectedRepos.add( "internal" ); - + SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos ); - + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( "user" ), selectedRepos ); - + searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results ); - + searchControl.replay(); userReposControl.replay(); action.setPrincipal( "user" ); String result = action.filteredSearch(); - + assertEquals( Action.SUCCESS, result ); assertEquals( 1, action.getTotalPages() ); assertEquals( 1, action.getResults().getTotalHits() ); - + searchControl.verify(); userReposControl.verify(); } - + public void testAdvancedSearchNoSearchHits() throws Exception { List managedRepos = new ArrayList(); managedRepos.add( "internal" ); managedRepos.add( "snapshots" ); - + action.setRepositoryId( "internal" ); action.setManagedRepositoryList( managedRepos ); action.setCurrentPage( 0 ); action.setRowCount( 30 ); action.setGroupId( "org" ); - + SearchResultLimits limits = new SearchResultLimits( action.getCurrentPage() ); limits.setPageSize( 30 ); - + SearchResults results = new SearchResults(); - + List selectedRepos = new ArrayList(); selectedRepos.add( "internal" ); selectedRepos.add( "snapshots" ); - + SearchFields searchFields = new SearchFields( "org", null, null, null, null, selectedRepos ); - + searchControl.expectAndReturn( search.search( "user", searchFields, limits ), results ); - + searchControl.replay(); - + String result = action.filteredSearch(); - + assertEquals( Action.INPUT, result ); assertFalse( action.getActionErrors().isEmpty() ); assertEquals( "No results found",( String ) action.getActionErrors().iterator().next() ); - + searchControl.verify(); } - + public void testAdvancedSearchUserHasNoAccessToAnyRepository() throws Exception { List managedRepos = new ArrayList(); - + action.setGroupId( "org.apache.archiva" ); action.setManagedRepositoryList( managedRepos ); - + String result = action.filteredSearch(); - + assertEquals( GlobalResults.ACCESS_TO_NO_REPOS, result ); } - + public void testAdvancedSearchNoSpecifiedCriteria() throws Exception { List managedRepos = new ArrayList(); - + action.setManagedRepositoryList( managedRepos ); - + String result = action.filteredSearch(); - + assertEquals( Action.INPUT, result ); assertFalse( action.getActionErrors().isEmpty() ); assertEquals( "Advanced Search - At least one search criteria must be provided.", (String) action.getActionErrors().iterator().next() ); } - + // find artifact.. - public void testFindArtifactWithOneHit() throws Exception { - action.setQ( "afbcdeaadbcffceabbba1" ); - - List dbResults = new ArrayList(); - dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0", - null, "jar", "test-repo" ) ); - - daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao ); - - artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults ); - - daoControl.replay(); - artifactDaoControl.replay(); - + action.setQ( TEST_CHECKSUM ); + + MockControl control = MockControl.createControl( MetadataRepository.class ); + MetadataRepository metadataRepository = (MetadataRepository) control.getMock(); + action.setMetadataRepository( metadataRepository ); + + ArtifactMetadata artifact = createArtifact( "archiva-configuration", "1.0" ); + control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ), + Collections.singletonList( artifact ) ); + + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ), + Collections.singletonList( TEST_REPO ) ); + + control.replay(); + userReposControl.replay(); + String result = action.findArtifact(); assertEquals( "artifact", result ); - assertFalse( action.getDatabaseResults().isEmpty() ); assertEquals( 1, action.getDatabaseResults().size() ); - - daoControl.verify(); - artifactDaoControl.verify(); + assertEquals( artifact, action.getDatabaseResults().get( 0 ) ); + + control.verify(); + userReposControl.verify(); } - + public void testFindArtifactWithMultipleHits() throws Exception { - action.setQ( "afbcdeaadbcffceabbba1" ); - - List dbResults = new ArrayList(); - dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-configuration", "1.0", - null, "jar", "test-repo" ) ); - dbResults.add( new ArchivaArtifact( "org.apache.archiva", "archiva-indexer", "1.0", - null, "jar", "test-repo" ) ); - - daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao ); - - artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults ); - - daoControl.replay(); - artifactDaoControl.replay(); - + action.setQ( TEST_CHECKSUM ); + + MockControl control = MockControl.createControl( MetadataRepository.class ); + MetadataRepository metadataRepository = (MetadataRepository) control.getMock(); + action.setMetadataRepository( metadataRepository ); + + List artifacts = Arrays.asList( createArtifact( "archiva-configuration", "1.0" ), + createArtifact( "archiva-indexer", "1.0" ) ); + control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ), artifacts ); + + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ), + Collections.singletonList( TEST_REPO ) ); + + control.replay(); + userReposControl.replay(); + String result = action.findArtifact(); assertEquals( "results", result ); assertFalse( action.getDatabaseResults().isEmpty() ); assertEquals( 2, action.getDatabaseResults().size() ); - - daoControl.verify(); - artifactDaoControl.verify(); + + control.verify(); + userReposControl.verify(); } - + public void testFindArtifactNoChecksumSpecified() throws Exception { String result = action.findArtifact(); - + assertEquals( Action.INPUT, result ); assertFalse( action.getActionErrors().isEmpty() ); assertEquals( "Unable to search for a blank checksum", ( String ) action.getActionErrors().iterator().next() ); } - + public void testFindArtifactNoResults() throws Exception { - action.setQ( "afbcdeaadbcffceabbba1" ); - - List dbResults = new ArrayList(); - - daoControl.expectAndReturn( dao.getArtifactDAO(), artifactDao ); - - artifactDaoControl.expectAndReturn( artifactDao.queryArtifacts( new ArtifactsByChecksumConstraint( action.getQ() ) ), dbResults ); - - daoControl.replay(); - artifactDaoControl.replay(); - + action.setQ( TEST_CHECKSUM ); + + MockControl control = MockControl.createControl( MetadataRepository.class ); + MetadataRepository metadataRepository = (MetadataRepository) control.getMock(); + action.setMetadataRepository( metadataRepository ); + + control.expectAndReturn( metadataRepository.getArtifactsByChecksum( TEST_REPO, TEST_CHECKSUM ), + Collections.emptyList() ); + + userReposControl.expectAndReturn( userRepos.getObservableRepositoryIds( GUEST ), + Collections.singletonList( TEST_REPO ) ); + + control.replay(); + userReposControl.replay(); + String result = action.findArtifact(); assertEquals( Action.INPUT, result ); assertFalse( action.getActionErrors().isEmpty() ); assertEquals( "No results found", ( String )action.getActionErrors().iterator().next() ); - - daoControl.verify(); - artifactDaoControl.verify(); + + control.verify(); + userReposControl.verify(); + } + + private ArtifactMetadata createArtifact( String project, String version ) + { + ArtifactMetadata metadata = new ArtifactMetadata(); + metadata.setNamespace( "org.apache.archiva" ); + metadata.setProject( project ); + metadata.setVersion( version ); + metadata.setRepositoryId( TEST_REPO ); + metadata.setId( project + "-" + version + ".jar" ); + return metadata; } } diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java index 99571538b..ec05ceb5e 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java @@ -136,6 +136,11 @@ public class TestMetadataRepository return null; //To change body of implemented methods use File | Settings | File Templates. } + public List getArtifactsByChecksum( String repoId, String checksum ) + { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + public Collection getArtifacts( String repoId, String namespace, String projectId, String projectVersion ) { diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java index 44354f690..f511bca27 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java @@ -62,4 +62,6 @@ public interface MetadataRepository List getArtifactsByDateRange( String repoId, Date startTime, Date endTime ); Collection getRepositories(); + + List getArtifactsByChecksum( String repoId, String checksum ); } diff --git a/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java b/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java index a280f3c6c..24c54e4c6 100644 --- a/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java +++ b/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java @@ -418,6 +418,44 @@ public class FileMetadataRepository return repoIds != null ? Arrays.asList( repoIds ) : Collections.emptyList(); } + public List getArtifactsByChecksum( String repositoryId, String checksum ) + { + // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index + // of this information (eg. in Lucene, as before) + // alternatively, we could build a referential tree in the content repository, however it would need some levels + // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567) + + List artifacts = new ArrayList(); + for ( String ns : getRootNamespaces( repositoryId ) ) + { + getArtifactsByChecksum( artifacts, repositoryId, ns, checksum ); + } + return artifacts; + } + + private void getArtifactsByChecksum( List artifacts, String repositoryId, String ns, + String checksum ) + { + for ( String namespace : getNamespaces( repositoryId, ns ) ) + { + getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum ); + } + + for ( String project : getProjects( repositoryId, ns ) ) + { + for ( String version : getProjectVersions( repositoryId, ns, project ) ) + { + for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) ) + { + if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) ) + { + artifacts.add( artifact ); + } + } + } + } + } + private File getMetadataDirectory( String repositoryId, String facetId ) { return new File( this.directory, repositoryId + "/.meta/" + facetId ); diff --git a/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java b/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java index b004a21b1..35d595ff8 100644 --- a/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java +++ b/archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java @@ -61,6 +61,10 @@ public class FileMetadataRepositoryTest private static final String OTHER_REPO = "other-repo"; + private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d"; + + private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9"; + public void setUp() throws Exception { @@ -346,6 +350,60 @@ public class FileMetadataRepositoryTest assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) ); } + public void testGetArtifactsByChecksumSingleResultMd5() + { + repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE ); + repository.updateProject( TEST_REPO_ID, createProject() ); + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + + assertEquals( Collections.singletonList( artifact ), + repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ); + } + + public void testGetArtifactsByChecksumSingleResultSha1() + { + repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE ); + repository.updateProject( TEST_REPO_ID, createProject() ); + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + + assertEquals( Collections.singletonList( artifact ), + repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ); + } + + public void testGetArtifactsByChecksumMultipleResult() + { + repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE ); + + ProjectMetadata projectMetadata = createProject(); + repository.updateProject( TEST_REPO_ID, projectMetadata ); + ArtifactMetadata artifact1 = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 ); + + projectMetadata = createProject(); + String newProjectId = "another-project"; + projectMetadata.setId( newProjectId ); + repository.updateProject( TEST_REPO_ID, projectMetadata ); + ArtifactMetadata artifact2 = createArtifact(); + artifact2.setProject( newProjectId ); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 ); + + assertEquals( Arrays.asList( artifact2, artifact1 ), + repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ); + } + + public void testGetArtifactsByChecksumNoResult() + { + repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE ); + repository.updateProject( TEST_REPO_ID, createProject() ); + ArtifactMetadata artifact = createArtifact(); + repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact ); + + assertEquals( Collections.emptyList(), + repository.getArtifactsByChecksum( TEST_REPO_ID, "not a checksum" ) ); + } + private ProjectMetadata createProject() { return createProject( TEST_NAMESPACE ); @@ -374,6 +432,8 @@ public class FileMetadataRepositoryTest artifact.setRepositoryId( TEST_REPO_ID ); artifact.setFileLastModified( System.currentTimeMillis() ); artifact.setVersion( TEST_PROJECT_VERSION ); + artifact.setMd5( TEST_MD5 ); + artifact.setSha1( TEST_SHA1 ); return artifact; }