use of com.vaadin.flow.data.binder.testcomponents.TestTextField 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.data.binder.testcomponents.TestTextField 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.data.binder.testcomponents.TestTextField 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());
}
use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderTest method setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator.
@Test
public void setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator() {
TestTextField textField = new TestTextField();
assertFalse(textField.isRequiredIndicatorVisible());
BindingBuilder<Person, String> binding = binder.forField(textField);
assertFalse(textField.isRequiredIndicatorVisible());
AtomicInteger invokes = new AtomicInteger();
binding.asRequired(context -> {
invokes.incrementAndGet();
return "foobar";
});
assertTrue(textField.isRequiredIndicatorVisible());
binding.bind(Person::getFirstName, Person::setFirstName);
binder.setBean(item);
assertThat(textField.getErrorMessage(), isEmptyString());
Assert.assertEquals(0, invokes.get());
textField.setValue(textField.getEmptyValue());
Assert.assertEquals("foobar", componentErrors.get(textField));
// validation is done for all changed bindings once.
Assert.assertEquals(1, invokes.get());
textField.setValue("value");
assertFalse(textField.isInvalid());
assertTrue(textField.isRequiredIndicatorVisible());
}
use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderTest method beanvalidation_validate_throws_with_readBean.
@Test(expected = IllegalStateException.class)
public void beanvalidation_validate_throws_with_readBean() {
TestTextField lastNameField = new TestTextField();
setBeanValidationFirstNameNotEqualsLastName(nameField, lastNameField);
binder.readBean(item);
Assert.assertTrue(binder.validate().isOk());
}
Aggregations