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