You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2009/02/23 06:21:45 UTC

svn commit: r746885 - in /incubator/jspwiki/trunk: src/java/org/apache/wiki/action/ tests/java/org/apache/wiki/action/

Author: ajaquith
Date: Mon Feb 23 05:21:44 2009
New Revision: 746885

URL: http://svn.apache.org/viewvc?rev=746885&view=rev
Log:
Finished DeleteActionBean and tests. Haven't connected it to JSPs yet, but expect to soon.

Added:
    incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/DeleteActionBeanTest.java
Modified:
    incubator/jspwiki/trunk/src/java/org/apache/wiki/action/DeleteActionBean.java
    incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/AllTests.java
    incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/RenameActionBeanTest.java

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/action/DeleteActionBean.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/action/DeleteActionBean.java?rev=746885&r1=746884&r2=746885&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/action/DeleteActionBean.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/action/DeleteActionBean.java Mon Feb 23 05:21:44 2009
@@ -21,23 +21,140 @@
 
 package org.apache.wiki.action;
 
-import org.apache.wiki.auth.permissions.PagePermission;
-import org.apache.wiki.ui.stripes.HandlerPermission;
-import org.apache.wiki.ui.stripes.WikiRequestContext;
+import javax.servlet.http.HttpServletRequest;
 
 import net.sourceforge.stripes.action.HandlesEvent;
+import net.sourceforge.stripes.action.RedirectResolution;
 import net.sourceforge.stripes.action.Resolution;
 import net.sourceforge.stripes.action.UrlBinding;
+import net.sourceforge.stripes.validation.*;
 
+import org.apache.wiki.PageManager;
+import org.apache.wiki.WikiEngine;
+import org.apache.wiki.api.WikiPage;
+import org.apache.wiki.attachment.Attachment;
+import org.apache.wiki.auth.permissions.PagePermission;
+import org.apache.wiki.log.Logger;
+import org.apache.wiki.log.LoggerFactory;
+import org.apache.wiki.providers.ProviderException;
+import org.apache.wiki.ui.stripes.HandlerPermission;
+import org.apache.wiki.ui.stripes.WikiRequestContext;
 
-@UrlBinding("/Delete.jsp")
+/**
+ * ActionBean for deleting a wiki page or attachment, optionally for a specific
+ * version.
+ */
+@UrlBinding( "/Delete.jsp" )
 public class DeleteActionBean extends AbstractPageActionBean
 {
-    @HandlesEvent("delete")
-    @HandlerPermission(permissionClass=PagePermission.class, target="${page.name}", actions=PagePermission.DELETE_ACTION)
-    @WikiRequestContext("del")
-    public Resolution delete()
+    private Logger log = LoggerFactory.getLogger( DeleteActionBean.class );
+
+    private int m_version = Integer.MIN_VALUE;
+
+    /**
+     * Event handler method that deletes the wiki page or attachment.
+     * 
+     * @return a RedirectResolution to the parent page (if the item to be
+     *         deleted was an attachment); the front page (if the item to be
+     *         deleted was all versions of a wiki page); or the wiki page (if
+     *         just a single version was deleted).
+     * @throws ProviderException if the delete failed for any reason
+     */
+    @HandlesEvent( "delete" )
+    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.qualifiedName}", actions = PagePermission.DELETE_ACTION )
+    @WikiRequestContext( "del" )
+    public Resolution delete() throws ProviderException
     {
-        return null;
+        // If all versions of a page or attachment should be deleted, redirect
+        // to the main page (for page) or parent page (for attachment)
+        WikiEngine engine = getContext().getEngine();
+        String pageName = m_page.getName();
+        if( m_version == Integer.MIN_VALUE )
+        {
+            HttpServletRequest request = getContext().getRequest();
+            log.info( "Deleting page " + pageName + ". User=" + request.getRemoteUser() + ", host="
+                      + request.getRemoteAddr() );
+            engine.deletePage( pageName );
+        }
+
+        // Just delete a single version
+        else
+        {
+            WikiPage p = engine.getPage( pageName, m_version );
+            log.debug( "Deleting page=" + pageName + ", version=" + m_version );
+            engine.deleteVersion( p );
+        }
+
+        // If attachment deleted; always redirect to parent page
+        if( m_page instanceof Attachment )
+        {
+            String redirPage = ((Attachment) m_page).getParentName();
+            return new RedirectResolution( ViewActionBean.class, "view" ).addParameter( "page", redirPage );
+        }
+
+        // If no more versions left, redirect to main page, otherwise INFO page
+        String redirPage = engine.pageExists( pageName ) ? pageName : engine.getFrontPage();
+        return new RedirectResolution( ViewActionBean.class, "view" ).addParameter( "page", redirPage );
     }
