Author: brett
Date: Tue Sep 11 04:40:28 2007
New Revision: 574556

URL: http://svn.apache.org/viewvc?rev=574556&view=rev
Log:
[MRM-462] start to add tests for configure repository action

Added:
    
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java
   (with props)
    
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.xml
   (with props)
Modified:
    maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/pom.xml
    
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryAction.java
    
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/include/repositoryForm.jspf

Modified: maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/pom.xml?rev=574556&r1=574555&r2=574556&view=diff
==============================================================================
--- maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/pom.xml (original)
+++ maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/pom.xml Tue Sep 
11 04:40:28 2007
@@ -236,6 +236,12 @@
       <version>${redback.version}</version>
       <scope>test</scope>
     </dependency>
+    <!-- Test Deps -->
+    <dependency>
+      <groupId>easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>

Modified: 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryAction.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryAction.java?rev=574556&r1=574555&r2=574556&view=diff
==============================================================================
--- 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryAction.java
 (original)
+++ 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryAction.java
 Tue Sep 11 04:40:28 2007
@@ -72,6 +72,7 @@
 
     private String repoid;
 
+    // TODO! consider removing? was just meant to be for delete...
     private String mode;
 
     /**
@@ -94,10 +95,9 @@
     {
         getLogger().info( ".confirm()" );
 
-        if ( operationAllowed( 
ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, getRepoid() ) )
+        if ( operationAllowed( 
ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, repoid ) )
         {
-            addActionError(
-                "You do not have the appropriate permissions to delete the " + 
getRepoid() + " repository." );
+            addActionError( "You do not have the appropriate permissions to 
delete the " + repoid + " repository." );
             return ERROR;
         }
 
@@ -108,17 +108,16 @@
     {
         getLogger().info( ".delete()" );
 
-        if ( operationAllowed( 
ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, getRepoid() ) )
+        if ( operationAllowed( 
ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, repoid ) )
         {
-            addActionError(
-                "You do not have the appropriate permissions to delete the " + 
getRepoid() + " repository." );
+            addActionError( "You do not have the appropriate permissions to 
delete the " + repoid + " repository." );
             return ERROR;
         }
 
         String result = SUCCESS;
         if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( 
mode, "delete-contents" ) )
         {
-            AdminRepositoryConfiguration existingRepository = getRepository();
+            AdminRepositoryConfiguration existingRepository = repository;
             if ( existingRepository == null )
             {
                 addActionError( "A repository with that id does not exist" );
@@ -129,8 +128,8 @@
 
             try
             {
-                removeRepository( getRepoid() );
-                result = saveConfiguration();
+                removeRepository( repoid );
+                result = saveConfiguration( 
archivaConfiguration.getConfiguration() );
 
                 if ( result.equals( SUCCESS ) )
                 {
@@ -171,9 +170,9 @@
         getLogger().info( ".edit()" );
         this.mode = "edit";
 
-        if ( operationAllowed( ArchivaRoleConstants.OPERATION_EDIT_REPOSITORY, 
getRepoid() ) )
+        if ( operationAllowed( ArchivaRoleConstants.OPERATION_EDIT_REPOSITORY, 
repoid ) )
         {
-            addActionError( "You do not have the appropriate permissions to 
edit the " + getRepoid() + " repository." );
+            addActionError( "You do not have the appropriate permissions to 
edit the " + repoid + " repository." );
             return ERROR;
         }
 
@@ -207,9 +206,8 @@
     }
 
     public void prepare()
-        throws Exception
     {
-        String id = getRepoid();
+        String id = repoid;
         if ( id == null )
         {
             this.repository = new AdminRepositoryConfiguration();
@@ -228,24 +226,23 @@
 
     public String save()
     {
-        String mode = getMode();
-        String repoId = getRepository().getId();
-        boolean containsError = false;
+        String repoId = repository.getId();
 
         getLogger().info( ".save(" + mode + ":" + repoId + ")" );
 
-        containsError = validateFields( mode );
+        Configuration configuration = archivaConfiguration.getConfiguration();
+        boolean containsError = validateFields( configuration );
 
         if ( containsError && StringUtils.equalsIgnoreCase( "add", mode ) )
         {
             return INPUT;
         }
-        else if ( containsError && StringUtils.equalsIgnoreCase( "edit", mode 
) )
+        else if ( containsError && StringUtils.equalsIgnoreCase( "edit", 
this.mode ) )
         {
             return ERROR;
         }
 
-        if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
+        if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
         {
             removeRepository( repoId );
         }
@@ -253,8 +250,8 @@
         String result;
         try
         {
-            addRepository( getRepository() );
-            result = saveConfiguration();
+            addRepository( repository, configuration );
+            result = saveConfiguration( configuration );
         }
         catch ( IOException e )
         {
@@ -280,12 +277,11 @@
         return result;
     }
 
-    private boolean validateFields( String mode )
+    private boolean validateFields( Configuration config )
     {
         boolean containsError = false;
         CronExpressionValidator validator = new CronExpressionValidator();
-        Configuration config = archivaConfiguration.getConfiguration();
-        String repoId = getRepository().getId();
+        String repoId = repository.getId();
 
         if ( StringUtils.isBlank( repoId ) )
         {
@@ -305,7 +301,7 @@
         // TODO! split
         if ( StringUtils.isBlank( repository.getLocation() ) )
         {
-            addFieldError( "repository.url", "You must enter a directory or 
url." );
+            addFieldError( "repository.url", "You must enter a directory." );
             containsError = true;
         }
         if ( StringUtils.isBlank( repository.getName() ) )
@@ -337,7 +333,7 @@
         this.repository = repository;
     }
 
-    private void addRepository( AdminRepositoryConfiguration repository )
+    private void addRepository( AdminRepositoryConfiguration repository, 
Configuration configuration )
         throws IOException, RoleManagerException
     {
         getLogger().info( ".addRepository(" + repository + ")" );
@@ -364,13 +360,13 @@
 */
 
         // TODO! others
