use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced.
@Test
public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
BindWithNoFieldInPerson form = new BindWithNoFieldInPerson();
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");
TestTextField ageField = new TestTextField();
form.noFieldInPerson = ageField;
binder.forField(form.noFieldInPerson).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.bindInstanceFields(form);
Person person = new Person();
String personName = "foo";
int age = 11;
person.setFirstName(personName);
person.setAge(age);
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(String.valueOf(person.getAge()), form.noFieldInPerson.getValue());
// the instances are not overridden
Assert.assertEquals(name, form.firstName);
Assert.assertEquals(ageField, form.noFieldInPerson);
// Test correct age
age += 56;
form.noFieldInPerson.setValue(String.valueOf(age));
Assert.assertEquals(form.noFieldInPerson.getValue(), String.valueOf(person.getAge()));
// Test incorrect name
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_bindOnlyOneFields.
@Test
public void bindInstanceFields_bindOnlyOneFields() {
BindOnlyOneField form = new BindOnlyOneField();
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());
Assert.assertNull(form.noFieldInPerson);
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_bindNotHasValueField_fieldIsNull.
@Test
public void bindInstanceFields_bindNotHasValueField_fieldIsNull() {
BindFieldHasWrongType form = new BindFieldHasWrongType();
Binder<Person> binder = new Binder<>(Person.class);
binder.bindInstanceFields(form);
Person person = new Person();
person.setFirstName("foo");
binder.setBean(person);
Assert.assertNull(form.firstName);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindAllFieldsUsingAnnotations.
@Test
public void bindInstanceFields_bindAllFieldsUsingAnnotations() {
BindFieldsUsingAnnotation form = new BindFieldsUsingAnnotation();
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.nameField.getValue());
Assert.assertEquals(person.getBirthDate(), form.birthDateField.getValue());
form.nameField.setValue("bar");
form.birthDateField.setValue(person.getBirthDate().plusDays(345));
Assert.assertEquals(form.nameField.getValue(), person.getFirstName());
Assert.assertEquals(form.birthDateField.getValue(), person.getBirthDate());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method validationStatusHandler_onlyRunForChangedField.
@Test
public void validationStatusHandler_onlyRunForChangedField() {
TestTextField firstNameField = new TestTextField();
TestTextField lastNameField = new TestTextField();
AtomicInteger invokes = new AtomicInteger();
binder.forField(firstNameField).withValidator(new NotEmptyValidator<>("")).withValidationStatusHandler(validationStatus -> invokes.addAndGet(1)).bind(Person::getFirstName, Person::setFirstName);
binder.forField(lastNameField).withValidator(new NotEmptyValidator<>("")).bind(Person::getLastName, Person::setLastName);
binder.setBean(item);
// setting the bean causes 2:
Assert.assertEquals(2, invokes.get());
lastNameField.setValue("");
Assert.assertEquals(2, invokes.get());
firstNameField.setValue("");
Assert.assertEquals(3, invokes.get());
binder.removeBean();
Person person = new Person();
person.setFirstName("a");
person.setLastName("a");
binder.readBean(person);
// reading from a bean causes 2:
Assert.assertEquals(5, invokes.get());
lastNameField.setValue("");
Assert.assertEquals(5, invokes.get());
firstNameField.setValue("");
Assert.assertEquals(6, invokes.get());
}
Aggregations