Search in sources :

Example 36 with MockRequest

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

the class WDateField_Test method processDoHandleRequestWithBadDate.

/**
 * @return a datefield processed with a request with a bad date
 */
private WDateField processDoHandleRequestWithBadDate() {
    resetContext();
    WDateField dateField = new WDateField();
    String dateFieldId = dateField.getId();
    dateField.setLocked(true);
    // Bad DateType
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(dateFieldId, REQUEST_BAD_USER_TEXT);
    dateField.doHandleRequest(request);
    return dateField;
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 37 with MockRequest

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

the class WDateField_Test method testDoHandleRequestBadDateScenarios.

@Test
public void testDoHandleRequestBadDateScenarios() {
    WDateField dateField = new WDateField();
    dateField.setLocked(true);
    setActiveContext(createUIContext());
    // Setup Request with Invalid DateType (User text only)
    MockRequest request = new MockRequest();
    request.setParameter(dateField.getId(), REQUEST_BAD_USER_TEXT);
    // Scenario #1
    // Setup datefield with null value
    dateField.setDate(null);
    boolean changed = dateField.doHandleRequest(request);
    Assert.assertTrue("Request with Bad Date and field is null - doHandleRequest should have returned true", changed);
    assertDateFieldBadDate(dateField, "Request with Bad Date and field is null");
    // Scenario #2
    // Setup datefield with a valid date set
    dateField.setDate(new Date());
    changed = dateField.doHandleRequest(request);
    Assert.assertTrue("Request with Bad Date and field has a valid date set - doHandleRequest should have returned true", changed);
    assertDateFieldBadDate(dateField, "Request with Bad Date and field has a valid date set");
    // Scenario #3
    // Setup datefield with DIFFERENT invalid date value
    MockRequest request2 = new MockRequest();
    request2.setParameter(dateField.getId(), "ANOTHER BAD DATE");
    dateField.doHandleRequest(request2);
    changed = dateField.doHandleRequest(request);
    Assert.assertTrue("Request with Bad Date and field has a different invalid date - doHandleRequest should have returned true", changed);
    assertDateFieldBadDate(dateField, "Request with Bad Date and field has a different invalid date");
    // Scenario #4
    // Setup datefield with SAME invalid date value
    changed = dateField.doHandleRequest(request);
    Assert.assertFalse("Request with Bad Date and field has same invalid date - doHandleRequest should have returned false", changed);
    assertDateFieldBadDate(dateField, "Request with Bad Date and field has same invalid date");
    // Scenario #5
    // Setup datefield with valid date value
    request2 = new MockRequest();
    request2.setParameter(dateField.getId(), REQUEST_VALID_USER_TEXT);
    request2.setParameter(dateField.getId() + "-date", REQUEST_VALID_INTERNAL_DATE_TEXT);
    dateField.doHandleRequest(request2);
    changed = dateField.doHandleRequest(request);
    Assert.assertTrue("Request with Bad Date and field has a valid value - doHandleRequest should have returned true", changed);
    assertDateFieldBadDate(dateField, "Request with Bad Date and field has a valid value");
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Date(java.util.Date) Test(org.junit.Test)

Example 38 with MockRequest

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

the class WRepeater_Test method testHandleRequest.

@Test
public void testHandleRequest() {
    WRepeater repeater = new WRepeater();
    repeater.setRepeatedComponent(new WTextField());
    repeater.setLocked(true);
    UIContext uic = createUIContext();
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    repeater.setBeanList(Arrays.asList(ROW_DATA));
    repeater.preparePaint(request);
    Assert.assertEquals("Incorrect row value before request", ROW_DATA[0], repeater.getBeanList().get(0));
    UIContext rowContext = repeater.getRowContexts().get(0);
    setActiveContext(rowContext);
    String name = repeater.getRepeatedComponent().getId();
    String newValue = ROW_DATA[0] + ".new";
    request.setParameter(name, newValue);
    setActiveContext(uic);
    repeater.handleRequest(request);
    DataBound component = (DataBound) repeater.getRepeatedComponent();
    setActiveContext(rowContext);
    Assert.assertEquals("Incorrect row value after request", newValue, component.getData());
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 39 with MockRequest

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

the class WShuffler_Test method testGetRequestValue.

@Test
public void testGetRequestValue() {
    WShuffler shuffler = new WShuffler(OPTIONS);
    shuffler.setLocked(true);
    // Empty Request
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    // Should return default options
    Assert.assertEquals("Incorrect default options returned", OPTIONS, shuffler.getRequestValue(request));
    // Shuffle options on the request
    request = new MockRequest();
    request.setParameter(shuffler.getId(), new String[] { OPTION_C, OPTION_A, OPTION_B });
    Assert.assertNotNull("Request Value should not be null", shuffler.getRequestValue(request));
    Assert.assertEquals("Invalid number of options", 3, shuffler.getRequestValue(request).size());
    Assert.assertEquals("Invalid first option", OPTION_C, shuffler.getRequestValue(request).get(0));
    Assert.assertEquals("Invalid second option", OPTION_A, shuffler.getRequestValue(request).get(1));
    Assert.assertEquals("Invalid third option", OPTION_B, shuffler.getRequestValue(request).get(2));
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 40 with MockRequest

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

the class WShuffler_Test method testDoHandleRequest.

@Test
public void testDoHandleRequest() {
    WShuffler shuffler = new WShuffler(OPTIONS);
    shuffler.setLocked(true);
    setActiveContext(createUIContext());
    // Empty Request (No Change)
    MockRequest request = new MockRequest();
    boolean changed = shuffler.doHandleRequest(request);
    Assert.assertFalse("Empty Request - Options should not have changed", changed);
    Assert.assertEquals("Empty Request - Incorrect default options returned", OPTIONS, shuffler.getOptions());
    // Request with Options in same order (No Change)
    setActiveContext(createUIContext());
    request = new MockRequest();
    request.setParameter(shuffler.getId(), OPTIONS.toArray(new String[] {}));
    changed = shuffler.doHandleRequest(request);
    Assert.assertFalse("Same Request - Options should not have changed", changed);
    Assert.assertEquals("Same Request - Incorrect default options returned", OPTIONS, shuffler.getOptions());
    // Shuffle options via a request (change)
    setActiveContext(createUIContext());
    request = new MockRequest();
    request.setParameter(shuffler.getId(), new String[] { OPTION_C, OPTION_A, OPTION_B });
    changed = shuffler.doHandleRequest(request);
    Assert.assertTrue("Shuffled Request - Options should not have changed", changed);
    Assert.assertEquals("Shuffled Request - Invalid number of options", 3, shuffler.getOptions().size());
    Assert.assertEquals("Shuffled Request - Invalid first option", OPTION_C, shuffler.getOptions().get(0));
    Assert.assertEquals("Shuffled Request - Invalid second option", OPTION_A, shuffler.getOptions().get(1));
    Assert.assertEquals("Shuffled Request - Invalid third option", OPTION_B, shuffler.getOptions().get(2));
    // Default should not have changed
    resetContext();
    Assert.assertEquals("Incorrect default options returned", OPTIONS, shuffler.getOptions());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

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