aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-10-11 15:15:46 +0300
committerHenri Sara <henri.sara@gmail.com>2017-10-11 15:15:46 +0300
commitdd806e8bb31ecc7bce50f3aed5ed3fab48364895 (patch)
tree78a37299c2bda5e6a5dff17f2d1289739f829a3c /server/src/test/java
parent75ac029f5a8f18ff42ef33644876f063e9981031 (diff)
downloadvaadin-framework-dd806e8bb31ecc7bce50f3aed5ed3fab48364895.tar.gz
vaadin-framework-dd806e8bb31ecc7bce50f3aed5ed3fab48364895.zip
Fix Binder bean writing to only validate and write given bindings (#10162)
Diffstat (limited to 'server/src/test/java')
-rw-r--r--server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java12
-rw-r--r--server/src/test/java/com/vaadin/data/BinderTest.java36
2 files changed, 42 insertions, 6 deletions
diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
index 4915bc9a5b..734aa5b194 100644
--- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
@@ -621,14 +621,22 @@ public class BinderConverterValidatorTest
// bind a new field that has invalid value in bean
TextField lastNameField = new TextField();
- person.setLastName("");
+
+ // The test starts with a valid value as the last name of the person,
+ // since the binder assumes any non-changed values to be valid.
+ person.setLastName("bar");
+
BindingBuilder<Person, String> binding2 = binder.forField(lastNameField)
.withValidator(notEmpty);
binding2.bind(Person::getLastName, Person::setLastName);
- // should not have error shown
+ // should not have error shown when initialized
assertNull(lastNameField.getComponentError());
+ // Set a value that breaks the validation
+ lastNameField.setValue("");
+ assertNotNull(lastNameField.getComponentError());
+
// add status label to show bean level error
Label statusLabel = new Label();
binder.setStatusLabel(statusLabel);
diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java
index 1169eb8a5d..a44e3b3e06 100644
--- a/server/src/test/java/com/vaadin/data/BinderTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderTest.java
@@ -392,9 +392,8 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
String customNullPointerRepresentation = "foo";
Binder<Person> binder = new Binder<>(Person.class);
binder.forField(nameField)
- .withConverter(value -> value,
- value -> value == null ? customNullPointerRepresentation
- : value)
+ .withConverter(value -> value, value -> value == null
+ ? customNullPointerRepresentation : value)
.bind("firstName");
Person person = new Person();
@@ -490,7 +489,7 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
ErrorMessage errorMessage = textField.getErrorMessage();
assertNotNull(errorMessage);
assertEquals("foobar", errorMessage.getFormattedHtmlMessage());
- // validation is done for the whole bean at once.
+ // validation is done for all changed bindings once.
assertEquals(1, invokes.get());
textField.setValue("value");
@@ -942,4 +941,33 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
assertEquals("Binding still affects bean even after unbind",
ageBeforeUnbind, String.valueOf(item.getAge()));
}
+
+ @Test
+ public void two_asRequired_fields_without_initial_values() {
+ binder.forField(nameField).asRequired("Empty name").bind(p -> "",
+ (p, s) -> {
+ });
+ binder.forField(ageField).asRequired("Empty age").bind(p -> "",
+ (p, s) -> {
+ });
+
+ binder.setBean(item);
+ assertNull("Initially there should be no errors",
+ nameField.getComponentError());
+ assertNull("Initially there should be no errors",
+ ageField.getComponentError());
+
+ nameField.setValue("Foo");
+ assertNull("Name with a value should not be an error",
+ nameField.getComponentError());
+ assertNull(
+ "Age field should not be in error, since it has not been modified.",
+ ageField.getComponentError());
+
+ nameField.setValue("");
+ assertNotNull("Empty name should now be in error.",
+ nameField.getComponentError());
+ assertNull("Age field should still be ok.",
+ ageField.getComponentError());
+ }
}