Fixes #8986tags/8.1.0.beta2^0
@@ -2141,10 +2141,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, |
@@ -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; | |||
@@ -35,6 +36,9 @@ public class BeanBinderTest | |||
private TextField number = new TextField(); | |||
} | |||
private class TestClassWithoutFields { | |||
} | |||
private static class TestBean implements Serializable { | |||
private Set<TestEnum> enums; | |||
private int number; | |||
@@ -145,6 +149,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); | |||
@@ -194,6 +214,35 @@ public class BeanBinderTest | |||
otherBinder.bindInstanceFields(testClass); | |||
} | |||
@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); |
@@ -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); | |||
} | |||
} |