Search in sources :

Example 21 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField 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 22 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField 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 23 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField 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 : HasValue(com.vaadin.flow.component.HasValue) Person(com.vaadin.flow.tests.data.bean.Person) HashMap(java.util.HashMap) Assert.assertSame(org.junit.Assert.assertSame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) 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) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Objects(java.util.Objects) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) 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)

Example 24 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.

the class BinderTest method setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator.

@Test
public void setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator() {
    TestTextField textField = new TestTextField();
    assertFalse(textField.isRequiredIndicatorVisible());
    BindingBuilder<Person, String> binding = binder.forField(textField);
    assertFalse(textField.isRequiredIndicatorVisible());
    AtomicInteger invokes = new AtomicInteger();
    binding.asRequired(context -> {
        invokes.incrementAndGet();
        return "foobar";
    });
    assertTrue(textField.isRequiredIndicatorVisible());
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.setBean(item);
    assertThat(textField.getErrorMessage(), isEmptyString());
    Assert.assertEquals(0, invokes.get());
    textField.setValue(textField.getEmptyValue());
    Assert.assertEquals("foobar", componentErrors.get(textField));
    // validation is done for all changed bindings once.
    Assert.assertEquals(1, invokes.get());
    textField.setValue("value");
    assertFalse(textField.isInvalid());
    assertTrue(textField.isRequiredIndicatorVisible());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 25 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.

the class BinderTest method beanvalidation_validate_throws_with_readBean.

@Test(expected = IllegalStateException.class)
public void beanvalidation_validate_throws_with_readBean() {
    TestTextField lastNameField = new TestTextField();
    setBeanValidationFirstNameNotEqualsLastName(nameField, lastNameField);
    binder.readBean(item);
    Assert.assertTrue(binder.validate().isOk());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Test(org.junit.Test)

Aggregations

TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)28 Test (org.junit.Test)26 Person (com.vaadin.flow.tests.data.bean.Person)13 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)7 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)6 Before (org.junit.Before)5 HasValue (com.vaadin.flow.component.HasValue)4 SubConstraint (com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubConstraint)4 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Assert (org.junit.Assert)4 Label (com.vaadin.flow.component.html.Label)3 SubSubConstraint (com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubSubConstraint)3 Binding (com.vaadin.flow.data.binder.Binder.Binding)3 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)3 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3