summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-06-15 20:04:38 +0300
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-07-11 10:01:02 +0300
commit30115368df1a50c516d9fa059756cc8ac3d88886 (patch)
tree7cc8ac1ad0433116672b5f70030878eaba42da58 /server
parent98bfa99f28ab8e1c636216f5a09b189321a2f97b (diff)
downloadvaadin-framework-30115368df1a50c516d9fa059756cc8ac3d88886.tar.gz
vaadin-framework-30115368df1a50c516d9fa059756cc8ac3d88886.zip
Only throw from Binder.bindInstanceFields if there are no bindings at all (#9487)
Fixes #8986
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/data/Binder.java7
-rw-r--r--server/src/test/java/com/vaadin/data/BeanBinderTest.java52
-rw-r--r--server/src/test/java/com/vaadin/data/BinderInstanceFieldTest.java13
3 files changed, 64 insertions, 8 deletions
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<BEAN> 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<TestEnum> 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);
@@ -141,6 +162,35 @@ public class BeanBinderTest
}
@Test(expected = IllegalStateException.class)
+ public void bindInstanceFields_throw_if_no_fields_bound() {
+ Binder<TestBean> 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<Person> 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<Person> 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<TestBean> otherBinder = new Binder<>(TestBean.class);
TestClass testClass = new TestClass();
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<Person> 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<Person> 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);
}
}