use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class DefaultNotificationDisplayer method renderNotification.
@Override
public Block renderNotification(CompositeEvent eventNotification) throws NotificationException {
try {
velocityManager.getCurrentVelocityContext().put(EVENT_BINDING_NAME, eventNotification);
String templateName = String.format("notification/%s.vm", eventNotification.getType().replaceAll("\\/", "."));
Template template = templateManager.getTemplate(templateName);
return (template != null) ? templateManager.execute(template) : templateManager.execute("notification/default.vm");
} catch (Exception e) {
throw new NotificationException("Failed to render the notification.", e);
} finally {
velocityManager.getCurrentVelocityContext().remove(EVENT_BINDING_NAME);
}
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class DefaultNotificationFilterDisplayer method display.
@Override
public Block display(NotificationFilter filter, NotificationFilterPreference preference) throws NotificationException {
try {
setUpContext(scriptContextManager, filter, preference);
// Try to get a template using the filter name ; if no template is found, fallback on the default one.
String templateName = String.format("notification/filters/%s.vm", filter.getName().replaceAll("\\/", "."));
Template template = templateManager.getTemplate(templateName);
return (template != null) ? templateManager.execute(template) : templateManager.execute("notification/filters/default.vm");
} catch (Exception e) {
throw new NotificationException(String.format("Failed to display the notification filter [%s] with the filter preference [%s].", filter, preference), e);
} finally {
cleanUpContext();
}
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class Less4jCompilerTest method compile.
@Test
public void compile() throws Exception {
// Mocks
when(skinManager.getSkin("skin")).thenReturn(skin);
// Is is actually more an integration test than a unit test
// Import 1
when(skin.getResource("less/style.less.vm")).thenReturn(mock(Resource.class));
StringWriter import1source = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/style.less.vm").getFile()), import1source);
when(templateManager.renderFromSkin("less/style.less.vm", skin)).thenReturn(import1source.toString());
// Import 2
when(skin.getResource("less/subdir/import2.less")).thenReturn(mock(Resource.class));
Template import2 = mock(Template.class);
when(templateManager.getTemplate("less/subdir/import2.less", skin)).thenReturn(import2);
TemplateContent importContent2 = mock(TemplateContent.class);
when(import2.getContent()).thenReturn(importContent2);
StringWriter import2source = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/import2.less").getFile()), import2source);
when(importContent2.getContent()).thenReturn(import2source.toString());
// Import 3
when(skin.getResource("less/subdir/import3.less")).thenReturn(mock(Resource.class));
Template import3 = mock(Template.class);
when(templateManager.getTemplate("less/subdir/import3.less", skin)).thenReturn(import3);
TemplateContent importContent3 = mock(TemplateContent.class);
when(import3.getContent()).thenReturn(importContent3);
StringWriter import3source = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/import3.less").getFile()), import3source);
when(importContent3.getContent()).thenReturn(import3source.toString());
// Test
StringWriter source = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/style3.less").getFile()), source);
String result = mocker.getComponentUnderTest().compile(source.toString(), "skin", false);
// Now with sourcemaps.
String result2 = mocker.getComponentUnderTest().compile(source.toString(), "skin", true);
// Verify
StringWriter expected = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/style3.css").getFile()), expected);
assertEquals(expected.toString(), result);
assertTrue(result2.contains("/*# sourceMappingURL=data:application/json;base64,"));
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class LESSSkinFileResourceReferenceTest method getContentWhenException.
@Test
public void getContentWhenException() throws Exception {
LESSSkinFileResourceReference lessSkinFileResourceReference = new LESSSkinFileResourceReference("file.less", templateManager, skinManager);
// Mocks
Template template = mock(Template.class);
when(templateManager.getTemplate("less/file.less", skin)).thenReturn(template);
Exception exception = new Exception("exception");
when(template.getContent()).thenThrow(exception);
// Test
LESSCompilerException caughtException = null;
try {
lessSkinFileResourceReference.getContent("skin");
} catch (LESSCompilerException e) {
caughtException = e;
}
// Verify
assertNotNull(caughtException);
assertEquals("Failed to get the content of the template [file.less].", caughtException.getMessage());
assertEquals(exception, caughtException.getCause());
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class LESSSkinFileResourceReferenceTest method getContent.
@Test
public void getContent() throws Exception {
LESSSkinFileResourceReference lessSkinFileResourceReference = new LESSSkinFileResourceReference("style.less", templateManager, skinManager);
// Mocks
Template template = mock(Template.class);
when(templateManager.getTemplate("less/style.less", skin)).thenReturn(template);
TemplateContent templateContent = mock(TemplateContent.class);
when(template.getContent()).thenReturn(templateContent);
when(templateContent.getContent()).thenReturn("// My LESS file");
// Test
assertEquals("// My LESS file", lessSkinFileResourceReference.getContent("skin"));
}
Aggregations