use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindDeepNestedFieldsUsingAnnotation.
@Test
public void bindInstanceFields_bindDeepNestedFieldsUsingAnnotation() {
BindDeepNestedFieldsUsingAnnotation form = new BindDeepNestedFieldsUsingAnnotation();
Binder<Couple> binder = new Binder<>(Couple.class, true);
binder.bindInstanceFields(form);
Person first = new Person();
Person second = new Person();
Address firstAddress = new Address();
firstAddress.setStreetAddress("Foo st.");
first.setAddress(firstAddress);
Address secondAddress = new Address();
second.setAddress(secondAddress);
secondAddress.setStreetAddress("Bar ave.");
Couple couple = new Couple();
couple.setFirst(first);
couple.setSecond(second);
binder.setBean(couple);
Assert.assertEquals("Binding deep nested properties using annotation", couple.first.getAddress().getStreetAddress(), form.firstStreetField.getValue());
Assert.assertEquals("Binding parallel deep nested properties using annotation", couple.second.getAddress().getStreetAddress(), form.secondStreetField.getValue());
form.firstStreetField.setValue(second.getAddress().getStreetAddress());
Assert.assertEquals("Updating value in deep nested properties", form.firstStreetField.getValue(), first.getAddress().getStreetAddress());
}
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 BinderTest method bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.
@Test(expected = BindingException.class)
public void bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() {
TestTextField testField = new TestTextField();
setExceptionHandler();
binder.forField(testField).withConverter(Converter.<String, String>from(name -> Result.ok(name), name -> {
throw new NullPointerException();
})).bind(Person::getFirstName, Person::setFirstName).read(new Person());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method execute_binding_status_handler_from_binder_status_handler.
@Test
public void execute_binding_status_handler_from_binder_status_handler() {
MyBindingHandler bindingHandler = new MyBindingHandler();
binder.forField(nameField).withValidator(t -> !t.isEmpty(), "No empty values.").withValidationStatusHandler(bindingHandler).bind(Person::getFirstName, Person::setFirstName);
String ageError = "CONVERSIONERROR";
binder.forField(ageField).withConverter(new StringToIntegerConverter(ageError)).bind(Person::getAge, Person::setAge);
binder.setValidationStatusHandler(BinderValidationStatus::notifyBindingValidationStatusHandlers);
String initialName = item.getFirstName();
int initialAge = item.getAge();
binder.setBean(item);
// Test specific error handling.
bindingHandler.expectingError = true;
nameField.setValue("");
// Test default error handling.
ageField.setValue("foo");
assertThat("Error message is not what was expected", ageField.getErrorMessage(), containsString(ageError));
// Restore values and test no errors.
ageField.setValue(String.valueOf(initialAge));
assertFalse("The field should be valid", ageField.isInvalid());
bindingHandler.expectingError = false;
nameField.setValue(initialName);
// Assert that the handler was called.
assertEquals("Unexpected callCount to binding validation status handler", 6, bindingHandler.callCount);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method setReadOnly_binderLoadedByReadBean.
@Test
public void setReadOnly_binderLoadedByReadBean() {
binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.readBean(new Person());
binder.setReadOnly(true);
assertTrue(nameField.isReadOnly());
assertTrue(ageField.isReadOnly());
binder.setReadOnly(false);
assertFalse(nameField.isReadOnly());
assertFalse(ageField.isReadOnly());
}
Aggregations