Search in sources :

Example 26 with MacroTransformationContext

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

the class IncludeMacroTest method createMacroTransformationContext.

private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline) {
    MacroTransformationContext context = new MacroTransformationContext();
    MacroBlock includeMacro = new MacroBlock("include", Collections.singletonMap("reference", documentName), isInline);
    context.setCurrentMacroBlock(includeMacro);
    return context;
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 27 with MacroTransformationContext

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

the class IncludeMacroTest method testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("relativePage");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the include Macro block.
    new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
    final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("relativePage", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(sourceReference));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 28 with MacroTransformationContext

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

the class IncludeMacroTest method testIncludeMacroWhenInvalidSectionSpecified.

@Test
public void testIncludeMacroWhenInvalidSectionSpecified() throws Exception {
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("document");
    parameters.setSection("unknown");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            allowing(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            allowing(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("Should have raised an exception");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("Cannot find section [unknown] in document [wiki:space.document]", expected.getMessage());
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 29 with MacroTransformationContext

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

the class IncludeMacroTest method testIncludeMacroWhenSectionSpecified.

@Test
public void testIncludeMacroWhenSectionSpecified() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n" + "beginHeader [1, Hsection]\n" + "onWord [section]\n" + "endHeader [1, Hsection]\n" + "beginParagraph\n" + "onWord [content2]\n" + "endParagraph\n" + "endMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n" + "endDocument";
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("document");
    parameters.setSection("Hsection");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            oneOf(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content1\n\n= section =\ncontent2")));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 30 with MacroTransformationContext

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

the class CacheMacroTest method executeWhenNoIdAndSameContent.

@Test
public void executeWhenNoIdAndSameContent() throws Exception {
    String expected = "beginDocument\n" + "beginMacroMarkerStandalone [velocity] [] [$var]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMacroMarkerStandalone [velocity] [] [$var]\n" + "endDocument";
    CacheMacroParameters params = new CacheMacroParameters();
    MacroTransformationContext context = createMacroTransformationContext();
    VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
    StringWriter writer = new StringWriter();
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'content')");
    List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
    assertBlocks(expected, result, this.rendererFactory);
    // Execute a second time with a different value for the velocity $var variable to ensure the returned result
    // is the cached one.
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'newcontent')");
    result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context);
    assertBlocks(expected, result, this.rendererFactory);
}
Also used : CacheMacroParameters(org.xwiki.rendering.macro.cache.CacheMacroParameters) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) Test(org.junit.Test)

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