use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced.
@Test
public void bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced() {
BindAllFields form = new BindAllFields();
Binder<Person> binder = new Binder<>(Person.class);
TestTextField name = new TestTextField();
form.firstName = name;
binder.forField(form.firstName).withValidator(new StringLengthValidator("Name is invalid", 3, 10)).bind("firstName");
binder.bindInstanceFields(form);
Person person = new Person();
String personName = "foo";
person.setFirstName(personName);
person.setBirthDate(LocalDate.now());
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
// the instance is not overridden
Assert.assertEquals(name, form.firstName);
// Test automatic binding
form.birthDate.setValue(person.getBirthDate().plusDays(345));
Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
// Test custom binding
form.firstName.setValue("aa");
Assert.assertEquals(personName, person.getFirstName());
Assert.assertFalse(binder.validate().isOk());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_preconfiguredFieldNotBoundToPropertyPreserved.
@Test
public void bindInstanceFields_preconfiguredFieldNotBoundToPropertyPreserved() {
BindOneFieldRequiresConverter form = new BindOneFieldRequiresConverter();
form.age = new TestTextField();
form.firstName = new TestTextField();
Binder<Person> binder = new Binder<>(Person.class);
binder.forField(form.age).withConverter(str -> Integer.parseInt(str) / 2, integer -> Integer.toString(integer * 2)).bind(Person::getAge, Person::setAge);
binder.bindInstanceFields(form);
Person person = new Person();
person.setFirstName("first");
person.setAge(45);
binder.setBean(person);
Assert.assertEquals("90", form.age.getValue());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_complexGenericHierarchy.
@Test
public void bindInstanceFields_complexGenericHierarchy() {
BindComplextHierarchyGenericType form = new BindComplextHierarchyGenericType();
Binder<Person> binder = new Binder<>(Person.class);
binder.bindInstanceFields(form);
Person person = new Person();
person.setFirstName("foo");
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
form.firstName.setValue("bar");
Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindNestedFieldUsingAnnotation.
@Test
public void bindInstanceFields_bindNestedFieldUsingAnnotation() {
BindNestedFieldsUsingAnnotation form = new BindNestedFieldsUsingAnnotation();
Binder<Person> binder = new Binder<>(Person.class, true);
binder.bindInstanceFields(form);
Person person = new Person();
Address address = new Address();
address.setStreetAddress("Foo st.");
person.setAddress(address);
binder.setBean(person);
Assert.assertEquals("Reading nested properties bound using annotation", person.getAddress().getStreetAddress(), form.streetAddressField.getValue());
form.streetAddressField.setValue("Bar ave.");
Assert.assertEquals("Changing nested properties bound using annotation", form.streetAddressField.getValue(), person.getAddress().getStreetAddress());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindAllFields.
@Test
public void bindInstanceFields_bindAllFields() {
BindAllFields form = new BindAllFields();
Binder<Person> binder = new Binder<>(Person.class);
binder.bindInstanceFields(form);
Person person = new Person();
person.setFirstName("foo");
person.setBirthDate(LocalDate.now());
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
form.firstName.setValue("bar");
form.birthDate.setValue(person.getBirthDate().plusDays(345));
Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
}
Aggregations