Search in sources :

Example 1 with MacroTransformationContext

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

the class DisplayMacroTest method createMacroTransformationContext.

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

Example 2 with MacroTransformationContext

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

the class DisplayMacroTest method testDisplayMacroWithRecursiveDisplay.

@Test
public void testDisplayMacroWithRecursiveDisplay() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
    this.displayMacro.setDocumentAccessBridge(mockSetup.bridge);
    FieldUtils.writeField(this.displayMacro, "documentDisplayer", mockDocumentDisplayer, true);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    final DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("wiki:space.page");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            allowing(mockSetup.bridge).getDocumentInstance(with(any(DocumentReference.class)));
            will(returnValue(null));
            allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)), with(any(DocumentDisplayerParameters.class)));
            will(new CustomAction("recursively call the include macro again") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    try {
                        displayMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive display")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });
    try {
        this.displayMacro.execute(parameters, null, macroContext);
        Assert.fail("The display macro hasn't checked the recursive display");
    } catch (MacroExecutionException expected) {
        if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 3 with MacroTransformationContext

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

the class DisplayMacroTest method testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("relativePage");
    MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the display 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");
    setUpDocumentMock("relativePage", resolvedReference, "content");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve(with("wiki:space.page"), with(IsArray.array(any(MacroBlock.class))));
            will(returnValue(sourceReference));
            allowing(mockDocumentReferenceResolver).resolve(with("relativePage"), with(IsArray.array(any(MacroBlock.class))));
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
        }
    });
    List<Block> blocks = this.displayMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 4 with MacroTransformationContext

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

the class DashboardMacroTest method executeWhenInsideDashboardMacro.

@Test
public void executeWhenInsideDashboardMacro() throws Exception {
    BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
    BeanDescriptor descriptor = mock(BeanDescriptor.class);
    when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
    when(descriptor.getProperties()).thenReturn(Collections.emptyList());
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext ec = new ExecutionContext();
    when(execution.getContext()).thenReturn(ec);
    ec.setProperty("dashboardMacroCalls", 1);
    DashboardMacroParameters parameters = new DashboardMacroParameters();
    MacroTransformationContext macroContext = new MacroTransformationContext();
    try {
        this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
        fail("Exception should have been raised here");
    } catch (MacroExecutionException expected) {
        assertEquals("Dashboard macro recursion detected. Don't call the Dashboard macro inside of itself...", expected.getMessage());
    }
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) BeanDescriptor(org.xwiki.properties.BeanDescriptor) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DashboardMacroParameters(org.xwiki.rendering.macro.dashboard.DashboardMacroParameters) BeanManager(org.xwiki.properties.BeanManager) Test(org.junit.Test)

Example 5 with MacroTransformationContext

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

the class DashboardMacroTest method executeWhenNotInsideDashboardMacro.

@Test
public void executeWhenNotInsideDashboardMacro() throws Exception {
    BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
    BeanDescriptor descriptor = mock(BeanDescriptor.class);
    when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
    when(descriptor.getProperties()).thenReturn(Collections.emptyList());
    DashboardRenderer renderer = this.mocker.registerMockComponent(DashboardRenderer.class, "columns");
    GadgetRenderer gadgetRenderer = this.mocker.registerMockComponent(GadgetRenderer.class);
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext ec = new ExecutionContext();
    when(execution.getContext()).thenReturn(ec);
    DashboardMacroParameters parameters = new DashboardMacroParameters();
    MacroTransformationContext macroContext = new MacroTransformationContext();
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
    // We verify that the counter ends up at 0 so that calls to subsequent dashboard macros can succeed.
    assertEquals(0, ec.getProperty("dashboardMacroCalls"));
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) BeanDescriptor(org.xwiki.properties.BeanDescriptor) DashboardRenderer(org.xwiki.rendering.macro.dashboard.DashboardRenderer) GadgetRenderer(org.xwiki.rendering.macro.dashboard.GadgetRenderer) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DashboardMacroParameters(org.xwiki.rendering.macro.dashboard.DashboardMacroParameters) BeanManager(org.xwiki.properties.BeanManager) 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