use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderCustomPropertySetTest method testBindByString.
@Test
public void testBindByString() {
TestTextField field = new TestTextField();
Map<String, String> map = new HashMap<>();
Binder<Map<String, String>> binder = Binder.withPropertySet(new MapPropertySet());
binder.bind(field, "key");
binder.setBean(map);
field.setValue("value");
Assert.assertEquals("Field value should propagate to the corresponding key in the map", "value", map.get("key"));
}
use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced.
@Test
public void bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced() {
BindAllFields form = new BindAllFields();
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");
binder.bindInstanceFields(form);
Person person = new Person();
String personName = "foo";
person.setFirstName(personName);
person.setBirthDate(LocalDate.now());
binder.setBean(person);
Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
// the instance is not overridden
Assert.assertEquals(name, form.firstName);
// Test automatic binding
form.birthDate.setValue(person.getBirthDate().plusDays(345));
Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
// Test custom binding
form.firstName.setValue("aa");
Assert.assertEquals(personName, person.getFirstName());
Assert.assertFalse(binder.validate().isOk());
}
use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderInstanceFieldTest method bindInstanceFields_preconfiguredFieldNotBoundToPropertyPreserved.
@Test
public void bindInstanceFields_preconfiguredFieldNotBoundToPropertyPreserved() {
BindOneFieldRequiresConverter form = new BindOneFieldRequiresConverter();
form.age = new TestTextField();
form.firstName = new TestTextField();
Binder<Person> binder = new Binder<>(Person.class);
binder.forField(form.age).withConverter(str -> Integer.parseInt(str) / 2, integer -> Integer.toString(integer * 2)).bind(Person::getAge, Person::setAge);
binder.bindInstanceFields(form);
Person person = new Person();
person.setFirstName("first");
person.setAge(45);
binder.setBean(person);
Assert.assertEquals("90", form.age.getValue());
}
use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderTest method setRequired_withErrorMessage_fieldGetsRequiredIndicatorAndValidator.
@Test
public void setRequired_withErrorMessage_fieldGetsRequiredIndicatorAndValidator() {
TestTextField textField = new TestTextField();
assertFalse(textField.isRequiredIndicatorVisible());
BindingBuilder<Person, String> binding = binder.forField(textField);
assertFalse(textField.isRequiredIndicatorVisible());
binding.asRequired("foobar");
assertTrue(textField.isRequiredIndicatorVisible());
binding.bind(Person::getFirstName, Person::setFirstName);
binder.setBean(item);
assertThat(textField.getErrorMessage(), isEmptyString());
textField.setValue(textField.getEmptyValue());
Assert.assertEquals("foobar", componentErrors.get(textField));
textField.setValue("value");
assertFalse(textField.isInvalid());
assertTrue(textField.isRequiredIndicatorVisible());
}
use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.
the class BinderTest method beanvalidation_initially_broken_bean.
@Test
public void beanvalidation_initially_broken_bean() {
TestTextField lastNameField = new TestTextField();
setBeanValidationFirstNameNotEqualsLastName(nameField, lastNameField);
item.setLastName(item.getFirstName());
binder.setBean(item);
Assert.assertFalse(binder.isValid());
Assert.assertFalse(binder.validate().isOk());
}
Aggregations