use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method removed_binding_not_updates_value.
@Test
public void removed_binding_not_updates_value() {
Binding<Person, Integer> binding = binder.forField(ageField).withConverter(new StringToIntegerConverter("Can't convert")).bind(Person::getAge, Person::setAge);
binder.setBean(item);
String modifiedAge = String.valueOf(item.getAge() + 10);
String ageBeforeUnbind = String.valueOf(item.getAge());
binder.removeBinding(binding);
ageField.setValue(modifiedAge);
assertEquals("Binding still affects bean even after unbind", ageBeforeUnbind, String.valueOf(item.getAge()));
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method symetricValue_setBean_beanNotUpdated.
@Test
public void symetricValue_setBean_beanNotUpdated() {
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
binder.setBean(new Person() {
@Override
public String getFirstName() {
return "First";
}
@Override
public void setFirstName(String firstName) {
Assert.fail("Setter should not be called");
}
});
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method conversionWithLocaleBasedErrorMessage.
@Test
public void conversionWithLocaleBasedErrorMessage() {
TestTextField ageField = new TestTextField();
String fiError = "VIRHE";
String otherError = "ERROR";
StringToIntegerConverter converter = new StringToIntegerConverter(context -> context.getLocale().map(Locale::getLanguage).orElse("en").equals("fi") ? fiError : otherError);
binder.forField(ageField).withConverter(converter).bind(Person::getAge, Person::setAge);
binder.setBean(item);
UI testUI = new UI();
UI.setCurrent(testUI);
testUI.add(ageField);
ageField.setValue("not a number");
assertEquals(otherError, ageField.getErrorMessage());
testUI.setLocale(new Locale("fi", "FI"));
// Re-validate to get the error message with correct locale
binder.validate();
assertEquals(fiError, ageField.getErrorMessage());
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderTest method setBean_converterThrows_setBean_exceptionHandlerSet_bindingExceptionIsThrown.
@Test(expected = BindingException.class)
public void setBean_converterThrows_setBean_exceptionHandlerSet_bindingExceptionIsThrown() {
TestTextField testField = new TestTextField();
setExceptionHandler();
binder.forField(testField).withConverter(Converter.<String, String>from(name -> {
throw new NullPointerException();
}, name -> name)).bind(Person::getFirstName, Person::setFirstName);
binder.setBean(new Person());
}
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());
}
Aggregations