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());
}
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());
}
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);
}
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);
}
}
}
}
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);
}
}
}
}
Aggregations