Search in sources :

Example 51 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced.

@Test
public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
    BindWithNoFieldInPerson form = new BindWithNoFieldInPerson();
    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");
    TestTextField ageField = new TestTextField();
    form.noFieldInPerson = ageField;
    binder.forField(form.noFieldInPerson).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    binder.bindInstanceFields(form);
    Person person = new Person();
    String personName = "foo";
    int age = 11;
    person.setFirstName(personName);
    person.setAge(age);
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
    Assert.assertEquals(String.valueOf(person.getAge()), form.noFieldInPerson.getValue());
    // the instances are not overridden
    Assert.assertEquals(name, form.firstName);
    Assert.assertEquals(ageField, form.noFieldInPerson);
    // Test correct age
    age += 56;
    form.noFieldInPerson.setValue(String.valueOf(age));
    Assert.assertEquals(form.noFieldInPerson.getValue(), String.valueOf(person.getAge()));
    // Test incorrect name
    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) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 52 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_bindOnlyOneFields.

@Test
public void bindInstanceFields_bindOnlyOneFields() {
    BindOnlyOneField form = new BindOnlyOneField();
    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());
    Assert.assertNull(form.noFieldInPerson);
    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 53 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_bindNotHasValueField_fieldIsNull.

@Test
public void bindInstanceFields_bindNotHasValueField_fieldIsNull() {
    BindFieldHasWrongType form = new BindFieldHasWrongType();
    Binder<Person> binder = new Binder<>(Person.class);
    binder.bindInstanceFields(form);
    Person person = new Person();
    person.setFirstName("foo");
    binder.setBean(person);
    Assert.assertNull(form.firstName);
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 54 with Person

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

the class BinderInstanceFieldTest method bindInstanceFields_bindAllFieldsUsingAnnotations.

@Test
public void bindInstanceFields_bindAllFieldsUsingAnnotations() {
    BindFieldsUsingAnnotation form = new BindFieldsUsingAnnotation();
    Binder<Person> binder = new Binder<>(Person.class);
    binder.bindInstanceFields(form);
    Person person = new Person();
    person.setFirstName("foo");
    person.setBirthDate(LocalDate.now());
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.nameField.getValue());
    Assert.assertEquals(person.getBirthDate(), form.birthDateField.getValue());
    form.nameField.setValue("bar");
    form.birthDateField.setValue(person.getBirthDate().plusDays(345));
    Assert.assertEquals(form.nameField.getValue(), person.getFirstName());
    Assert.assertEquals(form.birthDateField.getValue(), person.getBirthDate());
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 55 with Person

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

the class BinderTest method validationStatusHandler_onlyRunForChangedField.

@Test
public void validationStatusHandler_onlyRunForChangedField() {
    TestTextField firstNameField = new TestTextField();
    TestTextField lastNameField = new TestTextField();
    AtomicInteger invokes = new AtomicInteger();
    binder.forField(firstNameField).withValidator(new NotEmptyValidator<>("")).withValidationStatusHandler(validationStatus -> invokes.addAndGet(1)).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(lastNameField).withValidator(new NotEmptyValidator<>("")).bind(Person::getLastName, Person::setLastName);
    binder.setBean(item);
    // setting the bean causes 2:
    Assert.assertEquals(2, invokes.get());
    lastNameField.setValue("");
    Assert.assertEquals(2, invokes.get());
    firstNameField.setValue("");
    Assert.assertEquals(3, invokes.get());
    binder.removeBean();
    Person person = new Person();
    person.setFirstName("a");
    person.setLastName("a");
    binder.readBean(person);
    // reading from a bean causes 2:
    Assert.assertEquals(5, invokes.get());
    lastNameField.setValue("");
    Assert.assertEquals(5, invokes.get());
    firstNameField.setValue("");
    Assert.assertEquals(6, invokes.get());
}
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) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) 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