]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1283] write tests for artifact() method of ShowArtifactAction in preparation...
authorBrett Porter <brett@apache.org>
Fri, 20 Nov 2009 09:09:01 +0000 (09:09 +0000)
committerBrett Porter <brett@apache.org>
Fri, 20 Nov 2009 09:09:01 +0000 (09:09 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@882457 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java
archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractDeclarativeConstraint.java
archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/ShowArtifactAction.java
archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/security/UserRepositoriesStub.java
archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/ShowArtifactActionTest.java
archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ArchivaDAOStub.java

index 3f3e30c663edfe94d8851a2cb09b21cb0956e11e..d5bbfbf0ce98b39b31f4ac8654a2ecbb156b5944 100644 (file)
@@ -232,7 +232,7 @@ public class DefaultRepositoryBrowsing
                 {
                     if ( observableRepositoryIds.contains( artifact.getRepositoryId() ) )
                     {
-                        pomArtifact = artifacts.get( 0 );
+                        pomArtifact = artifact;
                         break;
                     }
                 }
index 6d42fb4e29392bab7b1c1a82fcf01ffd6156e60c..9202b7d8752a80536556da79b3b6ce1257849b06 100644 (file)
@@ -19,6 +19,8 @@ package org.apache.maven.archiva.database.constraints;
  * under the License.
  */
 
+import java.util.Arrays;
+
 import org.apache.maven.archiva.database.Constraint;
 import org.apache.maven.archiva.database.DeclarativeConstraint;
 
@@ -81,4 +83,59 @@ public abstract class AbstractDeclarativeConstraint
     {
        return range;
     }
+
+    @Override
+    public boolean equals( Object o )
+    {
+        if ( this == o )
+        {
+            return true;
+        }
+        if ( o == null || getClass() != o.getClass() )
+        {
+            return false;
+        }
+
+        AbstractDeclarativeConstraint that = (AbstractDeclarativeConstraint) o;
+
+        if ( !Arrays.equals( declImports, that.declImports ) )
+        {
+            return false;
+        }
+        if ( !Arrays.equals( declParams, that.declParams ) )
+        {
+            return false;
+        }
+        // Probably incorrect - comparing Object[] arrays with Arrays.equals
+        if ( !Arrays.equals( params, that.params ) )
+        {
+            return false;
+        }
+        if ( !Arrays.equals( range, that.range ) )
+        {
+            return false;
+        }
+        if ( sortDirection != null ? !sortDirection.equals( that.sortDirection ) : that.sortDirection != null )
+        {
+            return false;
+        }
+        if ( !Arrays.equals( variables, that.variables ) )
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        int result = declImports != null ? Arrays.hashCode( declImports ) : 0;
+        result = 31 * result + ( declParams != null ? Arrays.hashCode( declParams ) : 0 );
+        result = 31 * result + ( variables != null ? Arrays.hashCode( variables ) : 0 );
+        result = 31 * result + ( params != null ? Arrays.hashCode( params ) : 0 );
+        result = 31 * result + ( range != null ? Arrays.hashCode( range ) : 0 );
+        result = 31 * result + ( sortDirection != null ? sortDirection.hashCode() : 0 );
+        return result;
+    }
 }
index be7ba5d3a9a81b5bda7d81ef4cf31cde178b4fb2..18facd580ac1b20b0d474b7477ede2c92b05df52 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.maven.archiva.web.action;
 import java.util.Collections;
 import java.util.List;
 
+import com.opensymphony.xwork2.Validateable;
 import org.apache.commons.lang.StringUtils;
 import org.apache.maven.archiva.common.utils.VersionUtil;
 import org.apache.maven.archiva.database.ArchivaDatabaseException;
@@ -35,8 +36,6 @@ import org.apache.maven.archiva.security.ArchivaSecurityException;
 import org.apache.maven.archiva.security.PrincipalNotFoundException;
 import org.apache.maven.archiva.security.UserRepositories;
 
-import com.opensymphony.xwork2.Validateable;
-
 /**
  * Browse the repository. 
  * 
@@ -60,6 +59,8 @@ public class ShowArtifactAction
      */
     private UserRepositories userRepositories;
     
