Search in sources :

Example 56 with MockRequest

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

the class AbstractWSingleSelectList_Test method testGetNewSelection.

@Test
public void testGetNewSelection() {
    AbstractWSingleSelectList single = new MyWSingleSelectList(OPTIONS, true);
    single.setLocked(true);
    setActiveContext(createUIContext());
    // Empty Request
    MockRequest request = new MockRequest();
    Assert.assertNull("new selection should be null", single.getNewSelection(request));
    // Empty Value On Request
    request = new MockRequest();
    request.setParameter(single.getId(), "");
    Assert.assertNull("new selection should be empty", single.getNewSelection(request));
    // Valid item on request
    request = setupRequest(single, OPTION_B);
    Assert.assertEquals("new selection should return optionB", OPTION_B, single.getNewSelection(request));
    // Invalid item on request (should return the current value)
    single.setSelected(OPTION_A);
    request.setParameter(single.getId(), OPTION_INVALID);
    Assert.assertEquals("new selection should return optionA", OPTION_A, single.getNewSelection(request));
    // -------------
    // Null Options
    single.setOptions((List<?>) null);
    // Request with any value
    request = setupNothingSelectedRequest(single);
    request.setParameter(single.getId(), "any value");
    Assert.assertNull("result should be empty when null options", single.getNewSelection(request));
    // -------------
    // Empty Options
    single.setOptions(EMPTY_LIST);
    // Request with a value
    request = setupNothingSelectedRequest(single);
    request.setParameter(single.getId(), "any value");
    Assert.assertNull("result should be empty when options empty", single.getNewSelection(request));
    // -------------
    // Editable
    single.setOptions(OPTIONS);
    single.setEditable(true);
    request = setupRequest(single, OPTION_B);
    Assert.assertEquals("new selection should return optionB", OPTION_B, single.getNewSelection(request));
    // UserText
    request.setParameter(single.getId(), "usertext");
    Assert.assertEquals("new selection should return user text", "usertext", single.getNewSelection(request));
    // Null options
    single.setOptions((List<?>) null);
    Assert.assertEquals("new selection should return user text", "usertext", single.getNewSelection(request));
    // Empty options
    single.setOptions(EMPTY_LIST);
    Assert.assertEquals("new selection should return user text", "usertext", single.getNewSelection(request));
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 57 with MockRequest

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

the class AbstractWSingleSelectList_Test method testDoHandleRequestBeanBound.

@Test
public void testDoHandleRequestBeanBound() {
    // Set action on change
    AbstractWSingleSelectList single = new MyWSingleSelectList(OPTIONS, true);
    // Set Bean Property
    single.setBeanProperty("myOption");
    WContainer container = new WContainer();
    container.add(single);
    single.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(single);
    boolean changed = single.doHandleRequest(request);
    Assert.assertNull("Should have no option selected", single.getSelected());
    Assert.assertFalse("doHandleRequest should have returned false", changed);
    // Setup Request with optionA (Change)
    request = setupRequest(single, OPTION_A);
    changed = single.doHandleRequest(request);
    Assert.assertEquals("Selected option should be optionA", OPTION_A, single.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
    // Setup Request with optionA (No Change)
    request = setupRequest(single, OPTION_A);
    changed = single.doHandleRequest(request);
    Assert.assertEquals("Selected option should be optionA", OPTION_A, single.getSelected());
    Assert.assertFalse("doHandleRequest should have returned false", changed);
    // Update Bean (should have optionA on the bean)
    single.updateBeanValue();
    Assert.assertEquals("Bean should contain optionA", OPTION_A, bean.getMyOption());
    // Clear Context so gets value from the bean
    single.reset();
    Assert.assertEquals("Selected option should be optionA", OPTION_A, single.getSelected());
    // Change Bean to optionB (Make sure the value is coming from the bean)
    bean.setMyOption(OPTION_B);
    Assert.assertEquals("Selected should be optionB coming from the bean", OPTION_B, single.getSelected());
    // Setup Request with optionC (Change)
    request = setupRequest(single, OPTION_C);
    changed = single.doHandleRequest(request);
    Assert.assertEquals("Selected option should be optionC", OPTION_C, single.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
    // Setup Empty Request (Change)
    request = setupNothingSelectedRequest(single);
    changed = single.doHandleRequest(request);
    Assert.assertNull("Should have no option selected", single.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 58 with MockRequest

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

the class AbstractWSingleSelectList_Test method testDoHandleRequestEditable.

@Test
public void testDoHandleRequestEditable() {
    // Set action on change
    AbstractWSingleSelectList single = new MyWSingleSelectList(OPTIONS, true);
    // Set Editable
    single.setEditable(true);
    // Set selected to OptionA
    single.setSelected(OPTION_A);
    // Setup Request with "user entered text"
    String userText = "user entered text";
    setActiveContext(createUIContext());
    MockRequest request = setupNothingSelectedRequest(single);
    request.setParameter(single.getId(), userText);
    boolean changed = single.doHandleRequest(request);
    Assert.assertEquals("Selected should have changed to user text", userText, single.getSelected());
    Assert.assertTrue("doHandleRequest should have returned true", changed);
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 59 with MockRequest

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

the class AbstractWSingleSelectList_Test method testUpdateBeanDefaultFirstOption.

@Test
public void testUpdateBeanDefaultFirstOption() {
    AbstractWSingleSelectList single = new MyWSingleSelectList(OPTIONS, false);
    // Set Bean Property
    single.setBeanProperty("myOption");
    // With User Context
    single.setLocked(true);
    setActiveContext(createUIContext());
    // Set Bean Property and Bean
    MyBean bean = new MyBean();
    single.setBean(bean);
    // Null Bean Value - getValue will default to first Option
    Assert.assertEquals("Data should default to OptionA", OPTION_A, single.getValue());
    // Test handle request is "no change"
    MockRequest request = setupRequest(single, OPTION_A);
    boolean changed = single.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.getMyOption());
    single.updateBeanValue();
    Assert.assertEquals("Bean should contain optionA", OPTION_A, bean.getMyOption());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 60 with MockRequest

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

the class UIContextImpl_Test method testFocusableInWRepeater.

@Test
public void testFocusableInWRepeater() {
    WContainer root = new WContainer();
    // Setup the repeater
    WRepeater repeater = new WRepeater();
    WComponent content = new WBeanComponent() {

        @Override
        public void handleRequest(final Request request) {
            super.handleRequest(request);
            UIContext uic = UIContextHolder.getCurrent();
            if (uic instanceof SubUIContext) {
                SubUIContext suic = (SubUIContext) uic;
                // Set the component on the second row to have focus
                if (suic.getRowIndex() == 1) {
                    uic.setFocussed(this, uic);
                }
            }
        }
    };
    repeater.setRepeatedComponent(content);
    root.add(repeater);
    // Set the repeater as the "UI" for context
    UIContext uic = new UIContextImpl();
    uic.setUI(root);
    setActiveContext(uic);
    // Setup the bean list on the repeater
    final String optionA = "A";
    final String optionB = "B";
    final String optionC = "C";
    List<String> beans = Arrays.asList(new String[] { optionA, optionB, optionC });
    repeater.setBeanList(beans);
    // ServiceRequest on the repeater (to create the SubUiContexts)
    MockRequest request = new MockRequest();
    repeater.serviceRequest(request);
    // Simulate clearing of scratch map
    uic.clearScratchMap();
    // Get the focused component
    String focusId = uic.getFocussedId();
    // Get the ID of the component on the second row
    UIContext suic = repeater.getRowContext(optionB);
    UIContextHolder.pushContext(suic);
    String rowComponentId = null;
    try {
        rowComponentId = content.getId();
    } finally {
        UIContextHolder.popContext();
    }
    Assert.assertNotNull("Focus ID should not be null", focusId);
    Assert.assertEquals("Focus ID should be the ID for the component on the 2nd row", rowComponentId, focusId);
    // Make root hidden
    root.setHidden(true);
    Assert.assertNull("Focus ID should be null with hidden parent", uic.getFocussedId());
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) 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