Search in sources :

Example 46 with MockRequest

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

the class AbstractWMultiSelectList_Test method testDoHandleRequestBeanBound.

@Test
public void testDoHandleRequestBeanBound() {
    // Set action on change
    AbstractWMultiSelectList multi = new MyWMultiSelectList(OPTIONS, true);
    // Set Bean Property
    multi.setBeanProperty("myOptions");
    WContainer container = new WContainer();
    container.add(multi);
    multi.setLocked(true);
    setActiveContext(createUIContext());
    // Set a Bean that has no selections
    MyBean bean = new MyBean();
    container.setBean(bean);
    // Test Nothing Selected and Empty Request (No Change)
    MockRequest request = setupNothingSelectedRequest(multi);
    boolean changed = multi.doHandleRequest(request);
    Assert.assertEquals("Should have no option selected", EMPTY_LIST, multi.getSelected());
    Assert.assertFalse("doHandleRequest should have returned false", changed);
    // Setup Request with optionA (Change)
    request = setupSingleOptionRequest(multi, OPTION_A);
    changed = multi.doHandleRequest(request);
    Assert.assertEquals("Selected option should be optionA", SELECTED_A, multi.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
    // Setup Request with optionA (No Change)
    request = setupSingleOptionRequest(multi, OPTION_A);
    changed = multi.doHandleRequest(request);
    Assert.assertEquals("Selected option should be optionA", SELECTED_A, multi.getSelected());
    Assert.assertFalse("doHandleRequest should have returned false", changed);
    // Update Bean (should have optionA on the bean)
    multi.updateBeanValue();
    Assert.assertEquals("Bean should contain optionA", SELECTED_A, bean.getMyOptions());
    // Clear Context so gets value from the bean
    multi.reset();
    Assert.assertEquals("Selected option should be optionA", SELECTED_A, multi.getSelected());
    // Change Bean to optionB (Make sure the value is coming from the bean)
    bean.setMyOptions(SELECTED_B);
    Assert.assertEquals("Selected should be optionB coming from the bean", SELECTED_B, multi.getSelected());
    // Setup Request with optionA, optionB (Change)
    request = setupMultiOptionRequest(multi, ARRAY_OPTIONS_A_B);
    changed = multi.doHandleRequest(request);
    Assert.assertEquals("Selected option should be optionA and optionB", SELECTED_A_B, multi.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
    // Setup Empty Request (Change)
    request = setupNothingSelectedRequest(multi);
    changed = multi.doHandleRequest(request);
    Assert.assertEquals("Should have no option selected", EMPTY_LIST, multi.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 47 with MockRequest

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

the class AbstractWMultiSelectList_Test method testUpdateBeanDefaultFirstOption.

@Test
public void testUpdateBeanDefaultFirstOption() {
    AbstractWMultiSelectList multi = new MyWMultiSelectList(OPTIONS, false);
    // Set Bean Property
    multi.setBeanProperty("myOptions");
    // With User Context
    multi.setLocked(true);
    setActiveContext(createUIContext());
    // Set Bean Property and Bean
    MyBean bean = new MyBean();
    multi.setBean(bean);
    // Null Bean Value - getValue will default to first Option
    Assert.assertEquals("Data should default to OptionA", SELECTED_A, multi.getValue());
    // Test handle request is "no change"
    MockRequest request = setupSingleOptionRequest(multi, OPTION_A);
    boolean changed = multi.doHandleRequest(request);
    Assert.assertFalse("doHandleRequest should return false as option should not change", changed);
    // Update Bean (should have optionA on the bean)
    Assert.assertNull("Bean should still be null", bean.getMyOptions());
    multi.updateBeanValue();
    Assert.assertEquals("Bean should contain optionA", SELECTED_A, bean.getMyOptions());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 48 with MockRequest

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

the class AbstractWMultiSelectList_Test method testDoHandleRequestNothingSelected.

@Test
public void testDoHandleRequestNothingSelected() {
    AbstractWMultiSelectList multi = new MyWMultiSelectList(OPTIONS, true);
    // Setup Option Request with nothing selected
    MockRequest request = setupMultiOptionRequest(multi, null);
    boolean changed = multi.doHandleRequest(request);
    Assert.assertEquals("Should have no option selected", EMPTY_LIST, multi.getSelected());
    Assert.assertFalse("doHandleRequest should have returned false", changed);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 49 with MockRequest

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

the class AbstractWMultiSelectList_Test method setupSingleOptionRequest.

/**
 * @param target the target component
 * @param option the option to select on the request
 * @return a mock request with a single option selected
 */
private MockRequest setupSingleOptionRequest(final AbstractWMultiSelectList target, final Object option) {
    MockRequest request = new MockRequest();
    if (target.isAllowNoSelection()) {
        request.setParameter(target.getId() + "-h", "x");
    }
    request.setParameter(target.getId(), target.optionToCode(option));
    return request;
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 50 with MockRequest

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

the class AbstractWMultiSelectList_Test method testDoHandleRequestEditable.

@Test
public void testDoHandleRequestEditable() {
    // Set action on change
    AbstractWMultiSelectList multi = new MyWMultiSelectList(OPTIONS, true);
    // Set Editable
    multi.setEditable(true);
    // Set selected to OptionA
    multi.setSelected(SELECTED_A);
    // Setup Request with "user entered text"
    String userText = "user entered text";
    setActiveContext(createUIContext());
    MockRequest request = setupNothingSelectedRequest(multi);
    request.setParameter(multi.getId(), userText);
    boolean changed = multi.doHandleRequest(request);
    Assert.assertEquals("Selected should have changed to user text", userText, multi.getSelected().get(0));
    Assert.assertTrue("doHandleRequest should have returned true", changed);
}
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