Search in sources :

Example 11 with TestTextField

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"));
}
Also used : HashMap(java.util.HashMap) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 12 with TestTextField

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());
}
Also used : StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 13 with TestTextField

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());
}
Also used : HasValue(com.vaadin.flow.component.HasValue) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test) Address(com.vaadin.flow.tests.data.bean.Address) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Tag(com.vaadin.flow.component.Tag) LocalDate(java.time.LocalDate) TestFormLayout(com.vaadin.flow.data.binder.testcomponents.TestFormLayout) Optional(java.util.Optional) Assert(org.junit.Assert) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) TestDatePicker(com.vaadin.flow.data.binder.testcomponents.TestDatePicker) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 14 with TestTextField

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());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 15 with TestTextField

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());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Test(org.junit.Test)

Aggregations

TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)28 Test (org.junit.Test)26 Person (com.vaadin.flow.tests.data.bean.Person)13 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)7 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)6 Before (org.junit.Before)5 HasValue (com.vaadin.flow.component.HasValue)4 SubConstraint (com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubConstraint)4 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Assert (org.junit.Assert)4 Label (com.vaadin.flow.component.html.Label)3 SubSubConstraint (com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubSubConstraint)3 Binding (com.vaadin.flow.data.binder.Binder.Binding)3 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)3 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3