+
+    /**
+     * Returns the version to delete
+     * 
+     * @return the version
+     */
+    public int getVersion()
+    {
+        return m_version;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Validate( required = true )
+    public void setPage( WikiPage page )
+    {
+        super.setPage( page );
+    }
+    
+    /**
+     * Sets the version to delete. If not set, all versions of the page or
+     * attachment will be deleted.
+     * 
+     * @param version the version
+     */
+    public void setVersion( int version )
+    {
+        m_version = version;
+    }
+
+    /**
+     * Validates the version number of the page or attachment that should be
+     * deleted. If the version was not set, or is a valid version number,
+     * validation succeeds. Otherwise, if the version number supplied does not
+     * exist, validation fails.
+     */
+    @ValidationMethod( when = ValidationState.ALWAYS )
+    public void validateBeforeDelete() throws ProviderException
+    {
+        // If no version number supplied, always succeeds.
+        if( m_version == Integer.MIN_VALUE )
+        {
+            return;
+        }
+
+        // If version supplied exists, validation succeeds also.
+        WikiEngine engine = getContext().getEngine();
+        PageManager pm = engine.getPageManager();
+        if( pm.pageExists( getPage().getName(), m_version ) )
+        {
+            // While we're at it, set the correct version for the bean
+            m_page = engine.getPage( getPage().getName(), m_version );
+            return;
+        }
+
+        // Oops! User supplied an invalid version
+        ValidationErrors errors = getContext().getValidationErrors();
+        errors.add( "version", new LocalizableError( "version.invalid" ) );
+    }
+
 }

