Search in sources :

Example 1 with WNumberField

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

the class AbstractCompare method getCompareValue.

/**
 * Get the value to use in the compare.
 * <p>
 * It will return the same "value" the client would have used in its subordinate logic.
 * </p>
 * <p>
 * The compare value will either be (i) a date formatted String for WDateFields, (ii) a BigDecimal for WNumberFields
 * or (iii) a String value.
 * </p>
 *
 * @return the value to be used for the compare.
 */
protected Object getCompareValue() {
    // Date Compare (Use Date Formatted String - YYYY-MM-DD)
    if (trigger instanceof WDateField) {
        return value == null ? null : new SimpleDateFormat(INTERNAL_DATE_FORMAT).format(value);
    } else if (trigger instanceof WNumberField) {
        // Number Compare (Use Number Object)
        return value;
    } else if (trigger instanceof AbstractWSelectList) {
        // String Compare - List (Use the Option's Code)
        final AbstractWSelectList listTrigger = (AbstractWSelectList) trigger;
        final List<?> options = listTrigger.getOptions();
        // No options, just return the compare value (so that a test against null works correctly)
        if (options == null || options.isEmpty()) {
            return value == null ? null : value.toString();
        }
        // Check if the value is a valid option allowing for "Legacy" matching
        if (SelectListUtil.containsOptionWithMatching(options, value)) {
            Object option = SelectListUtil.getOptionWithMatching(options, value);
            String code = listTrigger.optionToCode(option);
            return code;
        }
        // Return the value as a String - Treat empty the same as null
        return (value == null || Util.empty(value.toString())) ? null : value.toString();
    } else if (trigger instanceof RadioButtonGroup && value instanceof WRadioButton) {
        // String Compare for RadioButtonGroup and value is WRadioButton (Use the button value)
        // Note - This is only for backward compatibility where projects have used a radio button
        // in the trigger. Projects should use the value expected, not the radio button.
        // If the radio button passed into the compare is used in a repeater, then this compare will not work.
        String data = ((WRadioButton) value).getValue();
        // Treat empty the same as null
        return Util.empty(data) ? null : data;
    } else {
        // Treat empty the same as null
        return (value == null || Util.empty(value.toString())) ? null : value.toString();
    }
}
Also used : AbstractWSelectList(com.github.bordertech.wcomponents.AbstractWSelectList) WRadioButton(com.github.bordertech.wcomponents.WRadioButton) WNumberField(com.github.bordertech.wcomponents.WNumberField) WDateField(com.github.bordertech.wcomponents.WDateField) RadioButtonGroup(com.github.bordertech.wcomponents.RadioButtonGroup) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with WNumberField

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

the class WNumberFieldRenderer_Test method testRendererCorrectlyConfigured.

@Test
public void testRendererCorrectlyConfigured() {
    WNumberField numberField = new WNumberField();
    Assert.assertTrue("Incorrect renderer supplied", getWebXmlRenderer(numberField) instanceof WNumberFieldRenderer);
}
Also used : WNumberField(com.github.bordertech.wcomponents.WNumberField) Test(org.junit.Test)

Example 3 with WNumberField

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

the class WNumberFieldRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WNumberField numberField = new WNumberField();
    MockRequest request = new MockRequest();
    request.setParameter(numberField.getId(), getMaliciousContent());
    numberField.serviceRequest(request);
    assertSafeContent(numberField);
    numberField.setToolTip(getMaliciousAttribute("ui:numberfield"));
    assertSafeContent(numberField);
    numberField.setAccessibleText(getMaliciousAttribute("ui:numberfield"));
    assertSafeContent(numberField);
}
Also used : WNumberField(com.github.bordertech.wcomponents.WNumberField) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 4 with WNumberField

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

the class AbstractCompare_Test method testGetCompareValueNumberField.

@Test
public void testGetCompareValueNumberField() {
    WNumberField trigger = new WNumberField();
    // Null Value
    AbstractCompare compare = new MyCompare(trigger, null);
    Assert.assertNull("NumberField Compare Value - should be null for null number", compare.getCompareValue());
    // Value
    BigDecimal value = BigDecimal.valueOf(5);
    compare = new MyCompare(trigger, value);
    Assert.assertEquals("NumberField Compare Value - should be the number value", value, compare.getCompareValue());
}
Also used : WNumberField(com.github.bordertech.wcomponents.WNumberField) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 5 with WNumberField

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

