Search in sources :

Example 51 with WebXmlRenderContext

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

the class SerializationPerformance_Test method sendRequest.

/**
 * Invokes WComponent request processing, so that this test case can more closely match a production scenario.
 *
 * @param comp the component to invoke request processing on.
 * @param uic the user context to use.
 */
private void sendRequest(final WComponent comp, final UIContext uic) {
    PrintWriter writer = new PrintWriter(new NullWriter());
    uic.setEnvironment(new WServlet.WServletEnvironment("", "http://localhost", ""));
    uic.setUI(comp);
    InterceptorComponent root = ServletUtil.createInterceptorChain(new MockHttpServletRequest());
    root.attachUI(comp);
    Response response = new MockResponse();
    root.attachResponse(response);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    try {
        root.serviceRequest(request);
        root.preparePaint(request);
        root.paint(new WebXmlRenderContext(writer));
    } finally {
        resetContext();
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) InterceptorComponent(com.github.bordertech.wcomponents.container.InterceptorComponent) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter)

Example 52 with WebXmlRenderContext

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

the class WCardManager_Test method testRequestHandling.

@Test
public void testRequestHandling() {
    WCardManager manager = new WCardManager();
    MockContainer cardOne = new MockContainer();
    MockContainer cardTwo = new MockContainer();
    manager.add(cardOne);
    manager.add(cardTwo);
    manager.setLocked(true);
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getHandleRequestCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getValidateCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getPreparePaintCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getPaintCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getHandleRequestCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getValidateCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getPreparePaintCount());
    Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getPaintCount());
    MockRequest request = new MockRequest();
    setActiveContext(createUIContext());
    PrintWriter writer = new XmlStringBuilder(new StringWriter());
    List<Diagnostic> diags = new ArrayList<>();
    manager.serviceRequest(request);
    Assert.assertEquals("Card One should be visible and therefore called", 1, cardOne.getHandleRequestCount());
    Assert.assertEquals("Card Two should be invisible and therefore not called", 0, cardTwo.getHandleRequestCount());
    cardOne.reset();
    manager.makeVisible(cardTwo);
    manager.serviceRequest(request);
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getHandleRequestCount());
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getHandleRequestCount());
    cardTwo.reset();
    manager.validateComponent(diags);
    manager.preparePaint(request);
    manager.paint(new WebXmlRenderContext(writer));
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getValidateCount());
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getPreparePaintCount());
    Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getPaintCount());
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getValidateCount());
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getPreparePaintCount());
    Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getPaintCount());
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 53 with WebXmlRenderContext

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

the class WebXmlRenderingPerformance_Test method sendRequest.

/**
 * Invokes WComponent request processing, so that this test case can more closely match a production scenario.
 *
 * @param comp the component to invoke request processing on.
 * @param uic the user context to use.
 */
private void sendRequest(final WComponent comp, final UIContext uic) {
    PrintWriter writer = new PrintWriter(new NullWriter());
    uic.setEnvironment(new WServlet.WServletEnvironment("", "http://localhost", ""));
    uic.setUI(comp);
    InterceptorComponent root = ServletUtil.createInterceptorChain(new MockHttpServletRequest());
    root.attachUI(comp);
    Response response = new MockResponse();
    root.attachResponse(response);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    try {
        root.serviceRequest(request);
        root.preparePaint(request);
        root.paint(new WebXmlRenderContext(writer));
    } finally {
        resetContext();
    }
}
Also used : Response(com.github.bordertech.wcomponents.Response) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) InterceptorComponent(com.github.bordertech.wcomponents.container.InterceptorComponent) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter)

Example 54 with WebXmlRenderContext

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

the class WebXmlRenderingPerformance_Test method testRenderingPerformance.

@Test
public void testRenderingPerformance() throws Exception {
    final AllComponents component = new AllComponents();
    final UIContext uic = createUIContext();
    sendRequest(component, uic);
    // Render and store the XML to compare against
    setActiveContext(uic);
    StringWriter tempStringWriter = new StringWriter();
    PrintWriter tempPrintWriter = new PrintWriter(tempStringWriter);
    component.paint(new WebXmlRenderContext(tempPrintWriter));
    tempPrintWriter.close();
    resetContext();
    // Run the test writing raw bytes to a writer - no computation necessary
    final byte[] xml = tempStringWriter.toString().getBytes("UTF-8");
    final CountingNullPrintWriter nullWriter = new CountingNullPrintWriter();
    LOG.info("Rendered UI size: " + xml.length + " bytes");
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < NUM_REPETITIONS; i++) {
                for (int j = 0; j < xml.length; j++) {
                    nullWriter.write(xml[j]);
                }
            }
        }
    };
    // JIT warm-up
    runnable.run();
    nullWriter.resetCount();
    long rawTime = time(runnable) / NUM_REPETITIONS;
    Assert.assertEquals("Incorrect amount of raw data written", xml.length * NUM_REPETITIONS, nullWriter.getCount());
    nullWriter.resetCount();
    // Run the test using WComponent rendering
    runnable = new Runnable() {

        @Override
        public void run() {
            setActiveContext(uic);
            for (int i = 0; i < NUM_REPETITIONS; i++) {
                component.paint(new WebXmlRenderContext(nullWriter));
            }
        }
    };
    // JIT warm-up
    runnable.run();
    nullWriter.resetCount();
    long renderTime = time(runnable) / NUM_REPETITIONS;
    Assert.assertEquals("Incorrect amount of rendered data written", xml.length * NUM_REPETITIONS, nullWriter.getCount());
    nullWriter.resetCount();
    LOG.info("Raw write time: " + (rawTime / 1000000.0) + "ms");
    LOG.info("WComponent render time: " + (renderTime / 1000000.0) + "ms");
    Assert.assertTrue("WComponent render time should not exceed 5x raw write time", renderTime < rawTime * 5);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) AllComponents(com.github.bordertech.wcomponents.AllComponents) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 55 with WebXmlRenderContext

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

the class WWindow_Test method testPaint.

@Test
public void testPaint() throws SAXException, IOException {
    final int width = 123;
    final int height = 456;
    MockRequest request = new MockRequest();
    StringWriter writer = new StringWriter();
    MockContainer content = new MockContainer();
    WWindow window = new WWindow();
    window.setContent(content);
    window.setWidth(width);
    window.setHeight(height);
    window.setScrollable(true);
    window.setLocked(true);
    // Test paint when hidden
    setActiveContext(createUIContext());
    window.handleRequest(request);
    window.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    Assert.assertEquals("Window should be hidden by default", "", writer.toString());
    Assert.assertEquals("Content should not have been painted", 0, content.getPaintCount());
    // Test paint when displaying
    window.handleRequest(request);
    window.display();
    window.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    String xhtml = writer.toString();
    Assert.assertTrue("Window should have emitted tag", xhtml.indexOf("<ui:popup") != -1);
    Assert.assertTrue("Incorrect window width", xhtml.indexOf("width=\"" + width) != -1);
    Assert.assertTrue("Incorrect window height", xhtml.indexOf("height=\"" + height) != -1);
    Assert.assertTrue("Window should be resizable", xhtml.indexOf("resizable=\"true") != -1);
    Assert.assertEquals("Content should not have been painted", 0, content.getPaintCount());
    writer.getBuffer().setLength(0);
    // Test paint when targetted
    request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
    window.handleRequest(request);
    window.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    Assert.assertEquals("Content should have been painted", 1, content.getPaintCount());
}
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)

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