diff options
author | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-09-19 16:09:56 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-19 16:09:56 +0300 |
commit | 1f59cb51af097b8e314fde6d7c3a25821d13868d (patch) | |
tree | d0541765efd5039adce7119b8261232ef9977a8c /server/src/test | |
parent | 159d21f55f0ef24fc2a1a31661be3227988a41ee (diff) | |
download | vaadin-framework-1f59cb51af097b8e314fde6d7c3a25821d13868d.tar.gz vaadin-framework-1f59cb51af097b8e314fde6d7c3a25821d13868d.zip |
Add API for running BindingValidationStatusHandlers (#9966)
This patch adds an API to Binding for accessing the validation status handlers from a custom BinderValidationStatusHandler.
Diffstat (limited to 'server/src/test')
-rw-r--r-- | server/src/test/java/com/vaadin/data/BinderTest.java | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 99317745d5..4d3fe2db9b 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -749,6 +749,68 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> { String.valueOf(item.getAge()), ageField.getValue()); } + static class MyBindingHandler implements BindingValidationStatusHandler { + + boolean expectingError = false; + int callCount = 0; + + @Override + public void statusChange(BindingValidationStatus<?> statusChange) { + ++callCount; + if (expectingError) { + Assert.assertTrue("Expecting error", statusChange.isError()); + } else { + Assert.assertFalse("Unexpected error", statusChange.isError()); + } + } + } + + @Test + public void execute_binding_status_handler_from_binder_status_handler() { + MyBindingHandler bindingHandler = new MyBindingHandler(); + binder.forField(nameField) + .withValidator(t -> !t.isEmpty(), "No empty values.") + .withValidationStatusHandler(bindingHandler) + .bind(Person::getFirstName, Person::setFirstName); + + String ageError = "CONVERSIONERROR"; + binder.forField(ageField) + .withConverter(new StringToIntegerConverter(ageError)) + .bind(Person::getAge, Person::setAge); + + binder.setValidationStatusHandler(status -> { + status.notifyBindingValidationStatusHandlers(); + }); + + String initialName = item.getFirstName(); + int initialAge = item.getAge(); + + binder.setBean(item); + + // Test specific error handling. + bindingHandler.expectingError = true; + nameField.setValue(""); + + // Test default error handling. + ageField.setValue("foo"); + Assert.assertTrue("Component error does not contain error message", + ageField.getComponentError().getFormattedHtmlMessage() + .contains(ageError)); + + // Restore values and test no errors. + ageField.setValue(String.valueOf(initialAge)); + Assert.assertNull("There should be no component error", + ageField.getComponentError()); + + bindingHandler.expectingError = false; + nameField.setValue(initialName); + + // Assert that the handler was called. + Assert.assertEquals( + "Unexpected callCount to binding validation status handler", 4, + bindingHandler.callCount); + } + @Test public void removed_binding_not_updates_value() { Binding<Person, Integer> binding = binder.forField(ageField) @@ -766,6 +828,5 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> { Assert.assertEquals("Binding still affects bean even after unbind", ageBeforeUnbind, String.valueOf(item.getAge())); - } } |