Search in sources :

Example 6 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testDefaultParameterNames.

/**
 * Makes sure getParameterNames return parameters with source case.
 */
@Test
public void testDefaultParameterNames() throws Exception {
    // Velocity Manager mock.
    final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class);
    DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>();
    descriptorVM.setRoleType(VelocityManager.class);
    getComponentManager().registerComponent(descriptorVM, mockVelocityManager);
    // Initialize velocity engine.
    final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class);
    Properties properties = new Properties();
    properties.setProperty("resource.loader", "file");
    vEngine.initialize(properties);
    // Hack into velocity context.
    Execution execution = getComponentManager().getInstance(Execution.class);
    Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext");
    final VelocityContext vContext = new VelocityContext();
    vContext.put("xcontext", xwikiContext);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockVelocityManager).getCurrentVelocityContext();
            will(returnValue(vContext));
            oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
            will(new Action() {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
                }

                @Override
                public void describeTo(Description description) {
                }
            });
        }
    });
    registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.parameterNames{{/velocity}}", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacro1 paRam1=\"value1\" paraM2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
    Assert.assertEquals("[paRam1, paraM2]", printer.toString());
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) StringReader(java.io.StringReader) Properties(java.util.Properties) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) Execution(org.xwiki.context.Execution) VelocityManager(org.xwiki.velocity.VelocityManager) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) Test(org.junit.Test)

Example 7 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testDefaultParameterValues.

/**
 * Test default parameter value injection.
 */
@Test
public void testDefaultParameterValues() throws Exception {
    // Velocity Manager mock.
    final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class);
    DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>();
    descriptorVM.setRoleType(VelocityManager.class);
    getComponentManager().registerComponent(descriptorVM, mockVelocityManager);
    // Initialize velocity engine.
    final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class);
    Properties properties = new Properties();
    properties.setProperty("resource.loader", "file");
    vEngine.initialize(properties);
    // Hack into velocity context.
    Execution execution = getComponentManager().getInstance(Execution.class);
    Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext");
    final VelocityContext vContext = new VelocityContext();
    vContext.put("xcontext", xwikiContext);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockVelocityManager).getCurrentVelocityContext();
            will(returnValue(vContext));
            oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
            will(new Action() {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
                }

                @Override
                public void describeTo(Description description) {
                }
            });
        }
    });
    List<WikiMacroParameterDescriptor> parameterDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", false, "default_value"));
    registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.param1 $xcontext.macro.params.paraM1{{/velocity}}", Syntax.XWIKI_2_0, parameterDescriptors);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacro1/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
    Assert.assertEquals("default_value default_value", printer.toString());
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) StringReader(java.io.StringReader) Properties(java.util.Properties) WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) Execution(org.xwiki.context.Execution) VelocityManager(org.xwiki.velocity.VelocityManager) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) Test(org.junit.Test)

Example 8 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecute.

// Tests
/**
 * Test normal wiki macro execution.
 */
@Test
public void testExecute() throws Exception {
    registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>This is <strong>bold</strong></p>", printer.toString());
    Assert.assertFalse(this.xcontext.containsKey("macro"));
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 9 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWikiMacroDirectlyProvideTheResult.

/**
 * A wiki macro can directly provide the list of blocks instead of having to render them to let
 * {@link DefaultWikiMacro} re-parse it.
 */
@Test
public void testExecuteWhenWikiMacroDirectlyProvideTheResult() throws Exception {
    registerWikiMacro("wikimacrowithresult", "{{groovy}}" + "xcontext.macro.result = java.util.Collections.singletonList(new org.xwiki.rendering.block.WordBlock(xcontext.macro.params.param1));" + "{{/groovy}}", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    // Note: We're putting the macro after the "Hello" text to force it as an inline macro.
    converter.convert(new StringReader("Hello {{wikimacrowithresult param1=\"World\" param2=\"param2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>Hello World</p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 10 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testGetCurrentMacroBlock.

@Test
public void testGetCurrentMacroBlock() throws Exception {
    registerWikiMacro("wikimacro", "{{groovy}}" + "println xcontext.macro.context.getCurrentMacroBlock().id\n" + "println xcontext.macro.context.getCurrentMacroBlock().parent.class\n" + "println xcontext.macro.context.getCurrentMacroBlock().nextSibling.children[0].word\n" + "println xcontext.macro.context.getCurrentMacroBlock().previousSibling.children[0].word\n" + "{{/groovy}}", Syntax.XWIKI_2_0, Collections.<WikiMacroParameterDescriptor>emptyList());
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("before\n\n{{wikimacro/}}\n\nafter"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
    Assert.assertEquals("before" + "\n\n" + "wikimacro\n" + "class org.xwiki.rendering.block.XDOM\n" + "after\n" + "before" + "\n\n" + "after", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)11 Converter (org.xwiki.rendering.converter.Converter)11 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)11 Test (org.junit.Test)10 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 Reader (java.io.Reader)2 Writer (java.io.Writer)2 Map (java.util.Map)2 Properties (java.util.Properties)2 VelocityContext (org.apache.velocity.VelocityContext)2 Description (org.hamcrest.Description)2 Action (org.jmock.api.Action)2 Invocation (org.jmock.api.Invocation)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 Execution (org.xwiki.context.Execution)2 VelocityEngine (org.xwiki.velocity.VelocityEngine)2 VelocityManager (org.xwiki.velocity.VelocityManager)2 HTMLConverter (info.bliki.wiki.filter.HTMLConverter)1 WikiModel (info.bliki.wiki.model.WikiModel)1