use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.
the class WPasswordFieldRenderer method doRender.
/**
* Paints the given WPasswordField.
*
* @param component the WPasswordField to paint.
* @param renderContext the RenderContext to paint to.
*/
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
WPasswordField field = (WPasswordField) component;
XmlStringBuilder xml = renderContext.getWriter();
boolean readOnly = field.isReadOnly();
xml.appendTagOpen(TAG_NAME);
xml.appendAttribute("id", component.getId());
xml.appendOptionalAttribute("class", component.getHtmlClass());
xml.appendOptionalAttribute("track", component.isTracking(), "true");
xml.appendOptionalAttribute("hidden", component.isHidden(), "true");
if (readOnly) {
xml.appendAttribute("readOnly", "true");
xml.appendEnd();
return;
}
int cols = field.getColumns();
int minLength = field.getMinLength();
int maxLength = field.getMaxLength();
WComponent submitControl = field.getDefaultSubmitButton();
String submitControlId = submitControl == null ? null : submitControl.getId();
xml.appendOptionalAttribute("disabled", field.isDisabled(), "true");
xml.appendOptionalAttribute("required", field.isMandatory(), "true");
xml.appendOptionalAttribute("minLength", minLength > 0, minLength);
xml.appendOptionalAttribute("maxLength", maxLength > 0, maxLength);
xml.appendOptionalAttribute("toolTip", field.getToolTip());
xml.appendOptionalAttribute("accessibleText", field.getAccessibleText());
xml.appendOptionalAttribute("size", cols > 0, cols);
xml.appendOptionalAttribute("buttonId", submitControlId);
xml.appendOptionalAttribute("placeholder", HtmlRenderUtil.getEffectivePlaceholder(field));
List<Diagnostic> diags = field.getDiagnostics(Diagnostic.ERROR);
if (diags == null || diags.isEmpty()) {
xml.appendEnd();
return;
}
xml.appendClose();
DiagnosticRenderUtil.renderDiagnostics(field, renderContext);
xml.appendEndTag(TAG_NAME);
}
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 WCardManager_Test method testRequestHandling.
@Test
public void testRequestHandling() {
WCardManager manager = new WCardManager();
MockContainer cardOne = new MockContainer();
MockContainer cardTwo = new MockContainer();
manager.add(cardOne);
manager.add(cardTwo);
manager.setLocked(true);
Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getHandleRequestCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getValidateCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getPreparePaintCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardOne.getPaintCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getHandleRequestCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getValidateCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getPreparePaintCount());
Assert.assertEquals("Detector initialisation incorrect", 0, cardTwo.getPaintCount());
MockRequest request = new MockRequest();
setActiveContext(createUIContext());
PrintWriter writer = new XmlStringBuilder(new StringWriter());
List<Diagnostic> diags = new ArrayList<>();
manager.serviceRequest(request);
Assert.assertEquals("Card One should be visible and therefore called", 1, cardOne.getHandleRequestCount());
Assert.assertEquals("Card Two should be invisible and therefore not called", 0, cardTwo.getHandleRequestCount());
cardOne.reset();
manager.makeVisible(cardTwo);
manager.serviceRequest(request);
Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getHandleRequestCount());
Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getHandleRequestCount());
cardTwo.reset();
manager.validateComponent(diags);
manager.preparePaint(request);
manager.paint(new WebXmlRenderContext(writer));
Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getValidateCount());
Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getPreparePaintCount());
Assert.assertEquals("Card Two should be visible and therefore called", 1, cardTwo.getPaintCount());
Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getValidateCount());
Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getPreparePaintCount());
Assert.assertEquals("Card One should be invisible and therefore not called", 0, cardOne.getPaintCount());
}
use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.
the class WValidationErrorsRenderer_Test method testDoPaintBasic.
@Test
public void testDoPaintBasic() throws IOException, SAXException, XpathException {
WValidationErrors errors = new WValidationErrors();
WTextArea text1 = new WTextArea();
text1.setText("text1");
WContainer root = new WContainer();
root.add(errors);
root.add(text1);
root.setLocked(true);
// Validate Schema with no errors
assertSchemaMatch(root);
// Simulate Error Message
setActiveContext(createUIContext());
List<Diagnostic> diags = new ArrayList<>();
diags.add(new DiagnosticImpl(Diagnostic.ERROR, text1, "Test Error1"));
root.showErrorIndicators(diags);
errors.setErrors(diags);
assertSchemaMatch(root);
assertXpathEvaluatesTo(text1.getId(), "//ui:validationerrors/ui:error/@for", root);
assertXpathEvaluatesTo("Test Error1", "//ui:validationerrors/ui:error", root);
String title = "WValidationErrorsTitle";
errors.setTitleText(title);
assertSchemaMatch(root);
assertXpathEvaluatesTo(title, "//ui:validationerrors/@title", root);
// Check for error message with no associated component
setActiveContext(createUIContext());
diags.clear();
diags.add(new DiagnosticImpl(Diagnostic.ERROR, null, "Test Error1"));
errors.setErrors(diags);
assertSchemaMatch(root);
assertXpathNotExists("//ui:validationerrors/ui:error/@for", root);
assertXpathEvaluatesTo("Test Error1", "//ui:validationerrors/ui:error", root);
}
use of com.github.bordertech.wcomponents.validation.Diagnostic in project wcomponents by BorderTech.
the class WFieldErrorIndicatorRenderer_Test method testDoPaint.
@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
WContainer root = new WContainer();
WPanel target = new WPanel();
WFieldErrorIndicator indicator = new WFieldErrorIndicator(target);
root.add(target);
root.add(indicator);
// Simulate Error Message
setActiveContext(createUIContext());
List<Diagnostic> diags = new ArrayList<>();
diags.add(new DiagnosticImpl(Diagnostic.ERROR, target, "Test Error"));
root.showErrorIndicators(diags);
// Validate Schema
assertSchemaMatch(root);
// Check Attributes
assertXpathEvaluatesTo(indicator.getId(), "//ui:fieldindicator/@id", root);
assertXpathEvaluatesTo("error", "//ui:fieldindicator/@type", root);
assertXpathEvaluatesTo(target.getId(), "//ui:fieldindicator/@for", root);
// Check Message
assertXpathEvaluatesTo("Test Error", "//ui:fieldindicator/ui:message", root);
}
Aggregations