Search in sources :

Example 21 with Person

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

the class BinderTest method beanBinder_nullRepresentationIsNotDisabled.

@Test
public void beanBinder_nullRepresentationIsNotDisabled() {
    Binder<Person> binder = new Binder<>(Person.class);
    binder.forField(nameField).bind("firstName");
    Person person = new Person();
    binder.setBean(person);
    Assert.assertEquals("", nameField.getValue());
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 22 with Person

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

the class BinderTest method readNullBeanRemovesError.

@Test
public void readNullBeanRemovesError() {
    TestTextField textField = new TestTextField();
    binder.forField(textField).asRequired("foobar").bind(Person::getFirstName, Person::setFirstName);
    Assert.assertTrue(textField.isRequiredIndicatorVisible());
    Assert.assertNull(componentErrors.get(textField));
    binder.readBean(item);
    Assert.assertNull(componentErrors.get(textField));
    textField.setValue(textField.getEmptyValue());
    Assert.assertTrue(textField.isRequiredIndicatorVisible());
    Assert.assertNotNull(componentErrors.get(textField));
    binder.readBean(null);
    assertTrue(textField.isRequiredIndicatorVisible());
    Assert.assertNull(componentErrors.get(textField));
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 23 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)

Example 24 with Person

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

the class BinderTest method multiple_calls_to_same_binding_builder.

@Test
public void multiple_calls_to_same_binding_builder() {
    String stringLength = "String length failure";
    String conversion = "Conversion failed";
    String ageLimit = "Age not in valid range";
    BindingValidationStatus validation;
    binder = new Binder<>(Person.class);
    BindingBuilder builder = binder.forField(ageField);
    builder.withValidator(new StringLengthValidator(stringLength, 0, 3));
    builder.withConverter(new StringToIntegerConverter(conversion));
    builder.withValidator(new IntegerRangeValidator(ageLimit, 3, 150));
    Binding<Person, ?> bind = builder.bind("age");
    binder.setBean(item);
    ageField.setValue("123123");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(stringLength, validation.getMessage().get());
    ageField.setValue("age");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(conversion, validation.getMessage().get());
    ageField.setValue("256");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(ageLimit, validation.getMessage().get());
    ageField.setValue("30");
    validation = bind.validate();
    Assert.assertFalse(validation.isError());
    Assert.assertEquals(30, item.getAge());
}
Also used : IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) 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 25 with Person

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

the class BinderTest method save_unbound_noChanges.

@Test
public void save_unbound_noChanges() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    Person person = new Person();
    int age = 10;
    person.setAge(age);
    binder.writeBean(person);
    Assert.assertEquals(age, person.getAge());
}
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