Search in sources :

Example 16 with MacroTransformationContext

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

the class CacheMacroTest method executeWithDifferentMaxEntries.

@Test
public void executeWithDifferentMaxEntries() throws Exception {
    CacheMacroParameters params = new CacheMacroParameters();
    MacroTransformationContext context = createMacroTransformationContext();
    params.setId("id");
    params.setMaxEntries(10);
    params.setTimeToLive(100);
    List<Block> result1 = this.cacheMacro.execute(params, "content1", context);
    // Execute a second time with different content but with different time to live param. This means another
    // cache will be used and thus the first cached content won't be returned.
    params.setMaxEntries(11);
    List<Block> result2 = this.cacheMacro.execute(params, "content2", context);
    assertFalse(result2.equals(result1));
}
Also used : CacheMacroParameters(org.xwiki.rendering.macro.cache.CacheMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) Test(org.junit.Test)

Example 17 with MacroTransformationContext

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

the class CacheMacroTest method executeWithIdGeneratedByVelocityMacro.

@Test
public void executeWithIdGeneratedByVelocityMacro() throws Exception {
    VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
    StringWriter writer = new StringWriter();
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'generatedid')");
    CacheMacroParameters params = new CacheMacroParameters();
    params.setId("{{velocity}}$var{{/velocity}}");
    MacroTransformationContext context = createMacroTransformationContext();
    List<Block> result1 = this.cacheMacro.execute(params, "whatever", context);
    // Execute a second time with the same id (specified explicitly this time) and ensures that the returned result
    // is the same even the cache macro content is different.
    params.setId("generatedid");
    List<Block> result2 = this.cacheMacro.execute(params, "something else", context);
    assertEquals(result1, result2);
}
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)

Example 18 with MacroTransformationContext

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

the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextNew.

@Test
public void testIncludeMacroWithRecursiveIncludeContextNew() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
    this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
    this.includeMacro.setDocumentDisplayer(mockDocumentDisplayer);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    final IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("wiki:space.page");
    parameters.setContext(Context.NEW);
    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 {
                        includeMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive inclusion")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("The include macro hasn't checked the recursive inclusion");
    } 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) 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) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 19 with MacroTransformationContext

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

the class IncludeMacroTest method testIncludeMacroWithRecursiveIncludeContextCurrent.

@Test
public void testIncludeMacroWithRecursiveIncludeContextCurrent() throws Exception {
    this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    // Add an Include Macro MarkerBlock as a parent of the include Macro block since this is what would have
    // happened if an Include macro is included in another Include macro.
    final MacroMarkerBlock includeMacroMarker = new MacroMarkerBlock("include", Collections.singletonMap("reference", "space.page"), Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), false);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockDocumentReferenceResolver).resolve("space.page", includeMacroMarker);
            will(returnValue(new DocumentReference("wiki", "space", "page")));
        }
    });
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("wiki:space.page");
    parameters.setContext(Context.CURRENT);
    try {
        this.includeMacro.execute(parameters, null, macroContext);
        Assert.fail("The include macro hasn't checked the recursive inclusion");
    } catch (MacroExecutionException expected) {
        if (!expected.getMessage().startsWith("Found recursive inclusion")) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 20 with MacroTransformationContext

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

the class IncludeMacroTest method runIncludeMacro.

private List<Block> runIncludeMacro(final Context context, String includedContent, boolean restricted) throws Exception {
    final DocumentReference includedDocumentReference = new DocumentReference("wiki", "Space", "IncludedPage");
    String includedDocStringRef = "wiki:space.page";
    setUpDocumentMock(includedDocStringRef, includedDocumentReference, includedContent);
    getMockery().checking(new Expectations() {

        {
            allowing(mockSetup.bridge).isDocumentViewable(with(same(includedDocumentReference)));
            will(returnValue(true));
            // Verify that push/pop are called when context is NEW
            if (context == Context.NEW) {
                oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
                oneOf(mockSetup.bridge).getCurrentDocumentReference();
                will(returnValue(includedDocumentReference));
                oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
            } else {
                oneOf(mockSetup.bridge).getCurrentDocumentReference();
                will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
            }
        }
    });
    this.includeMacro.setDocumentAccessBridge(this.mockSetup.bridge);
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference(includedDocStringRef);
    parameters.setContext(context);
    // Create a Macro transformation context with the Macro transformation object defined so that the include
    // macro can transform included page which is using a new context.
    MacroTransformation macroTransformation = getComponentManager().getInstance(Transformation.class, "macro");
    MacroTransformationContext macroContext = createMacroTransformationContext(includedDocStringRef, false);
    macroContext.setId("wiki:Space.IncludingPage");
    macroContext.setTransformation(macroTransformation);
    macroContext.getTransformationContext().setRestricted(restricted);
    return this.includeMacro.execute(parameters, null, macroContext);
}
Also used : Expectations(org.jmock.Expectations) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) 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