Search in sources :

Example 11 with VelocityEngine

use of org.xwiki.velocity.VelocityEngine 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);
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) SkinExtension(org.xwiki.skinx.SkinExtension) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) VelocityContext(org.apache.velocity.VelocityContext) WordBlock(org.xwiki.rendering.block.WordBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Reader(java.io.Reader) Properties(java.util.Properties) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Map(java.util.Map) Writer(java.io.Writer)

Example 12 with VelocityEngine

use of org.xwiki.velocity.VelocityEngine 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");
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 13 with VelocityEngine

use of org.xwiki.velocity.VelocityEngine 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");
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 14 with VelocityEngine

use of org.xwiki.velocity.VelocityEngine 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());
    }
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) MessagingException(javax.mail.MessagingException) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) Test(org.junit.Test)

Example 15 with VelocityEngine

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

the class DefaultOldRendering method parseContent.

@Override
public String parseContent(String content, XWikiContext xcontext) {
    try {
        if (StringUtils.isNotEmpty(content)) {
            VelocityManager velocityManager = this.velocityManagerProvider.get();
            VelocityContext velocityContext = velocityManager.getVelocityContext();
            VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
            StringWriter writer = new StringWriter();
            velocityEngine.evaluate(velocityContext, writer, xcontext.getDoc().getPrefixedFullName(), content);
            return writer.toString();
        }
    } catch (XWikiVelocityException e) {
        this.logger.error("Faield to parse content [" + content + "]", e);
    }
    return "";
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext)

Aggregations

VelocityEngine (org.xwiki.velocity.VelocityEngine)21 VelocityContext (org.apache.velocity.VelocityContext)18 VelocityManager (org.xwiki.velocity.VelocityManager)14 Test (org.junit.Test)11 Reader (java.io.Reader)6 StringWriter (java.io.StringWriter)6 Writer (java.io.Writer)6 Properties (java.util.Properties)5 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 Map (java.util.Map)4 XWikiVelocityException (org.xwiki.velocity.XWikiVelocityException)4 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 Invocation (org.jmock.api.Invocation)3 Execution (org.xwiki.context.Execution)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringReader (java.io.StringReader)2 Description (org.hamcrest.Description)2