-        archivaConfiguration.getConfiguration().addManagedRepository( 
repository );
+        configuration.addManagedRepository( repository );
 
         // TODO: double check these are configured on start up
+        // TODO: belongs in the business logic
         roleManager.createTemplatedRole( "archiva-repository-manager", 
repository.getId() );
 
         roleManager.createTemplatedRole( "archiva-repository-observer", 
repository.getId() );
-
     }
 
     private boolean operationAllowed( String permission, String repoid )
@@ -378,10 +374,9 @@
         ActionContext context = ActionContext.getContext();
         SecuritySession securitySession = (SecuritySession) context.get( 
SecuritySession.ROLE );
 
-        AuthorizationResult authzResult;
         try
         {
-            authzResult = securitySystem.authorize( securitySession, 
permission, repoid );
+            AuthorizationResult authzResult = securitySystem.authorize( 
securitySession, permission, repoid );
 
             return authzResult.isAuthorized();
         }
@@ -422,14 +417,14 @@
         getLogger().info( "removed user roles associated with repository " + 
existingRepository.getId() );
     }
 
-    private String saveConfiguration()
+    private String saveConfiguration( Configuration configuration )
         throws IOException, InvalidConfigurationException, RegistryException
     {
         getLogger().info( ".saveConfiguration()" );
 
         try
         {
-            archivaConfiguration.save( archivaConfiguration.getConfiguration() 
);
+            archivaConfiguration.save( configuration );
             addActionMessage( "Successfully saved configuration" );
         }
         catch ( IndeterminateConfigurationException e )
@@ -439,5 +434,15 @@
         }
 
         return SUCCESS;
+    }
+
+    public void setRoleManager( RoleManager roleManager )
+    {
+        this.roleManager = roleManager;
+    }
+
+    public void setArchivaConfiguration( ArchivaConfiguration 
archivaConfiguration )
+    {
+        this.archivaConfiguration = archivaConfiguration;
     }
 }

Modified: 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/include/repositoryForm.jspf
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/include/repositoryForm.jspf?rev=574556&r1=574555&r2=574556&view=diff
==============================================================================
--- 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/include/repositoryForm.jspf
 (original)
+++ 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/admin/include/repositoryForm.jspf
 Tue Sep 11 04:40:28 2007
@@ -20,16 +20,16 @@
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>
 
 <ww:textfield name="repository.name" label="Name" size="50" required="true"/>
-<ww:textfield name="repository.url" label="Directory or URL" size="50" 
required="true"/>  
+<ww:textfield name="repository.location" label="Directory" size="50" 
required="true"/>
 <ww:select list="[EMAIL PROTECTED]@{'default' : 'Maven 2.x Repository', 
'legacy' : 'Maven 1.x Repository'}"
            name="repository.layout" label="Type"/>
-<ww:textfield name="repository.refreshCronExpression" label="Cron" size="40" 
required="true" />
+<ww:textfield name="repository.refreshCronExpression" label="Cron" size="40" 
required="true"/>
 <ww:textfield name="repository.daysOlder" label="Repository Purge By Days 
Older Than" size="5"/>
 <ww:textfield name="repository.retentionCount" label="Repository Purge By 
Retention Count" size="5"/>
 <ww:checkbox name="repository.releases" value="repository.releases" 
label="Releases Included"/>
 <ww:checkbox name="repository.snapshots" value="repository.snapshots" 
label="Snapshots Included"/>
 <ww:checkbox name="repository.indexed" value="repository.indexed" 
label="Scannable"/>
 <ww:checkbox name="repository.deleteReleasedSnapshots" 
value="repository.deleteReleasedSnapshots"
-           label="Delete Released Snapshots"/>
+             label="Delete Released Snapshots"/>
 
  

Added: 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java?rev=574556&view=auto
==============================================================================
--- 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java
 (added)
+++ 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java
 Tue Sep 11 04:40:28 2007