+    /* .\ Exposed Output Objects \.__________________________________ */
+
     private String groupId;
 
     private String artifactId;
@@ -68,8 +69,6 @@ public class ShowArtifactAction
 
     private String repositoryId;
 
-    /* .\ Exposed Output Objects \.__________________________________ */
-
     /**
      * The model of this versioned project.
      */
index 6558e38f974fa480e43e576e5d2086bb58f27228..c73e2090a66c380a2c2f15fa22d040be50849fdc 100644 (file)
@@ -19,7 +19,7 @@ package org.apache.maven.archiva.security;
  * under the License.
  */
 
-import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -30,6 +30,7 @@ import java.util.List;
 public class UserRepositoriesStub
     implements UserRepositories
 {
+    private List<String> repoIds = Collections.singletonList( "test-repo" );
 
     public void createMissingRepositoryRoles( String repoId )
         throws ArchivaSecurityException
@@ -41,12 +42,14 @@ public class UserRepositoriesStub
     public List<String> getObservableRepositoryIds( String principal )
         throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
     {
-        List<String> repoIds = new ArrayList<String>();
-        repoIds.add( "test-repo" );
-
         return repoIds;
     }
 
+    public void setObservableRepositoryIds( List<String> repoIds )
+    {
+        this.repoIds = repoIds; 
+    }
+
     public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
         throws PrincipalNotFoundException, ArchivaSecurityException
     {
index ba4344eb9999c3c11761dd1d9775d1770e926377..259121a083dea488f8eb405d71147df8a49deb9b 100644 (file)
@@ -19,15 +19,534 @@ package org.apache.maven.archiva.web.action;
  * under the License.
  */
 
-import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 import com.opensymphony.xwork2.Action;
+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.ProjectModelDAO;
+import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint;
+import org.apache.maven.archiva.model.ArchivaArtifact;
+import org.apache.maven.archiva.model.ArchivaArtifactModel;
+import org.apache.maven.archiva.model.ArchivaProjectModel;
+import org.apache.maven.archiva.model.CiManagement;
+import org.apache.maven.archiva.model.IssueManagement;
+import org.apache.maven.archiva.model.License;
+import org.apache.maven.archiva.model.Organization;
+import org.apache.maven.archiva.model.Scm;
+import org.apache.maven.archiva.model.VersionedReference;
+import org.apache.maven.archiva.security.UserRepositories;
+import org.apache.maven.archiva.security.UserRepositoriesStub;
+import org.apache.maven.archiva.web.action.admin.repositories.ArchivaDAOStub;
+import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import org.easymock.MockControl;
 
 public class ShowArtifactActionTest
     extends PlexusInSpringTestCase
 {
+    private static final String ACTION_HINT = "showArtifactAction";
+
+    private static final String TEST_GROUP_ID = "groupId";
+
+    private static final String TEST_ARTIFACT_ID = "artifactId";
+
+    private static final String TEST_VERSION = "version";
+
+    private static final String TEST_PACKAGING = "packaging";
+
+    private static final String TEST_ISSUE_URL = "http://jira.codehaus.org/browse/MRM";
+
+    private static final String TEST_ISSUE_SYSTEM = "jira";
+
+    private static final String TEST_CI_SYSTEM = "continuum";
+
+    private static final String TEST_CI_URL = "http://vmbuild.apache.org/";
+
+    private static final String TEST_URL = "url";
+
+    private static final String TEST_NAME = "name";
+
+    private static final String TEST_DESCRIPTION = "description";
+
+    private static final String TEST_PARENT_GROUP_ID = "parentGroupId";
+
+    private static final String TEST_PARENT_ARTIFACT_ID = "parentArtifactId";
+
+    private static final String TEST_PARENT_VERSION = "parentVersion";
+
+    private static final String TEST_ORGANIZATION_NAME = "organizationName";
+
+    private static final String TEST_ORGANIZATION_URL = "organizationUrl";
+
+    private static final String TEST_LICENSE_URL = "licenseUrl";
+
+    private static final String TEST_LICENSE_NAME = "licenseName";
+
+    private static final String TEST_LICENSE_URL_2 = "licenseUrl_2";
+
+    private static final String TEST_LICENSE_NAME_2 = "licenseName_2";
+
+    private static final String TEST_REPO = "test-repo";
+
+    private static final String TEST_SCM_CONNECTION = "scmConnection";
+
+    private static final String TEST_SCM_DEV_CONNECTION = "scmDevConnection";
+
+    private static final String TEST_SCM_URL = "scmUrl";
+
+    private ShowArtifactAction action;
+
+    private static final String TEST_SNAPSHOT_VERSION = "1.0-SNAPSHOT";
+
+    private static final String TEST_TS_SNAPSHOT_VERSION = "1.0-20091120.111111-1";
+
+    private ArchivaDAOStub archivaDao;
+
+    private static final List<String> ALL_TEST_SNAPSHOT_VERSIONS =
+        Arrays.asList( TEST_TS_SNAPSHOT_VERSION, "1.0-20091120.222222-2", "1.0-20091123.333333-3" );
+
+    private static final String OTHER_TEST_REPO = "first-repo";
+
     public void testInstantiation()
     {
-        assertFalse( lookup( Action.class, "showArtifactAction" ) == lookup( Action.class, "showArtifactAction" ) );
+        assertFalse( action == lookup( Action.class, ACTION_HINT ) );
+    }
+
+    public void testGetArtifactUniqueRelease()
+        throws ArchivaDatabaseException
+    {
+        List<ArchivaArtifact> artifacts =
+            Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
+        MockControl projectDaoMockControl =
+            createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+
+        setActionParameters();
+
+        String result = action.artifact();
+
+        assertActionSuccess( action, result );
+
+        artifactDaoMockControl.verify();
+        projectDaoMockControl.verify();
+
+        assertActionParameters( action );
+        ArchivaProjectModel model = action.getModel();
+        assertDefaultModel( model );
+
+        assertEquals( TEST_REPO, action.getRepositoryId() );
+
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+        assertNull( action.getSnapshotVersions() );
+    }
+
+    public void testGetArtifactUniqueSnapshot()
+        throws ArchivaDatabaseException
+    {
+        List<ArchivaArtifact> artifacts =
+            Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, TEST_SNAPSHOT_VERSION, 2 );
+        MockControl projectDaoMockControl =
+            createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_SNAPSHOT_VERSION ) );
+        archivaDao.setVersions( ALL_TEST_SNAPSHOT_VERSIONS );
+
+        action.setGroupId( TEST_GROUP_ID );
+        action.setArtifactId( TEST_ARTIFACT_ID );
+        action.setVersion( TEST_SNAPSHOT_VERSION );
+
+        String result = action.artifact();
+
+        assertActionSuccess( action, result );
+
+        artifactDaoMockControl.verify();
+        projectDaoMockControl.verify();
+
+        assertEquals( TEST_GROUP_ID, action.getGroupId() );
+        assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
+        assertEquals( TEST_SNAPSHOT_VERSION, action.getVersion() );
+        ArchivaProjectModel model = action.getModel();
+        assertDefaultModel( model, TEST_SNAPSHOT_VERSION );
+
+        assertEquals( TEST_REPO, action.getRepositoryId() );
+
+        assertEquals( ALL_TEST_SNAPSHOT_VERSIONS, action.getSnapshotVersions() );
+
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+    }
+
+    public void testGetArtifactUniqueSnapshotTimestamped()
+        throws ArchivaDatabaseException
+    {
+        List<ArchivaArtifact> artifacts =
+            Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_TS_SNAPSHOT_VERSION ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, TEST_TS_SNAPSHOT_VERSION, 2 );
+        MockControl projectDaoMockControl =
+            createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_TS_SNAPSHOT_VERSION ) );
+        archivaDao.setVersions( ALL_TEST_SNAPSHOT_VERSIONS );
+
+        action.setGroupId( TEST_GROUP_ID );
+        action.setArtifactId( TEST_ARTIFACT_ID );
+        action.setVersion( TEST_TS_SNAPSHOT_VERSION );
+
+        String result = action.artifact();
+
+        assertActionSuccess( action, result );
+
+        artifactDaoMockControl.verify();
+        projectDaoMockControl.verify();
+
+        assertEquals( TEST_GROUP_ID, action.getGroupId() );
+        assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
+        assertEquals( TEST_TS_SNAPSHOT_VERSION, action.getVersion() );
+        ArchivaProjectModel model = action.getModel();
+        assertDefaultModel( model, TEST_TS_SNAPSHOT_VERSION );
+
+        assertEquals( TEST_REPO, action.getRepositoryId() );
+
+        assertEquals( Arrays.asList( ALL_TEST_SNAPSHOT_VERSIONS.get( 1 ), ALL_TEST_SNAPSHOT_VERSIONS.get( 2 ) ),
+                      action.getSnapshotVersions() );
+
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+    }
+
+    public void testGetMissingProject()
+        throws ArchivaDatabaseException
+    {
+        MockControl artifactDaoMockControl = createArtifactDaoMock( Collections.<ArchivaArtifact>emptyList(), 1 );
+
+        setActionParameters();
+
+        String result = action.artifact();
+        assertError( result );
+
+        artifactDaoMockControl.verify();
+
+        assertActionParameters( action );
+        assertNoOutputFields();
+    }
+
+    public void testGetArtifactNoObservableRepos()
+        throws ArchivaDatabaseException
+    {
+        setObservableRepos( Collections.<String>emptyList() );
+
+        setActionParameters();
+
+        try
+        {
+            action.artifact();
+
+            // Actually, it'd be better to have an error:
+//            assertError( result );
+//            assertActionParameters( action );
+//            assertNoOutputFields();
+            fail();
+        }
+        catch ( ArchivaDatabaseException e )
+        {
+            assertTrue( true );
+        }
+    }
+
+    public void testGetArtifactNotInObservableRepos()
+        throws ArchivaDatabaseException
+    {
+        List<ArchivaArtifact> artifacts = Collections.singletonList(
+            createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, OTHER_TEST_REPO ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 1 );
+
+        setActionParameters();
+
+        String result = action.artifact();
+        assertError( result );
+
+        artifactDaoMockControl.verify();
+
+        assertActionParameters( action );
+        assertNoOutputFields();
+    }
+
+    public void testGetArtifactOnlySeenInSecondObservableRepo()
+        throws ArchivaDatabaseException
+    {
+        setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
+        List<ArchivaArtifact> artifacts =
+            Collections.singletonList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
+        MockControl projectDaoMockControl =
+            createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+
+        setActionParameters();
+
+        String result = action.artifact();
+
+        assertActionSuccess( action, result );
+
+        artifactDaoMockControl.verify();
+        projectDaoMockControl.verify();
+
+        assertActionParameters( action );
+        ArchivaProjectModel model = action.getModel();
+        assertDefaultModel( model );
+
+        assertEquals( TEST_REPO, action.getRepositoryId() );
+
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+        assertNull( action.getSnapshotVersions() );
+    }
+
+    public void testGetArtifactSeenInBothObservableRepo()
+        throws ArchivaDatabaseException
+    {
+        setObservableRepos( Arrays.asList( OTHER_TEST_REPO, TEST_REPO ) );
+        List<ArchivaArtifact> artifacts =
+            Arrays.asList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ),
+                           createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, OTHER_TEST_REPO ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
+        MockControl projectDaoMockControl =
+            createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+
+        setActionParameters();
+
+        String result = action.artifact();
+
+        assertActionSuccess( action, result );
+
+        artifactDaoMockControl.verify();
+        projectDaoMockControl.verify();
+
+        assertActionParameters( action );
+        ArchivaProjectModel model = action.getModel();
+        assertDefaultModel( model );
+
+        assertEquals( TEST_REPO, action.getRepositoryId() );
+
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+        assertNull( action.getSnapshotVersions() );
+    }
+
+    public void testGetArtifactCanOnlyObserveInOneOfTwoRepos()
+        throws ArchivaDatabaseException
+    {
+        setObservableRepos( Arrays.asList( TEST_REPO ) );
+        List<ArchivaArtifact> artifacts =
+            Arrays.asList( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, OTHER_TEST_REPO ),
+                           createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+        MockControl artifactDaoMockControl = createArtifactDaoMock( artifacts, 2 );
+        MockControl projectDaoMockControl =
+            createProjectDaoMock( createProjectModel( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
+
+        setActionParameters();
+
+        String result = action.artifact();
+
+        assertActionSuccess( action, result );
+
+        artifactDaoMockControl.verify();
+        projectDaoMockControl.verify();
+
+        assertActionParameters( action );
+        ArchivaProjectModel model = action.getModel();
+        assertDefaultModel( model );
+
+        assertEquals( TEST_REPO, action.getRepositoryId() );
+
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+        assertNull( action.getSnapshotVersions() );
+    }
+
+    private void assertNoOutputFields()
+    {
+        assertNull( action.getModel() );
+        assertNull( action.getDependees() );
+        assertNull( action.getDependencies() );
+        assertNull( action.getMailingLists() );
+        assertNull( action.getSnapshotVersions() );
+    }
+
+    private void assertError( String result )
+    {
+        assertEquals( Action.ERROR, result );
+        assertEquals( 1, action.getActionErrors().size() );
+    }
+
+    private void setObservableRepos( List<String> repoIds )
+    {
+        UserRepositoriesStub repos = (UserRepositoriesStub) lookup( UserRepositories.class );
+        repos.setObservableRepositoryIds( repoIds );
+    }
+
+    private void assertDefaultModel( ArchivaProjectModel model )
+    {
+        assertDefaultModel( model, TEST_VERSION );
+    }
+
+    private void assertDefaultModel( ArchivaProjectModel model, String version )
+    {
+        assertEquals( TEST_GROUP_ID, model.getGroupId() );
+        assertEquals( TEST_ARTIFACT_ID, model.getArtifactId() );
+        assertEquals( version, model.getVersion() );
+        assertEquals( TEST_PACKAGING, model.getPackaging() );
+        assertEquals( TEST_URL, model.getUrl() );
+        assertEquals( TEST_NAME, model.getName() );
+        assertEquals( TEST_DESCRIPTION, model.getDescription() );
+        assertEquals( TEST_ORGANIZATION_NAME, model.getOrganization().getName() );
+        assertEquals( TEST_ORGANIZATION_URL, model.getOrganization().getUrl() );
+        assertEquals( 2, model.getLicenses().size() );
+        License l = model.getLicenses().get( 0 );
+        assertEquals( TEST_LICENSE_NAME, l.getName() );
+        assertEquals( TEST_LICENSE_URL, l.getUrl() );
+        l = model.getLicenses().get( 1 );
+        assertEquals( TEST_LICENSE_NAME_2, l.getName() );
+        assertEquals( TEST_LICENSE_URL_2, l.getUrl() );
+        assertEquals( TEST_PARENT_GROUP_ID, model.getParentProject().getGroupId() );
+        assertEquals( TEST_PARENT_ARTIFACT_ID, model.getParentProject().getArtifactId() );
+        assertEquals( TEST_PARENT_VERSION, model.getParentProject().getVersion() );
+        assertEquals( TEST_ISSUE_SYSTEM, model.getIssueManagement().getSystem() );
+        assertEquals( TEST_ISSUE_URL, model.getIssueManagement().getUrl() );
+        assertEquals( TEST_CI_SYSTEM, model.getCiManagement().getSystem() );
+        assertEquals( TEST_CI_URL, model.getCiManagement().getUrl() );
+        assertEquals( TEST_SCM_CONNECTION, model.getScm().getConnection() );
+        assertEquals( TEST_SCM_DEV_CONNECTION, model.getScm().getDeveloperConnection() );
+        assertEquals( TEST_SCM_URL, model.getScm().getUrl() );
+    }
+
+    private void setActionParameters()
+    {
+        action.setGroupId( TEST_GROUP_ID );
+        action.setArtifactId( TEST_ARTIFACT_ID );
+        action.setVersion( TEST_VERSION );
+    }
+
+    private void assertActionParameters( ShowArtifactAction action )
+    {
+        assertEquals( TEST_GROUP_ID, action.getGroupId() );
+        assertEquals( TEST_ARTIFACT_ID, action.getArtifactId() );
+        assertEquals( TEST_VERSION, action.getVersion() );
+    }
+
+    private void assertActionSuccess( ShowArtifactAction action, String result )
+    {
+        assertEquals( Action.SUCCESS, result );
+        assertTrue( action.getActionErrors().isEmpty() );
+        assertTrue( action.getActionMessages().isEmpty() );
+    }
+
+    private ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
+    {
+        ArchivaProjectModel model = new ArchivaProjectModel();
+        model.setGroupId( groupId );
+        model.setArtifactId( artifactId );
+        model.setVersion( version );
+        model.setPackaging( TEST_PACKAGING );
+        model.setUrl( TEST_URL );
+        model.setName( TEST_NAME );
+        model.setDescription( TEST_DESCRIPTION );
+        VersionedReference parent = new VersionedReference();
+        parent.setGroupId( TEST_PARENT_GROUP_ID );
+        parent.setArtifactId( TEST_PARENT_ARTIFACT_ID );
+        parent.setVersion( TEST_PARENT_VERSION );
+        model.setParentProject( parent );
+        CiManagement ci = new CiManagement();
+        ci.setSystem( TEST_CI_SYSTEM );
+        ci.setUrl( TEST_CI_URL );
+        model.setCiManagement( ci );
+        IssueManagement issue = new IssueManagement();
+        issue.setSystem( TEST_ISSUE_SYSTEM );
+        issue.setUrl( TEST_ISSUE_URL );
+        model.setIssueManagement( issue );
+        Organization org = new Organization();
+        org.setName( TEST_ORGANIZATION_NAME );
+        org.setUrl( TEST_ORGANIZATION_URL );
+        model.setOrganization( org );
+        License l = new License();
+        l.setName( TEST_LICENSE_NAME );
+        l.setUrl( TEST_LICENSE_URL );
+        model.addLicense( l );
+        l = new License();
+        l.setName( TEST_LICENSE_NAME_2 );
+        l.setUrl( TEST_LICENSE_URL_2 );
+        model.addLicense( l );
+        Scm scm = new Scm();
+        scm.setConnection( TEST_SCM_CONNECTION );
+        scm.setDeveloperConnection( TEST_SCM_DEV_CONNECTION );
+        scm.setUrl( TEST_SCM_URL );
+        model.setScm( scm );
+        return model;
+    }
+
+    private MockControl createArtifactDaoMock( List<ArchivaArtifact> artifacts, int count )
+        throws ArchivaDatabaseException
+    {
+        return createArtifactDaoMock( artifacts, TEST_VERSION, count );
+    }
+
+    private MockControl createArtifactDaoMock( List<ArchivaArtifact> artifacts, String version, int count )
+        throws ArchivaDatabaseException
+    {
+        // testing deeper than normal with the mocks as we intend to replace RepositoryBrowsing, not just the database
+        // underneath it - those sections will be adjusted with a mock content repository later
+        MockControl control = MockControl.createNiceControl( ArtifactDAO.class );
+        ArtifactDAO dao = (ArtifactDAO) control.getMock();
+        archivaDao.setArtifactDao( dao );
+
+        ArtifactsRelatedConstraint c = new ArtifactsRelatedConstraint( TEST_GROUP_ID, TEST_ARTIFACT_ID, version );
+        dao.queryArtifacts( c );
+        control.setReturnValue( artifacts, count );
+
+        control.replay();
+        return control;
+    }
+
+    private MockControl createProjectDaoMock( ArchivaProjectModel project )
+        throws ArchivaDatabaseException
+    {
+        MockControl control = MockControl.createNiceControl( ProjectModelDAO.class );
+        ProjectModelDAO dao = (ProjectModelDAO) control.getMock();
+        archivaDao.setProjectDao( dao );
+
+        control.expectAndReturn(
+            dao.getProjectModel( project.getGroupId(), project.getArtifactId(), project.getVersion() ), project );
+
+        control.replay();
+        return control;
+    }
+
+    private ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
+    {
+        return createArtifact( groupId, artifactId, version, TEST_REPO );
+    }
+
+    private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String repoId )
+    {
+        ArchivaArtifactModel model = new ArchivaArtifactModel();
+        model.setGroupId( groupId );
+        model.setArtifactId( artifactId );
+        model.setVersion( version );
+        model.setRepositoryId( repoId );
+        return new ArchivaArtifact( model );
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+        action = (ShowArtifactAction) lookup( Action.class, ACTION_HINT );
+        archivaDao = (ArchivaDAOStub) lookup( ArchivaDAO.class, "jdo" );
     }
 }
