Skip to content

Commit

Permalink
Only throw from Binder.bindInstanceFields if there are no bindings at…
Browse files Browse the repository at this point in the history
… all (#9487)

Fixes #8986
  • Loading branch information
Artur- authored Jun 15, 2017
1 parent 620b413 commit 2c5dd49
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
7 changes: 6 additions & 1 deletion server/src/main/java/com/vaadin/data/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2141,10 +2141,15 @@ public void bindInstanceFields(Object objectWithMemberFields) {
(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,
Expand Down
49 changes: 49 additions & 0 deletions server/src/test/java/com/vaadin/data/BeanBinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertSame;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -35,6 +36,9 @@ private class TestClass {
private TextField number = new TextField();
}

private class TestClassWithoutFields {
}

private static class TestBean implements Serializable {
private Set<TestEnum> enums;
private int number;
Expand Down Expand Up @@ -145,6 +149,22 @@ public void setValue(String value) {
}
}

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);
Expand Down Expand Up @@ -194,6 +214,35 @@ public void bindInstanceFields_does_not_automatically_bind_incomplete_forField_b
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ public void bindInstanceFields_preconfiguredFieldNotBoundToPropertyPreserved() {
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);

Expand All @@ -424,8 +424,8 @@ public void bindInstanceFields_explicitelyBoundFieldAndNotBoundField_throwNoBoun
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);

Expand All @@ -436,8 +436,9 @@ public void bindInstanceFields_tentativelyBoundFieldAndNotBoundField_throwNoBoun
// 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);
}
}

0 comments on commit 2c5dd49

Please sign in to comment.