/*
 * 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 org.xwiki.rendering.internal.transformation.wikiword;

import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.MacroMarkerBlock;
import org.xwiki.rendering.block.WordBlock;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.parser.Parser;
import org.xwiki.rendering.renderer.BlockRenderer;
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
import org.xwiki.rendering.renderer.printer.WikiPrinter;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.transformation.Transformation;
import org.xwiki.rendering.transformation.TransformationContext;
import org.xwiki.test.jmock.AbstractComponentTestCase;

/**
 * Unit tests for {@link org.xwiki.rendering.internal.transformation.wikiword.WikiWordTransformation}.
 *
 * @version $Id$
 * @since 2.6RC1
 */
public class WikiWordTransformationTest extends AbstractComponentTestCase
{
    private Transformation wikiWordTransformation;

    @Before
    @Override
    public void setUp() throws Exception
    {
        super.setUp();
        this.wikiWordTransformation = getComponentManager().getInstance(Transformation.class, "wikiword");
    }

    @Test
    public void testWikiWordTransformation() throws Exception
    {
        // Tests the following at once:
        // - that a wiki word is recognized
        // - that several wiki words in a row are recognized
        // - that wiki words with non ASCII chars are recognized (accented chars)
        // - that two uppercase letters following each other (as in "XWiki") are not considered a wiki word
        // - that several uppercases chars followed by lowercases and then one uppercase and lowercase chars is
        //   recognized as a wiki word (eg "XWikiEnterprise")
        String testInput = "This is a WikiWord, Another\u00D9ne, XWikiEnterprise, not one: XWiki";

        Parser parser = getComponentManager().getInstance(Parser.class, "xwiki/2.1");
        XDOM xdom = parser.parse(new StringReader(testInput));
        this.wikiWordTransformation.transform(xdom, new TransformationContext());
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer xwiki21BlockRenderer = getComponentManager().getInstance(BlockRenderer.class, "xwiki/2.1");
        xwiki21BlockRenderer.render(xdom, printer);
        Assert.assertEquals("This is a [[doc:WikiWord]], [[doc:Another\u00D9ne]], [[doc:XWikiEnterprise]], "
            + "not one: XWiki", printer.toString());
    }

    @Test
    public void testWikiWordTransformationIgnoresProtectedContent() throws Exception
    {
        String expected = "beginDocument\n"
            + "beginMacroMarkerStandalone [code] []\n"
            + "onWord [WikiWord]\n"
            + "endMacroMarkerStandalone [code] []\n"
            + "endDocument";

        XDOM xdom = new XDOM(Arrays.asList((Block) new MacroMarkerBlock("code", Collections.<String, String>emptyMap(),
            Arrays.asList((Block) new WordBlock("WikiWord")), false)));
        this.wikiWordTransformation.transform(xdom, new TransformationContext());

        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer eventBlockRenderer =
            getComponentManager().getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
        eventBlockRenderer.render(xdom, printer);
        Assert.assertEquals(expected, printer.toString());
    }

}

@@ -1085,16 +1085,39 @@
 Before;%0A
+import org.junit.Rule;%0A
 import o
@@ -1710,22 +1710,66 @@
 est.
-jmock.Abstract
+ComponentManagerRule;%0Aimport org.xwiki.test.annotation.All
 Comp
@@ -1773,24 +1773,17 @@
 omponent
-TestCase
+s
 ;%0A%0A/**%0A 
@@ -1925,16 +1925,31 @@
 RC1%0A */%0A
+@AllComponents%0A
 public c
@@ -1983,44 +1983,100 @@
 Test
- extends AbstractComponentTestCase%0A%7B
+%0A%7B%0A    @Rule%0A    public ComponentManagerRule componentManager = new ComponentManagerRule();%0A
 %0A   
@@ -2140,22 +2140,8 @@
 ore%0A
-    @Override%0A
     
@@ -2187,31 +2187,8 @@
   %7B%0A
-        super.setUp();%0A
     
@@ -2221,20 +2221,22 @@
 ation = 
-getC
+this.c
 omponent
@@ -2234,34 +2234,32 @@
 componentManager
-()
 .getInstance(Tra
@@ -3004,28 +3004,30 @@
 er parser = 
-getC
+this.c
 omponentMana
@@ -3021,34 +3021,32 @@
 componentManager
-()
 .getInstance(Par
@@ -3316,20 +3316,22 @@
 derer = 
-getC
+this.c
 omponent
@@ -3329,34 +3329,32 @@
 componentManager
-()
 .getInstance(Blo
@@ -4315,12 +4315,14 @@
     
-getC
+this.c
 ompo
@@ -4332,18 +4332,16 @@
 tManager
-()
 .getInst
@@ -4513,11 +4513,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Remove deprecation