index 7437342d89b804297e13a471144ff13cd26e33f0..560f53c3dde52538498b74f6ecd77871dcffecbb 100644 (file)
@@ -1,5 +1,10 @@
 package org.apache.maven.archiva.web.action.admin.repositories;
 
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 import org.apache.maven.archiva.database.ArchivaDAO;
 import org.apache.maven.archiva.database.ArtifactDAO;
@@ -7,14 +12,9 @@ import org.apache.maven.archiva.database.ProjectModelDAO;
 import org.apache.maven.archiva.database.RepositoryContentStatisticsDAO;
 import org.apache.maven.archiva.database.RepositoryProblemDAO;
 import org.apache.maven.archiva.database.SimpleConstraint;
+import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
 import org.apache.maven.archiva.model.RepositoryContentStatistics;
 
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-import junit.framework.Assert;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -44,19 +44,31 @@ public class ArchivaDAOStub
 {
     private ArchivaConfiguration configuration;
 
-    public List<RepositoryContentStatistics> query( SimpleConstraint constraint )
-    {
-        Assert.assertEquals( RepositoryContentStatistics.class, constraint.getResultClass() );
-        
+    private ArtifactDAO artifactDao;
 
-        List<RepositoryContentStatistics> stats = new ArrayList<RepositoryContentStatistics>();
-        for ( String repo : configuration.getConfiguration().getManagedRepositoriesAsMap().keySet() )
+    private ProjectModelDAO projectDao;
+
+    private List<String> versions;
+
+    public List<?> query( SimpleConstraint constraint )
+    {
+        if ( constraint instanceof UniqueVersionConstraint )
         {
-            RepositoryContentStatistics statistics = new RepositoryContentStatistics();
-            statistics.setRepositoryId( repo );
-            stats.add( statistics );
+            return versions;
+        }
+        else
+        {
+            Assert.assertEquals( RepositoryContentStatistics.class, constraint.getResultClass() );
+
+            List<RepositoryContentStatistics> stats = new ArrayList<RepositoryContentStatistics>();
+            for ( String repo : configuration.getConfiguration().getManagedRepositoriesAsMap().keySet() )
+            {
+                RepositoryContentStatistics statistics = new RepositoryContentStatistics();
+                statistics.setRepositoryId( repo );
+                stats.add( statistics );
+            }
+            return stats;
         }
-        return stats;
     }
 
     public Object save( Serializable obj )
@@ -66,22 +78,36 @@ public class ArchivaDAOStub
 
     public ArtifactDAO getArtifactDAO()
     {
-        throw new UnsupportedOperationException( "method not implemented for stub" );
+        return artifactDao;
     }
 
     public ProjectModelDAO getProjectModelDAO()
     {
-        throw new UnsupportedOperationException( "method not implemented for stub" );
+        return projectDao;
     }
 
     public RepositoryProblemDAO getRepositoryProblemDAO()
     {
         throw new UnsupportedOperationException( "method not implemented for stub" );
     }
-    
+
     public RepositoryContentStatisticsDAO getRepositoryContentStatisticsDAO()
     {
         throw new UnsupportedOperationException( "method not implemented for stub" );
     }
-    
+
+    public void setArtifactDao( ArtifactDAO artifactDao )
+    {
+        this.artifactDao = artifactDao;
+    }
+
+    public void setProjectDao( ProjectModelDAO projectDao )
+    {
+        this.projectDao = projectDao;
+    }
+
+    public void setVersions( List<String> versions )
+    {
+        this.versions = versions;
+    }
 }