Search in sources :

Example 31 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class AbstractWComponent_Test method testVisibilityAndFlow.

@Test
public void testVisibilityAndFlow() {
    // a
    // /|\
    // b c d
    // |
    // e
    TestComp a = new TestComp("a");
    TestComp b = new TestComp("b");
    TestComp c = new TestComp("c");
    TestComp d = new TestComp("d");
    TestComp e = new TestComp("e");
    a.add(b);
    a.add(c);
    a.add(d);
    b.add(e);
    // c is invisible by default.
    c.setVisible(false);
    a.setLocked(true);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    // The standard lifecycle flow.
    a.serviceRequest(request);
    a.preparePaint(request);
    a.paint(new WebXmlRenderContext(printWriter));
    // Check that lifecycle methods were called as expected.
    Assert.assertEquals("Handle request should have been called on 'a'", 1, a.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been called on 'b'", 1, b.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been called on static invisible 'c'", 0, c.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been calle on 'd'", 1, d.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been calle on 'e'", 1, e.getHandleRequestCount());
    Assert.assertEquals("Prepare paint should have been called on 'a'", 1, a.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on 'b'", 1, b.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on static invisible 'c'", 0, c.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on 'd'", 1, d.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on 'e'", 1, e.getPreparePaintCount());
    Assert.assertEquals("Incorrect paint order", "abed", stringWriter.toString());
    // Reset the TestComp call flags.
    a.reset();
    // Make b invisible for user
    b.setVisible(false);
    // Create a new writer.
    stringWriter = new StringWriter();
    printWriter = new PrintWriter(stringWriter);
    a.serviceRequest(request);
    a.preparePaint(request);
    a.paint(new WebXmlRenderContext(printWriter));
    // Check that lifecycle methods were called as expected.
    Assert.assertEquals("Handle request should have been called on 'a'", 1, a.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been called on dynamic invisible 'b'", 0, b.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been called on static invisible 'c'", 0, c.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been calle on 'd'", 1, d.getHandleRequestCount());
    Assert.assertEquals("Handle request should have been calle on child 'e' of dynamic invisible 'b'", 0, e.getHandleRequestCount());
    Assert.assertEquals("Prepare paint should have been called on 'a'", 1, a.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on dynamic invisible 'b'", 0, b.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on static invisible 'c'", 0, c.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on 'd'", 1, d.getPreparePaintCount());
    Assert.assertEquals("Prepare paint should have been called on child 'e' of dynamic invisible 'b'", 0, e.getPreparePaintCount());
    Assert.assertEquals("Incorrect paint order", "ad", stringWriter.toString());
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 32 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class UicProfileButton_Test method testAfterPaintButtonPressed.

@Test
public void testAfterPaintButtonPressed() {
    UicProfileButton button = new UicProfileButton();
    button.setLocked(true);
    setActiveContext(createUIContext());
    StringWriter strWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(strWriter);
    MockRequest request = new MockRequest();
    button.setPressed(true, request);
    button.afterPaint(new WebXmlRenderContext(writer));
    Assert.assertTrue("for button pressed afterpaint writer output should start with <br/><br/>", strWriter.toString().startsWith("<br/><br/>"));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 33 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class UicProfileButton_Test method testAfterPaintButtonNotPressed.

@Test
public void testAfterPaintButtonNotPressed() {
    UicProfileButton button = new UicProfileButton();
    button.setLocked(true);
    setActiveContext(createUIContext());
    StringWriter strWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(strWriter);
    MockRequest request = new MockRequest();
    button.setPressed(false, request);
    button.afterPaint(new WebXmlRenderContext(writer));
    Assert.assertEquals("for button not pressed afterPaint writer output should be empty", strWriter.toString(), "");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 34 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class WPopup_Test method testNotVisibleAfterPaint.

@Test
public void testNotVisibleAfterPaint() {
    WPopup popup = new WPopup();
    // Should default to not visible
    Assert.assertFalse("Popup should not be visible by default", popup.isVisible());
    // Make visible
    popup.setLocked(true);
    setActiveContext(createUIContext());
    popup.setVisible(true);
    // Check not visible after paint
    popup.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertFalse("Popup should not be visible after paint", popup.isVisible());
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 35 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class WebUtilities method renderWithTransformToHTML.

/**
 * Renders and transforms the given WComponent to a HTML String outside of the context of a Servlet.
 *
 * @param request the request being responded to
 * @param component the root WComponent to render
 * @param includePageShell true if include page shell
 * @return the rendered output as a String.
 */
public static String renderWithTransformToHTML(final Request request, final WComponent component, final boolean includePageShell) {
    // Setup a context (if needed)
    boolean needsContext = UIContextHolder.getCurrent() == null;
    if (needsContext) {
        UIContextHolder.pushContext(new UIContextImpl());
    }
    try {
        // Link Interceptors
        InterceptorComponent templateRender = new TemplateRenderInterceptor();
        InterceptorComponent transformXML = new TransformXMLInterceptor();
        templateRender.setBackingComponent(transformXML);
        if (includePageShell) {
            transformXML.setBackingComponent(new PageShellInterceptor());
        }
        // Attach Component and Mock Response
        InterceptorComponent chain = templateRender;
        chain.attachUI(component);
        chain.attachResponse(new MockResponse());
        // Render chain
        StringWriter buffer = new StringWriter();
        chain.preparePaint(request);
        try (PrintWriter writer = new PrintWriter(buffer)) {
            chain.paint(new WebXmlRenderContext(writer));
        }
        return buffer.toString();
    } finally {
        if (needsContext) {
            UIContextHolder.popContext();
        }
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) InterceptorComponent(com.github.bordertech.wcomponents.container.InterceptorComponent) StringWriter(java.io.StringWriter) TransformXMLInterceptor(com.github.bordertech.wcomponents.container.TransformXMLInterceptor) PageShellInterceptor(com.github.bordertech.wcomponents.container.PageShellInterceptor) TemplateRenderInterceptor(com.github.bordertech.wcomponents.container.TemplateRenderInterceptor) PrintWriter(java.io.PrintWriter)

Aggregations

WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)63 PrintWriter (java.io.PrintWriter)48 StringWriter (java.io.StringWriter)27 Test (org.junit.Test)27 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)17 UIContext (com.github.bordertech.wcomponents.UIContext)15 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)7 SystemException (com.github.bordertech.wcomponents.util.SystemException)7 WText (com.github.bordertech.wcomponents.WText)6 RenderContext (com.github.bordertech.wcomponents.RenderContext)5 WComponent (com.github.bordertech.wcomponents.WComponent)5 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)5 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)4 Configuration (org.apache.commons.configuration.Configuration)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 AllComponents (com.github.bordertech.wcomponents.AllComponents)3 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)3 Response (com.github.bordertech.wcomponents.Response)3