use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method binderHasChanges.
@Test
public void binderHasChanges() throws ValidationException {
binder.forField(nameField).withValidator(Validator.from(name -> !"".equals(name), "Name can't be empty")).bind(Person::getFirstName, Person::setFirstName);
assertFalse(binder.hasChanges());
binder.setBean(item);
assertFalse(binder.hasChanges());
// Bound binder + valid user changes: hasChanges == false
nameField.setValue("foo");
assertFalse(binder.hasChanges());
nameField.setValue("bar");
binder.writeBeanIfValid(new Person());
assertFalse(binder.hasChanges());
// Bound binder + invalid user changes: hasChanges() == true
nameField.setValue("");
binder.writeBeanIfValid(new Person());
assertTrue(binder.hasChanges());
// Read bean resets hasChanges
binder.readBean(item);
assertFalse(binder.hasChanges());
// Removing a bound bean resets hasChanges
nameField.setValue("");
assertTrue(binder.hasChanges());
binder.removeBean();
assertFalse(binder.hasChanges());
// Unbound binder + valid user changes: hasChanges() == true
nameField.setValue("foo");
assertTrue(binder.hasChanges());
// successful writeBean resets hasChanges to false
binder.writeBeanIfValid(new Person());
assertFalse(binder.hasChanges());
// Unbound binder + invalid user changes: hasChanges() == true
nameField.setValue("");
assertTrue(binder.hasChanges());
// unsuccessful writeBean doesn't affect hasChanges
nameField.setValue("");
binder.writeBeanIfValid(new Person());
assertTrue(binder.hasChanges());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method validatorForSuperTypeCanBeUsed.
@Test
public void validatorForSuperTypeCanBeUsed() {
// Validates that a validator for a super type can be used, e.g.
// validator for Number can be used on a Double
TestTextField salaryField = new TestTextField();
Validator<Number> positiveNumberValidator = (value, context) -> {
if (value.doubleValue() >= 0) {
return ValidationResult.ok();
} else {
return ValidationResult.error(NEGATIVE_ERROR_MESSAGE);
}
};
binder.forField(salaryField).withConverter(Double::valueOf, String::valueOf).withValidator(positiveNumberValidator).bind(Person::getSalaryDouble, Person::setSalaryDouble);
Person person = new Person();
binder.setBean(person);
salaryField.setValue("10");
assertEquals(10, person.getSalaryDouble(), 0);
// Does not pass validator
salaryField.setValue("-1");
assertEquals(10, person.getSalaryDouble(), 0);
assertInvalidField(NEGATIVE_ERROR_MESSAGE, salaryField);
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method save_beanValidationErrors.
@Test(expected = ValidationException.class)
public void save_beanValidationErrors() throws ValidationException {
Binder<Person> binder = new Binder<>();
binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
binder.withValidator(Validator.from(person -> false, "b"));
Person person = new Person();
nameField.setValue("foo");
try {
binder.writeBean(person);
} finally {
// Bean should have been updated for item validation but reverted
assertNull(person.getFirstName());
}
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method validate_failedBothBeanValidatorAndFieldValidator.
@Test
public void validate_failedBothBeanValidatorAndFieldValidator() {
String msg1 = "foo";
BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg1));
binding.bind(Person::getFirstName, Person::setFirstName);
String msg2 = "bar";
binder.withValidator(Validator.from(bean -> false, msg2));
Person person = new Person();
binder.setBean(person);
List<BindingValidationStatus<?>> errors = binder.validate().getFieldValidationErrors();
assertEquals(1, errors.size());
BindingValidationStatus<?> error = errors.get(0);
assertEquals(msg1, error.getMessage().get());
assertEquals(nameField, error.getField());
assertEquals(msg1, nameField.getErrorMessage());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindNotHasValueField_fieldIsNotReplaced.
@Test
public void bindInstanceFields_bindNotHasValueField_fieldIsNotReplaced() {
BindFieldHasWrongType form = new BindFieldHasWrongType();
Binder<Person> binder = new Binder<>(Person.class);
String name = "foo";
form.firstName = name;
Person person = new Person();
person.setFirstName("foo");
binder.setBean(person);
Assert.assertEquals(name, form.firstName);
}
Aggregations