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