Search in sources :

Example 26 with Diagnostic

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

the class WFieldRenderer_Test method testWithValidationMessages.

@Test
public void testWithValidationMessages() throws IOException, SAXException, XpathException {
    WTextField text = new WTextField();
    text.setText("text1");
    WFieldLayout test = new WFieldLayout();
    WField field = test.addField("label1", text);
    setActiveContext(createUIContext());
    assertXpathEvaluatesTo("text1", "//ui:field/ui:input/ui:textfield/text()", field);
    // Simulate Error Message
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.ERROR, text, "Test Error"));
    diags.add(new DiagnosticImpl(Diagnostic.WARNING, text, "Test Warning"));
    field.showErrorIndicators(diags);
    field.showWarningIndicators(diags);
    // Validate Schema
    assertSchemaMatch(test);
    // Check Attributes
    assertXpathEvaluatesTo(field.getId(), "//ui:field/@id", field);
    // Check Label
    assertXpathEvaluatesTo("label1", "//ui:field/ui:label", field);
    // Check Input
    assertXpathEvaluatesTo("text1", "//ui:field/ui:input/ui:textfield/text()", field);
    assertXpathExists("//ui:field/ui:input/ui:textfield/ui:fieldindicator", field);
    assertXpathExists("//ui:field/ui:input/ui:textfield/ui:fieldindicator[@type='error']", field);
    assertXpathExists("//ui:field/ui:input/ui:textfield/ui:fieldindicator[@type='warn']", field);
    assertXpathEvaluatesTo("Test Error", "//ui:field/ui:input/ui:textfield/ui:fieldindicator[@type='error']/ui:message", field);
    assertXpathEvaluatesTo("Test Warning", "//ui:field/ui:input/ui:textfield/ui:fieldindicator[@type='warn']/ui:message", field);
}
Also used : WField(com.github.bordertech.wcomponents.WField) DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WTextField(com.github.bordertech.wcomponents.WTextField) Test(org.junit.Test)

Example 27 with Diagnostic

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

the class WFieldWarningIndicatorRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WContainer root = new WContainer();
    WPanel target = new WPanel();
    WFieldWarningIndicator indicator = new WFieldWarningIndicator(target);
    root.add(target);
    root.add(indicator);
    setActiveContext(createUIContext());
    List<Diagnostic> diags = new ArrayList<>();
    diags.add(new DiagnosticImpl(Diagnostic.WARNING, target, getMaliciousContent()));
    root.showWarningIndicators(diags);
    assertSafeContent(root);
}
Also used : WContainer(com.github.bordertech.wcomponents.WContainer) DiagnosticImpl(com.github.bordertech.wcomponents.validation.DiagnosticImpl) WPanel(com.github.bordertech.wcomponents.WPanel) ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) WFieldWarningIndicator(com.github.bordertech.wcomponents.validation.WFieldWarningIndicator) Test(org.junit.Test)

Example 28 with Diagnostic

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

the class WDateField_Test method testValidateMinValue.

@Test
public void testValidateMinValue() {
    WDateField dateField = new WDateField();
    dateField.setLocked(true);
    Date value = DateUtilities.createDate(01, 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 minimum set should be valid", diags.isEmpty());
    dateField.setDate(value);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Value with no minimum set should be valid", diags.isEmpty());
    dateField.reset();
    dateField.setMinDate(value);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Null value with minimum set should be valid", diags.isEmpty());
    dateField.setDate(value);
    dateField.setMinDate(valueMinusOne);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Value is greater than minimum so should be valid", diags.isEmpty());
    dateField.setMinDate(value);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertTrue("Value is the same as minimum so should be valid", diags.isEmpty());
    dateField.setMinDate(valuePlusOne);
    diags = new ArrayList<>();
    dateField.validate(diags);
    Assert.assertFalse("Value is less than minimum so should be invalid", diags.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Date(java.util.Date) Test(org.junit.Test)

Example 29 with Diagnostic

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

the class WDateField_Test method testValidateCustomError.

@Test
public void testValidateCustomError() {
    WDateField dateField = new WDateField(false);
    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);
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Example 30 with Diagnostic

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

the class WDateField_Test method testValidate.

@Test
public void testValidate() {
    WDateField dateField = new WDateField();
    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());
    dateField.setMandatory(true);
    dateField.validate(diags);
    Assert.assertFalse("Empty should not validation error when mandatory", diags.isEmpty());
    diags.clear();
    // 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.assertFalse("Validator should not be lenient when validating dates", diags.isEmpty());
    Diagnostic diag = diags.get(0);
    String text = diag.getDescription();
    Assert.assertTrue("Error message should not be empty", text != null && text.length() > 0);
    diags.clear();
    // Invalid date
    doRequest(dateField, INVALID_INTERNAL_DATE);
    dateField.validate(diags);
    Assert.assertEquals("Invalid date should have a validation error", 1, diags.size());
    diag = diags.get(0);
    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());
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) Test(org.junit.Test)

Aggregations

Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)34 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)24 DiagnosticImpl (com.github.bordertech.wcomponents.validation.DiagnosticImpl)9 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)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 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)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