]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1300] move search and find artifact action over to metadata repository
authorBrett Porter <brett@apache.org>
Thu, 10 Dec 2009 08:22:22 +0000 (08:22 +0000)
committerBrett Porter <brett@apache.org>
Thu, 10 Dec 2009 08:22:22 +0000 (08:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@889130 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java
archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java
archiva-modules/archiva-web/archiva-webapp/src/main/resources/struts.xml
archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/results.jsp
archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java
archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/SearchActionTest.java
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java
archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java
archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java
archiva-modules/plugins/metadata-repository-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java

index 99571538b27545f114a34da339461cb255603421..ec05ceb5e08ed4307556ab66ec7b693e453e7eef 100644 (file)
@@ -136,6 +136,11 @@ public class TestMetadataRepository
         return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
 
+    public List<ArtifactMetadata> getArtifactsByChecksum( String repoId, String checksum )
+    {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
                                                       String projectVersion )
     {
index ac91cd9e9154e392de9401d685d2a54f8dd61d0a..27f046e50d14eecb796b1fb84659c5df8d788d39 100644 (file)
@@ -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<ArchivaArtifact> databaseResults;
+    private List<ArtifactMetadata> databaseResults;
     
     private int currentPage = 0;
     
@@ -126,7 +116,12 @@ public class SearchAction
     private Map<String, String> 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<String> versions =
-                (List<String>) 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<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 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<ArtifactMetadata>();
+        for ( String repoId : getObservableRepos() )
+        {
+            databaseResults.addAll( metadataRepository.getArtifactsByChecksum( repoId, q ) );
+        }
 
         if ( databaseResults.isEmpty() )
         {
@@ -471,7 +433,7 @@ public class SearchAction
         return results;
     }
 
-    public List<ArchivaArtifact> getDatabaseResults()
+    public List<ArtifactMetadata> 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;
+    }
 }
index b147fd6e51d00fcb507f5ed32ebc57fceef3fad2..3c9a6b5fd94be7d768cb01a1e192713aab4206fc 100644 (file)
       <result name="results">/WEB-INF/jsp/results.jsp</result>
       <result name="error">/WEB-INF/jsp/findArtifact.jsp</result>
       <result name="artifact" type="redirect">
-        /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()}
       </result>
     </action>    
 
index 5a92d85abc87456d0755901073fff639a4683e56..5b301f950a771e455becdc7857bd09ee37c353ff 100644 (file)
           <c:otherwise>
             <c:forEach items="${databaseResults}" var="artifactModel" varStatus="i">
               <c:choose>
-                <c:when test="${not empty (artifactModel.groupId)}">
+                <c:when test="${not empty (artifactModel.namespace)}">
                   <h3 class="artifact-title">
-                    <archiva:showArtifactTitle groupId="${artifactModel.groupId}"
-                                               artifactId="${artifactModel.artifactId}"
+                    <archiva:showArtifactTitle groupId="${artifactModel.namespace}"
+                                               artifactId="${artifactModel.project}"
                                                version="${artifactModel.version}"/>
 
                   </h3>
                   <p>
-                    <archiva:showArtifactLink  groupId="${artifactModel.groupId}"
-                                               artifactId="${artifactModel.artifactId}"
+                    <archiva:showArtifactLink  groupId="${artifactModel.namespace}"
+                                               artifactId="${artifactModel.project}"
                                                version="${artifactModel.version}"/>
 
                   </p>
index f12ad2a83ded0c61ffcc1cd3176606f3c73fc22b..11529eba254b37fe147723d5cd2f39c0a2c5290d 100644 (file)
@@ -176,6 +176,11 @@ public class TestMetadataRepository
         return Collections.singletonList( TEST_REPO );
     }
 
+    public List<ArtifactMetadata> getArtifactsByChecksum( String repoId, String checksum )
+    {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
                                                       String projectVersion )
     {
index 87dff73e1d7f0c40357201b536bdf53b16aa813c..6cfd42d5f38e8c4f9d664c3c93ae7d8cdd6932d8 100644 (file)
@@ -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<String> selectedRepos = new ArrayList<String>();
         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<String> versions = new ArrayList<String>();
-        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<String> parsed = new ArrayList<String>();
         parsed.add( "org" );
         parsed.add( "apache" );
-        
+
         List<String> selectedRepos = new ArrayList<String>();
         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<String> versions = new ArrayList<String>();
-        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<String> selectedRepos = new ArrayList<String>();
-        
+
         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<String> selectedRepos = new ArrayList<String>();
         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<String> managedRepos = new ArrayList<String>();
         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<String> selectedRepos = new ArrayList<String>();
         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<String> managedRepos = new ArrayList<String>();
         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<String> selectedRepos = new ArrayList<String>();
         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<String> managedRepos = new ArrayList<String>();
         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<String> selectedRepos = new ArrayList<String>();
         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<String> managedRepos = new ArrayList<String>();
-        
+
         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<String> managedRepos = new ArrayList<String>();
-        
+
         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<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
-        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<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
-        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<ArtifactMetadata> 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<ArchivaArtifact> dbResults = new ArrayList<ArchivaArtifact>();
-        
-        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.<ArtifactMetadata>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;
     }
 }
index 99571538b27545f114a34da339461cb255603421..ec05ceb5e08ed4307556ab66ec7b693e453e7eef 100644 (file)
@@ -136,6 +136,11 @@ public class TestMetadataRepository
         return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
 
+    public List<ArtifactMetadata> getArtifactsByChecksum( String repoId, String checksum )
+    {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
                                                       String projectVersion )
     {
index 44354f6909890a61d8b9ab3f57d7e892ad489251..f511bca2741c265e962afeb7aba01ea5c38c01ef 100644 (file)
@@ -62,4 +62,6 @@ public interface MetadataRepository
     List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime );
 
     Collection<String> getRepositories();
+
+    List<ArtifactMetadata> getArtifactsByChecksum( String repoId, String checksum );
 }
index a280f3c6c6ee32f217d4a3690976cadfaa5b6dfd..24c54e4c612d0bfb3ef89b1b8f42d6f9738759b4 100644 (file)
@@ -418,6 +418,44 @@ public class FileMetadataRepository
         return repoIds != null ? Arrays.asList( repoIds ) : Collections.<String>emptyList();
     }
 
+    public List<ArtifactMetadata> 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<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
+        for ( String ns : getRootNamespaces( repositoryId ) )
+        {
+            getArtifactsByChecksum( artifacts, repositoryId, ns, checksum );
+        }
+        return artifacts;
+    }
+
+    private void getArtifactsByChecksum( List<ArtifactMetadata> 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 );
index b004a21b1684ca6e1be8ccaae52e661d95e6d75f..35d595ff8680a5fa4591d208a650ae0bbd113c74 100644 (file)
@@ -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.<ArtifactMetadata>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;
     }