From 30115368df1a50c516d9fa059756cc8ac3d88886 Mon Sep 17 00:00:00 2001 From: Artur Date: Thu, 15 Jun 2017 20:04:38 +0300 Subject: Only throw from Binder.bindInstanceFields if there are no bindings at all (#9487) Fixes #8986 --- server/src/main/java/com/vaadin/data/Binder.java | 7 ++- .../test/java/com/vaadin/data/BeanBinderTest.java | 52 +++++++++++++++++++++- .../com/vaadin/data/BinderInstanceFieldTest.java | 13 +++--- 3 files changed, 64 insertions(+), 8 deletions(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index ce843afacc..1b05bb44b1 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -2138,10 +2138,15 @@ public class Binder implements Serializable { (property, type) -> bindProperty(objectWithMemberFields, memberField, property, type))) .reduce(0, this::accumulate, Integer::sum); - if (numberOfBoundFields == 0) { + if (numberOfBoundFields == 0 && bindings.isEmpty() + && incompleteBindings.isEmpty()) { + // Throwing here for incomplete bindings would be wrong as they + // may be completed after this call. If they are not, setBean and + // other methods will throw for those cases throw new IllegalStateException("There are no instance fields " + "found for automatic binding"); } + } private boolean isFieldBound(Field memberField, diff --git a/server/src/test/java/com/vaadin/data/BeanBinderTest.java b/server/src/test/java/com/vaadin/data/BeanBinderTest.java index 810f7e4e48..b01f17d755 100644 --- a/server/src/test/java/com/vaadin/data/BeanBinderTest.java +++ b/server/src/test/java/com/vaadin/data/BeanBinderTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import java.io.Serializable; +import java.time.LocalDate; import java.util.List; import java.util.Set; @@ -33,7 +34,11 @@ public class BeanBinderTest private TextField number = new TextField(); } - private static class TestBean implements Serializable{ + + private class TestClassWithoutFields { + } + + private static class TestBean implements Serializable { private Set enums; private int number; @@ -91,6 +96,22 @@ public class BeanBinderTest } } + public static class Person { + LocalDate mydate; + + public LocalDate getMydate() { + return mydate; + } + + public void setMydate(LocalDate mydate) { + this.mydate = mydate; + } + } + + public static class PersonForm { + private TextField mydate = new TextField(); + } + @Before public void setUp() { binder = new BeanValidationBinder<>(BeanToValidate.class); @@ -140,6 +161,35 @@ public class BeanBinderTest otherBinder.bindInstanceFields(testClass); } + @Test(expected = IllegalStateException.class) + public void bindInstanceFields_throw_if_no_fields_bound() { + Binder otherBinder = new Binder<>(TestBean.class); + TestClassWithoutFields testClass = new TestClassWithoutFields(); + + // Should throw an IllegalStateException no fields are bound + otherBinder.bindInstanceFields(testClass); + } + + @Test + public void bindInstanceFields_does_not_throw_if_fields_are_bound_manually() { + PersonForm form = new PersonForm(); + Binder binder = new Binder<>(Person.class); + binder.forMemberField(form.mydate) + .withConverter(str -> LocalDate.now(), date -> "Hello") + .bind("mydate"); + binder.bindInstanceFields(form); + + } + + @Test + public void bindInstanceFields_does_not_throw_if_there_are_incomplete_bindings() { + PersonForm form = new PersonForm(); + Binder binder = new Binder<>(Person.class); + binder.forMemberField(form.mydate).withConverter(str -> LocalDate.now(), + date -> "Hello"); + binder.bindInstanceFields(form); + } + @Test(expected = IllegalStateException.class) public void incomplete_forMemberField_bindings() { Binder otherBinder = new Binder<>(TestBean.class); diff --git a/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java b/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java index d0cd75649d..b09283936d 100644 --- a/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java +++ b/server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java @@ -414,8 +414,8 @@ public class BinderInstanceFieldTest { Assert.assertEquals("90", form.age.getValue()); } - @Test(expected = IllegalStateException.class) - public void bindInstanceFields_explicitelyBoundFieldAndNotBoundField_throwNoBoundFields() { + @Test + public void bindInstanceFields_explicitelyBoundFieldAndNotBoundField() { BindOnlyOneField form = new BindOnlyOneField(); Binder binder = new Binder<>(Person.class); @@ -424,8 +424,8 @@ public class BinderInstanceFieldTest { binder.bindInstanceFields(form); } - @Test(expected = IllegalStateException.class) - public void bindInstanceFields_tentativelyBoundFieldAndNotBoundField_throwNoBoundFields() { + @Test + public void bindInstanceFields_tentativelyBoundFieldAndNotBoundField() { BindOnlyOneField form = new BindOnlyOneField(); Binder binder = new Binder<>(Person.class); @@ -436,8 +436,9 @@ public class BinderInstanceFieldTest { // manually binder.forMemberField(field); - // bindInstance expects at least one auto configured field (there is no - // such, only incomplete one) and fails + // bindInstanceFields will not complain even though it can't bind + // anything as there is a binding in progress (an exception will be + // thrown later if the binding is not completed) binder.bindInstanceFields(form); } } -- cgit v1.2.3