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