@Test public void evaluate() throws Exception { DocumentAccessBridge documentBridge = this.componentManager.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.componentManager.getInstance(VelocityManager.class); when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine); when(this.velocityEvaluator.evaluateVelocity(eq("Hello <b>${name}</b> <br />${email}"), any(), any())).thenReturn("Hello <b>John Doe</b> <br />john@doe.com"); String result = this.templateManager.evaluate(documentReference, "html", Collections.emptyMap()); assertEquals("Hello <b>John Doe</b> <br />john@doe.com", result); }
@Override public String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables, Object localeValue) throws MessagingException { Locale locale = getLocale(localeValue); DocumentReference mailClassReference = this.resolver.resolve(MAIL_CLASS, templateReference.getWikiReference()); VelocityContext velocityContext = createVelocityContext(velocityVariables); String templateFullName = this.serializer.serialize(templateReference); int objectNumber = getObjectMailNumber(templateReference, mailClassReference, locale); String content = this.documentBridge.getProperty(templateReference, mailClassReference, objectNumber, property).toString(); XWikiContext xcontext = this.xwikiContextProvider.get(); XWikiURLFactory originalURLFactory = xcontext.getURLFactory(); Locale originalLocale = xcontext.getLocale(); try { xcontext.setURLFactory(new ExternalServletURLFactory(xcontext)); xcontext.setLocale(locale); return velocityEvaluator.evaluateVelocity(content, templateFullName, velocityContext); } catch (XWikiException e) { throw new MessagingException(String.format( "Failed to evaluate property [%s] for Document [%s] and locale [%s]", property, templateReference, localeValue), e); } finally { xcontext.setURLFactory(originalURLFactory); xcontext.setLocale(originalLocale); } }
DefaultMailTemplateManager implements MailTemplateManager { @Override public String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables, Object localeValue) throws MessagingException { Locale locale = getLocale(localeValue); DocumentReference mailClassReference = this.resolver.resolve(MAIL_CLASS, templateReference.getWikiReference()); VelocityContext velocityContext = createVelocityContext(velocityVariables); String templateFullName = this.serializer.serialize(templateReference); int objectNumber = getObjectMailNumber(templateReference, mailClassReference, locale); String content = this.documentBridge.getProperty(templateReference, mailClassReference, objectNumber, property).toString(); XWikiContext xcontext = this.xwikiContextProvider.get(); XWikiURLFactory originalURLFactory = xcontext.getURLFactory(); Locale originalLocale = xcontext.getLocale(); try { xcontext.setURLFactory(new ExternalServletURLFactory(xcontext)); xcontext.setLocale(locale); return velocityEvaluator.evaluateVelocity(content, templateFullName, velocityContext); } catch (XWikiException e) { throw new MessagingException(String.format( "Failed to evaluate property [%s] for Document [%s] and locale [%s]", property, templateReference, localeValue), e); } finally { xcontext.setURLFactory(originalURLFactory); xcontext.setLocale(originalLocale); } } }
DefaultMailTemplateManager implements MailTemplateManager { @Override public String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables, Object localeValue) throws MessagingException { Locale locale = getLocale(localeValue); DocumentReference mailClassReference = this.resolver.resolve(MAIL_CLASS, templateReference.getWikiReference()); VelocityContext velocityContext = createVelocityContext(velocityVariables); String templateFullName = this.serializer.serialize(templateReference); int objectNumber = getObjectMailNumber(templateReference, mailClassReference, locale); String content = this.documentBridge.getProperty(templateReference, mailClassReference, objectNumber, property).toString(); XWikiContext xcontext = this.xwikiContextProvider.get(); XWikiURLFactory originalURLFactory = xcontext.getURLFactory(); Locale originalLocale = xcontext.getLocale(); try { xcontext.setURLFactory(new ExternalServletURLFactory(xcontext)); xcontext.setLocale(locale); return velocityEvaluator.evaluateVelocity(content, templateFullName, velocityContext); } catch (XWikiException e) { throw new MessagingException(String.format( "Failed to evaluate property [%s] for Document [%s] and locale [%s]", property, templateReference, localeValue), e); } finally { xcontext.setURLFactory(originalURLFactory); xcontext.setLocale(originalLocale); } }  }
DefaultMailTemplateManager implements MailTemplateManager { @Override public String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables, Object localeValue) throws MessagingException { Locale locale = getLocale(localeValue); DocumentReference mailClassReference = this.resolver.resolve(MAIL_CLASS, templateReference.getWikiReference()); VelocityContext velocityContext = createVelocityContext(velocityVariables); String templateFullName = this.serializer.serialize(templateReference); int objectNumber = getObjectMailNumber(templateReference, mailClassReference, locale); String content = this.documentBridge.getProperty(templateReference, mailClassReference, objectNumber, property).toString(); XWikiContext xcontext = this.xwikiContextProvider.get(); XWikiURLFactory originalURLFactory = xcontext.getURLFactory(); Locale originalLocale = xcontext.getLocale(); try { xcontext.setURLFactory(new ExternalServletURLFactory(xcontext)); xcontext.setLocale(locale); return velocityEvaluator.evaluateVelocity(content, templateFullName, velocityContext); } catch (XWikiException e) { throw new MessagingException(String.format( "Failed to evaluate property [%s] for Document [%s] and locale [%s]", property, templateReference, localeValue), e); } finally { xcontext.setURLFactory(originalURLFactory); xcontext.setLocale(originalLocale); } }  @Override String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables,
        Object localeValue); @Override String evaluate(DocumentReference templateReference, String property, Map<String, Object> data); }
DefaultMailTemplateManager implements MailTemplateManager { @Override public String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables, Object localeValue) throws MessagingException { Locale locale = getLocale(localeValue); DocumentReference mailClassReference = this.resolver.resolve(MAIL_CLASS, templateReference.getWikiReference()); VelocityContext velocityContext = createVelocityContext(velocityVariables); String templateFullName = this.serializer.serialize(templateReference); int objectNumber = getObjectMailNumber(templateReference, mailClassReference, locale); String content = this.documentBridge.getProperty(templateReference, mailClassReference, objectNumber, property).toString(); XWikiContext xcontext = this.xwikiContextProvider.get(); XWikiURLFactory originalURLFactory = xcontext.getURLFactory(); Locale originalLocale = xcontext.getLocale(); try { xcontext.setURLFactory(new ExternalServletURLFactory(xcontext)); xcontext.setLocale(locale); return velocityEvaluator.evaluateVelocity(content, templateFullName, velocityContext); } catch (XWikiException e) { throw new MessagingException(String.format( "Failed to evaluate property [%s] for Document [%s] and locale [%s]", property, templateReference, localeValue), e); } finally { xcontext.setURLFactory(originalURLFactory); xcontext.setLocale(originalLocale); } }  @Override String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables,
        Object localeValue); @Override String evaluate(DocumentReference templateReference, String property, Map<String, Object> data);  }