Search in sources :

Example 1 with JMXVelocityEngineMBean

use of org.xwiki.velocity.internal.jmx.JMXVelocityEngineMBean in project xwiki-platform by xwiki.

the class XWikiDocumentRenderingTest method testGetRenderedContentCleansVelocityMacroCache.

/**
 * See XWIKI-5277 for details.
 */
public void testGetRenderedContentCleansVelocityMacroCache() throws Exception {
    // Make sure we start not in the rendering engine since this is what happens in real: a document is
    // called by a template thus outside of the rendering engine.
    getContext().remove("isInRenderingEngine");
    // We display a text area since we know that rendering a text area will call getRenderedContent inside our top
    // level getRenderedContent call, thus testing that velocity macros are not removed during nested calls to
    // getRenderedContent.
    this.baseObject.setLargeStringValue("area", "{{velocity}}#macro(testmacrocache)ok#end{{/velocity}}");
    this.document.setContent("{{velocity}}$doc.display(\"area\")#testmacrocache{{/velocity}}");
    this.document.setSyntax(Syntax.XWIKI_2_0);
    // We need to put the current doc in the Velocity Context since it's normally set before the rendering is
    // called in the execution flow.
    VelocityManager originalVelocityManager = getComponentManager().getInstance(VelocityManager.class);
    VelocityContext vcontext = originalVelocityManager.getVelocityContext();
    vcontext.put("doc", new Document(this.document, getContext()));
    // Register a Mock for the VelocityManager to bypass skin APIs that we would need to mock otherwise.
    Mock mockVelocityManager = registerMockComponent(VelocityManager.class);
    mockVelocityManager.stubs().method("getCurrentVelocityContext").will(returnValue(vcontext));
    VelocityFactory velocityFactory = getComponentManager().getInstance(VelocityFactory.class);
    VelocityEngine vengine = velocityFactory.createVelocityEngine("default", new Properties());
    // Save the number of cached macro templates in the Velocity engine so that we can compare after the
    // document is rendered.
    JMXVelocityEngineMBean mbean = new JMXVelocityEngine(vengine);
    int cachedMacroNamespaceSize = mbean.getTemplates().values().size();
    assertTrue(cachedMacroNamespaceSize > 0);
    mockVelocityManager.stubs().method("getVelocityEngine").will(returnValue(vengine));
    mockVelocityManager.stubs().method("evaluate").will(new CustomStub("evaluate") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            return vengine.evaluate(vcontext, (Writer) invocation.parameterValues.get(0), (String) invocation.parameterValues.get(1), (Reader) invocation.parameterValues.get(2));
        }
    });
    // $doc.display will ask for the syntax of the current document so we need to mock it.
    this.mockXWiki.stubs().method("getCurrentContentSyntaxId").with(eq("xwiki/2.0"), ANYTHING).will(returnValue("xwiki/2.0"));
    // Verify that the macro located inside the TextArea has been taken into account when executing the doc's
    // content.
    assertEquals("<p>ok</p>", this.document.getRenderedContent(getContext()));
    // Now verify that the Velocity Engine doesn't contain any more cached macro namespace to prove that
    // getRenderedContent has correctly cleaned the Velocity macro cache.
    assertEquals(cachedMacroNamespaceSize, mbean.getTemplates().values().size());
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) JMXVelocityEngine(org.xwiki.velocity.internal.jmx.JMXVelocityEngine) Invocation(org.jmock.core.Invocation) VelocityFactory(org.xwiki.velocity.VelocityFactory) VelocityContext(org.apache.velocity.VelocityContext) JMXVelocityEngineMBean(org.xwiki.velocity.internal.jmx.JMXVelocityEngineMBean) Reader(java.io.Reader) Document(com.xpn.xwiki.api.Document) Properties(java.util.Properties) Mock(org.jmock.Mock) JMXVelocityEngine(org.xwiki.velocity.internal.jmx.JMXVelocityEngine) CustomStub(org.jmock.core.stub.CustomStub) VelocityManager(org.xwiki.velocity.VelocityManager) BaseObject(com.xpn.xwiki.objects.BaseObject) Writer(java.io.Writer)

Aggregations

Document (com.xpn.xwiki.api.Document)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 Properties (java.util.Properties)1 VelocityContext (org.apache.velocity.VelocityContext)1 Mock (org.jmock.Mock)1 Invocation (org.jmock.core.Invocation)1 CustomStub (org.jmock.core.stub.CustomStub)1 VelocityEngine (org.xwiki.velocity.VelocityEngine)1 VelocityFactory (org.xwiki.velocity.VelocityFactory)1 VelocityManager (org.xwiki.velocity.VelocityManager)1 JMXVelocityEngine (org.xwiki.velocity.internal.jmx.JMXVelocityEngine)1 JMXVelocityEngineMBean (org.xwiki.velocity.internal.jmx.JMXVelocityEngineMBean)1