/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 */
package com.xpn.xwiki.render;

import java.io.ByteArrayInputStream;

import javax.servlet.ServletContext;

import org.jmock.Mock;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiConfig;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiServletContext;

/**
 * Unit tests for {@link DefaultXWikiRenderingEngine}.
 * 
 * @version $Id$
 */
public class DefaultXWikiRenderingEngineTest extends AbstractBridgedXWikiComponentTestCase
{
    private DefaultXWikiRenderingEngine engine;

    protected void setUp() throws Exception
    {
        super.setUp();

        XWikiConfig config = new XWikiConfig();

        Mock mockServletContext = mock(ServletContext.class);
        ByteArrayInputStream bais =
            new ByteArrayInputStream("code=wiki:code:type:content".getBytes());
        mockServletContext.stubs().method("getResourceAsStream")
            .with(eq("/templates/macros.txt")).will(returnValue(bais));
        mockServletContext.stubs().method("getResourceAsStream").with(
            eq("/WEB-INF/oscache.properties")).will(
            returnValue(new ByteArrayInputStream("".getBytes())));
        mockServletContext.stubs().method("getResourceAsStream").with(
            eq("/WEB-INF/oscache-local.properties")).will(
            returnValue(new ByteArrayInputStream("".getBytes())));
        mockServletContext.stubs().method("getResourceAsStream").with(
            eq("/skins/albatross/macros.vm")).will(
            returnValue(new ByteArrayInputStream("".getBytes())));
        mockServletContext.stubs().method("getResourceAsStream").with(eq("/templates/macros.vm"))
            .will(returnValue(new ByteArrayInputStream("".getBytes())));
        XWikiServletContext engineContext =
            new XWikiServletContext((ServletContext) mockServletContext.proxy());

        XWiki xwiki = new XWiki(config, getContext(), engineContext, false);
        xwiki.setVersion("1.0");

        this.engine = (DefaultXWikiRenderingEngine) xwiki.getRenderingEngine();
    }

    public void testRenderTextWhenUsingCodeMacro() throws Exception
    {
        // We verify that the code macro doesn't render wiki markup, velocity, HTML, or other radeox
        // macros.
        // We also ensure that any Radeox macro coming after the code macro is rendered properly.
        // Last we also ensure that a second code macro works too.
        String text =
            "{code:none}\n" + "1 Title\n" + "c:\\dev\n" + "#info(\"test\")\n"
                + "<pre>hello</pre>\n" + "$xwiki.getVersion()\n" + "{style}style{style}\n"
                + "&#123;code}nested&#123;code}\n" + "<% print(\"hello\") %>\n" + "{code}\n"
                + "{table}\n" + "a | b\n" + "c | d\n" + "{table}\n" + "#set ($var = 'dummy')\n"
                + "{code:none}\n" + "1 Something\n" + "{code}";

        String expectedText =
            "<div class=\"code\"><pre>1 Title\n"
                + "c:&#92;dev\n"
                + "&#35;info(\"test\")\n"
                + "&#60;pre&#62;hello&#60;/pre&#62;\n"
                + "&#36;xwiki.getVersion()\n"
                + "&#123;style&#125;style&#123;style&#125;\n"
                + "&&#35;123;code&#125;nested&&#35;123;code&#125;\n"
                + "&#60;% print(\"hello\") %&#62;</pre></div>\n"
                + "<table class=\"wiki-table\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><th>a</th>"
                + "<th>b</th></tr><tr class=\"table-odd\"><td>c</td><td>d</td></tr></table>\n"
                + "<div class=\"code\"><pre>1 Something</pre></div>";

        XWikiDocument document = new XWikiDocument();
        assertEquals(expectedText, engine.renderText(text, document, getContext()));
    }

    /**
     * Test that links are preserved after rendering.
     * XWIKI-2672
     */
    public void testLinksAndCache() throws Exception
    {
        String link = "http://some:123/link";
        String text =
            "$context.setCacheDuration(1800)\n" +
            link;
        XWikiDocument document = new XWikiDocument();

        Utils.enablePlaceholders(getContext());
        String out = engine.renderText(text, document, getContext());
        assertTrue( out.contains(link) );
    }
}

@@ -1493,16 +1493,152 @@
 ngine;%0A%0A
+    /**%0A     * %7B@inheritDoc%7D%0A     * %0A     * @see com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase#setUp()%0A     */%0A    @Override%0A
     prot
@@ -1849,28 +1849,16 @@
 m bais =
-%0A           
  new Byt
@@ -1982,29 +1982,16 @@
 Stream%22)
-%0A            
 .with(eq
@@ -2018,24 +2018,37 @@
 txt%22)).will(
+%0A            
 returnValue(
@@ -2121,37 +2121,24 @@
 ream%22).with(
-%0A            
 eq(%22/WEB-INF
@@ -2299,37 +2299,24 @@
 ream%22).with(
-%0A            
 eq(%22/WEB-INF
@@ -2487,29 +2487,16 @@
 %22).with(
-%0A            
 eq(%22/ski
@@ -2691,16 +2691,22 @@
 os.vm%22))
+.will(
 %0A       
@@ -2710,22 +2710,16 @@
         
-.will(
 returnVa
@@ -2808,28 +2808,16 @@
 ontext =
-%0A           
  new XWi
@@ -4818,23 +4818,16 @@
 ndering.
-%0A     *
  XWIKI-2
@@ -4830,16 +4830,16 @@
 KI-2672%0A
+
      */%0A
@@ -4964,28 +4964,16 @@
 g text =
-%0A           
  %22$conte
@@ -5002,28 +5002,16 @@
 00)%5Cn%22 +
-%0A           
  link;%0A 
@@ -5201,17 +5201,16 @@
 ertTrue(
- 
 out.cont
@@ -5219,17 +5219,16 @@
 ns(link)
- 
 );%0A    %7D

Apply codestyle