Search in sources :

Example 11 with Diagnostic

use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.

the class WDateField_Test method testValidateMaxValue.

@Test
public void testValidateMaxValue() {
    WDateField dateField = new WDateField();
    dateField.setLocked(true);
    Date value = DateUtilities.createDate(02, 02, 2003);
    Date valueMinusOne = DateUtilities.createDate(01, 02, 2003);
    Date valuePlusOne = DateUtilities.createDate(03, 02, 2003);
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Null value with no maximum set should be valid", diags.isEmpty());
    dateField.setDate(value);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Value with no maximum set should be valid", diags.isEmpty());
    dateField.reset();
    dateField.setMaxDate(value);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Null value with maximum set should be valid", diags.isEmpty());
    dateField.setDate(value);
    dateField.setMaxDate(valuePlusOne);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Value is less than maximum so should be valid", diags.isEmpty());
    dateField.setMaxDate(value);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Value is the same as maximum so should be valid", diags.isEmpty());
    dateField.setMaxDate(valueMinusOne);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertFalse("Value is larger than maximum so should be invalid", diags.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Date(java.util.Date) Test(org.junit.Test)

Example 12 with Diagnostic

use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.

the class WDateField_Test method testValidateLenient.

@Test
public void testValidateLenient() {
    WDateField dateField = new WDateField(true);
    dateField.setLocked(true);
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Empty field should not have a validation error", diags.isEmpty());
    // Valid date
    doRequest(dateField, VALID_INTERNAL_DATE);
    dateField.validate(diags);
    Assert.assertTrue("A valid date should not have a validation error", diags.isEmpty());
    // Valid (lenient) date
    doRequest(dateField, LENIENT_VALID_INTERNAL_DATE_TEXT);
    dateField.validate(diags);
    Assert.assertTrue("Validator should be lenient when validating dates", diags.isEmpty());
    // Invalid date
    doRequest(dateField, INVALID_INTERNAL_DATE);
    dateField.validate(diags);
    Assert.assertEquals("Invalid date should have a validation error", 1, diags.size());
    Diagnostic diag = diags.get(0);
    String text = diag.getDescription();
    Assert.assertTrue("Error message should not be empty", text != null && text.length() > 0);
    // Invalid date - 2
    diags.clear();
    doRequest(dateField, INVALID_INTERNAL_DATE2);
    dateField.validate(diags);
    Assert.assertEquals("Invalid date should have a validation error", 1, diags.size());
    // Invalid date - 3
    diags.clear();
    doRequest(dateField, INVALID_INTERNAL_DATE3);
    dateField.validate(diags);
    Assert.assertEquals("Invalid date should have a validation error", 1, diags.size());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Example 13 with Diagnostic

use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.

the class WPartialDateField_Test method testValidateCustomError.

@Test
public void testValidateCustomError() {
    WPartialDateField dateField = new WPartialDateField();
    dateField.setInvalidDateErrorMessage(ERROR_MESSAGE);
    dateField.setLocked(true);
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Empty field should not have a validation error", diags.isEmpty());
    // Valid date
    doRequest(dateField, VALID_INTERNAL_DATE);
    dateField.validate(diags);
    Assert.assertTrue("A valid date should not have a validation error", diags.isEmpty());
    // Invalid date
    doRequest(dateField, INVALID_INTERNAL_DATE);
    dateField.validate(diags);
    Assert.assertEquals("Invalid date should have a validation error", 1, diags.size());
    Diagnostic diag = diags.get(0);
    String text = diag.getDescription();
    Assert.assertEquals("Incorrect error text", ERROR_MESSAGE, text);
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Example 14 with Diagnostic

use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.

the class WFieldSet method showIndicatorsForComponent.

/**
 * Set the diagnostics for a given severity.
 * @param diags the list of Diagnostics
 * @param severity the message severity
 */
private void showIndicatorsForComponent(final List<Diagnostic> diags, final int severity) {
    FieldSetModel model = getOrCreateComponentModel();
    if (severity == Diagnostic.ERROR) {
        model.errorDiagnostics.clear();
    } else {
        model.warningDiagnostics.clear();
    }
    UIContext uic = UIContextHolder.getCurrent();
    for (int i = 0; i < diags.size(); i++) {
        Diagnostic diagnostic = diags.get(i);
        // NOTE: double equals because they must be the same instance.
        if (diagnostic.getSeverity() == severity && uic == diagnostic.getContext() && this == diagnostic.getComponent()) {
            if (severity == Diagnostic.ERROR) {
                model.errorDiagnostics.add(diagnostic);
            } else {
                model.warningDiagnostics.add(diagnostic);
            }
        }
    }
}
Also used : Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic)

Example 15 with Diagnostic

use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.

the class AbstractInput method showIndicatorsForComponent.

// Diagnostics
/**
 * Iterates over the {@link Diagnostic}s and finds the diagnostics that related to the current component.
 *
 * @param diags A List of Diagnostic objects.
 * @param severity A Diagnostic severity code. e.g. {@link Diagnostic#ERROR}
 */
protected void showIndicatorsForComponent(final List<Diagnostic> diags, final int severity) {
    InputModel model = getOrCreateComponentModel();
    if (severity == Diagnostic.ERROR) {
        model.errorDiagnostics.clear();
    } else {
        model.warningDiagnostics.clear();
    }
    UIContext uic = UIContextHolder.getCurrent();
    for (int i = 0; i < diags.size(); i++) {
        Diagnostic diagnostic = diags.get(i);
        // NOTE: double equals because they must be the same instance.
        if (diagnostic.getSeverity() == severity && uic == diagnostic.getContext() && this == diagnostic.getComponent()) {
            if (severity == Diagnostic.ERROR) {
                model.errorDiagnostics.add(diagnostic);
            } else {
                model.warningDiagnostics.add(diagnostic);
            }
        }
    }
}
Also used : Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic)

Aggregations

Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)34 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)24 DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)9 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)7 WContainer (com.github.bordertech.wcomponents.WContainer)6 WComponent (com.github.bordertech.wcomponents.WComponent)5 WPanel (com.github.bordertech.wcomponents.WPanel)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 WValidationErrors (com.github.bordertech.wcomponents.validation.WValidationErrors)3 WDateField (com.github.bordertech.wcomponents.WDateField)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 WFieldErrorIndicator (com.github.bordertech.wcomponents.validation.WFieldErrorIndicator)2 WFieldWarningIndicator (com.github.bordertech.wcomponents.validation.WFieldWarningIndicator)2 Date (java.util.Date)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)1 Diagnosable (com.github.bordertech.wcomponents.Diagnosable)1 Input (com.github.bordertech.wcomponents.Input)1 UIContext (com.github.bordertech.wcomponents.UIContext)1 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)1