@Test public void evaluateWithErrorNoObjectMatches() throws Exception { XWikiDocument document = mock(XWikiDocument.class); when(this.xwiki.getDocument(any(DocumentReference.class), any())).thenReturn(document); BaseObject object1 = mock(BaseObject.class); BaseObject object2 = mock(BaseObject.class); List<BaseObject> xobjects = Arrays.asList(object1, object2); when(document.getXObjects(any())).thenReturn(xobjects); DocumentAccessBridge documentBridge = this.componentManager.getInstance(DocumentAccessBridge.class); DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1); when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(-1); Throwable exception = assertThrows(MessagingException.class, () -> this.templateManager.evaluate(documentReference, "html", Collections.emptyMap(), Locale.FRENCH)); assertEquals("No [Document XWiki.Mail] object matches the locale [fr] or the default locale [en] " + "in the Document [wiki:space.page]", exception.getMessage()); }
@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);  }