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);
}
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));
}
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;
}
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());
}
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;
}
Aggregations