the class WSubordinateControlRenderer_Test method testAllConditions.

@Test
public void testAllConditions() throws IOException, SAXException, XpathException {
    SubordinateTrigger condTrigger = new WNumberField();
    SubordinateTrigger condTrigger2 = new WTextField();
    SubordinateTarget actionTarget = new WTextField();
    BigDecimal value = BigDecimal.valueOf(2);
    Condition cond1 = new Equal(condTrigger, value);
    Condition cond2 = new NotEqual(condTrigger, value);
    Condition cond3 = new LessThan(condTrigger, value);
    Condition cond4 = new LessThanOrEqual(condTrigger, value);
    Condition cond5 = new GreaterThan(condTrigger, value);
    Condition cond6 = new GreaterThanOrEqual(condTrigger, value);
    Condition cond7 = new Match(condTrigger2, "[abc]");
    // Basic Condition
    Rule rule = new Rule();
    rule.setCondition(new And(cond1, cond2, cond3, cond4, cond5, cond6, cond7));
    rule.addActionOnTrue(new Show(actionTarget));
    rule.addActionOnFalse(new Hide(actionTarget));
    // Setup Subordinate
    WSubordinateControl control = new WSubordinateControl();
    control.addRule(rule);
    WContainer root = new WContainer();
    root.add(condTrigger);
    root.add(condTrigger2);
    root.add(actionTarget);
    root.add(control);
    setActiveContext(createUIContext());
    // Validate Schema
    assertSchemaMatch(root);
    assertXpathNotExists("//ui:subordinate/ui:and/ui:condition[1]/@operator", root);
    assertXpathEvaluatesTo("ne", "//ui:subordinate/ui:and/ui:condition[2]/@operator", root);
    assertXpathEvaluatesTo("lt", "//ui:subordinate/ui:and/ui:condition[3]/@operator", root);
    assertXpathEvaluatesTo("le", "//ui:subordinate/ui:and/ui:condition[4]/@operator", root);
    assertXpathEvaluatesTo("gt", "//ui:subordinate/ui:and/ui:condition[5]/@operator", root);
    assertXpathEvaluatesTo("ge", "//ui:subordinate/ui:and/ui:condition[6]/@operator", root);
    assertXpathEvaluatesTo("rx", "//ui:subordinate/ui:and/ui:condition[7]/@operator", root);
}
Also used : Condition(com.github.bordertech.wcomponents.subordinate.Condition) Hide(com.github.bordertech.wcomponents.subordinate.Hide) WContainer(com.github.bordertech.wcomponents.WContainer) NotEqual(com.github.bordertech.wcomponents.subordinate.NotEqual) WNumberField(com.github.bordertech.wcomponents.WNumberField) LessThanOrEqual(com.github.bordertech.wcomponents.subordinate.LessThanOrEqual) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) GreaterThanOrEqual(com.github.bordertech.wcomponents.subordinate.GreaterThanOrEqual) BigDecimal(java.math.BigDecimal) Match(com.github.bordertech.wcomponents.subordinate.Match) SubordinateTarget(com.github.bordertech.wcomponents.SubordinateTarget) LessThan(com.github.bordertech.wcomponents.subordinate.LessThan) Equal(com.github.bordertech.wcomponents.subordinate.Equal) LessThanOrEqual(com.github.bordertech.wcomponents.subordinate.LessThanOrEqual) NotEqual(com.github.bordertech.wcomponents.subordinate.NotEqual) GreaterThanOrEqual(com.github.bordertech.wcomponents.subordinate.GreaterThanOrEqual) GreaterThan(com.github.bordertech.wcomponents.subordinate.GreaterThan) And(com.github.bordertech.wcomponents.subordinate.And) SubordinateTrigger(com.github.bordertech.wcomponents.SubordinateTrigger) Show(com.github.bordertech.wcomponents.subordinate.Show) Rule(com.github.bordertech.wcomponents.subordinate.Rule) WTextField(com.github.bordertech.wcomponents.WTextField) 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