Search in sources :

Example 11 with WNumberField

use of com.github.bordertech.wcomponents.WNumberField in project wcomponents by BorderTech.

the class AbstractCompare_Test method testGetTriggerValueNumberField.

@Test
public void testGetTriggerValueNumberField() {
    WNumberField trigger = new WNumberField();
    AbstractCompare compare = new MyCompare(trigger, null);
    // No Value
    trigger.setNumber(null);
    Assert.assertNull("NumberField Trigger Value - should be null for null number", compare.getTriggerValue(null));
    // Value
    BigDecimal value = BigDecimal.valueOf(5);
    trigger.setNumber(value);
    Assert.assertEquals("NumberField Trigger Value - should be the number value", value, compare.getTriggerValue(null));
    // Value on the Request
    value = BigDecimal.valueOf(200);
    MockRequest request = new MockRequest();
    request.setParameter(trigger.getId(), value.toString());
    Assert.assertEquals("NumberField Trigger Value - should be the value on the request", value, compare.getTriggerValue(request));
}
Also used : WNumberField(com.github.bordertech.wcomponents.WNumberField) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 12 with WNumberField

use of com.github.bordertech.wcomponents.WNumberField in project wcomponents by BorderTech.

the class AbstractCompare_Test method testConstructors.

@Test
public void testConstructors() {
    AbstractCompare compare;
    // --------------------------
    // Constructor - Valid Value
    SubordinateTrigger trigger = new MyInput();
    String value = "test";
    compare = new MyCompare(trigger, value);
    Assert.assertEquals("Invalid trigger returned", trigger, compare.getTrigger());
    Assert.assertEquals("Invalid compare value returned", value, compare.getValue());
    // --------------------------
    // Constructor - Null Value
    trigger = new MyInput();
    compare = new MyCompare(trigger, null);
    Assert.assertEquals("Invalid trigger returned", trigger, compare.getTrigger());
    Assert.assertNull("Invalid compare value returned", compare.getValue());
    // Constructor - Null Trigger
    try {
        compare = new MyCompare(null, "Test");
        Assert.fail("A null trigger should not be allowed");
    } catch (IllegalArgumentException e) {
        Assert.assertNotNull("Exception message for null trigger should not be null", e.getMessage());
    }
    // --------------------------
    // Constructor - DateField and Valid Value
    SubordinateTrigger dateTrigger = new WDateField();
    Date dateValue = new Date();
    compare = new MyCompare(dateTrigger, dateValue);
    Assert.assertEquals("Invalid date trigger returned", dateTrigger, compare.getTrigger());
    Assert.assertEquals("Invalid date compare value returned", dateValue, compare.getValue());
    // Constructor - DateField and Invalid Value
    try {
        compare = new MyCompare(new WDateField(), "Invalid Date");
        Assert.fail("A datefield trigger and invalid date value should not be allowed");
    } catch (IllegalArgumentException e) {
        Assert.assertNotNull("Exception message for datefield trigger and invalid date should not be null", e.getMessage());
    }
    // --------------------------
    // Constructor - NumberField and Valid Value
    SubordinateTrigger numberTrigger = new WNumberField();
    BigDecimal numberValue = BigDecimal.valueOf(5);
    compare = new MyCompare(numberTrigger, numberValue);
    Assert.assertEquals("Invalid number trigger returned", numberTrigger, compare.getTrigger());
    Assert.assertEquals("Invalid number compare value returned", numberValue, compare.getValue());
    // Constructor - NumberField and Invalid Value
    try {
        compare = new MyCompare(new WNumberField(), "Invalid Number");
        Assert.fail("A numberField trigger and invalid number value should not be allowed");
    } catch (IllegalArgumentException e) {
        Assert.assertNotNull("Exception message for numberField trigger and invalid number should not be null", e.getMessage());
    }
}
Also used : WNumberField(com.github.bordertech.wcomponents.WNumberField) SubordinateTrigger(com.github.bordertech.wcomponents.SubordinateTrigger) WDateField(com.github.bordertech.wcomponents.WDateField) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 13 with WNumberField

use of com.github.bordertech.wcomponents.WNumberField in project wcomponents by BorderTech.

the class AbstractCompare method getTriggerValue.

