Search in sources :

Example 36 with Person

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

the class BinderTest method removed_binding_not_updates_value.

@Test
public void removed_binding_not_updates_value() {
    Binding<Person, Integer> binding = binder.forField(ageField).withConverter(new StringToIntegerConverter("Can't convert")).bind(Person::getAge, Person::setAge);
    binder.setBean(item);
    String modifiedAge = String.valueOf(item.getAge() + 10);
    String ageBeforeUnbind = String.valueOf(item.getAge());
    binder.removeBinding(binding);
    ageField.setValue(modifiedAge);
    assertEquals("Binding still affects bean even after unbind", ageBeforeUnbind, String.valueOf(item.getAge()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 37 with Person

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

the class BinderTest method symetricValue_setBean_beanNotUpdated.

@Test
public void symetricValue_setBean_beanNotUpdated() {
    binder.bind(nameField, Person::getFirstName, Person::setFirstName);
    binder.setBean(new Person() {

        @Override
        public String getFirstName() {
            return "First";
        }

        @Override
        public void setFirstName(String firstName) {
            Assert.fail("Setter should not be called");
        }
    });
}
Also used : 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 38 with Person

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

the class BinderTest method conversionWithLocaleBasedErrorMessage.

@Test
public void conversionWithLocaleBasedErrorMessage() {
    TestTextField ageField = new TestTextField();
    String fiError = "VIRHE";
    String otherError = "ERROR";
    StringToIntegerConverter converter = new StringToIntegerConverter(context -> context.getLocale().map(Locale::getLanguage).orElse("en").equals("fi") ? fiError : otherError);
    binder.forField(ageField).withConverter(converter).bind(Person::getAge, Person::setAge);
    binder.setBean(item);
    UI testUI = new UI();
    UI.setCurrent(testUI);
    testUI.add(ageField);
    ageField.setValue("not a number");
    assertEquals(otherError, ageField.getErrorMessage());
    testUI.setLocale(new Locale("fi", "FI"));
    // Re-validate to get the error message with correct locale
    binder.validate();
    assertEquals(fiError, ageField.getErrorMessage());
}
Also used : Locale(java.util.Locale) UI(com.vaadin.flow.component.UI) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) 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 39 with Person

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

the class BinderTest method setBean_converterThrows_setBean_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void setBean_converterThrows_setBean_exceptionHandlerSet_bindingExceptionIsThrown() {
    TestTextField testField = new TestTextField();
    setExceptionHandler();
    binder.forField(testField).withConverter(Converter.<String, String>from(name -> {
        throw new NullPointerException();
    }, name -> name)).bind(Person::getFirstName, Person::setFirstName);
    binder.setBean(new Person());
}
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 40 with Person

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

the class BinderConverterValidatorTest method binderHasChanges.

@Test
public void binderHasChanges() throws ValidationException {
    binder.forField(nameField).withValidator(Validator.from(name -> !"".equals(name), "Name can't be empty")).bind(Person::getFirstName, Person::setFirstName);
    assertFalse(binder.hasChanges());
    binder.setBean(item);
    assertFalse(binder.hasChanges());
    // Bound binder + valid user changes: hasChanges == false
    nameField.setValue("foo");
    assertFalse(binder.hasChanges());
    nameField.setValue("bar");
    binder.writeBeanIfValid(new Person());
    assertFalse(binder.hasChanges());
    // Bound binder + invalid user changes: hasChanges() == true
    nameField.setValue("");
    binder.writeBeanIfValid(new Person());
    assertTrue(binder.hasChanges());
    // Read bean resets hasChanges
    binder.readBean(item);
    assertFalse(binder.hasChanges());
    // Removing a bound bean resets hasChanges
    nameField.setValue("");
    assertTrue(binder.hasChanges());
    binder.removeBean();
    assertFalse(binder.hasChanges());
    // Unbound binder + valid user changes: hasChanges() == true
    nameField.setValue("foo");
    assertTrue(binder.hasChanges());
    // successful writeBean resets hasChanges to false
    binder.writeBeanIfValid(new Person());
    assertFalse(binder.hasChanges());
    // Unbound binder + invalid user changes: hasChanges() == true
    nameField.setValue("");
    assertTrue(binder.hasChanges());
    // unsuccessful writeBean doesn't affect hasChanges
    nameField.setValue("");
    binder.writeBeanIfValid(new Person());
    assertTrue(binder.hasChanges());
}
Also used : 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