Search in sources :

Example 11 with Person

use of com.vaadin.flow.tests.data.bean.Person 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 12 with Person

use of com.vaadin.flow.tests.data.bean.Person 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 13 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderInstanceFieldTest method bindInstanceFields_complexGenericHierarchy.

@Test
public void bindInstanceFields_complexGenericHierarchy() {
    BindComplextHierarchyGenericType form = new BindComplextHierarchyGenericType();
    Binder<Person> binder = new Binder<>(Person.class);
    binder.bindInstanceFields(form);
    Person person = new Person();
    person.setFirstName("foo");
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
    form.firstName.setValue("bar");
    Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 14 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderInstanceFieldTest method bindInstanceFields_bindNestedFieldUsingAnnotation.

@Test
public void bindInstanceFields_bindNestedFieldUsingAnnotation() {
    BindNestedFieldsUsingAnnotation form = new BindNestedFieldsUsingAnnotation();
    Binder<Person> binder = new Binder<>(Person.class, true);
    binder.bindInstanceFields(form);
    Person person = new Person();
    Address address = new Address();
    address.setStreetAddress("Foo st.");
    person.setAddress(address);
    binder.setBean(person);
    Assert.assertEquals("Reading nested properties bound using annotation", person.getAddress().getStreetAddress(), form.streetAddressField.getValue());
    form.streetAddressField.setValue("Bar ave.");
    Assert.assertEquals("Changing nested properties bound using annotation", form.streetAddressField.getValue(), person.getAddress().getStreetAddress());
}
Also used : Address(com.vaadin.flow.tests.data.bean.Address) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 15 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderInstanceFieldTest method bindInstanceFields_bindAllFields.

@Test
public void bindInstanceFields_bindAllFields() {
    BindAllFields form = new BindAllFields();
    Binder<Person> binder = new Binder<>(Person.class);
    binder.bindInstanceFields(form);
    Person person = new Person();
    person.setFirstName("foo");
    person.setBirthDate(LocalDate.now());
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
    Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
    form.firstName.setValue("bar");
    form.birthDate.setValue(person.getBirthDate().plusDays(345));
    Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
    Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

Person (com.vaadin.flow.tests.data.bean.Person)68 Test (org.junit.Test)63 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)33 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)26 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)24 Label (com.vaadin.flow.component.html.Label)21 Before (org.junit.Before)21 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)20 HasValue (com.vaadin.flow.component.HasValue)17 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)17 Assert (org.junit.Assert)17 Binding (com.vaadin.flow.data.binder.Binder.Binding)16 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Assert.assertEquals (org.junit.Assert.assertEquals)16 Assert.assertFalse (org.junit.Assert.assertFalse)16 Assert.assertNotNull (org.junit.Assert.assertNotNull)16 Assert.assertNull (org.junit.Assert.assertNull)16 Assert.assertTrue (org.junit.Assert.assertTrue)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15