Search in sources :

Example 1 with FieldValidator

use of com.github.bordertech.wcomponents.validator.FieldValidator 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 2 with FieldValidator

use of com.github.bordertech.wcomponents.validator.FieldValidator in project wcomponents by BorderTech.

the class AbstractInput_Test method testValidatorsAccessors.

@Test
public void testValidatorsAccessors() {
    AbstractInput input = new MyInput();
    Assert.assertFalse("Default input should have no validators", input.getValidators().hasNext());
    // Add validator
    FieldValidator validator = new AbstractFieldValidator() {

        @Override
        protected boolean isValid() {
            return false;
        }
    };
    input.addValidator(validator);
    Assert.assertEquals("Validator Iterator did not contain the valid validator", validator, input.getValidators().next());
}
Also used : FieldValidator(com.github.bordertech.wcomponents.validator.FieldValidator) AbstractFieldValidator(com.github.bordertech.wcomponents.validator.AbstractFieldValidator) AbstractFieldValidator(com.github.bordertech.wcomponents.validator.AbstractFieldValidator) Test(org.junit.Test)

Aggregations

AbstractFieldValidator (com.github.bordertech.wcomponents.validator.AbstractFieldValidator)2 FieldValidator (com.github.bordertech.wcomponents.validator.FieldValidator)2 Test (org.junit.Test)2 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)1 ArrayList (java.util.ArrayList)1