Search in sources :

Example 1 with VelocityRenderer

use of com.github.bordertech.wcomponents.render.webxml.VelocityRenderer in project wcomponents by BorderTech.

the class VelocityRenderer_Test method testGetUrl.

@Test
public void testGetUrl() {
    String layoutUrl = "com/github/bordertech/wcomponents/velocity/VelocityRenderer_Test.vm";
    VelocityRenderer layout = new VelocityRenderer(layoutUrl);
    Assert.assertEquals("Incorrect url", layoutUrl, layout.getUrl());
}
Also used : VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer) Test(org.junit.Test)

Example 2 with VelocityRenderer

use of com.github.bordertech.wcomponents.render.webxml.VelocityRenderer in project wcomponents by BorderTech.

the class UIManager method createRenderer.

/**
 * Attempts to create a Renderer with the given name.
 *
 * @param rendererName the name of the Renderer
 * @return a Renderer of the given type, or null if the class was not found.
 */
private static Renderer createRenderer(final String rendererName) {
    if (rendererName.endsWith(".vm")) {
        // This is a velocity template, so use a VelocityLayout
        return new VelocityRenderer(rendererName);
    }
    try {
        Class<?> managerClass = Class.forName(rendererName);
        Object manager = managerClass.newInstance();
        if (!(manager instanceof Renderer)) {
            throw new SystemException(rendererName + " is not a Renderer");
        }
        return (Renderer) manager;
    } catch (ClassNotFoundException e) {
        // Legal - there might not a manager implementation in a given theme
        return null;
    } catch (InstantiationException e) {
        throw new SystemException("Failed to instantiate " + rendererName, e);
    } catch (IllegalAccessException e) {
        throw new SystemException("Failed to access " + rendererName, e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Renderer(com.github.bordertech.wcomponents.Renderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)

Example 3 with VelocityRenderer

use of com.github.bordertech.wcomponents.render.webxml.VelocityRenderer in project wcomponents by BorderTech.

the class VelocityRenderer_Test method testPaint.

@Test
public void testPaint() {
    TestComponent component = new TestComponent();
    VelocityRenderer layout = new VelocityRenderer("com/github/bordertech/wcomponents/velocity/VelocityRenderer_Test.vm");
    setActiveContext(createUIContext());
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    layout.render(component, new WebXmlRenderContext(printWriter));
    printWriter.close();
    String result = writer.toString();
    Assert.assertTrue("Missing 'this'", result.contains("this=com.github.bordertech.wcomponents.velocity.VelocityRenderer_Test$TestComponent"));
    Assert.assertTrue("Missing 'this.someProperty'", result.contains("[this.someProperty=somePropertyValue]"));
    Assert.assertTrue("Missing 'uicontext'", result.contains("[uicontext=com.github.bordertech.wcomponents.UIContextImpl"));
    Assert.assertTrue("Missing 'uic'", result.contains("uicontext=com.github.bordertech.wcomponents.UIContextImpl"));
    Assert.assertTrue("Missing 'children'", result.contains("[children=[A, B, C, D]]"));
    Assert.assertTrue("Missing 'childA'", result.contains("[childA=A]"));
    Assert.assertTrue("Missing 'childB'", result.contains("[childB=B]"));
    Assert.assertTrue("Missing 'childC'", result.contains("[childC=]"));
    Assert.assertTrue("Missing 'test_list'", result.contains("[test_list=[C, D]]"));
    Assert.assertTrue("Missing velocity map params", result.contains("[velocityMapParam=velocityMapParamValue]"));
    Assert.assertTrue("Map should have been used", component.mapUsedCalled);
    Assert.assertFalse("Should not contain debug markers", result.contains("<!-- Start"));
    Config.getInstance().setProperty(ConfigurationProperties.DEVELOPER_VELOCITY_DEBUG, "true");
    writer = new StringWriter();
    printWriter = new PrintWriter(writer);
    layout.render(component, new WebXmlRenderContext(printWriter));
    printWriter.close();
    result = writer.toString();
    int startIndex = result.indexOf("<!-- Start " + layout.getUrl() + " -->");
    int endIndex = result.indexOf("<!-- End   " + layout.getUrl() + " -->");
    Assert.assertTrue("Should contain start debug marker", startIndex != -1);
    Assert.assertTrue("Should contain end debug marker", endIndex != -1);
    Assert.assertTrue("Start debug marker should be before end debug marker", startIndex < endIndex);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with VelocityRenderer

use of com.github.bordertech.wcomponents.render.webxml.VelocityRenderer in project wcomponents by BorderTech.

the class VelocityRenderer_Test method testPaintMissingTemplate.

@Test
public void testPaintMissingTemplate() {
    TestComponent component = new TestComponent();
    VelocityRenderer layout = new VelocityRenderer("/com/github/bordertech/wcomponents/velocity/VelocityRenderer_Test_missing_template.vm");
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    layout.render(component, new WebXmlRenderContext(printWriter));
    printWriter.close();
    // This should still render, and just dump out the contents of the Velocity context.
    String result = writer.toString();
    Assert.assertTrue("Should have rendered output", result.contains("<td>this</td>"));
    Assert.assertTrue("Should have rendered output", result.contains("<td>" + component.getClass().getName() + "</td>"));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) VelocityRenderer(com.github.bordertech.wcomponents.render.webxml.VelocityRenderer) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

VelocityRenderer (com.github.bordertech.wcomponents.render.webxml.VelocityRenderer)4 Test (org.junit.Test)3 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Renderer (com.github.bordertech.wcomponents.Renderer)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1