Search in sources :

Example 71 with MockRequest

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

the class WDataTable_Test method testHandleFilterRequest.

@Test
public void testHandleFilterRequest() {
    String[] filters = new String[] { "filterA", "filterB", "filterC" };
    WDataTable table = new WDataTable();
    table.setFilterable(true);
    table.setDataModel(new SimpleTableDataModel(new String[100][1]));
    table.setLocked(true);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    request.setParameter(table.getId() + ".filters", filters);
    table.handleRequest(request);
    Assert.assertEquals("Incorrect selection after handleRequest", Arrays.asList(filters), table.getActiveFilters());
    resetContext();
    Assert.assertTrue("Incorrect default filters after handleRequest", table.getActiveFilters().isEmpty());
    setActiveContext(createUIContext());
    request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    table.handleRequest(request);
    Assert.assertTrue("Incorrect filters after handleRequest with no filters set", table.getActiveFilters().isEmpty());
    resetContext();
    Assert.assertTrue("Incorrect default filters after handleRequest with no filters set", table.getActiveFilters().isEmpty());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 72 with MockRequest

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

the class WDataTable_Test method testHandleSortRequest.

@Test
public void testHandleSortRequest() {
    SimpleTableDataModel model = new SimpleTableDataModel(new String[][] { { "1" }, { "3" }, { "2" } });
    model.setComparator(0, SimpleTableDataModel.COMPARABLE_COMPARATOR);
    WDataTable table = new WDataTable();
    table.setSelectMode(WDataTable.SelectMode.SINGLE);
    table.setDataModel(model);
    table.addColumn(new WTableColumn("dummy", WText.class));
    table.setLocked(true);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    request.setParameter(table.getId() + ".sort", "0");
    table.handleRequest(request);
    Assert.assertTrue("Incorrect selection after handleRequest", table.isSorted());
    Assert.assertEquals("Incorrect sort column after handleRequest", 0, table.getSortColumnIndex());
    Assert.assertTrue("Incorrect sort direction after handleRequest", table.isSortAscending());
    List rowIndices = table.getRepeater().getBeanList();
    Assert.assertEquals("Incorrect sort", Arrays.asList(new Integer[] { 0, 2, 1 }), rowIndices);
    resetContext();
    Assert.assertFalse("Incorrect default sort after handleRequest", table.isSorted());
    setActiveContext(createUIContext());
    request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    request.setParameter(table.getId() + ".sort", "0");
    request.setParameter(table.getId() + ".sortDesc", "true");
    table.handleRequest(request);
    rowIndices = table.getRepeater().getBeanList();
    Assert.assertEquals("Incorrect sort", Arrays.asList(new Integer[] { 1, 2, 0 }), rowIndices);
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 73 with MockRequest

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

the class WDataTable_Test method testMultipleHandleSelectionSortedDataRequest.

@Test
public void testMultipleHandleSelectionSortedDataRequest() {
    WDataTable table = new WDataTable();
    table.setSelectMode(WDataTable.SelectMode.MULTIPLE);
    table.setPaginationMode(PaginationMode.DYNAMIC);
    table.setRowsPerPage(3);
    SimpleBeanBoundTableDataModel model = new SimpleBeanBoundTableDataModel(new String[] { "." });
    model.setComparator(0, SimpleBeanBoundTableDataModel.COMPARABLE_COMPARATOR);
    table.setDataModel(model);
    table.setLocked(true);
    setActiveContext(createUIContext());
    // Set Bean
    List<String> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        list.add("A" + i);
    }
    table.setBean(list);
    // Select 2nd item
    MockRequest request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    request.setParameter(table.getId() + ".selected", new String[] { "1" });
    table.handleRequest(request);
    Assert.assertEquals("Incorrect selection after handleRequest", Arrays.asList(new Integer[] { 1 }), table.getSelectedRows());
    // Sort table
    table.sort(0, false);
    // Select new 2nd item (after sort)
    request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    request.setParameter(table.getId() + ".selected", new String[] { "8" });
    table.handleRequest(request);
    Assert.assertEquals("Incorrect selection after handleRequest", Arrays.asList(new Integer[] { 1, 8 }), table.getSelectedRows());
    // Select 1st item (after sort)
    request = new MockRequest();
    request.setParameter(table.getId() + "-h", "x");
    request.setParameter(table.getId() + ".selected", new String[] { "9" });
    table.handleRequest(request);
    Assert.assertEquals("Incorrect selection after handleRequest", Arrays.asList(new Integer[] { 1, 9 }), table.getSelectedRows());
    resetContext();
    Assert.assertTrue("Incorrect default selection after handleRequest with no selection set", table.getSelectedRows().isEmpty());
}
Also used : ArrayList(java.util.ArrayList) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 74 with MockRequest

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

the class WAudio_Test method testHandleRequest.

@Test
public void testHandleRequest() throws IOException {
    MockAudio clip1 = new MockAudio();
    clip1.setBytes("WAudio_Test.testHandleRequest.one".getBytes(CHAR_ENCODING));
    MockAudio clip2 = new MockAudio();
    clip2.setBytes("WAudio_Test.testHandleRequest.two".getBytes(CHAR_ENCODING));
    WAudio audio = new WAudio(new Audio[] { clip1, clip2 });
    MockRequest request = new MockRequest();
    setActiveContext(createUIContext());
    // Should not do anything when target is not present
    audio.handleRequest(request);
    try {
        request.setParameter(Environment.TARGET_ID, audio.getTargetId());
        request.setParameter("WAudio.index", "0");
        audio.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(clip1.getBytes(), 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
    audio.setCacheKey("key");
    // Should produce the content with cache flag set
    try {
        request.setParameter("WAudio.index", "1");
        audio.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(clip2.getBytes(), 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 75 with MockRequest

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

the class WCollapsible_Test method testHandleRequestClientSide.

@Test
public void testHandleRequestClientSide() {
    WCollapsible collapsible = new WCollapsible(new WText(""), "", WCollapsible.CollapsibleMode.CLIENT);
    collapsible.setCollapsed(false);
    collapsible.setLocked(true);
    setActiveContext(createUIContext());
    MockRequest request = new MockRequest();
    request.setParameter(collapsible.getId(), "closed");
    collapsible.handleRequest(request);
    Assert.assertTrue("Collapsible should be collapsed after handleRequest to close", collapsible.isCollapsed());
    request.setParameter(collapsible.getId(), "open");
    Assert.assertTrue("Collapsible should not be collapsed after handleRequest to open", collapsible.isCollapsed());
}
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