use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderTest method multiple_calls_to_same_binding_builder.
@Test
public void multiple_calls_to_same_binding_builder() {
String stringLength = "String length failure";
String conversion = "Conversion failed";
String ageLimit = "Age not in valid range";
BindingValidationStatus validation;
binder = new Binder<>(Person.class);
BindingBuilder builder = binder.forField(ageField);
builder.withValidator(new StringLengthValidator(stringLength, 0, 3));
builder.withConverter(new StringToIntegerConverter(conversion));
builder.withValidator(new IntegerRangeValidator(ageLimit, 3, 150));
Binding<Person, ?> bind = builder.bind("age");
binder.setBean(item);
ageField.setValue("123123");
validation = bind.validate();
Assert.assertTrue(validation.isError());
Assert.assertEquals(stringLength, validation.getMessage().get());
ageField.setValue("age");
validation = bind.validate();
Assert.assertTrue(validation.isError());
Assert.assertEquals(conversion, validation.getMessage().get());
ageField.setValue("256");
validation = bind.validate();
Assert.assertTrue(validation.isError());
Assert.assertEquals(ageLimit, validation.getMessage().get());
ageField.setValue("30");
validation = bind.validate();
Assert.assertFalse(validation.isError());
Assert.assertEquals(30, item.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 BeanBinderTest method bindInstanceFields_does_not_automatically_bind_incomplete_forField_bindings.
@Test(expected = IllegalStateException.class)
public void bindInstanceFields_does_not_automatically_bind_incomplete_forField_bindings() {
Binder<TestBean> otherBinder = new Binder<>(TestBean.class);
TestClass testClass = new TestClass();
otherBinder.forField(testClass.number).withConverter(new StringToIntegerConverter(""));
// Should throw an IllegalStateException since the binding for number is
// not completed with bind
otherBinder.bindInstanceFields(testClass);
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced.
@Test
public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
BindWithNoFieldInPerson form = new BindWithNoFieldInPerson();
Binder<Person> binder = new Binder<>(Person.class);
TestTextField name = new TestTextField();
form.firstName = name;
binder.forField(form.firstName).withValidator(new StringLengthValidator("Name is invalid", 3, 10)).bind("firstName");
TestTextField ageField = new TestTextField();
form.noFieldInPerson = ageField;
binder.forField(form.noFieldInPerson).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
binder.bindInstanceFields(form);
Person person = new Person();
String personName = "foo";
int age = 11;
person.setFirstName(personName);
person.setAge(age);
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(String.valueOf(person.getAge()), form.noFieldInPerson.getValue());
// the instances are not overridden
Assert.assertEquals(name, form.firstName);
Assert.assertEquals(ageField, form.noFieldInPerson);
// Test correct age
age += 56;
form.noFieldInPerson.setValue(String.valueOf(age));
Assert.assertEquals(form.noFieldInPerson.getValue(), String.valueOf(person.getAge()));
// Test incorrect name
form.firstName.setValue("aa");
Assert.assertEquals(personName, person.getFirstName());
Assert.assertFalse(binder.validate().isOk());
}
use of com.vaadin.flow.data.converter.StringToIntegerConverter 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());
}
Aggregations