/**
 * Get the value for the trigger.
 * <p>
 * If no request is passed in, the current value of the trigger is used.
 * </p>
 * <p>
 * It will return the same "value" the client would have used in its subordinate logic.
 * </p>
 * <p>
 * The trigger value will either be (i) a date formatted String for WDateFields, (ii) a BigDecimal for WNumberFields
 * or (iii) a List of String values for MultiSelect components or (iv) a String value.
 * </p>
 *
 * @param request the request being processed, can be null
 * @return the value to be used for the trigger
 */
protected Object getTriggerValue(final Request request) {
    // Date Compare (Use Date Formatted String - YYYY-MM-DD)
    if (trigger instanceof WDateField) {
        final WDateField input = (WDateField) trigger;
        Date date;
        if (request == null) {
            date = input.getValue();
        } else {
            date = input.getRequestValue(request);
        }
        return date == null ? null : new SimpleDateFormat(INTERNAL_DATE_FORMAT).format(date);
    } else if (trigger instanceof WNumberField) {
        // Number Compare (Use Number Object)
        final WNumberField input = (WNumberField) trigger;
        if (request == null) {
            return input.getValue();
        } else {
            return input.getRequestValue(request);
        }
    } else if (trigger instanceof AbstractWSingleSelectList) {
        // String Compare for Single Select Lists (Use the Option's Code)
        final AbstractWSingleSelectList list = (AbstractWSingleSelectList) trigger;
        final Object selected;
        if (request == null) {
            selected = list.getValue();
        } else {
            selected = list.getRequestValue(request);
        }
        // Convert selected option to its "code" (Should always have a value)
        String code = list.optionToCode(selected);
        return code;
    } else if (trigger instanceof AbstractWMultiSelectList) {
        // String Compare for Multi Select Lists (Use the Option's Code)
        final AbstractWMultiSelectList list = (AbstractWMultiSelectList) trigger;
        final List<?> selected;
        if (request == null) {
            selected = list.getValue();
        } else {
            selected = list.getRequestValue(request);
        }
        // Empty is treated the same as null
        if (selected == null || selected.isEmpty()) {
            return null;
        }
        // Convert selected options to their "code" (Should always have a value)
        List<String> codes = new ArrayList<>(selected.size());
        for (Object select : selected) {
            String code = list.optionToCode(select);
            codes.add(code);
        }
        return codes;
    } else if (trigger instanceof Input) {
        // String Compare - Use the String Value of the Input
        final Input input = (Input) trigger;
        final Object inputValue;
        if (request == null) {
            inputValue = input.getValue();
        } else {
            inputValue = input.getRequestValue(request);
        }
        // Treat empty the same as null
        return (inputValue == null || Util.empty(inputValue.toString())) ? null : inputValue.toString();
    } else {
        throw new SystemException("Trigger is not a valid type.");
    }
}
Also used : Input(com.github.bordertech.wcomponents.Input) WNumberField(com.github.bordertech.wcomponents.WNumberField) SystemException(com.github.bordertech.wcomponents.util.SystemException) WDateField(com.github.bordertech.wcomponents.WDateField) ArrayList(java.util.ArrayList) List(java.util.List) AbstractWSelectList(com.github.bordertech.wcomponents.AbstractWSelectList) AbstractWSingleSelectList(com.github.bordertech.wcomponents.AbstractWSingleSelectList) AbstractWMultiSelectList(com.github.bordertech.wcomponents.AbstractWMultiSelectList) SimpleDateFormat(java.text.SimpleDateFormat) AbstractWMultiSelectList(com.github.bordertech.wcomponents.AbstractWMultiSelectList) Date(java.util.Date) AbstractWSingleSelectList(com.github.bordertech.wcomponents.AbstractWSingleSelectList)

Example 14 with WNumberField

use of com.github.bordertech.wcomponents.WNumberField in project wcomponents by BorderTech.

the class WNumberFieldRenderer_Test method testReadOnly.

@Test
public void testReadOnly() throws IOException, SAXException, XpathException {
    WNumberField numberField = new WNumberField();
    numberField.setReadOnly(true);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("true", "//ui:numberfield/@readOnly", numberField);
}
Also used : WNumberField(com.github.bordertech.wcomponents.WNumberField) Test(org.junit.Test)

