jspwiki-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ajaqu...@apache.org
Subject svn commit: r722775 - in /incubator/jspwiki/trunk: src/com/ecyrd/jspwiki/url/DefaultURLConstructor.java tests/com/ecyrd/jspwiki/content/PageRenamerTest.java
Date Wed, 03 Dec 2008 04:56:24 GMT
Author: ajaquith
Date: Tue Dec  2 20:56:24 2008
New Revision: 722775

URL: http://svn.apache.org/viewvc?rev=722775&view=rev
Log:
Checked in the Stripes code (about 220 classes, tests, JSPs and other files. All code compiles
clean, but the JSPs do NOT work at the moment. Unit tests run clean except for some stray
failing Stripes-related unit tests. See the file doc/README - Stripes Migration for some (repeat,
SOME) details of what it all means. I'll sort out the JSP issues later this week.

Added:
    incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/url/DefaultURLConstructor.java
    incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/content/PageRenamerTest.java

Added: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/url/DefaultURLConstructor.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/url/DefaultURLConstructor.java?rev=722775&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/url/DefaultURLConstructor.java (added)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/url/DefaultURLConstructor.java Tue Dec 
2 20:56:24 2008
@@ -0,0 +1,339 @@
+/* 
+    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 com.ecyrd.jspwiki.url;
+
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.lang.StringUtils;
+
+import com.ecyrd.jspwiki.TextUtil;
+import com.ecyrd.jspwiki.WikiContext;
+import com.ecyrd.jspwiki.WikiEngine;
+
+/**
+ *  Implements the default URL constructor using links directly to the
+ *  JSP pages.  This is what JSPWiki by default is using.  For example,
+ *  WikiContext.VIEW points at "Wiki.jsp", etc.
+ *  
+ *  @since 2.2
+ */
+public class DefaultURLConstructor
+    implements URLConstructor
+{
+    protected WikiEngine m_engine;
+
+    /** Are URL styles relative or absolute? */
+    protected boolean          m_useRelativeURLStyle = true;
+
+    /**
+     *  Contains the absolute path of the JSPWiki Web application without the
+     *  actual servlet (which is the m_urlPrefix).
+     */
+    protected String m_pathPrefix = "";
+    
+    protected static final Map<String,String> c_urlPatterns = new HashMap<String,String>();
+    
+    static
+    {
+        c_urlPatterns.put( WikiContext.ATTACH, "%uattach/%n" );
+        c_urlPatterns.put( WikiContext.COMMENT, "%uComment.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.CONFLICT, "%uPageModified.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.DELETE, "%uDelete.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.DIFF, "%uDiff.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.EDIT, "%uEdit.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.INFO, "%uPageInfo.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.PREVIEW, "%uPreview.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.RENAME, "%uRename.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.RSS, "%urss.jsp" );
+        c_urlPatterns.put( WikiContext.UPLOAD, "%uUpload.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.VIEW, "%uWiki.jsp?page=%n" );
+        c_urlPatterns.put( WikiContext.NONE,"%u%n" );
+        c_urlPatterns.put( WikiContext.OTHER, "%u%n" );
+        c_urlPatterns.put( WikiContext.DELETE_GROUP, "%uDeleteGroup.jsp?group=%n" );
+        c_urlPatterns.put( WikiContext.EDIT_GROUP, "%uEditGroup.jsp?group=%n" );
+        c_urlPatterns.put( WikiContext.VIEW_GROUP, "%uGroup.jsp?group=%n" );
+        c_urlPatterns.put( WikiContext.CREATE_GROUP, "%uNewGroup.jsp" );
+        c_urlPatterns.put( WikiContext.ERROR, "%uError.jsp" );
+        c_urlPatterns.put( WikiContext.FIND, "%uSearch.jsp" );
+        c_urlPatterns.put( WikiContext.INSTALL, "%uInstall.jsp" );
+        c_urlPatterns.put( WikiContext.LOGIN, "%uLogin.jsp?redirect=%n" );
+        c_urlPatterns.put( WikiContext.LOGOUT, "%uLogout.jsp" );
+        c_urlPatterns.put( WikiContext.MESSAGE, "%uMessage.jsp" );
+        c_urlPatterns.put( WikiContext.PREFS, "%uUserPreferences.jsp" );
+        c_urlPatterns.put( WikiContext.WORKFLOW, "%uWorkflow.jsp" );
+        c_urlPatterns.put( WikiContext.ADMIN, "%uadmin/Admin.jsp" );
+    };
+    
+    /**
+     * 
+     * {@inheritDoc}
+     */
+    public void initialize( WikiEngine engine, 
+                            Properties properties )
+    {
+        m_engine = engine;
+
+        m_useRelativeURLStyle = "relative".equals( properties.getProperty( WikiEngine.PROP_REFSTYLE,
+                                                                           "relative" ) );
+
+        String baseurl = engine.getBaseURL();
+
+        if( baseurl != null && baseurl.length() > 0 )
+        {
+            try
+            {
+                URL url = new URL( baseurl );
+        
+                String path = url.getPath();
+        
+                m_pathPrefix = path;
+            }
+            catch( MalformedURLException e )
+            {
+                m_pathPrefix = "/JSPWiki/"; // Just a guess.
+            }
+        }
+    }
+
+    /**
+     *  Does replacement of some particular variables.  The variables are:
+     *  
+     *  <ul>
+     *  <li> "%u" - inserts either the base URL (when absolute is required), or the
base path
+     *       (which is an absolute path without the host name).
+     *  <li> "%U" - always inserts the base URL
+     *  <li> "%p" - always inserts the base path
+     *  <li> "%n" - inserts the page name
+     *  </ul>
+     *  
+     * @param baseptrn  The pattern to use
+     * @param name The page name
+     * @param absolute If true, %u is always the entire base URL, otherwise it depends on
+     *                 the setting in jspwiki.properties.
+     * @return A replacement.
+     */
+    protected final String doReplacement( String baseptrn, String name, boolean absolute
)
+    {
+        String baseurl = m_pathPrefix;
+
+        if( absolute ) baseurl = m_engine.getBaseURL();
+
+        baseptrn = TextUtil.replaceString( baseptrn, "%u", baseurl );
+        baseptrn = TextUtil.replaceString( baseptrn, "%U", m_engine.getBaseURL() );
+        baseptrn = TextUtil.replaceString( baseptrn, "%n", encodeURI(name) );
+        baseptrn = TextUtil.replaceString( baseptrn, "%p", m_pathPrefix );
+
+        return baseptrn;
+    }
+
+    /**
+     *  URLEncoder returns pluses, when we want to have the percent
+     *  encoding.  See http://issues.apache.org/bugzilla/show_bug.cgi?id=39278
+     *  for more info.
+     *  
+     *  We also convert any %2F's back to slashes to make nicer-looking URLs.
+     */
+    private final String encodeURI( String uri )
+    {
+        uri = m_engine.encodeName(uri);
+        
+        uri = StringUtils.replace( uri, "+", "%20" );
+        uri = StringUtils.replace( uri, "%2F", "/" );
+        
+        return uri;
+    }
+    
+    /**
+     * Returns the URL pattern for a supplied wiki request context.
+     * @param context the wiki context
+     * @param name the wiki page
+     * @return A pattern for replacement.
+     * @throws IllegalArgumentException if the context cannot be found
+     */
+    public static String getURLPattern( String context, String name )
+        throws IllegalArgumentException
+    {
+        if( context.equals(WikiContext.VIEW) )
+        {
+            if( name == null ) return "%uWiki.jsp"; // FIXME
+        }
+        
+        // Find the action matching our pattern (could throw exception)
+        return c_urlPatterns.get( context );
+    }
+    
+    /**
+     *  Constructs the actual URL based on the context.
+     */
+    private String makeURL( String context,
+                            String name,
+                            boolean absolute )
+    {
+        return doReplacement( getURLPattern(context,name), name, absolute );
+    }
+
+    /**
+     *  Constructs the URL with a bunch of parameters.
+     *  @param parameters If null or empty, no parameters are added.
+     *  
+     *  {@inheritDoc}
+     */
+    public String makeURL( String context,
+                           String name,
+                           boolean absolute,
+                           String parameters )
+    {
+        if( parameters != null && parameters.length() > 0 )
+        {            
+            if( context.equals(WikiContext.ATTACH) )
+            {
+                parameters = "?"+parameters;
+            }
+            else if( context.equals(WikiContext.NONE) )
+            {
+                parameters = (name.indexOf('?') != -1 ) ? "&amp;" : "?" + parameters;
+            }
+            else
+            {
+                parameters = "&amp;"+parameters;
+            }
+        }
+        else
+        {
+            parameters = "";
+        }
+        return makeURL( context, name, absolute )+parameters;
+    }
+
+    /**
+     *  Should parse the "page" parameter from the actual
+     *  request.
+     *  
+     *  {@inheritDoc}
+     */
+    public String parsePage( String context,
+                             HttpServletRequest request,
+                             String encoding )
+        throws UnsupportedEncodingException
+    {
+        String pagereq = request.getParameter( "page" );
+
+        if( context.equals(WikiContext.ATTACH) )
+        {
+            pagereq = parsePageFromURL( request, encoding );
+        }
+
+        return pagereq;
+    }
+
+    /**
+     *  There's a bug in Tomcat until 5.5.16 at least: The "+" sign is not
+     *  properly decoded by the servlet container, and therefore request.getPathInfo()
+     *  will return faulty results for paths which contains + signs to signify spaces.
+     *  <p>
+     *  This method provides a workaround by simply parsing the getRequestURI(), which
+     *  is returned from the servlet container undedecoded.
+     *  <p>
+     *  Please see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=39278">Tomcat
Bug 39278</a>
+     *  for more information.
+     *  
+     *  @param request A HTTP servlet request
+     *  @param encoding The used encoding
+     *  @return a String, decoded by JSPWiki, specifying extra path information that comes

+     *          after the servlet path but before the query string in the request URL; 
+     *          or null if the URL does not have any extra path information
+     *  @throws UnsupportedEncodingException
+     */
+    /*
+    private static String getPathInfo( HttpServletRequest request, String encoding )
+        throws UnsupportedEncodingException
+    {
+        String c = request.getContextPath(); // Undecoded
+        String s = request.getServletPath(); // Decoded
+        String u = request.getRequestURI();  // Undecoded
+        
+        c = URLDecoder.decode( c, encoding );
+        u = URLDecoder.decode( u, encoding );
+        
+        String pi = u.substring( s.length()+c.length() );
+        
+        if( pi.length() == 0 ) pi = null;
+        
+        return pi;
+    }
+    */
+    /**
+     *  Takes the name of the page from the request URI.
+     *  The initial slash is also removed.  If there is no page,
+     *  returns null.
+     *  
+     *  @param request The request to parse
+     *  @param encoding The encoding to use
+     *  
+     *  @return a parsed page name, or null, if it cannot be found
+     *  
+     *  @throws UnsupportedEncodingException If the encoding is not recognized.
+     */
+    public static String parsePageFromURL( HttpServletRequest request,
+                                           String encoding )
+        throws UnsupportedEncodingException
+    {
+        String name = request.getPathInfo();
+
+        if( name == null || name.length() <= 1 )
+        {
+            return null;
+        }
+        else if( name.charAt(0) == '/' )
+        {
+            name = name.substring(1);
+        }
+       
+        //
+        //  This is required, because by default all URLs are handled
+        //  as Latin1, even if they are really UTF-8.
+        //
+        
+        // name = TextUtil.urlDecode( name, encoding );
+        
+        return name;
+    }
+
+    
+    /**
+     *  This method is not needed for the DefaultURLConstructor.
+     *  
+     * @param request The HTTP Request that was used to end up in this page.
+     * @return "Wiki.jsp", "PageInfo.jsp", etc.  Just return the name,
+     *         JSPWiki will figure out the page.
+     */
+    public String getForwardPage( HttpServletRequest request )
+    {
+        return request.getPathInfo();
+    }
+}

Added: incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/content/PageRenamerTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/content/PageRenamerTest.java?rev=722775&view=auto
==============================================================================
--- incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/content/PageRenamerTest.java (added)
+++ incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/content/PageRenamerTest.java Tue Dec 
2 20:56:24 2008
@@ -0,0 +1,489 @@
+package com.ecyrd.jspwiki.content;
+
+import java.util.Collection;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import com.ecyrd.jspwiki.*;
+import com.ecyrd.jspwiki.attachment.Attachment;
+
+public class PageRenamerTest extends TestCase
+{
+    TestEngine m_engine;
+    
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        
+        Properties props = new Properties();
+        
+        props.load( TestEngine.findTestProperties() );
+
+        props.setProperty( WikiEngine.PROP_MATCHPLURALS, "true" );
+        
+        TestEngine.emptyWorkDir();
+        m_engine = new TestEngine(props);  
+    }
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        
+        TestEngine.deleteTestPage("TestPage");
+        TestEngine.deleteTestPage("TestPage2");
+        TestEngine.deleteTestPage("FooTest");
+        TestEngine.deleteTestPage("Test");
+        TestEngine.deleteTestPage("CdauthNew");
+        TestEngine.deleteTestPage("Cdauth");
+        TestEngine.deleteTestPage("TestPageReferring");
+        TestEngine.deleteTestPage("TestPageReferredNew");
+        TestEngine.deleteTestPage("Main");
+        TestEngine.deleteTestPage("Main8887");
+        TestEngine.deleteTestPage("TestPage1234");
+        TestEngine.deleteTestPage("TestPageReferred");
+        TestEngine.deleteTestPage("RenameTest");
+        TestEngine.deleteTestPage("Link one");
+        TestEngine.deleteTestPage("Link uno");
+        TestEngine.deleteTestPage("Link two");
+
+        TestEngine.emptyWorkDir();
+    }
+
+    public void testSimpleRename()
+        throws Exception
+    {
+        // Count the numberof existing references
+        int refCount = m_engine.getReferenceManager().findCreated().size();
+        
+        m_engine.saveText("TestPage", "the big lazy dog thing" );
+        
+        WikiPage p = m_engine.getPage("TestPage");
+        
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+        
+        m_engine.renamePage(context, "TestPage", "FooTest", false);
+        
+        WikiPage newpage = m_engine.getPage("FooTest");
+        
+        assertNotNull( "no new page", newpage );
+        assertNull( "old page not gone", m_engine.getPage("TestPage") );
+        
+        // Refmgr
+        
+        Collection refs = m_engine.getReferenceManager().findCreated();
+        
+        assertTrue( "FooTest does not exist", refs.contains("FooTest") );
+        assertFalse( "TestPage exists", refs.contains("TestPage") );
+        assertEquals( "wrong list size", refCount+1, refs.size() );
+    }
+    
+    public void testReferrerChange()
+       throws Exception
+    {
+        m_engine.saveText("TestPage", "foofoo" );
+        m_engine.saveText("TestPage2", "[TestPage]");
+        
+        WikiPage p = m_engine.getPage("TestPage");
+        
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+        
+        m_engine.renamePage(context, "TestPage", "FooTest", true);
+        
+        String data = m_engine.getPureText("TestPage2", WikiProvider.LATEST_VERSION);
+        
+        assertEquals( "no rename", "[FooTest]", data.trim() );
+        
+        Collection refs = m_engine.getReferenceManager().findReferrers("TestPage");
+        
+        assertNull( "oldpage", refs );
+        
+        refs = m_engine.getReferenceManager().findReferrers( "FooTest" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "TestPage2", (String)refs.iterator().next() );
+    }
+
+    public void testReferrerChangeCC()
+        throws Exception
+    {
+        m_engine.saveText("TestPage", "foofoo" );
+        m_engine.saveText("TestPage2", "TestPage");
+     
+        WikiPage p = m_engine.getPage("TestPage");
+     
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+     
+        m_engine.renamePage(context, "TestPage", "FooTest", true);
+     
+        String data = m_engine.getPureText("TestPage2", WikiProvider.LATEST_VERSION);
+     
+        assertEquals( "no rename", "FooTest", data.trim() );
+        Collection refs = m_engine.getReferenceManager().findReferrers("TestPage");
+        
+        assertNull( "oldpage", refs );
+        
+        refs = m_engine.getReferenceManager().findReferrers( "FooTest" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "TestPage2", (String)refs.iterator().next() );
+    }
+    
+    public void testReferrerChangeAnchor()
+        throws Exception
+    {
+        m_engine.saveText("TestPage", "foofoo" );
+        m_engine.saveText("TestPage2", "[TestPage#heading1]");
+     
+        WikiPage p = m_engine.getPage("TestPage");
+     
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+     
+        m_engine.renamePage(context, "TestPage", "FooTest", true);
+     
+        String data = m_engine.getPureText("TestPage2", WikiProvider.LATEST_VERSION);
+     
+        assertEquals( "no rename", "[FooTest#heading1]", data.trim() );
+        Collection refs = m_engine.getReferenceManager().findReferrers("TestPage");
+        
+        assertNull( "oldpage", refs );
+        
+        refs = m_engine.getReferenceManager().findReferrers( "FooTest" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "TestPage2", (String)refs.iterator().next() );
+    }
+    
+    public void testReferrerChangeMultilink()
+        throws Exception
+    {
+        m_engine.saveText("TestPage", "foofoo" );
+        m_engine.saveText("TestPage2", "[TestPage] [TestPage] [linktext|TestPage] TestPage
[linktext|TestPage] [TestPage#Anchor] [TestPage] TestPage [TestPage]");
+     
+        WikiPage p = m_engine.getPage("TestPage");
+     
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+     
+        m_engine.renamePage(context, "TestPage", "FooTest", true);
+     
+        String data = m_engine.getPureText("TestPage2", WikiProvider.LATEST_VERSION);
+     
+        assertEquals( "no rename", 
+                      "[FooTest] [FooTest] [linktext|FooTest] FooTest [linktext|FooTest]
[FooTest#Anchor] [FooTest] FooTest [FooTest]", 
+                      data.trim() );
+
+        Collection refs = m_engine.getReferenceManager().findReferrers("TestPage");
+        
+        assertNull( "oldpage", refs );
+        
+        refs = m_engine.getReferenceManager().findReferrers( "FooTest" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "TestPage2", (String)refs.iterator().next() );
+    }
+    
+    public void testReferrerNoWikiName()
+        throws Exception
+    {
+        m_engine.saveText("Test","foo");
+        m_engine.saveText("TestPage2", "[Test] [Test#anchor] test Test [test] [link|test]
[link|test]");
+        
+        WikiPage p = m_engine.getPage("TestPage");
+        
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+     
+        m_engine.renamePage(context, "Test", "TestPage", true);
+        
+        String data = m_engine.getPureText("TestPage2", WikiProvider.LATEST_VERSION );
+        
+        assertEquals( "wrong data", "[TestPage] [TestPage#anchor] test Test [TestPage] [link|TestPage]
[link|TestPage]", data.trim() );
+    }
+
+    public void testAttachmentChange()
+        throws Exception
+    {
+        m_engine.saveText("TestPage", "foofoo" );
+        m_engine.saveText("TestPage2", "[TestPage/foo.txt] [linktext|TestPage/bar.jpg]");
+ 
+        m_engine.addAttachment("TestPage", "foo.txt", "testing".getBytes() );
+        m_engine.addAttachment("TestPage", "bar.jpg", "pr0n".getBytes() );
+        WikiPage p = m_engine.getPage("TestPage");
+ 
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+ 
+        m_engine.renamePage(context, "TestPage", "FooTest", true);
+ 
+        String data = m_engine.getPureText("TestPage2", WikiProvider.LATEST_VERSION);
+ 
+        assertEquals( "no rename", 
+                      "[FooTest/foo.txt] [linktext|FooTest/bar.jpg]", 
+                      data.trim() );
+
+        Attachment att = m_engine.getAttachmentManager().getAttachmentInfo("FooTest/foo.txt");
+        assertNotNull("footext",att);
+        
+        att = m_engine.getAttachmentManager().getAttachmentInfo("FooTest/bar.jpg");
+        assertNotNull("barjpg",att);
+        
+        att = m_engine.getAttachmentManager().getAttachmentInfo("TestPage/bar.jpg");
+        assertNull("testpage/bar.jpg exists",att);
+        
+        att = m_engine.getAttachmentManager().getAttachmentInfo("TestPage/foo.txt");
+        assertNull("testpage/foo.txt exists",att);
+        
+        Collection refs = m_engine.getReferenceManager().findReferrers("TestPage/bar.jpg");
+    
+        assertNull( "oldpage", refs );
+    
+        refs = m_engine.getReferenceManager().findReferrers( "FooTest/bar.jpg" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "TestPage2", (String)refs.iterator().next() );
+    }
+
+    public void testSamePage() throws Exception
+    {
+        m_engine.saveText( "TestPage", "[TestPage]");
+        
+        rename( "TestPage", "FooTest" );
+
+        WikiPage p = m_engine.getPage( "FooTest" );
+        
+        assertNotNull( "no page", p );
+        
+        assertEquals("[FooTest]", m_engine.getText("FooTest").trim() );
+    }
+
+    public void testBrokenLink1() throws Exception
+    {
+        m_engine.saveText( "TestPage", "hubbub");
+        m_engine.saveText( "TestPage2", "[TestPage|]" );
+        
+        rename( "TestPage", "FooTest" );
+
+        WikiPage p = m_engine.getPage( "FooTest" );
+        
+        assertNotNull( "no page", p );
+        
+        // Should be no change
+        assertEquals("[TestPage|]", m_engine.getText("TestPage2").trim() );
+    }
+
+    public void testBrokenLink2() throws Exception
+    {
+        m_engine.saveText( "TestPage", "hubbub");
+        m_engine.saveText( "TestPage2", "[|TestPage]" );
+        
+        WikiPage p;
+        rename( "TestPage", "FooTest" );
+
+        p = m_engine.getPage( "FooTest" );
+        
+        assertNotNull( "no page", p );
+        
+        assertEquals("[|FooTest]", m_engine.getText("TestPage2").trim() );
+    }
+
+    private void rename( String src, String dst ) throws WikiException
+    {
+        WikiPage p = m_engine.getPage(src);
+
+        WikiContext context = m_engine.getWikiContextFactory().newViewContext( null, null,
p );
+        
+        m_engine.renamePage(context, src, dst, true);
+    }
+
+    public void testBug25() throws Exception
+    {
+        String src = "[Cdauth/attach.txt] [link|Cdauth/attach.txt] [cdauth|Cdauth/attach.txt]"+
+                     "[CDauth/attach.txt] [link|CDauth/attach.txt] [cdauth|CDauth/attach.txt]"+
+                     "[cdauth/attach.txt] [link|cdauth/attach.txt] [cdauth|cdauth/attach.txt]";
+        
+        String dst = "[CdauthNew/attach.txt] [link|CdauthNew/attach.txt] [cdauth|CdauthNew/attach.txt]"+
+                     "[CDauth/attach.txt] [link|CDauth/attach.txt] [cdauth|CDauth/attach.txt]"+
+                     "[CdauthNew/attach.txt] [link|CdauthNew/attach.txt] [cdauth|CdauthNew/attach.txt]";
+        
+        m_engine.saveText( "Cdauth", "xxx" );
+        m_engine.saveText( "TestPage", src );
+        
+        m_engine.addAttachment( "Cdauth", "attach.txt", "Puppua".getBytes() );
+        
+        rename( "Cdauth", "CdauthNew" );
+        
+        assertEquals( dst, m_engine.getText("TestPage").trim() );
+    }
+    
+    public void testBug21() throws Exception
+    {
+        String src = "[Link to TestPage2|TestPage2]";
+        
+        m_engine.saveText( "TestPage", src );
+        m_engine.saveText( "TestPage2", "foo" );
+        
+        rename ("TestPage2", "Test");
+        
+        assertEquals( "[Link to Test|Test]", m_engine.getText( "TestPage" ).trim() );
+    }
+
+    public void testExtendedLinks() throws Exception
+    {
+        String src = "[Link to TestPage2|TestPage2|target='_new']";
+        
+        m_engine.saveText( "TestPage", src );
+        m_engine.saveText( "TestPage2", "foo" );
+        
+        rename ("TestPage2", "Test");
+        
+        assertEquals( "[Link to Test|Test|target='_new']", m_engine.getText( "TestPage" ).trim()
);
+    }
+    
+    public void testBug85_case1() throws Exception 
+    {
+        // renaming a non-existing page
+        // This fails under 2.5.116, cfr. with http://bugs.jspwiki.org/show_bug.cgi?id=85
+        // m_engine.saveText( "TestPage", "blablahblahbla" );
+        try
+        {
+            rename("TestPage123", "Main8887");
+            rename("Main8887", "TestPage123"); 
+        }
+        catch (NullPointerException npe)
+        {
+            npe.printStackTrace();
+            System.out.println("NPE: Bug 85 caught?");
+            fail();
+        }
+        catch( WikiException e )
+        {
+            // Expected
+        }
+    }
+   
+    public void testBug85_case2() throws Exception 
+    {
+        try
+        {
+            // renaming a non-existing page, but we call m_engine.saveText() before renaming

+            // this does not fail under 2.5.116
+            m_engine.saveText( "TestPage1234", "blablahblahbla" );
+            rename("TestPage1234", "Main8887");
+            rename("Main8887", "TestPage1234");
+        }
+        catch (NullPointerException npe)
+        {
+            npe.printStackTrace();
+            System.out.println("NPE: Bug 85 caught?");
+            fail();
+        }
+    }
+    
+    public void testBug85_case3() throws Exception 
+    {
+        try
+        {
+            // renaming an existing page
+            // this does not fail under 2.5.116
+            // m_engine.saveText( "Main", "blablahblahbla" );
+            rename("Main", "Main8887");
+            rename("Main8887", "Main");
+        }
+        catch (NullPointerException npe)
+        {
+            npe.printStackTrace();
+            System.out.println("NPE: Bug 85 caught?");
+            fail();
+        }
+        catch( WikiException e )
+        {
+            // Expected
+        }
+    }
+    
+    public void testBug85_case4() throws Exception 
+    {
+        try
+        {
+            // renaming an existing page, and we call m_engine.saveText() before renaming
+            // this does not fail under 2.5.116
+            m_engine.saveText( "Main", "blablahblahbla" );
+            rename("Main", "Main8887");
+            rename("Main8887", "Main");
+        }
+        catch (NullPointerException npe)
+        {
+            npe.printStackTrace();
+            System.out.println("NPE: Bug 85 caught?");
+            fail();
+        }
+    }
+    
+    public void testRenameOfEscapedLinks() throws Exception
+    {
+        String src = "[[Link to TestPage2|TestPage2|target='_new']";
+        
+        m_engine.saveText( "TestPage", src );
+        m_engine.saveText( "TestPage2", "foo" );
+        
+        rename ("TestPage2", "Test");
+        
+        assertEquals( "[[Link to TestPage2|TestPage2|target='_new']", m_engine.getText( "TestPage"
).trim() );
+    }
+
+    public void testRenameOfEscapedLinks2() throws Exception
+    {
+        String src = "~[Link to TestPage2|TestPage2|target='_new']";
+        
+        m_engine.saveText( "TestPage", src );
+        m_engine.saveText( "TestPage2", "foo" );
+        
+        rename ("TestPage2", "Test");
+        
+        assertEquals( "~[Link to TestPage2|TestPage2|target='_new']", m_engine.getText( "TestPage"
).trim() );
+    }
+
+    /**
+     * Test for a referrer containing blanks
+     * 
+     * @throws Exception
+     */
+    public void testReferrerChangeWithBlanks() throws Exception
+    {
+        m_engine.saveText( "TestPageReferred", "bla bla bla som content" );
+        m_engine.saveText( "TestPageReferring", "[Test Page Referred]" );
+
+        rename( "TestPageReferred", "TestPageReferredNew" );
+
+        String data = m_engine.getPureText( "TestPageReferring", WikiProvider.LATEST_VERSION
);
+        assertEquals( "page not renamed", "[Test Page Referred|TestPageReferredNew]", data.trim()
);
+
+        Collection refs = m_engine.getReferenceManager().findReferrers( "TestPageReferred"
);
+        assertNull( "oldpage", refs );
+
+        refs = m_engine.getReferenceManager().findReferrers( "TestPageReferredNew" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "TestPageReferring", (String) refs.iterator().next() );
+    }
+
+    /** https://issues.apache.org/jira/browse/JSPWIKI-398 */
+    public void testReferrerChangeWithBlanks2() throws Exception
+    {
+        m_engine.saveText( "RenameTest", "[link one] [link two]" );
+        m_engine.saveText( "Link one", "Leonard" );
+        m_engine.saveText( "Link two", "Cohen" );
+
+        rename( "Link one", "Link uno" );
+       
+        String data = m_engine.getPureText( "RenameTest", WikiProvider.LATEST_VERSION );
+        assertEquals( "page not renamed", "[link one|Link uno] [link two]", data.trim() );
+
+        Collection refs = m_engine.getReferenceManager().findReferrers( "Link one" );
+        assertNull( "oldpage", refs );
+
+        refs = m_engine.getReferenceManager().findReferrers( "Link uno" );
+        assertEquals( "new size", 1, refs.size() );
+        assertEquals( "wrong ref", "RenameTest", (String) refs.iterator().next() );
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite( PageRenamerTest.class );
+    }
+
+}



Mime
View raw message