Search in sources :

Example 21 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class VelocityMacroIsolationTest method testVelocityMacroIsolation.

@Test
public void testVelocityMacroIsolation() throws Exception {
    String expected = "beginDocument\n" + "beginParagraph\n" + "onSpecialSymbol [#]\n" + "onWord [testMacrosAreLocal]\n" + "onSpecialSymbol [(]\n" + "onSpecialSymbol [)]\n" + "endParagraph\n" + "endDocument";
    VelocityMacroParameters params = new VelocityMacroParameters();
    MacroTransformationContext context = new MacroTransformationContext();
    context.setSyntax(Syntax.XWIKI_2_0);
    context.setCurrentMacroBlock(new MacroBlock("velocity", Collections.<String, String>emptyMap(), false));
    // Execute the velocity macro in the context of a first page
    context.setId("page1");
    this.velocityMacro.execute(params, "#macro(testMacrosAreLocal)mymacro#end", context);
    // And then in the context of a second independent page
    context.setId("page2");
    PrintRendererFactory eventRendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0");
    assertBlocks(expected, this.velocityMacro.execute(params, "#testMacrosAreLocal()", context), eventRendererFactory);
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 22 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class MacroContentExecutorTest method executeWhenTransformationException.

@Test
public void executeWhenTransformationException() throws Exception {
    XDOM parsedBlocks = new XDOM(Collections.emptyList());
    ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
    when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
    TransformationContext transformationContext = new TransformationContext();
    MacroTransformationContext context = new MacroTransformationContext(transformationContext);
    Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
    doThrow(new TransformationException("error")).when(macroTransformation).transform(parsedBlocks, transformationContext);
    try {
        this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
        fail("Should have raised a ContentExecutorException");
    } catch (ContentExecutorException expected) {
        assertEquals("Failed to execute content", expected.getMessage());
    }
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) ContentExecutorException(org.xwiki.rendering.executor.ContentExecutorException) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 23 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class MacroContentExecutorTest method executeWithSource.

@Test
public void executeWithSource() throws Exception {
    XDOM parsedBlocks = new XDOM(Collections.emptyList());
    ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
    when(contentParser.parse("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE)).thenReturn(parsedBlocks);
    TransformationContext transformationContext = new TransformationContext();
    MacroTransformationContext context = new MacroTransformationContext(transformationContext);
    this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE, context);
    // The test is here: Verify that the Macro Transformation has been called
    Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
    verify(macroTransformation).transform(parsedBlocks, transformationContext);
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 24 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class MacroContentExecutorTest method executeWithNoSource.

@Test
public void executeWithNoSource() throws Exception {
    XDOM parsedBlocks = new XDOM(Collections.emptyList());
    ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
    when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
    TransformationContext transformationContext = new TransformationContext();
    MacroTransformationContext context = new MacroTransformationContext(transformationContext);
    this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
    // The test is here: Verify that the Macro Transformation has been called
    Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
    verify(macroTransformation).transform(parsedBlocks, transformationContext);
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 25 with MacroTransformationContext

use of org.xwiki.rendering.transformation.MacroTransformationContext in project xwiki-platform by xwiki.

the class DisplayMacroTest method runDisplayMacro.

private List<Block> runDisplayMacro(DisplayMacroParameters parameters, String displayedContent) throws Exception {
    final DocumentReference displayedDocumentReference = new DocumentReference("wiki", "Space", "DisplayedPage");
    String displayedDocStringRef = "wiki:space.page";
    setUpDocumentMock(displayedDocStringRef, displayedDocumentReference, displayedContent);
    getMockery().checking(new Expectations() {

        {
            allowing(mockSetup.bridge).isDocumentViewable(with(same(displayedDocumentReference)));
            will(returnValue(true));
            oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
            atMost(1).of(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(displayedDocumentReference));
            oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
        }
    });
    this.displayMacro.setDocumentAccessBridge(this.mockSetup.bridge);
    parameters.setReference(displayedDocStringRef);
    // Create a Macro transformation context with the Macro transformation object defined so that the display
    // macro can transform displayed page which is using a new context.
    MacroTransformation macroTransformation = (MacroTransformation) getComponentManager().getInstance(Transformation.class, "macro");
    MacroTransformationContext macroContext = createMacroTransformationContext(displayedDocStringRef, false);
    macroContext.setId("wiki:Space.DisplayingPage");
    macroContext.setTransformation(macroTransformation);
    return this.displayMacro.execute(parameters, null, macroContext);
}
Also used : Expectations(org.jmock.Expectations) MacroTransformation(org.xwiki.rendering.internal.transformation.macro.MacroTransformation) Transformation(org.xwiki.rendering.transformation.Transformation) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroTransformation(org.xwiki.rendering.internal.transformation.macro.MacroTransformation)

Aggregations

MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)41 Test (org.junit.Test)30 MacroBlock (org.xwiki.rendering.block.MacroBlock)17 DocumentReference (org.xwiki.model.reference.DocumentReference)15 Block (org.xwiki.rendering.block.Block)13 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)10 XDOM (org.xwiki.rendering.block.XDOM)10 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)10 Expectations (org.jmock.Expectations)9 HashMap (java.util.HashMap)6 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)6 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 MetaData (org.xwiki.rendering.listener.MetaData)6 CacheMacroParameters (org.xwiki.rendering.macro.cache.CacheMacroParameters)6 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)6 ContextMacroParameters (org.xwiki.rendering.macro.context.ContextMacroParameters)5 Transformation (org.xwiki.rendering.transformation.Transformation)5 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)5 StringWriter (java.io.StringWriter)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4