Search in sources :

Example 31 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_bindDeepNestedFieldsUsingAnnotation.

@Test
public void bindInstanceFields_bindDeepNestedFieldsUsingAnnotation() {
    BindDeepNestedFieldsUsingAnnotation form = new BindDeepNestedFieldsUsingAnnotation();
    Binder<Couple> binder = new Binder<>(Couple.class, true);
    binder.bindInstanceFields(form);
    Person first = new Person();
    Person second = new Person();
    Address firstAddress = new Address();
    firstAddress.setStreetAddress("Foo st.");
    first.setAddress(firstAddress);
    Address secondAddress = new Address();
    second.setAddress(secondAddress);
    secondAddress.setStreetAddress("Bar ave.");
    Couple couple = new Couple();
    couple.setFirst(first);
    couple.setSecond(second);
    binder.setBean(couple);
    Assert.assertEquals("Binding deep nested properties using annotation", couple.first.getAddress().getStreetAddress(), form.firstStreetField.getValue());
    Assert.assertEquals("Binding parallel deep nested properties using annotation", couple.second.getAddress().getStreetAddress(), form.secondStreetField.getValue());
    form.firstStreetField.setValue(second.getAddress().getStreetAddress());
    Assert.assertEquals("Updating value in deep nested properties", form.firstStreetField.getValue(), first.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 32 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 33 with Person

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

the class BinderTest method bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() {
    TestTextField testField = new TestTextField();
    setExceptionHandler();
    binder.forField(testField).withConverter(Converter.<String, String>from(name -> Result.ok(name), name -> {
        throw new NullPointerException();
    })).bind(Person::getFirstName, Person::setFirstName).read(new Person());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) HasValue(com.vaadin.flow.component.HasValue) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) StringToDoubleConverter(com.vaadin.flow.data.converter.StringToDoubleConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) Assert.assertSame(org.junit.Assert.assertSame) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) After(org.junit.After) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) UI(com.vaadin.flow.component.UI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) DecimalFormat(java.text.DecimalFormat) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StringToBigDecimalConverter(com.vaadin.flow.data.converter.StringToBigDecimalConverter) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Serializable(java.io.Serializable) Objects(java.util.Objects) Converter(com.vaadin.flow.data.converter.Converter) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Assert(org.junit.Assert) Binding(com.vaadin.flow.data.binder.Binder.Binding) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Sex(com.vaadin.flow.tests.data.bean.Sex) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 34 with Person

use of com.vaadin.flow.tests.data.bean.Person 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);
}
Also used : StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) 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 35 with Person

use of com.vaadin.flow.tests.data.bean.Person 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());
}
Also used : StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

Person (com.vaadin.flow.tests.data.bean.Person)97 Test (org.junit.Test)92 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)51 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)49 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)34 Matchers.containsString (org.hamcrest.Matchers.containsString)31 Before (org.junit.Before)30 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 HasValue (com.vaadin.flow.component.HasValue)26 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)26 Assert (org.junit.Assert)26 Binding (com.vaadin.flow.data.binder.Binder.Binding)25 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)25 Serializable (java.io.Serializable)25 HashMap (java.util.HashMap)25 Map (java.util.Map)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 Assert.assertFalse (org.junit.Assert.assertFalse)25 Assert.assertNotNull (org.junit.Assert.assertNotNull)25