Search in sources :

Example 26 with MockRequest

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

the class WSelectToggle_Test method testHandleRequest.

@Test
public void testHandleRequest() {
    // Create a target panel containing a variety of "selectable" components.
    WCheckBox checkBox = new WCheckBox();
    WCheckBoxSelect checkBoxSelect = new WCheckBoxSelect(new String[] { "a", "b", "c" });
    WMultiSelect multiSelectList = new WMultiSelect(new String[] { "a", "b", "c" });
    WDataTable dataTable = new WDataTable();
    dataTable.setSelectMode(WDataTable.SelectMode.MULTIPLE);
    dataTable.addColumn(new WTableColumn("dummy", WText.class));
    dataTable.setDataModel(new MyDataModel());
    WPanel targetPanel = new WPanel();
    targetPanel.add(checkBox);
    targetPanel.add(checkBoxSelect);
    targetPanel.add(multiSelectList);
    targetPanel.add(dataTable);
    // Set some default selections, to ensure that existing selections are not toggled.
    checkBoxSelect.setSelected(Arrays.asList(new String[] { "a" }));
    multiSelectList.setSelected(Arrays.asList(new String[] { "b" }));
    dataTable.setSelectedRows(Arrays.asList(new Integer[] { 2 }));
    // Set up the toggle to be tested
    WSelectToggle toggle = new WSelectToggle(false, targetPanel);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    // Check select all
    request.setParameter(toggle.getId(), "all");
    toggle.serviceRequest(request);
    Assert.assertTrue("Checkbox should be checked", checkBox.isSelected());
    Assert.assertEquals("CheckboxSelect should have all items checked", 3, checkBoxSelect.getSelected().size());
    Assert.assertEquals("List should have all items selected", 3, multiSelectList.getSelected().size());
    Assert.assertEquals("Table should have all rows selected", 3, dataTable.getSelectedRows().size());
    // Check select none
    request.setParameter(toggle.getId(), "none");
    toggle.serviceRequest(request);
    Assert.assertFalse("Checkbox should not be checked", checkBox.isSelected());
    Assert.assertEquals("CheckboxSelect should have no items checked", 0, checkBoxSelect.getSelected().size());
    Assert.assertEquals("List should have no items selected", 0, multiSelectList.getSelected().size());
    Assert.assertEquals("Table should have no rows selected", 0, dataTable.getSelectedRows().size());
    // Check that nothing happens when disabled
    toggle.setDisabled(true);
    request.setParameter(toggle.getId(), "all");
    toggle.serviceRequest(request);
    Assert.assertFalse("Checkbox should not be checked", checkBox.isSelected());
    Assert.assertEquals("CheckboxSelect should have no items checked", 0, checkBoxSelect.getSelected().size());
    Assert.assertEquals("List should have no items selected", 0, multiSelectList.getSelected().size());
    Assert.assertEquals("Table should have no rows selected", 0, dataTable.getSelectedRows().size());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 27 with MockRequest

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

the class WImage_Test method testHandleRequest.

@Test
public void testHandleRequest() throws IOException {
    byte[] data = "WImage_Test.testHandleRequest".getBytes(CHAR_ENCODING);
    MockRequest request = new MockRequest();
    MockImage content = new MockImage();
    content.setBytes(data);
    WImage image = new WImage();
    image.setLocked(true);
    setActiveContext(createUIContext());
    image.setImage(content);
    // Should not do anything when target is not present
    image.handleRequest(request);
    try {
        request.setParameter(Environment.TARGET_ID, image.getTargetId());
        image.handleRequest(request);
        Assert.fail("Should have thrown a content escape");
    } catch (ContentEscape escape) {
        MockResponse response = new MockResponse();
        escape.setResponse(response);
        escape.escape();
        String output = new String(response.getOutput(), CHAR_ENCODING);
        Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output);
        Assert.assertFalse("Cache flag should not be set", escape.isCacheable());
        Assert.assertEquals("Response should have header set for no caching", ConfigurationProperties.RESPONSE_DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
    }
    // Test Cached Response
    image.setCacheKey("key");
    // Should produce the content with cache flag set
    try {
        image.handleRequest(request);
        Assert.fail("Should have thrown a content escape");
    } catch (ContentEscape escape) {
        MockResponse response = new MockResponse();
        escape.setResponse(response);
        escape.escape();
        String output = new String(response.getOutput(), CHAR_ENCODING);
        Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output);
        Assert.assertTrue("Cache flag should be set", escape.isCacheable());
        Assert.assertEquals("Response should have header set for caching", ConfigurationProperties.RESPONSE_DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
    }
}
Also used : MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 28 with MockRequest

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

the class WMenuItem_Test method testHandleRequest.

@Test
public void testHandleRequest() {
    TestAction action = new TestAction();
    WMenu menu = new WMenu();
    WMenuItem item = new WMenuItem("", action);
    menu.add(item);
    menu.setLocked(true);
    // Menu not in request
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    menu.serviceRequest(request);
    Assert.assertFalse("Action should not have been called when item was not selected", action.wasTriggered());
    // Menu in request, but item not selected
    request = new MockRequest();
    request.setParameter(menu.getId() + "-h", "x");
    menu.serviceRequest(request);
    Assert.assertFalse("Action should not have been called when item was not selected", action.wasTriggered());
    // Menu in request and item selected
    request = new MockRequest();
    request.setParameter(menu.getId() + "-h", "x");
    request.setParameter(item.getId(), "x");
    menu.serviceRequest(request);
    Assert.assertTrue("Action should have been called when item is selected", action.wasTriggered());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 29 with MockRequest

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

the class WDateField_Test method getRequestValueLenient.

@Test
public void getRequestValueLenient() throws ParseException {
    WDateField dateField = new WDateField(true);
    dateField.setLocked(true);
    setActiveContext(createUIContext());
    // Request with invalid value but passes with lenient (should return date)
    MockRequest request = new MockRequest();
    request.setParameter(dateField.getId(), LENIENT_VALID_USER_TEXT);
    request.setParameter(dateField.getId() + "-date", LENIENT_VALID_INTERNAL_DATE_TEXT);
    Assert.assertEquals("Value from request should have been returned when lenient", LENIENT_VALID_DATE_VALUE, dateField.getRequestValue(request));
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 30 with MockRequest

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

the class WDateField_Test method processDoHandleRequestWithBadThemeDate.

/**
 * @return a datefield processed with a request with a bad theme date
 */
private WDateField processDoHandleRequestWithBadThemeDate() {
    resetContext();
    WDateField dateField = new WDateField();
    String dateFieldId = dateField.getId();
    dateField.setLocked(true);
    // Valid DateType
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(dateFieldId, REQUEST_THEME_BAD_DATE_USER_TEXT);
    request.setParameter(dateFieldId + "-date", REQUEST_THEME_BAD_INTERNAL_DATE_VALUE);
    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