Example 15 with WNumberField

use of com.github.bordertech.wcomponents.WNumberField in project wcomponents by BorderTech.

the class WNumberFieldRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    WNumberField numberField = new WNumberField();
    WButton button = new WButton("Test");
    WContainer root = new WContainer();
    root.add(numberField);
    root.add(button);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo(numberField.getId(), "//ui:numberfield/@id", numberField);
    assertXpathNotExists("//ui:numberfield/@disabled", numberField);
    assertXpathNotExists("//ui:numberfield/@hidden", numberField);
    assertXpathNotExists("//ui:numberfield/@required", numberField);
    assertXpathNotExists("//ui:numberfield/@readOnly", numberField);
    assertXpathNotExists("//ui:numberfield/@toolTip", numberField);
    assertXpathNotExists("//ui:numberfield/@accessibleText", numberField);
    assertXpathNotExists("//ui:numberfield/@min", numberField);
    assertXpathNotExists("//ui:numberfield/@max", numberField);
    assertXpathNotExists("//ui:numberfield/@step", numberField);
    assertXpathNotExists("//ui:numberfield/@decimals", numberField);
    assertXpathNotExists("//ui:numberfield/@buttonId", numberField);
    assertXpathNotExists("//ui:numberfield/@autocomplete", numberField);
    numberField.setDisabled(true);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("true", "//ui:numberfield/@disabled", numberField);
    setFlag(numberField, ComponentModel.HIDE_FLAG, true);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("true", "//ui:numberfield/@hidden", numberField);
    numberField.setMandatory(true);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("true", "//ui:numberfield/@required", numberField);
    numberField.setToolTip("toolTip");
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo(numberField.getToolTip(), "//ui:numberfield/@toolTip", numberField);
    numberField.setAccessibleText("accessibleText");
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo(numberField.getAccessibleText(), "//ui:numberfield/@accessibleText", numberField);
    numberField.setMinValue(45);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("45", "//ui:numberfield/@min", numberField);
    numberField.setMaxValue(50);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("50", "//ui:numberfield/@max", numberField);
    numberField.setStep(0.5);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("0.5", "//ui:numberfield/@step", numberField);
    numberField.setDecimalPlaces(2);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("2", "//ui:numberfield/@decimals", numberField);
    numberField.setDefaultSubmitButton(button);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo(button.getId(), "//ui:numberfield/@buttonId", numberField);
    numberField.setNumber(123);
    assertSchemaMatch(numberField);
    assertXpathEvaluatesTo("123", "normalize-space(//ui:numberfield)", numberField);
}
Also used : WContainer(com.github.bordertech.wcomponents.WContainer) WNumberField(com.github.bordertech.wcomponents.WNumberField) WButton(com.github.bordertech.wcomponents.WButton) Test(org.junit.Test)

Aggregations

WNumberField (com.github.bordertech.wcomponents.WNumberField)20 Test (org.junit.Test)16 BigDecimal (java.math.BigDecimal)5 WDateField (com.github.bordertech.wcomponents.WDateField)4 AbstractWSelectList (com.github.bordertech.wcomponents.AbstractWSelectList)2 RadioButtonGroup (com.github.bordertech.wcomponents.RadioButtonGroup)2 SubordinateTrigger (com.github.bordertech.wcomponents.SubordinateTrigger)2 WContainer (com.github.bordertech.wcomponents.WContainer)2 WRadioButton (com.github.bordertech.wcomponents.WRadioButton)2 WTextField (com.github.bordertech.wcomponents.WTextField)2 Date (java.util.Date)2 AbstractWMultiSelectList (com.github.bordertech.wcomponents.AbstractWMultiSelectList)1 AbstractWSingleSelectList (com.github.bordertech.wcomponents.AbstractWSingleSelectList)1 Input (com.github.bordertech.wcomponents.Input)1 SubordinateTarget (com.github.bordertech.wcomponents.SubordinateTarget)1 WButton (com.github.bordertech.wcomponents.WButton)1 WCheckBox (com.github.bordertech.wcomponents.WCheckBox)1 WCheckBoxSelect (com.github.bordertech.wcomponents.WCheckBoxSelect)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 WDropdown (com.github.bordertech.wcomponents.WDropdown)1