@@ -0,0 +1,155 @@
+package org.apache.maven.archiva.web.action.admin.repositories;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import com.opensymphony.xwork.Action;
+import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.Configuration;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.redback.role.RoleManager;
+import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
+import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
+import org.easymock.MockControl;
+
+import java.util.Collections;
+
+/**
+ * Test the repositories action returns the correct data.
+ */
+public class ConfigureRepositoryActionTest
+    extends PlexusTestCase
+{
+    private ConfigureRepositoryAction action;
+
+    private RoleManager roleManager;
+
+    private MockControl roleManagerControl;
+
+    private MockControl archivaConfigurationControl;
+
+    private ArchivaConfiguration archivaConfiguration;
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        // TODO: purely to quiet logging - shouldn't be needed
+        String appserverBase = getTestFile( "target/appserver-base" 
).getAbsolutePath();
+        System.setProperty( "appserver.base", appserverBase );
+        action = (ConfigureRepositoryAction) lookup( Action.class.getName(), 
"configureRepositoryAction" );
+
+        archivaConfigurationControl = MockControl.createControl( 
ArchivaConfiguration.class );
+        archivaConfiguration = (ArchivaConfiguration) 
archivaConfigurationControl.getMock();
+        action.setArchivaConfiguration( archivaConfiguration );
+
+        roleManagerControl = MockControl.createControl( RoleManager.class );
+        roleManager = (RoleManager) roleManagerControl.getMock();
+        action.setRoleManager( roleManager );
+    }
+
+    public void testAddRepositoryInitialPage()
+        throws Exception
+    {
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( new Configuration() );
+        archivaConfigurationControl.replay();
+
+        action.prepare();
+        assertNull( action.getRepoid() );
+        assertNull( action.getMode() );
+        AdminRepositoryConfiguration configuration = action.getRepository();
+        assertNotNull( configuration );
+        assertNull( configuration.getId() );
+        // check all booleans are false
+        assertFalse( configuration.isDeleteReleasedSnapshots() );
+        assertFalse( configuration.isIndexed() );
+        assertFalse( configuration.isReleases() );
+        assertFalse( configuration.isSnapshots() );
+
+        String status = action.add();
+        assertEquals( Action.INPUT, status );
+
+        // check defaults
+        assertFalse( configuration.isDeleteReleasedSnapshots() );
+        assertTrue( configuration.isIndexed() );
+        assertTrue( configuration.isReleases() );
+        assertFalse( configuration.isSnapshots() );
+    }
+
+    public void testAddRepository()
+        throws Exception
+    {
+        String repoId = "repo-ident";
+        // TODO: should be in the business model
+        roleManager.createTemplatedRole( "archiva-repository-manager", repoId 
);
+        roleManager.createTemplatedRole( "archiva-repository-observer", repoId 
);
+
+        roleManagerControl.replay();
+
+        Configuration configuration = new Configuration();
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( configuration );
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( configuration );
+
+        archivaConfiguration.save( configuration );
+
+        archivaConfigurationControl.replay();
+
+        action.prepare();
+        action.setMode( "add" );
+        AdminRepositoryConfiguration repository = action.getRepository();
+        repository.setId( repoId );
+        repository.setName( "repo name" );
+        repository.setLocation( "location" );
+        repository.setLayout( "default" );
+        repository.setRefreshCronExpression( "* 0/5 * * * ?" );
+        repository.setDaysOlder( 31 );
+        repository.setRetentionCount( 20 );
+        repository.setReleases( true );
+        repository.setSnapshots( true );
+        repository.setIndexed( true );
+        repository.setDeleteReleasedSnapshots( true );
+
+        String status = action.save();
+        assertEquals( Action.SUCCESS, status );
+
+        assertEquals( configuration.getManagedRepositories(), 
Collections.singletonList( repository ) );
+
+        roleManagerControl.verify();
+        archivaConfigurationControl.verify();
+    }
+
+    // TODO: test errors during add, other actions
+
+    public void testSecureActionBundle()
+        throws SecureActionException
+    {
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( new Configuration() );
+        archivaConfigurationControl.replay();
+
+        action.prepare();
+        SecureActionBundle bundle = action.getSecureActionBundle();
+        assertTrue( bundle.requiresAuthentication() );
+        assertEquals( 1, bundle.getAuthorizationTuples().size() );
+    }
+}

Propchange: 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.xml
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.xml?rev=574556&view=auto
==============================================================================
--- 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.xml
 (added)
+++ 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.xml
 Tue Sep 11 04:40:28 2007
@@ -0,0 +1,34 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~  http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<plexus>
+  <components>
+    <component>
+      <role>org.codehaus.plexus.logging.LoggerManager</role>
+      
<implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
+      <lifecycle-handler>basic</lifecycle-handler>
+    </component>
+    <component>
+      <role>com.opensymphony.xwork.Action</role>
+      <role-hint>configureRepositoryAction</role-hint>
+      
<implementation>org.apache.maven.archiva.web.action.admin.repositories.ConfigureRepositoryAction</implementation>
+      <instantiation-strategy>per-lookup</instantiation-strategy>
+    </component>
+  </components>
+</plexus>

Propchange: 
maven/archiva/branches/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to