Search in sources :

Example 6 with WApplication

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

the class DebugStructureInterceptor_Test method doRequest.

/**
 * Does a request/response cycle for a WApplication.
 *
 * @return the xml output.
 */
private String doRequest() {
    WApplication app = new WApplication();
    app.setLocked(true);
    UIContext uic = createUIContext();
    uic.setUI(app);
    setActiveContext(uic);
    // Create interceptor
    PageShellInterceptor pageInterceptor = new PageShellInterceptor();
    DebugStructureInterceptor debugInterceptor = new DebugStructureInterceptor();
    pageInterceptor.setBackingComponent(debugInterceptor);
    pageInterceptor.attachUI(app);
    // Action phase
    MockRequest request = new MockRequest();
    pageInterceptor.serviceRequest(request);
    pageInterceptor.preparePaint(request);
    // Render phase
    MockResponse response = new MockResponse();
    pageInterceptor.attachResponse(response);
    pageInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
    return response.getWriterOutput();
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WApplication(com.github.bordertech.wcomponents.WApplication) UIContext(com.github.bordertech.wcomponents.UIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 7 with WApplication

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

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

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

the class UicStats_Test method testGetWCTreeStats.

/**
 * Test getWCTreeStats.
 */
@Test
public void testGetWCTreeStats() {
    stats.analyseWC(app);
    Map<WComponent, Stat> resultStats = stats.getWCTreeStats(app);
    // label
    for (Map.Entry<WComponent, Stat> entry : resultStats.entrySet()) {
        WComponent comp = entry.getKey();
        Stat stat = entry.getValue();
        if (comp instanceof WLabel) {
            Assert.assertEquals("this should be the label created", label, comp);
            Assert.assertEquals("stat should have correct label name", label.getId(), stat.getName());
        } else if (comp instanceof WButton) {
            Assert.assertEquals("this should be the button in the app", button, comp);
            Assert.assertEquals("stat should have correct button name", button.getId(), stat.getName());
        } else if (comp instanceof WApplication) {
            Assert.assertEquals("this should be the app", app, comp);
            Assert.assertEquals("stat should have correct app name", app.getId(), stat.getName());
        }
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Stat(com.github.bordertech.wcomponents.monitor.UicStats.Stat) WApplication(com.github.bordertech.wcomponents.WApplication) WButton(com.github.bordertech.wcomponents.WButton) Map(java.util.Map) WLabel(com.github.bordertech.wcomponents.WLabel) Test(org.junit.Test)

Example 10 with WApplication

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

the class WApplicationRenderer_Test method testRenderedFormatWithHiddenFields.

@Test
public void testRenderedFormatWithHiddenFields() throws XpathException, IOException, SAXException {
    MockWEnvironment environment = new MockWEnvironment();
    environment.setPostPath("WApplicationRendererTest.postPath");
    WApplication application = new WApplication();
    UIContext uic = createUIContext();
    uic.setEnvironment(environment);
    uic.setUI(application);
    setActiveContext(uic);
    WLabel label = new WLabel("dummy");
    application.add(label);
    Map<String, String> hiddenParameters = new HashMap<>();
    hiddenParameters.put("keyA", "valueA");
    hiddenParameters.put("keyB", "valueB");
    environment.setHiddenParameters(hiddenParameters);
    assertSchemaMatch(application);
    assertXpathEvaluatesTo(label.getText(), "normalize-space(//ui:application/ui:label)", application);
    assertXpathEvaluatesTo("valueA", "//ui:application/ui:param[@name='keyA']/@value", application);
    assertXpathEvaluatesTo("valueB", "//ui:application/ui:param[@name='keyB']/@value", application);
}
Also used : MockWEnvironment(com.github.bordertech.wcomponents.MockWEnvironment) WApplication(com.github.bordertech.wcomponents.WApplication) UIContext(com.github.bordertech.wcomponents.UIContext) HashMap(java.util.HashMap) WLabel(com.github.bordertech.wcomponents.WLabel) Test(org.junit.Test)

Aggregations

WApplication (com.github.bordertech.wcomponents.WApplication)28 Test (org.junit.Test)14 UIContext (com.github.bordertech.wcomponents.UIContext)13 WComponent (com.github.bordertech.wcomponents.WComponent)8 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 WButton (com.github.bordertech.wcomponents.WButton)4 WLabel (com.github.bordertech.wcomponents.WLabel)4 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)3 WText (com.github.bordertech.wcomponents.WText)3 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)3 PrintWriter (java.io.PrintWriter)3 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)2 WTextField (com.github.bordertech.wcomponents.WTextField)2 WWindow (com.github.bordertech.wcomponents.WWindow)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 Map (java.util.Map)2 Before (org.junit.Before)2 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)1 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)1 Environment (com.github.bordertech.wcomponents.Environment)1