Search in sources :

Example 26 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class CacheMacroTest method executeWhenNoIdAndDifferentContent.

@Test
public void executeWhenNoIdAndDifferentContent() throws Exception {
    String expected1 = "beginDocument\n" + "beginMacroMarkerStandalone [velocity] [] [$var]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMacroMarkerStandalone [velocity] [] [$var]\n" + "endDocument";
    String expected2 = "beginDocument\n" + "beginMacroMarkerStandalone [velocity] [] [$var##]\n" + "beginParagraph\n" + "onWord [newcontent]\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(expected1, result, this.rendererFactory);
    // Execute a second time with a different cache macro content to ensure it's not cached
    velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", "#set ($var = 'newcontent')");
    result = this.cacheMacro.execute(params, "{{velocity}}$var##{{/velocity}}", context);
    assertBlocks(expected2, 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)

Example 27 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class WebJarsResourceReferenceHandlerTest method failingResourceEvaluation.

@Test
public void failingResourceEvaluation() throws Exception {
    WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList("angular", "2.1.11", "angular.js"));
    reference.addParameter("evaluate", "true");
    ByteArrayInputStream resourceStream = new ByteArrayInputStream("content".getBytes());
    when(this.classLoader.getResourceAsStream("META-INF/resources/webjars/angular/2.1.11/angular.js")).thenReturn(resourceStream);
    VelocityManager velocityManager = this.componentManager.getInstance(VelocityManager.class);
    VelocityEngine velocityEngine = mock(VelocityEngine.class);
    when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
    when(velocityEngine.evaluate(any(), any(), eq("angular/2.1.11/angular.js"), any(Reader.class))).thenThrow(new VelocityException("Bad code!"));
    this.handler.handle(reference, this.chain);
    // Verify the exception is logged.
    verify(this.componentManager.getMockedLogger()).error(eq("Failed to evaluate the Velocity code from WebJar resource [angular/2.1.11/angular.js]"), any(ResourceReferenceHandlerException.class));
    // Verify that the client is properly notified about the failure.
    verify(this.response.getHttpServletResponse()).sendError(500, "Failed to evaluate the Velocity code from WebJar resource [angular/2.1.11/angular.js]");
    // The next handlers are still called.
    verify(this.chain).handleNext(reference);
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) VelocityEngine(org.xwiki.velocity.VelocityEngine) WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityException(org.apache.velocity.exception.VelocityException) Reader(java.io.Reader) Test(org.junit.Test)

Example 28 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class WikiUIExtensionComponentBuilderTest method buildComponents.

@Test
public void buildComponents() throws Exception {
    BaseObject extensionObject = createExtensionObject("name", "extensionPointId", "content", "key=value=foo\nkey2=value2\ninvalid=\n\n=invalid", "user");
    ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
    VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
    List<WikiComponent> components = this.mocker.getComponentUnderTest().buildComponents(extensionObject);
    assertEquals(1, components.size());
    verify(contentParser).parse("content", Syntax.XWIKI_2_1, DOC_REF);
    UIExtension uiExtension = (UIExtension) components.get(0);
    Map<String, String> parameters = uiExtension.getParameters();
    assertEquals(2, parameters.size());
    verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("name:key"), eq("value=foo"));
    verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("name:key2"), eq("value2"));
}
Also used : StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) WikiComponent(org.xwiki.component.wiki.WikiComponent) VelocityContext(org.apache.velocity.VelocityContext) ContentParser(org.xwiki.component.wiki.internal.bridge.ContentParser) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 29 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class WikiUIExtensionComponentBuilderTest method configure.

@Before
public void configure() throws Exception {
    // Required by BaseObjectReference
    DocumentReferenceResolver<String> resolver = this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
    when(resolver.resolve("XWiki.UIExtension")).thenReturn(new DocumentReference("xwiki", "XWiki", "UIExtensionClass"));
    DelegateComponentManager wikiComponentManager = new DelegateComponentManager();
    wikiComponentManager.setComponentManager(this.mocker);
    this.mocker.registerComponent(ComponentManager.class, "wiki", wikiComponentManager);
    // Components accessed through dynamic lookup.
    VelocityManager velocityManager = this.mocker.registerMockComponent(VelocityManager.class);
    when(velocityManager.getVelocityEngine()).thenReturn(mock(VelocityEngine.class));
    when(velocityManager.getVelocityContext()).thenReturn(mock(VelocityContext.class));
    ModelContext modelContext = this.mocker.registerMockComponent(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(DOC_REF);
    this.mocker.registerMockComponent(RenderingContext.class);
    this.mocker.registerMockComponent(Transformation.class, "macro");
    this.mocker.registerMockComponent(ContentParser.class);
    // The document holding the UI extension object.
    this.componentDoc = mock(XWikiDocument.class, "xwiki:XWiki.MyUIExtension");
    when(this.componentDoc.getDocumentReference()).thenReturn(DOC_REF);
    when(this.componentDoc.getAuthorReference()).thenReturn(AUTHOR_REFERENCE);
    when(this.componentDoc.getContentAuthorReference()).thenReturn(AUTHOR_REFERENCE);
    when(this.componentDoc.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.oldcore.getDocuments().put(DOC_REF, componentDoc);
}
Also used : ModelContext(org.xwiki.model.ModelContext) VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DelegateComponentManager(org.xwiki.component.internal.multi.DelegateComponentManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 30 with VelocityManager

use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.

the class WikiUIExtensionParametersTest method setUp.

@Before
public void setUp() throws Exception {
    VelocityManager velocityManager = componentManager.registerMockComponent(VelocityManager.class);
    execution = componentManager.registerMockComponent(Execution.class);
    modelContext = componentManager.registerMockComponent(ModelContext.class);
    velocityEngine = mock(VelocityEngine.class);
    velocityContext = new VelocityContext();
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    when(velocityManager.getVelocityContext()).thenReturn(velocityContext);
    when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
}
Also used : ModelContext(org.xwiki.model.ModelContext) VelocityEngine(org.xwiki.velocity.VelocityEngine) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) Before(org.junit.Before)

Aggregations

VelocityManager (org.xwiki.velocity.VelocityManager)31 VelocityContext (org.apache.velocity.VelocityContext)19 Test (org.junit.Test)15 VelocityEngine (org.xwiki.velocity.VelocityEngine)15 StringWriter (java.io.StringWriter)12 DocumentReference (org.xwiki.model.reference.DocumentReference)8 Reader (java.io.Reader)6 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 XWikiException (com.xpn.xwiki.XWikiException)4 BaseObject (com.xpn.xwiki.objects.BaseObject)4 IOException (java.io.IOException)4 Writer (java.io.Writer)4 Map (java.util.Map)4 Properties (java.util.Properties)4 Execution (org.xwiki.context.Execution)4 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Expectations (org.jmock.Expectations)3 Invocation (org.jmock.api.Invocation)3