/* 
    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.render;

import java.util.Properties;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.lang.time.StopWatch;

import org.apache.wiki.TestEngine;
import org.apache.wiki.WikiContext;
import org.apache.wiki.WikiPage;
import org.apache.wiki.parser.MarkupParser;
import org.apache.wiki.parser.WikiDocument;

public class RenderingManagerTest extends TestCase
{
    RenderingManager m_manager;
    TestEngine       m_engine;
    
    protected void setUp() throws Exception
    {
        Properties props = TestEngine.getTestProperties();
        m_engine = new TestEngine( props );
        
        m_manager = new RenderingManager();
        m_manager.initialize( m_engine, props );
    }

    protected void tearDown() throws Exception
    {
        m_engine.deletePage( "TestPage" );
    }
        
    /**
     * Tests the relative speed of the DOM cache with respect to
     * page being parsed every single time.
     * @throws Exception
     */
    public void testCache()
        throws Exception
    {
        m_engine.saveText( "TestPage", TEST_TEXT );
        
        StopWatch sw = new StopWatch();
        
        System.out.println("DOM cache speed test:");
        sw.start();
        
        for( int i = 0; i < 300; i++ )
        {
            WikiPage page = m_engine.getPage( "TestPage" );
            String pagedata = m_engine.getPureText( page );
            
            WikiContext context = new WikiContext( m_engine, page );
            
            MarkupParser p = m_manager.getParser( context, pagedata );
            
            WikiDocument d = p.parse();
            
            String html = m_manager.getHTML( context, d );
            assertNotNull( "noncached got null response",html);
        }
        
        sw.stop();
        System.out.println("  Nocache took "+sw);

        long nocachetime = sw.getTime();
        
        sw.reset();
        sw.start();
        
        for( int i = 0; i < 300; i++ )
        {
            WikiPage page = m_engine.getPage( "TestPage" );
            String pagedata = m_engine.getPureText( page );
            
            WikiContext context = new WikiContext( m_engine, page );
            
            String html = m_manager.getHTML( context, pagedata );
            
            assertNotNull("cached got null response",html);
        }
        
        sw.stop();
        System.out.println("  Cache took "+sw);
        
        long speedup = nocachetime / sw.getTime();
        System.out.println("  Approx speedup: "+speedup+"x");
    }

    public static Test suite()
    {
        TestSuite suite = new TestSuite( RenderingManagerTest.class );
        
        return suite;
    }
    
    private static final String TEST_TEXT =
        "Please ''check [RecentChanges].\n" + 
        "\n" + 
        "Testing. fewfwefe\n" + 
        "\n" + 
        "CHeck [testpage]\n" + 
        "\n" + 
        "More testing.\n" + 
        "dsadsadsa''\n" + 
        "Is this {{truetype}} or not?\n" + 
        "What about {{{This}}}?\n" + 
        "How about {{this?\n" + 
        "\n" + 
        "{{{\n" + 
        "{{text}}\n" + 
        "}}}\n" + 
        "goo\n" + 
        "\n" + 
        "<b>Not bold</b>\n" + 
        "\n" + 
        "motto\n" + 
        "\n" + 
        "* This is a list which we\n" + 
        "shall continue on a other line.\n" + 
        "* There is a list item here.\n" + 
        "*  Another item.\n" + 
        "* More stuff, which continues\n" + 
        "on a second line.  And on\n" + 
        "a third line as well.\n" + 
        "And a fourth line.\n" + 
        "* Third item.\n" + 
        "\n" + 
        "Foobar.\n" + 
        "\n" + 
        "----\n" + 
        "\n" + 
        "!!!Really big heading\n" + 
        "Text.\n" + 
        "!! Just a normal heading [with a hyperlink|Main]\n" + 
        "More text.\n" + 
        "!Just a small heading.\n" + 
        "\n" + 
        "This should be __bold__ text.\n" + 
        "\n" + 
        "__more bold text continuing\n" + 
        "on the next line.__\n" + 
        "\n" + 
        "__more bold text continuing\n" + 
        "\n" + 
        "on the next paragraph.__\n" + 
        "\n" + 
        "\n" + 
        "This should be normal.\n" +
        "\n" + 
        "Now, let's try ''italic text''.\n" + 
        "\n" + 
        "Bulleted lists:\n" + 
        "* One\n" + 
        "Or more.\n" + 
        "* Two\n" + 
        "\n" + 
        "** Two.One\n" + 
        "\n" + 
        "*** Two.One.One\n" + 
        "\n" + 
        "* Three\n" + 
        "\n" + 
        "Numbered lists.\n" + 
        "# One\n" + 
        "# Two\n" + 
        "# Three\n" + 
        "## Three.One\n" + 
        "## Three.Two\n" + 
        "## Three.Three\n" + 
        "### Three.Three.One\n" + 
        "# Four\n" + 
        "\n" +
        "End?\n" + 
        "\n" + 
        "No, let's {{break}} things.\\ {{{ {{{ {{text}} }}} }}}\n" +
        "\n" + 
        "More breaking.\n" + 
        "\n" +
        "{{{\n" + 
        "code.}}\n" + 
        "----\n" +
        "author: [Asser], [Ebu], [JanneJalkanen], [Jarmo|mailto:jarmo@regex.com.au]\n";
}

@@ -975,16 +975,52 @@
 Suite;%0A%0A
+import net.sf.ehcache.CacheManager;%0A
 import o
@@ -1415,32 +1415,81 @@
 Exception%0A    %7B%0A
+        CacheManager.getInstance().removalAll();%0A
         Properti

remove caches to (hopefully) make this test more predictable