Modified: incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/AllTests.java?rev=746885&r1=746884&r2=746885&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/AllTests.java (original)
+++ incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/AllTests.java Mon Feb 23 05:21:44 2009
@@ -36,6 +36,7 @@
     {
         TestSuite suite = new TestSuite("ActionBean tests");
 
+        suite.addTest( DeleteActionBeanTest.suite() );
         suite.addTest( GroupActionBeanTest.suite() );
         suite.addTest( LoginActionBeanTest.suite() );
         suite.addTest( RenameActionBeanTest.suite() );

Added: incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/DeleteActionBeanTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/DeleteActionBeanTest.java?rev=746885&view=auto
==============================================================================
--- incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/DeleteActionBeanTest.java (added)
+++ incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/DeleteActionBeanTest.java Mon Feb 23 05:21:44 2009
@@ -0,0 +1,215 @@
+/*
+    JSPWiki - a JSP-based WikiWiki clone.
+
+    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.    
+ */
+package org.apache.wiki.action;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import net.sourceforge.stripes.mock.MockRoundtrip;
+import net.sourceforge.stripes.validation.ValidationErrors;
+
+import org.apache.wiki.PageManager;
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.api.WikiPage;
+import org.apache.wiki.attachment.Attachment;
+import org.apache.wiki.attachment.AttachmentManager;
+import org.apache.wiki.auth.Users;
+
+public class DeleteActionBeanTest extends TestCase
+{
+    TestEngine m_engine;
+
+    public void setUp()
+    {
+        // Start the WikiEngine, and stash reference
+        Properties props = new Properties();
+        try
+        {
+            TestEngine.emptyWorkDir();
+            props.load( TestEngine.findTestProperties( "/jspwiki_vers.properties" ) );
+            props.setProperty( PageManager.PROP_USECACHE, "false" );
+            m_engine = new TestEngine( props );
+        }
+        catch( Exception e )
+        {
+            throw new RuntimeException( "Could not set up TestEngine: " + e.getMessage() );
+        }
+    }
+
+    public void tearDown() throws Exception
+    {
+        // Delete test page
+        m_engine.deletePage( "Test" );
+        m_engine.deletePage( "TestDeleteAttachment" );
+        m_engine.shutdown();
+    }
+
+
+    public void testDeleteAttachment() throws Exception
+    {
+        // Re-initialized the WikiEngine with default test managers
+        Properties props = new Properties();
+        TestEngine.emptyWorkDir();
+        props.load( TestEngine.findTestProperties() );
+        props.setProperty( PageManager.PROP_USECACHE, "false" );
+        m_engine = new TestEngine( props );
+        
+        // Create wiki page
+        String pageName = "TestDeleteAttachment";
+        m_engine.saveText( pageName, "This is a test." );
+        WikiPage page = m_engine.getPage( "TestDeleteAttachment" );
+        
+        // Create attachment file
+        String attachContents = "ABCDEFGHIJKLMNOPQRSTUVWxyz";
+        File attachFile = File.createTempFile("Attach",".txt");
+        FileWriter out = new FileWriter( attachFile );
+        out.write( attachContents );
+        out.close();
+        
+        // Store attachment
+        Attachment att = new Attachment( m_engine, pageName, attachFile.getName() );
+        AttachmentManager mgr = m_engine.getAttachmentManager();
+        att.setAuthor( "AttachmentAuthor" );
+        mgr.storeAttachment( att, attachFile );
+
+        // Make sure it was saved
+        Attachment att2 = mgr.getAttachmentInfo( m_engine.getWikiContextFactory().newViewContext( page ), attachFile.getName() );
+        assertNotNull( "Attachment disappeared! Is the AttachmentManager running?", att2 );
+        
+        // Now, delete the page
+        MockRoundtrip trip;
+        ValidationErrors errors;
+
+        // Try deleting the attachment without specifying a version (==all pages)
+        trip = m_engine.authenticatedTrip( Users.ADMIN, Users.ADMIN_PASS, DeleteActionBean.class );
+        trip.setParameter( "page", att.getName() );
+        trip.execute( "delete" );
+        errors = trip.getValidationErrors();
+        assertEquals( 0, errors.size() );
+
+        // Verify that we deleted the attachment but not the page
+        att2 = mgr.getAttachmentInfo( m_engine.getWikiContextFactory().newViewContext( page ), attachFile.getName() );
+        assertNull( "Attachment wasn't removed!", att2 );
+        assertTrue( m_engine.pageExists( pageName ) );
+    }
+
+    public void testDeleteAllVersions() throws Exception
+    {
+        // Save two versions of the test page
+        m_engine.saveText( "Test", "This is the first version" );
+        m_engine.saveText( "Test", "This is the second version" );
+
+        // Make sure they both saved ok
+        WikiPage v1 = m_engine.getPage( "Test", 1 );
+        WikiPage v2 = m_engine.getPage( "Test", 2 );
+        assertNotNull( "Did not save page Test, v1!", v1 );
+        assertNotNull( "Did not save page Test, v2!", v2 );
+        assertEquals( "This is the first version", m_engine.getPureText( v1 ).trim() );
+        assertEquals( "This is the second version", m_engine.getPureText( v2 ).trim() );
+
+        MockRoundtrip trip;
+        ValidationErrors errors;
+
+        // Try deleting the page without specifying a version (==all pages)
+        trip = m_engine.authenticatedTrip( Users.ADMIN, Users.ADMIN_PASS, DeleteActionBean.class );
+        trip.setParameter( "page", "Test" );
+        trip.execute( "delete" );
+        errors = trip.getValidationErrors();
+        assertEquals( 0, errors.size() );
+
+        // Verify that we deleted all the pages
+        assertFalse( m_engine.pageExists( "Test" ) );
+    }
+
+    public void testDeleteVersion() throws Exception
+    {
+        // Save two versions of the test page
+        m_engine.saveText( "Test", "This is the first version" );
+        m_engine.saveText( "Test", "This is the second version" );
+
+        // Make sure they both saved ok
+        WikiPage v1 = m_engine.getPage( "Test", 1 );
+        WikiPage v2 = m_engine.getPage( "Test", 2 );
+        assertNotNull( "Did not save page Test, v1!", v1 );
+        assertNotNull( "Did not save page Test, v2!", v2 );
+        assertEquals( "This is the first version", m_engine.getPureText( v1 ).trim() );
+        assertEquals( "This is the second version", m_engine.getPureText( v2 ).trim() );
+
+        MockRoundtrip trip;
+        ValidationErrors errors;
+
+        // Try deleting one version
+        trip = m_engine.authenticatedTrip( Users.ADMIN, Users.ADMIN_PASS, DeleteActionBean.class );
+        trip.setParameter( "page", "Test" );
+        trip.setParameter( "version", "1" );
+        trip.execute( "delete" );
+        errors = trip.getValidationErrors();
+        assertEquals( 0, errors.size() );
+
+        // Verify that there is only one version left
+        assertFalse( m_engine.pageExists( "Test", 1 ) );
+        assertTrue( m_engine.pageExists( "Test", 2 ) );
+    }
+
+    public void testValidation() throws Exception
+    {
+        // Save two versions of the test page
+        m_engine.saveText( "Test", "This is the first version" );
+        m_engine.saveText( "Test", "This is the second version" );
+
+        // Make sure they both saved ok
+        WikiPage v1 = m_engine.getPage( "Test", 1 );
+        WikiPage v2 = m_engine.getPage( "Test", 2 );
+        assertNotNull( "Did not save page Test, v1!", v1 );
+        assertNotNull( "Did not save page Test, v2!", v2 );
+        assertEquals( "This is the first version", m_engine.getPureText( v1 ).trim() );
+        assertEquals( "This is the second version", m_engine.getPureText( v2 ).trim() );
+
+        MockRoundtrip trip;
+        ValidationErrors errors;
+
+        // Try deleting without 'page' param; should see 1 validation errors
+        trip = m_engine.authenticatedTrip( Users.ADMIN, Users.ADMIN_PASS, DeleteActionBean.class );
+        trip.execute( "delete" );
+        errors = trip.getValidationErrors();
+        assertEquals( 1, errors.size() );
+        assertTrue( errors.containsKey( "page" ) );
+
+        // Try again, with value 'page' param but invalid 'version'; should see
+        // 1 validation error
+        trip = m_engine.authenticatedTrip( Users.ADMIN, Users.ADMIN_PASS, DeleteActionBean.class );
+        trip.setParameter( "page", "Test" );
+        trip.setParameter( "version", "10000" );
+        trip.execute( "delete" );
+        errors = trip.getValidationErrors();
+        assertEquals( 1, errors.size() );
+        assertTrue( errors.containsKey( "version" ) );
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite( DeleteActionBeanTest.class );
+    }
+}

Modified: incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/RenameActionBeanTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/RenameActionBeanTest.java?rev=746885&r1=746884&r2=746885&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/RenameActionBeanTest.java (original)
+++ incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/RenameActionBeanTest.java Mon Feb 23 05:21:44 2009
@@ -221,6 +221,24 @@
         m_engine.deletePage( "ReferstoTest" );
     }
     
+    public void testRenameToSameName() throws Exception {
+        MockRoundtrip trip;
+        ValidationErrors errors;
+        
+        // Try renaming to 'TestRenamed' to same name; should fail
+        m_engine.saveText("Test", "This is a test.");
+        trip = m_engine.authenticatedTrip( Users.ADMIN,Users.ADMIN_PASS, RenameActionBean.class );
+        trip.setParameter("page", "Test");
+        trip.setParameter("renameTo", "Test");
+        trip.execute("rename");
+        errors = trip.getValidationErrors();
+        assertEquals( 1, errors.size() );
+        m_engine.deletePage( "Test" );
+        
+        // Clean up
+        m_engine.deletePage( "Test" );
+    }
+    
     public static Test suite()
     {
         return new TestSuite( RenameActionBeanTest.class );