Search in sources :

Example 46 with Person

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

the class BinderConverterValidatorTest method saveIfValid_beanValidationErrors.

@Test
public void saveIfValid_beanValidationErrors() {
    Binder<Person> binder = new Binder<>();
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    String msg = "foo";
    binder.withValidator(Validator.from(prsn -> prsn.getAddress() != null || prsn.getEmail() != null, msg));
    Person person = new Person();
    person.setFirstName("foo");
    nameField.setValue("");
    Assert.assertFalse(binder.writeBeanIfValid(person));
    Assert.assertEquals("foo", person.getFirstName());
}
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 47 with Person

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

the class BinderConverterValidatorTest method save_beanValidationErrors.

@Test(expected = ValidationException.class)
public void save_beanValidationErrors() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(person -> false, "b"));
    Person person = new Person();
    nameField.setValue("foo");
    try {
        binder.writeBean(person);
    } finally {
        // Bean should have been updated for item validation but reverted
        assertNull(person.getFirstName());
    }
}
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) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 48 with Person

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

the class BinderConverterValidatorTest method saveIfValid_noValidationErrors.

@Test
public void saveIfValid_noValidationErrors() {
    String msg = "foo";
    binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    person.setFirstName("foo");
    nameField.setValue("bar");
    Assert.assertTrue(binder.writeBeanIfValid(person));
    Assert.assertEquals("bar", person.getFirstName());
}
Also used : NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 49 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 50 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_genericField.

@Test
public void bindInstanceFields_genericField() {
    BindGenericField form = new BindGenericField();
    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)

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