Search in sources :

Example 76 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class SimpleWComponentTest method testRequest.

@Test
public void testRequest() {
    // Create a test wcomponent with a label and an entry field.
    WTextField name = new WTextField();
    WLabel label = new WLabel("Hero", name);
    WPanel panel = new WPanel();
    panel.add(label);
    panel.add(name);
    // Create a mock context and request.
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    // Check that the text field successfully stores text.
    name.setText("Batman");
    Assert.assertEquals("text accessors incorrect", "Batman", name.getText());
    // Service a request that simulates a user entering a value
    // into the entry field.
    request.setParameter(name.getId(), "Superman");
    panel.serviceRequest(request);
    Assert.assertEquals("text incorrect after request", "Superman", name.getText());
    // Render the output and log it.
    String output = WebUtilities.render(request, panel);
    LOG.debug(output);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 77 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class RadioButtonGroup_Test method testGetRequestValue.

@Test
public void testGetRequestValue() {
    RadioButtonGroup group = new RadioButtonGroup();
    group.setLocked(true);
    setActiveContext(createUIContext());
    // Set current value
    group.setSelectedValue("current");
    // Empty Request (not present, should return current value)
    MockRequest request = new MockRequest();
    Assert.assertEquals("Current value of the group should have been returned for empty request", "current", group.getRequestValue(request));
    // Request with "empty" value (should return null as an empty value on the request is treated as null)
    request = setupRequest(group, "");
    Assert.assertNull("Null should have been returned for request with empty value", group.getRequestValue(request));
    // Request with value (should return the value on the request)
    request = setupRequest(group, "X");
    Assert.assertEquals("Value from the request should have been returned", "X", group.getRequestValue(request));
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 78 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class RadioButtonGroup_Test method setupRequest.

/**
 * @param group the radio group to include on the request
 * @param value the value for the radio group
 * @return the request with the radio group
 */
private MockRequest setupRequest(final RadioButtonGroup group, final String value) {
    MockRequest request = new MockRequest();
    request.setParameter(group.getId() + "-h", "x");
    if (value != null) {
        request.setParameter(group.getId(), value);
    }
    return request;
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 79 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class WrongStepContentInterceptor_Test method testServiceRequestNoTarget.

@Test(expected = SystemException.class)
public void testServiceRequestNoTarget() {
    setActiveContext(createUIContext());
    new WrongStepContentInterceptor().serviceRequest(new MockRequest());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 80 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest 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)

Aggregations

MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)250 Test (org.junit.Test)216 UIContext (com.github.bordertech.wcomponents.UIContext)22 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)16 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)13 PrintWriter (java.io.PrintWriter)13 Request (com.github.bordertech.wcomponents.Request)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)8 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)7 StringWriter (java.io.StringWriter)7 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 SubUIContext (com.github.bordertech.wcomponents.WRepeater.SubUIContext)6 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 BigDecimal (java.math.BigDecimal)5 Escape (com.github.bordertech.wcomponents.Escape)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3