Search in sources :

Example 11 with Template

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);
    }
}
Also used : NotificationException(org.xwiki.notifications.NotificationException) NotificationException(org.xwiki.notifications.NotificationException) Template(org.xwiki.template.Template)

Example 12 with Template

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();
    }
}
Also used : NotificationException(org.xwiki.notifications.NotificationException) NotificationException(org.xwiki.notifications.NotificationException) Template(org.xwiki.template.Template)

Example 13 with Template

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,"));
}
Also used : StringWriter(java.io.StringWriter) TemplateContent(org.xwiki.template.TemplateContent) Resource(org.xwiki.skin.Resource) FileInputStream(java.io.FileInputStream) Template(org.xwiki.template.Template) Test(org.junit.Test)

Example 14 with Template

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());
}
Also used : LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) Template(org.xwiki.template.Template) Test(org.junit.Test)

Example 15 with Template

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"));
}
Also used : TemplateContent(org.xwiki.template.TemplateContent) Template(org.xwiki.template.Template) Test(org.junit.Test)

Aggregations

Template (org.xwiki.template.Template)15 Test (org.junit.Test)4 NotificationException (org.xwiki.notifications.NotificationException)3 TemplateContent (org.xwiki.template.TemplateContent)3 StringWriter (java.io.StringWriter)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 Skin (org.xwiki.skin.Skin)2 SyndContent (com.rometools.rome.feed.synd.SyndContent)1 SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)1 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)1 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)1 SyndPerson (com.rometools.rome.feed.synd.SyndPerson)1 SyndPersonImpl (com.rometools.rome.feed.synd.SyndPersonImpl)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ScriptContext (javax.script.ScriptContext)1