use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method validate_okBeanValidatorWithoutFieldValidators.
@Test
public void validate_okBeanValidatorWithoutFieldValidators() {
binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
String msg = "foo";
binder.withValidator(Validator.from(bean -> true, msg));
Person person = new Person();
binder.setBean(person);
assertFalse(binder.validate().hasErrors());
assertTrue(binder.validate().isOk());
assertValidField(nameField);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method binder_saveIfValid.
@Test
public void binder_saveIfValid() {
String msg1 = "foo";
BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg1));
binding.bind(Person::getFirstName, Person::setFirstName);
String beanValidatorErrorMessage = "bar";
binder.withValidator(Validator.from(bean -> false, beanValidatorErrorMessage));
Person person = new Person();
String firstName = "first name";
person.setFirstName(firstName);
binder.readBean(person);
nameField.setValue("");
assertFalse(binder.writeBeanIfValid(person));
// check that field level-validation failed and bean is not updated
assertEquals(firstName, person.getFirstName());
assertInvalidField(msg1, nameField);
nameField.setValue("new name");
assertFalse(binder.writeBeanIfValid(person));
// Bean is updated but reverted
assertEquals(firstName, person.getFirstName());
assertValidField(nameField);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_tentativelyBoundFieldAndNotBoundField.
@Test
public void bindInstanceFields_tentativelyBoundFieldAndNotBoundField() {
BindOnlyOneField form = new BindOnlyOneField();
Binder<Person> binder = new Binder<>(Person.class);
TestTextField field = new TestTextField();
form.firstName = field;
// This is an incomplete binding which is supposed to be configured
// manually
binder.forMemberField(field);
// 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);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method readBean_converterThrows_readBean_exceptionHandlerSet_bindingExceptionIsThrown.
@Test(expected = BindingException.class)
public void readBean_converterThrows_readBean_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);
binder.readBean(new Person());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method valueChangeListenerOrder.
@Test
public void valueChangeListenerOrder() {
AtomicBoolean beanSet = new AtomicBoolean();
nameField.addValueChangeListener(e -> {
if (!beanSet.get()) {
assertEquals("Value in bean updated earlier than expected", e.getOldValue(), item.getFirstName());
}
});
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
nameField.addValueChangeListener(e -> {
if (!beanSet.get()) {
assertEquals("Value in bean not updated when expected", e.getValue(), item.getFirstName());
}
});
beanSet.set(true);
binder.setBean(item);
beanSet.set(false);
nameField.setValue("Foo");
}
Aggregations