Search in sources :

Example 16 with WebXmlRenderContext

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

the class WhitespaceFilterInterceptor_Test method testPaint.

@Test
public void testPaint() {
    final String testString = "    foo    bar    ";
    final String filteredString = " foo bar ";
    WLabel label = new WLabel(testString);
    WhitespaceFilterInterceptor interceptor = new WhitespaceFilterInterceptor();
    interceptor.attachUI(label);
    label.setLocked(true);
    setActiveContext(createUIContext());
    // Test when disabled
    Config.getInstance().setProperty(ConfigurationProperties.WHITESPACE_FILTER, "false");
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    interceptor.paint(new WebXmlRenderContext(printWriter));
    printWriter.close();
    Assert.assertTrue("Should not have filtered text when disabled", writer.toString().contains(testString));
    // Test when enabled
    Config.getInstance().setProperty(ConfigurationProperties.WHITESPACE_FILTER, "true");
    writer = new StringWriter();
    printWriter = new PrintWriter(writer);
    interceptor.paint(new WebXmlRenderContext(printWriter));
    printWriter.close();
    Assert.assertTrue("Should have filtered text when enabled", writer.toString().contains(filteredString));
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) WLabel(com.github.bordertech.wcomponents.WLabel) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 17 with WebXmlRenderContext

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

the class WrongStepContentInterceptor_Test method sendContentRequest.

/**
 * Utility method to send a mock request to the application.
 *
 * @param target the target content.
 * @param clientStep the client-side step count
 * @param serverStep the server-side step count
 * @return the response.
 */
private MockResponse sendContentRequest(final WComponent target, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    uic.setUI(WebUtilities.getTop(target));
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment("/app", "http://localhost", "");
    env.setStep(serverStep);
    uic.setEnvironment(env);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, target.getId());
    request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
    MockResponse response = new MockResponse();
    InterceptorComponent interceptor = new WrongStepContentInterceptor();
    interceptor.attachUI(uic.getUI());
    interceptor.attachResponse(response);
    // Handle the request. This will either return the content or a step error
    try {
        interceptor.serviceRequest(request);
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ContentEscape escape) {
        try {
            // Content has been returned
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write content");
        }
    } catch (ErrorCodeEscape escape) {
        try {
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write error content");
        }
    } catch (ActionEscape ignored) {
    // don't care
    }
    // Step error
    return response;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) ContentEscape(com.github.bordertech.wcomponents.ContentEscape) IOException(java.io.IOException) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 18 with WebXmlRenderContext

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

the class ProfileContainer_Test method testAfterPaint.

/**
 * Test afterPaint.
 */
@Test
public void testAfterPaint() {
    ProfileContainer app = new ProfileContainer();
    app.setLocked(true);
    UIContext uic = new UIContextImpl();
    uic.setUI(app);
    setActiveContext(uic);
    WButton button = new WButton("PUSH");
    app.add(button);
    WLabel label = new WLabel("HERE");
    app.add(label);
    StringWriter outStr = new StringWriter();
    PrintWriter writer = new PrintWriter(outStr);
    RenderContext renderContext = new WebXmlRenderContext(writer);
    app.afterPaint(renderContext);
    // expecting 1 root class, 3 components, class names as shown, profiler
    // class
    String profileLine0 = PROFILER_UIC_HEADER;
    String profileLine1 = PROFILER_LINE1.replaceAll("<<NUM_ROOTS>>", "1");
    String profileLine2 = PROFILER_LINE2.replaceAll("<<NUM_COMPONENTS>>", "4");
    String profileLine31 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WButton");
    String profileLine32 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.monitor.ProfileContainer");
    String profileLine33 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WLabel");
    String profileLine4 = PROFILER_PROFILE_HEADER.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.monitor.ProfileContainer");
    String[] expectedResults = { profileLine0, profileLine1, profileLine2, profileLine31, profileLine32, profileLine33, profileLine4 };
    String result = outStr.toString();
    for (int i = 0; i < expectedResults.length; i++) {
        Assert.assertTrue("result should contain substring " + i + "  ", result.indexOf(expectedResults[i]) != -1);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) WButton(com.github.bordertech.wcomponents.WButton) WLabel(com.github.bordertech.wcomponents.WLabel) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 19 with WebXmlRenderContext

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

the class AbstractWebXmlRendererTestCase method render.

/**
 * Renders the given component to xml.
 *
 * @param component the component to render.
 * @return the rendered format of the component.
 */
public String render(final WebComponent component) {
    boolean needsContext = UIContextHolder.getCurrent() == null;
    if (needsContext) {
        setActiveContext(createUIContext());
    }
    try {
        StringWriter buffer = new StringWriter();
        component.preparePaint(new MockRequest());
        PrintWriter writer = new PrintWriter(buffer);
        component.paint(new WebXmlRenderContext(writer));
        writer.close();
        return buffer.toString();
    } finally {
        if (needsContext) {
            UIContextHolder.popContext();
        }
    }
}
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)

Example 20 with WebXmlRenderContext

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

the class AjaxInterceptor method paintResponse.

/**
 * Paint the ajax response.
 *
 * @param renderContext the render context
 * @param operation the ajax operation
 */
private void paintResponse(final RenderContext renderContext, final AjaxOperation operation) {
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    XmlStringBuilder xml = webRenderContext.getWriter();
    // Get trigger's context
    ComponentWithContext trigger = AjaxHelper.getCurrentTriggerAndContext();
    if (trigger == null) {
        throw new SystemException("No context available for trigger " + operation.getTriggerId());
    }
    for (String targetId : operation.getTargets()) {
        ComponentWithContext target;
        if (targetId.equals(operation.getTriggerId())) {
            target = trigger;
        } else {
            target = WebUtilities.getComponentById(targetId, true);
            if (target == null) {
                LOG.warn("Could not find ajax target to render [" + targetId + "]");
                continue;
            }
        }
        UIContextHolder.pushContext(target.getContext());
        try {
            xml.appendTagOpen("ui:ajaxtarget");
            xml.appendAttribute("id", targetId);
            xml.appendAttribute("action", operation.getAction().getDesc());
            xml.appendClose();
            target.getComponent().paint(renderContext);
            xml.appendEndTag("ui:ajaxtarget");
        } finally {
            UIContextHolder.popContext();
        }
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

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