Search in sources :

Example 31 with MockRequest

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

the class WDateField_Test method doRequest.

/**
 * Emulates user interaction with the date field.
 *
 * @param dateField the date field to modify.
 * @param dateStr the parsed date string.
 */
private void doRequest(final WDateField dateField, final String dateStr) {
    MockRequest request = new MockRequest();
    // don't really care about the user text
    request.setParameter(dateField.getId(), dateStr);
    request.setParameter(dateField.getId() + "-date", dateStr);
    dateField.serviceRequest(request);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 32 with MockRequest

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

the class WDateField_Test method processDoHandleRequestWithValidDate.

/**
 * @return a datefield processed with a request with a valid date
 */
private WDateField processDoHandleRequestWithValidDate() {
    resetContext();
    WDateField dateField = new WDateField();
    String dateFieldId = dateField.getId();
    dateField.setLocked(true);
    // Valid DateType
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(dateFieldId, REQUEST_VALID_USER_TEXT);
    request.setParameter(dateFieldId + "-date", REQUEST_VALID_INTERNAL_DATE_TEXT);
    dateField.doHandleRequest(request);
    return dateField;
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 33 with MockRequest

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

the class WEmailField_Test method testDoHandleRequest.

@Test
public void testDoHandleRequest() {
    WEmailField field = new WEmailField();
    field.setLocked(true);
    setActiveContext(createUIContext());
    // Request with Empty Value and Field is null (No Change)
    field.setData(null);
    MockRequest request = new MockRequest();
    request.setParameter(field.getId(), "");
    boolean changed = field.doHandleRequest(request);
    Assert.assertFalse("doHandleRequest should have returned false for request with empty value and field is null", changed);
    Assert.assertNull("Value should still be null after empty request", field.getData());
    // Request with Empty Value and Field is empty (No Change)
    field.setData("");
    request = new MockRequest();
    request.setParameter(field.getId(), "");
    changed = field.doHandleRequest(request);
    Assert.assertFalse("doHandleRequest should have returned false for request with empty value and field is empty", changed);
    Assert.assertEquals("Value should still be empty after empty request", "", field.getData());
    // Request with Different Value (Change)
    request = new MockRequest();
    request.setParameter(field.getId(), "X");
    changed = field.doHandleRequest(request);
    Assert.assertTrue("doHandleRequest should have returned true for request with different value", changed);
    Assert.assertEquals("Value not set after request", "X", field.getData());
    // Request with Same Value (No Change)
    request = new MockRequest();
    request.setParameter(field.getId(), "X");
    changed = field.doHandleRequest(request);
    Assert.assertFalse("doHandleRequest should have returned false for request with same value", changed);
    Assert.assertEquals("Value should not have changed after request with same value", "X", field.getData());
    // Request with Empty Value (Change)
    request = new MockRequest();
    request.setParameter(field.getId(), "");
    changed = field.doHandleRequest(request);
    Assert.assertTrue("doHandleRequest should have returned true for request going back to an empty value", changed);
    Assert.assertNull("Value should go back to null for request with empty value", field.getData());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 34 with MockRequest

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

the class WDateField_Test method getRequestValue.

@Test
public void getRequestValue() throws ParseException {
    WDateField dateField = new WDateField();
    dateField.setLocked(true);
    setActiveContext(createUIContext());
    // Set current value
    Date current = new Date();
    dateField.setDate(current);
    // Empty Request (not present, should return current value)
    MockRequest request = new MockRequest();
    Assert.assertEquals("Current value of the field should have been returned for empty request", current, dateField.getRequestValue(request));
    // Request with "empty" value (should return null)
    request = new MockRequest();
    request.setParameter(dateField.getId(), "");
    Assert.assertNull("Null should have been returned for request with empty value", dateField.getRequestValue(request));
    // Request with "invalid" data parameter by T&S - wrong length (should return null)
    request = new MockRequest();
    request.setParameter(dateField.getId(), "TEST");
    request.setParameter(dateField.getId() + "-date", "ABCDEFGHIHJKLMNOP");
    Assert.assertNull("Null should have been returned for request with invalid length date parameter", dateField.getRequestValue(request));
    // Request with "invalid" data parameter by T&S - empty (should return null)
    request = new MockRequest();
    request.setParameter(dateField.getId(), "TEST");
    request.setParameter(dateField.getId() + "-date", "");
    Assert.assertNull("Null should have been returned for request with invalid empty date parameter", dateField.getRequestValue(request));
    // Request with invalid value but could be parsed if lenient (should return null)
    request = new MockRequest();
    request.setParameter(dateField.getId(), LENIENT_VALID_USER_TEXT);
    request.setParameter(dateField.getId() + "-date", LENIENT_VALID_INTERNAL_DATE_TEXT);
    Assert.assertNull("Null should have been returned for request with invalid value", dateField.getRequestValue(request));
    // Request with valid value
    request = new MockRequest();
    request.setParameter(dateField.getId(), REQUEST_VALID_USER_TEXT);
    request.setParameter(dateField.getId() + "-date", REQUEST_VALID_INTERNAL_DATE_TEXT);
    Assert.assertEquals("Value from the request should have been returned", REQUEST_VALID_DATE_VALUE, dateField.getRequestValue(request));
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Date(java.util.Date) Test(org.junit.Test)

Example 35 with MockRequest

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

the class WDateField_Test method processDoHandleRequestWithEmptyRequest.

/**
 * @return a datefield processed with a request with an empty value
 */
private WDateField processDoHandleRequestWithEmptyRequest() {
    resetContext();
    WDateField dateField = new WDateField();
    String dateFieldId = dateField.getId();
    dateField.setLocked(true);
    // Empty Request
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(dateFieldId, "");
    dateField.doHandleRequest(request);
    return dateField;
}
Also used : 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