use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderConverterValidatorTest method save_beanValidationErrorsWithConverter.
@Test(expected = ValidationException.class)
public void save_beanValidationErrorsWithConverter() throws ValidationException {
Binder<Person> binder = new Binder<>();
binder.forField(ageField).withConverter(new StringToIntegerConverter("Can't convert")).bind(Person::getAge, Person::setAge);
binder.withValidator(Validator.from(person -> false, "b"));
Person person = new Person();
ageField.setValue("1");
try {
binder.writeBean(person);
} finally {
// Bean should have been updated for item validation but reverted
Assert.assertEquals(0, person.getAge());
}
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderTest method execute_binding_status_handler_from_binder_status_handler.
@Test
public void execute_binding_status_handler_from_binder_status_handler() {
MyBindingHandler bindingHandler = new MyBindingHandler();
binder.forField(nameField).withValidator(t -> !t.isEmpty(), "No empty values.").withValidationStatusHandler(bindingHandler).bind(Person::getFirstName, Person::setFirstName);
String ageError = "CONVERSIONERROR";
binder.forField(ageField).withConverter(new StringToIntegerConverter(ageError)).bind(Person::getAge, Person::setAge);
binder.setValidationStatusHandler(BinderValidationStatus::notifyBindingValidationStatusHandlers);
String initialName = item.getFirstName();
int initialAge = item.getAge();
binder.setBean(item);
// Test specific error handling.
bindingHandler.expectingError = true;
nameField.setValue("");
// Test default error handling.
ageField.setValue("foo");
assertThat("Error message is not what was expected", ageField.getErrorMessage(), containsString(ageError));
// Restore values and test no errors.
ageField.setValue(String.valueOf(initialAge));
assertFalse("The field should be valid", ageField.isInvalid());
bindingHandler.expectingError = false;
nameField.setValue(initialName);
// Assert that the handler was called.
assertEquals("Unexpected callCount to binding validation status handler", 6, bindingHandler.callCount);
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderTest method setReadOnly_binderLoadedByReadBean.
@Test
public void setReadOnly_binderLoadedByReadBean() {
binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.readBean(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.data.converter.StringToIntegerConverter 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.data.converter.StringToIntegerConverter 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());
}
Aggregations