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());
}
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());
}
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"));
}
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());
}
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());
}
Aggregations