Search in sources :

Example 36 with Diagnostic

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

the class WFieldSet_Test method testMandatoryWithMessage.

@Test
public void testMandatoryWithMessage() {
    WFieldSet fieldSet = new WFieldSet("");
    fieldSet.setMandatory(true, "test message");
    Diagnostic msg = fieldSet.createMandatoryDiagnostic();
    Assert.assertEquals("Message incorrect", "test message", msg.getDescription());
}
Also used : Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Example 37 with Diagnostic

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

the class WMessages_Test method testHasMessage.

@Test
public void testHasMessage() {
    WMessages messages = new WMessages();
    UIContext uic = new UIContextImpl();
    setActiveContext(uic);
    Assert.assertFalse("Should have no messages by default", messages.hasMessages());
    messages.addMessage(new Message(Message.SUCCESS_MESSAGE, "x"));
    Assert.assertTrue("Should be true if there are messages", messages.hasMessages());
    messages = new WMessages();
    messages.addMessage(new Message(Message.INFO_MESSAGE, "x"));
    Assert.assertTrue("Should be true if there are messages", messages.hasMessages());
    messages = new WMessages();
    messages.addMessage(new Message(Message.WARNING_MESSAGE, "x"));
    Assert.assertTrue("Should be true if there are messages", messages.hasMessages());
    messages = new WMessages();
    messages.addMessage(new Message(Message.ERROR_MESSAGE, "x"));
    Assert.assertTrue("Should be true if there are messages", messages.hasMessages());
    messages = new WMessages();
    Diagnostic error = new DiagnosticImpl(Diagnostic.ERROR, new WTextField(), "Error");
    List<Diagnostic> errors = new ArrayList<>();
    errors.add(error);
    messages.getValidationErrors().setErrors(errors);
    Assert.assertTrue("Should be true if there are messages", messages.hasMessages());
}
Also used : DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Example 38 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)

Example 39 with Diagnostic

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

the class AbstractInput_Test method testValidateComponent.

@Test
public void testValidateComponent() {
    // No Validation
    AbstractInput input = new MyInput();
    List<Diagnostic> diags = new ArrayList<>();
    input.validate(diags);
    Assert.assertTrue("Input with no validation should return an empty daignostics list", diags.isEmpty());
    // Mandatory with no value - Validation Error
    input = new MyInput();
    diags.clear();
    input.setMandatory(true);
    input.validate(diags);
    Assert.assertEquals("Mandatory input with no value should return a diagnostic message", 1, diags.size());
    // Switch to ReadOnly and should have no validation errors
    input.setReadOnly(true);
    diags.clear();
    input.validate(diags);
    Assert.assertTrue("Mandatory input that is ReadOnly with no value should return an empty diagnostic message", diags.isEmpty());
    // Mandatory with a value - No Validation Errors
    input = new MyInput();
    diags.clear();
    input.setMandatory(true);
    input.setData("value");
    input.validate(diags);
    Assert.assertTrue("Mandatory input with a value should return an empty daignostics list", diags.isEmpty());
    // Custom Validator - Validation Error
    FieldValidator validator = new AbstractFieldValidator() {

        @Override
        protected boolean isValid() {
            return false;
        }
    };
    input = new MyInput();
    diags.clear();
    input.addValidator(validator);
    input.validate(diags);
    Assert.assertEquals("Input with custom validator should return a diagnostic message", 1, diags.size());
}
Also used : ArrayList(java.util.ArrayList) FieldValidator(com.github.bordertech.wcomponents.validator.FieldValidator) AbstractFieldValidator(com.github.bordertech.wcomponents.validator.AbstractFieldValidator) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) AbstractFieldValidator(com.github.bordertech.wcomponents.validator.AbstractFieldValidator) Test(org.junit.Test)

Example 40 with Diagnostic

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

the class DateFieldPivotValidator_Test method runTest.

/**
 * @param operator the date operator
 * @return the test results
 */
private String[][] runTest(final int operator) {
    String[][] result = new String[DATES.length][DATES[0].length];
    for (int run = 0; run < DATES.length; run++) {
        DateFieldPivotValidator validator = new DateFieldPivotValidator(operator);
        validator.setFixedPivot(DATES[run][2]);
        WDateField dateField = new WDateField();
        dateField.addValidator(validator);
        List<Diagnostic> diags = new ArrayList<>();
        for (int i = 0; i < DATES[run].length; i++) {
            diags.clear();
            dateField.setDate(DATES[run][i]);
            dateField.validate(diags);
            Assert.assertTrue("Should only have a maximum of 1 error", diags.size() < 2);
            if (!diags.isEmpty()) {
                result[run][i] = (diags.get(0)).getDescription();
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) WDateField(com.github.bordertech.wcomponents.WDateField) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic)

Aggregations

Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)41 ArrayList (java.util.ArrayList)29 Test (org.junit.Test)29 DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)10 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)8 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)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 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