Search in sources :

Example 86 with Person

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

the class BinderConverterValidatorTest method validate_okBeanValidatorWithoutFieldValidators.

@Test
public void validate_okBeanValidatorWithoutFieldValidators() {
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    String msg = "foo";
    binder.withValidator(Validator.from(bean -> true, msg));
    Person person = new Person();
    binder.setBean(person);
    assertFalse(binder.validate().hasErrors());
    assertTrue(binder.validate().isOk());
    assertValidField(nameField);
}
Also used : 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) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) Matchers.hasSize(org.hamcrest.Matchers.hasSize) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Serializable(java.io.Serializable) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) Assert(org.junit.Assert) Binding(com.vaadin.flow.data.binder.Binder.Binding) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Assert.assertEquals(org.junit.Assert.assertEquals) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 87 with Person

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

the class BinderConverterValidatorTest method binder_saveIfValid.

@Test
public void binder_saveIfValid() {
    String msg1 = "foo";
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg1));
    binding.bind(Person::getFirstName, Person::setFirstName);
    String beanValidatorErrorMessage = "bar";
    binder.withValidator(Validator.from(bean -> false, beanValidatorErrorMessage));
    Person person = new Person();
    String firstName = "first name";
    person.setFirstName(firstName);
    binder.readBean(person);
    nameField.setValue("");
    assertFalse(binder.writeBeanIfValid(person));
    // check that field level-validation failed and bean is not updated
    assertEquals(firstName, person.getFirstName());
    assertInvalidField(msg1, nameField);
    nameField.setValue("new name");
    assertFalse(binder.writeBeanIfValid(person));
    // Bean is updated but reverted
    assertEquals(firstName, person.getFirstName());
    assertValidField(nameField);
}
Also used : 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) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) Matchers.hasSize(org.hamcrest.Matchers.hasSize) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Serializable(java.io.Serializable) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) Assert(org.junit.Assert) Binding(com.vaadin.flow.data.binder.Binder.Binding) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Assert.assertEquals(org.junit.Assert.assertEquals) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 88 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_tentativelyBoundFieldAndNotBoundField.

@Test
public void bindInstanceFields_tentativelyBoundFieldAndNotBoundField() {
    BindOnlyOneField form = new BindOnlyOneField();
    Binder<Person> binder = new Binder<>(Person.class);
    TestTextField field = new TestTextField();
    form.firstName = field;
    // This is an incomplete binding which is supposed to be configured
    // manually
    binder.forMemberField(field);
    // bindInstanceFields will not complain even though it can't bind
    // anything as there is a binding in progress (an exception will be
    // thrown later if the binding is not completed)
    binder.bindInstanceFields(form);
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 89 with Person

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

the class BinderTest method readBean_converterThrows_readBean_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void readBean_converterThrows_readBean_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);
    binder.readBean(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 90 with Person

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

the class BinderTest method valueChangeListenerOrder.

@Test
public void valueChangeListenerOrder() {
    AtomicBoolean beanSet = new AtomicBoolean();
    nameField.addValueChangeListener(e -> {
        if (!beanSet.get()) {
            assertEquals("Value in bean updated earlier than expected", e.getOldValue(), item.getFirstName());
        }
    });
    binder.bind(nameField, Person::getFirstName, Person::setFirstName);
    nameField.addValueChangeListener(e -> {
        if (!beanSet.get()) {
            assertEquals("Value in bean not updated when expected", e.getValue(), item.getFirstName());
        }
    });
    beanSet.set(true);
    binder.setBean(item);
    beanSet.set(false);
    nameField.setValue("Foo");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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