use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class IntegrationTest method initialize.
@RenderingTestSuite.Initialized
@SuppressWarnings("unchecked")
public void initialize(final MockingComponentManager componentManager) throws Exception {
Mockery mockery = new JUnit4Mockery();
// Since we have a dependency on XWiki Platform Oldcore the Context Component Manager will be found and the
// test will try to look up the Dashboard macro in the User and Wiki Component Manager and thus need a Current
// User and a Current Wiki. It's easier for this test to simply unregister the Context Component Manager rather
// than have to provide mocks for them.
componentManager.unregisterComponent(ComponentManager.class, "context");
final SkinExtension mockSsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "ssfx", "ssfxMock");
final SkinExtension mockJsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "jsfx", "jsfxMock");
mockery.checking(new Expectations() {
{
allowing(mockSsfx).use(with("uicomponents/container/columns.css"), with(any(Map.class)));
allowing(mockSsfx).use(with("uicomponents/dashboard/dashboard.css"), with(any(Map.class)));
allowing(mockJsfx).use(with("js/scriptaculous/dragdrop.js"));
allowing(mockJsfx).use(with("js/scriptaculous/effects.js"));
allowing(mockJsfx).use(with("uicomponents/dashboard/dashboard.js"), with(any(Map.class)));
}
});
final GadgetSource mockGadgetSource = componentManager.registerMockComponent(mockery, GadgetSource.class);
mockery.checking(new Expectations() {
{
// Mock gadget for macrodashboard_nested_velocity.test
allowing(mockGadgetSource).getGadgets(with("nested_velocity"), with(any(MacroTransformationContext.class)));
will(returnValue(Arrays.asList(new Gadget("0", Arrays.asList(new WordBlock("title")), Arrays.asList(new MacroMarkerBlock("velocity", Collections.emptyMap(), "someVelocityCodeHere", Collections.singletonList(new WordBlock("someVelocityOutputHere")), true)), "1,1"))));
// Mock gadget for macrodashboard1.test
allowing(mockGadgetSource).getGadgets(with(aNull(String.class)), with(any(MacroTransformationContext.class)));
will(returnValue(Arrays.asList(new Gadget("0", Arrays.<Block>asList(new WordBlock("title")), Arrays.<Block>asList(new WordBlock("content")), "1,1"))));
allowing(mockGadgetSource).getDashboardSourceMetadata(with(AnyOf.anyOf(aNull(String.class), any(String.class))), with(any(MacroTransformationContext.class)));
will(returnValue(Collections.<Block>emptyList()));
allowing(mockGadgetSource).isEditing();
// return true on is editing, to take as many paths possible
will(returnValue(true));
}
});
// Mock VelocityManager used in macrodashboard_nested_velocity.test because we do not have an XWikiContext
// instance in the ExecutionContext.
final VelocityManager mockVelocityManager = componentManager.registerMockComponentWithId(mockery, VelocityManager.class, "velocityManagerMock");
mockery.checking(new Expectations() {
{
allowing(mockVelocityManager).getVelocityContext();
will(returnValue(new VelocityContext()));
allowing(mockVelocityManager).getCurrentVelocityContext();
will(returnValue(new VelocityContext()));
allowing(mockVelocityManager).getVelocityEngine();
will(doAll(new CustomAction("mockGetVelocityEngine") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
VelocityEngine velocityEngine = componentManager.getInstance(VelocityEngine.class);
Properties properties = new Properties();
properties.setProperty("resource.loader", "file");
velocityEngine.initialize(properties);
return velocityEngine;
}
}));
allowing(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
will(doAll(new CustomAction("mockEvaluate") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
VelocityEngine velocityEngine = mockVelocityManager.getVelocityEngine();
return velocityEngine.evaluate(mockVelocityManager.getVelocityContext(), (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
}
}));
}
});
componentManager.registerMockComponent(mockery, ContextualAuthorizationManager.class);
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluateWithLanguage.
@Test
public void evaluateWithLanguage() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(1);
when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(1), eq("html"))).thenReturn("Salut <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Salut <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenReturn("Salut <b>John Doe</b> <br />john@doe.com");
// Set the default Locale to be different from the locale we pass to verify we restore it properly
when(this.xwikiContext.getLocale()).thenReturn(Locale.ITALIAN);
String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(), Locale.FRENCH);
verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
// Make sure we set the right locale in the XWiki Context
verify(this.xwikiContext).setLocale(Locale.FRENCH);
verify(this.xwikiContext).setLocale(Locale.ITALIAN);
assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluate.
@Test
public void evaluate() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(documentBridge.getProperty(same(documentReference), any(), anyInt(), eq("html"))).thenReturn("Hello <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Hello <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenReturn("Hello <b>John Doe</b> <br />john@doe.com");
String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap());
assertEquals(result, "Hello <b>John Doe</b> <br />john@doe.com");
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluateWhenVelocityError.
@Test
public void evaluateWhenVelocityError() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(documentBridge.getProperty(same(documentReference), any(), anyInt(), eq("html"))).thenReturn("Hello <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Hello <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenThrow(new XWikiException(0, 0, "Error"));
try {
this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.<String, Object>emptyMap());
fail("Should have thrown an exception here!");
} catch (MessagingException expected) {
assertEquals("Failed to evaluate property [html] for Document [wiki:space.page] and locale [null]", expected.getMessage());
}
}
use of org.xwiki.velocity.VelocityManager in project xwiki-platform by xwiki.
the class MailSenderPlugin method prepareVelocityContext.
/**
* Prepares a Mail Velocity context
*
* @param fromAddr Mail from
* @param toAddr Mail to
* @param ccAddr Mail cc
* @param bccAddr Mail bcc
* @param vcontext The Velocity context to prepare
* @return The prepared context
*/
public VelocityContext prepareVelocityContext(String fromAddr, String toAddr, String ccAddr, String bccAddr, VelocityContext vcontext, XWikiContext context) {
if (vcontext == null) {
// Use the original velocity context as a starting point
VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
vcontext = new VelocityContext(velocityManager.getVelocityContext());
}
vcontext.put("from.name", fromAddr);
vcontext.put("from.address", fromAddr);
vcontext.put("to.name", toAddr);
vcontext.put("to.address", toAddr);
vcontext.put("to.cc", ccAddr);
vcontext.put("to.bcc", bccAddr);
vcontext.put("bounce", fromAddr);
return vcontext;
}
Aggregations