Search in sources :

Example 26 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class WWindowInterceptor_Test method testServiceRequest.

@Test
public void testServiceRequest() {
    final int initialServletStep = 123;
    final int initialWindowStep = 111;
    // Create app
    WApplication app = new WApplication();
    WWindow window = new WWindow();
    window.setContent(new MockLabel("dummy"));
    app.add(window);
    WContent content = new WContent();
    content.setContentAccess(new InternalResource("/wcomponents-test.properties", "test"));
    app.add(content);
    app.setLocked(true);
    // Set up servlet env
    WServlet.WServletEnvironment servletEnvironment = new WServlet.WServletEnvironment("/app", "", "");
    servletEnvironment.setStep(initialServletStep);
    // Set user session
    UIContext uic = createUIContext();
    uic.setUI(app);
    uic.setEnvironment(servletEnvironment);
    setActiveContext(uic);
    window.setStep(initialWindowStep);
    window.display();
    // Target the content first - should pass through and update the environment's step
    WWindowInterceptor interceptor = new WWindowInterceptor(true);
    TestInterceptor testInterceptor = new TestInterceptor();
    interceptor.setBackingComponent(testInterceptor);
    interceptor.attachUI(app);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, content.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Servlet step should have changed after serviceRequest", initialServletStep + 1, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Servlet step should have changed after preparePaint", initialServletStep + 2, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Servlet step should have changed after paint", initialServletStep + 3, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    servletEnvironment.setStep(initialServletStep);
    request = new MockRequest();
    request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Window step should have changed after serviceRequest", initialWindowStep + 1, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Window step should have changed after preparePaintnot have changed", initialWindowStep + 2, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Window step should have changed after paint", initialWindowStep + 3, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    String actualTargetId = testInterceptor.hiddenParams.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    Assert.assertEquals("Hidden params target id should be window id", window.getId(), actualTargetId);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) InternalResource(com.github.bordertech.wcomponents.InternalResource) UIContext(com.github.bordertech.wcomponents.UIContext) WWindow(com.github.bordertech.wcomponents.WWindow) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) WContent(com.github.bordertech.wcomponents.WContent) WApplication(com.github.bordertech.wcomponents.WApplication) MockLabel(com.github.bordertech.wcomponents.MockLabel) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 27 with UIContext

use of com.github.bordertech.wcomponents.UIContext 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 28 with UIContext

use of com.github.bordertech.wcomponents.UIContext 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 29 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class UicStatsAsHtml_Test method testWriter.

/**
 * Test writer.
 */
@Test
public void testWriter() {
    // Set up a mock UI to test
    WApplication app = new WApplication();
    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);
    // Run the UIStats extract
    UicStats stats = new UicStats(uic);
    stats.analyseWC(app);
    StringWriter outStr = new StringWriter();
    PrintWriter writer = new PrintWriter(outStr);
    UicStatsAsHtml.write(writer, stats);
    // expecting 1 root class, 3 components, class names as shown
    String uicStatsHtml1 = UICSTATS_HTML1.replaceAll("<<NUM_ROOTS>>", "1");
    String uicStatsHtml2 = UICSTATS_HTML2.replaceAll("<<NUM_COMPONENTS>>", "4");
    String uicStatsHtml31 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WApplication");
    String uicStatsHtml32 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WButton");
    String uicStatsHtml33 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WLabel");
    String[] expectedResults = { uicStatsHtml1, uicStatsHtml2, uicStatsHtml31, uicStatsHtml32, uicStatsHtml33 };
    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 : StringWriter(java.io.StringWriter) WApplication(com.github.bordertech.wcomponents.WApplication) 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 30 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class AjaxPageShellInterceptor method preparePaint.

/**
 * Override to set the content type of the response and reset the headers.
 *
 * @param request The request being serviced.
 */
@Override
public void preparePaint(final Request request) {
    UIContext uic = UIContextHolder.getCurrent();
    Headers headers = uic.getUI().getHeaders();
    headers.reset();
    headers.setContentType(WebUtilities.CONTENT_TYPE_XML);
    super.preparePaint(request);
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) Headers(com.github.bordertech.wcomponents.Headers)

Aggregations

UIContext (com.github.bordertech.wcomponents.UIContext)114 Test (org.junit.Test)47 WComponent (com.github.bordertech.wcomponents.WComponent)18 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)15 SystemException (com.github.bordertech.wcomponents.util.SystemException)14 WApplication (com.github.bordertech.wcomponents.WApplication)13 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)11 WText (com.github.bordertech.wcomponents.WText)11 PrintWriter (java.io.PrintWriter)11 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 SeleniumWComponentsWebDriver (com.github.bordertech.wcomponents.test.selenium.driver.SeleniumWComponentsWebDriver)9 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)7 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)7 WDropdown (com.github.bordertech.wcomponents.WDropdown)7 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)7 StringWriter (java.io.StringWriter)7 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)6 Environment (com.github.bordertech.wcomponents.Environment)6 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 WButton (com.github.bordertech.wcomponents.WButton)6