use of org.apache.commons.lang3.builder.ToStringStyleTest.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());
}
use of org.apache.commons.lang3.builder.ToStringStyleTest.Person in project flow by vaadin.
the class BinderTest method beanBinder_withConverter_nullRepresentationIsNotDisabled.
@Test
public void beanBinder_withConverter_nullRepresentationIsNotDisabled() {
String customNullPointerRepresentation = "foo";
Binder<Person> binder = new Binder<>(Person.class);
binder.forField(nameField).withConverter(value -> value, value -> value == null ? customNullPointerRepresentation : value).bind("firstName");
Person person = new Person();
binder.setBean(person);
Assert.assertEquals(customNullPointerRepresentation, nameField.getValue());
}
use of org.apache.commons.lang3.builder.ToStringStyleTest.Person in project flow by vaadin.
the class BinderTest method invalidUsage_modifyFieldsInsideValidator_binderDoesNotThrow.
@Test
public void invalidUsage_modifyFieldsInsideValidator_binderDoesNotThrow() {
TestTextField field = new TestTextField();
AtomicBoolean validatorIsExecuted = new AtomicBoolean();
binder.forField(field).asRequired().withValidator((val, context) -> {
nameField.setValue("foo");
ageField.setValue("bar");
validatorIsExecuted.set(true);
return ValidationResult.ok();
}).bind(Person::getEmail, Person::setEmail);
binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).bind(Person::getLastName, Person::setLastName);
binder.setBean(new Person());
field.setValue("baz");
// mostly self control, the main check is: not exception is thrown
Assert.assertTrue(validatorIsExecuted.get());
}
use of org.apache.commons.lang3.builder.ToStringStyleTest.Person in project flow by vaadin.
the class BinderTest method withConverter_disablesDefaulNullRepresentation.
@Test
public void withConverter_disablesDefaulNullRepresentation() {
Integer customNullConverter = 0;
binder.forField(ageField).withNullRepresentation("foo").withConverter(new StringToIntegerConverter("")).withConverter(age -> age, age -> age == null ? customNullConverter : age).bind(Person::getSalary, Person::setSalary);
binder.setBean(item);
Assert.assertEquals(customNullConverter.toString(), ageField.getValue());
Integer salary = 11;
ageField.setValue(salary.toString());
Assert.assertEquals(11, salary.intValue());
}
use of org.apache.commons.lang3.builder.ToStringStyleTest.Person in project flow by vaadin.
the class BinderTest method do_test_save_bound_beanAsDraft.
private void do_test_save_bound_beanAsDraft(boolean setBean) {
Binder<Person> binder = new Binder<>();
binder.forField(nameField).withValidator((value, context) -> {
if (value.equals("Mike")) {
return ValidationResult.ok();
} else {
return ValidationResult.error("value must be Mike");
}
}).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
Person person = new Person();
if (setBean) {
binder.setBean(person);
}
String fieldValue = "John";
nameField.setValue(fieldValue);
int age = 10;
ageField.setValue("10");
person.setFirstName("Mark");
binder.writeBeanAsDraft(person);
// name is not written to draft as validation / conversion
// does not pass
assertNotEquals(fieldValue, person.getFirstName());
// age is written to draft even if firstname validation
// fails
assertEquals(age, person.getAge());
binder.writeBeanAsDraft(person, true);
// name is now written despite validation as write was forced
assertEquals(fieldValue, person.getFirstName());
}
Aggregations