use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method setReadOnly_boundBinder.
@Test
public void setReadOnly_boundBinder() {
binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.setBean(new Person());
binder.setReadOnly(true);
assertTrue(nameField.isReadOnly());
assertTrue(ageField.isReadOnly());
binder.setReadOnly(false);
assertFalse(nameField.isReadOnly());
assertFalse(ageField.isReadOnly());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method bound_validatorsFail_errors.
@Test
public void bound_validatorsFail_errors() {
BindingBuilder<Person, String> binding = binder.forField(nameField);
binding.withValidator(Validator.alwaysPass());
String errorMessage = "foo";
binding.withValidator((String value, ValueContext context) -> ValidationResult.error(errorMessage));
binding.withValidator(value -> false, "bar");
binding.bind(Person::getFirstName, Person::setFirstName);
BinderValidationStatus<Person> status = binder.validate();
List<BindingValidationStatus<?>> errors = status.getFieldValidationErrors();
assertEquals(1, errors.size());
BindingValidationStatus<?> validationStatus = errors.stream().findFirst().get();
assertEquals(errorMessage, validationStatus.getMessage().get());
HasValue<?, ?> field = validationStatus.getField();
assertEquals(nameField, field);
assertEquals(errorMessage, componentErrors.get(nameField));
assertInvalidField(errorMessage, nameField);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method validate_failedBeanValidatorWithFieldValidator.
@Test
public void validate_failedBeanValidatorWithFieldValidator() {
String msg = "foo";
BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg));
binding.bind(Person::getFirstName, Person::setFirstName);
binder.withValidator(Validator.from(bean -> false, msg));
Person person = new Person();
binder.setBean(person);
List<BindingValidationStatus<?>> errors = binder.validate().getFieldValidationErrors();
assertEquals(1, errors.size());
BindingValidationStatus<?> error = errors.get(0);
assertEquals(msg, error.getMessage().get());
assertEquals(nameField, error.getField());
assertInvalidField(msg, nameField);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method save_validationErrors_exceptionContainsErrors.
@Test
public void save_validationErrors_exceptionContainsErrors() throws ValidationException {
String msg = "foo";
BindingBuilder<Person, String> nameBinding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg));
nameBinding.bind(Person::getFirstName, Person::setFirstName);
BindingBuilder<Person, Integer> ageBinding = binder.forField(ageField).withConverter(stringToInteger).withValidator(notNegative);
ageBinding.bind(Person::getAge, Person::setAge);
Person person = new Person();
nameField.setValue("");
ageField.setValue("-1");
try {
binder.writeBean(person);
Assert.fail();
} catch (ValidationException exception) {
List<BindingValidationStatus<?>> validationErrors = exception.getFieldValidationErrors();
Assert.assertEquals(2, validationErrors.size());
BindingValidationStatus<?> error = validationErrors.get(0);
Assert.assertEquals(nameField, error.getField());
Assert.assertEquals(msg, error.getMessage().get());
error = validationErrors.get(1);
Assert.assertEquals(ageField, error.getField());
Assert.assertEquals(NEGATIVE_ERROR_MESSAGE, error.getMessage().get());
}
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method save_fieldsAndBeanLevelValidation.
@Test
public void save_fieldsAndBeanLevelValidation() throws ValidationException {
binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
binder.withValidator(Validator.from(person -> person.getLastName() != null, "b"));
Person person = new Person();
person.setLastName("bar");
nameField.setValue("foo");
binder.writeBean(person);
Assert.assertEquals(nameField.getValue(), person.getFirstName());
Assert.assertEquals("bar", person.getLastName());
}
Aggregations