Search in sources :

Example 6 with TestTextField

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

the class BeanBinderTest method subsubfield_name_fieldIsRequired.

@Test
public void subsubfield_name_fieldIsRequired() {
    BeanValidationBinder<RequiredConstraints> binder = new BeanValidationBinder<>(RequiredConstraints.class);
    RequiredConstraints bean = new RequiredConstraints();
    RequiredConstraints.SubConstraint subfield = new RequiredConstraints.SubConstraint();
    subfield.setSubsub(new SubSubConstraint());
    bean.setSubfield(subfield);
    TestTextField field = new TestTextField();
    binder.bind(field, "subfield.subsub.value");
    binder.setBean(bean);
    Assert.assertTrue(field.isRequiredIndicatorVisible());
    testSerialization(binder);
}
Also used : SubConstraint(com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubConstraint) SubSubConstraint(com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubSubConstraint) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) SubSubConstraint(com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubSubConstraint) SubConstraint(com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubConstraint) Test(org.junit.Test)

Example 7 with TestTextField

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

the class BinderConverterValidatorTest method binderBindAndLoad_clearsErrors.

@Test
public void binderBindAndLoad_clearsErrors() {
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(bean -> !bean.getFirstName().contains("error"), "error");
    Person person = new Person();
    person.setFirstName("");
    binder.setBean(person);
    // initial value is invalid but no error
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // make error show
    nameField.setValue("foo");
    nameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    // bind to another person to see that error is cleared
    person = new Person();
    person.setFirstName("");
    binder.setBean(person);
    // error has been cleared
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // make show error
    nameField.setValue("foo");
    nameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    // load should also clear error
    binder.readBean(person);
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // bind a new field that has invalid value in bean
    TestTextField lastNameField = new TestTextField();
    // The test starts with a valid value as the last name of the person,
    // since the binder assumes any non-changed values to be valid.
    person.setLastName("bar");
    BindingBuilder<Person, String> binding2 = binder.forField(lastNameField).withValidator(notEmpty);
    binding2.bind(Person::getLastName, Person::setLastName);
    // should not have error shown when initialized
    assertThat(lastNameField.getErrorMessage(), isEmptyString());
    // Set a value that breaks the validation
    lastNameField.setValue("");
    assertNotNull(lastNameField.getErrorMessage());
    // add status label to show bean level error
    Label statusLabel = new Label();
    binder.setStatusLabel(statusLabel);
    nameField.setValue("error");
    // no error shown yet because second field validation doesn't pass
    Assert.assertEquals("", statusLabel.getText());
    // make second field validation pass to get bean validation error
    lastNameField.setValue("foo");
    Assert.assertEquals("error", statusLabel.getText());
    // reload bean to clear error
    binder.readBean(person);
    Assert.assertEquals("", statusLabel.getText());
    // reset() should clear all errors and status label
    nameField.setValue("");
    lastNameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    Assert.assertTrue(componentErrors.containsKey(lastNameField));
    binder.removeBean();
    Assert.assertFalse(componentErrors.containsKey(nameField));
    Assert.assertFalse(componentErrors.containsKey(lastNameField));
    Assert.assertEquals("", statusLabel.getText());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Label(com.vaadin.flow.component.html.Label) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 8 with TestTextField

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

the class BinderConverterValidatorTest method convertInvalidDataToField.

@Test(expected = IllegalArgumentException.class)
public void convertInvalidDataToField() {
    TestTextField field = new TestTextField();
    StatusBean bean = new StatusBean();
    bean.setStatus("1");
    Binder<StatusBean> binder = new Binder<>();
    BindingBuilder<StatusBean, String> binding = binder.forField(field).withConverter(presentation -> {
        if (presentation.equals("OK")) {
            return "1";
        } else if (presentation.equals("NOTOK")) {
            return "2";
        }
        throw new IllegalArgumentException("Value must be OK or NOTOK");
    }, model -> {
        if (model.equals("1")) {
            return "OK";
        } else if (model.equals("2")) {
            return "NOTOK";
        } else {
            throw new IllegalArgumentException("Value in model must be 1 or 2");
        }
    });
    binding.bind(StatusBean::getStatus, StatusBean::setStatus);
    binder.setBean(bean);
    bean.setStatus("3");
    binder.readBean(bean);
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test)

Example 9 with TestTextField

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

the class BinderConverterValidatorTest method binderLoad_withCrossFieldValidation_clearsErrors.

@Test
public void binderLoad_withCrossFieldValidation_clearsErrors() {
    TestTextField lastNameField = new TestTextField();
    final SerializablePredicate<String> lengthPredicate = v -> v.length() > 2;
    BindingBuilder<Person, String> firstNameBinding = binder.forField(nameField).withValidator(lengthPredicate, "length");
    firstNameBinding.bind(Person::getFirstName, Person::setFirstName);
    Binding<Person, String> lastNameBinding = binder.forField(lastNameField).withValidator(v -> !nameField.getValue().isEmpty() || lengthPredicate.test(v), "err").withValidator(lengthPredicate, "length").bind(Person::getLastName, Person::setLastName);
    // this will be triggered as a new bean is bound with binder.bind(),
    // causing a validation error to be visible until reset is done
    nameField.addValueChangeListener(v -> lastNameBinding.validate());
    Person person = new Person();
    binder.setBean(person);
    Assert.assertFalse(componentErrors.containsKey(nameField));
    Assert.assertFalse(componentErrors.containsKey(lastNameField));
    nameField.setValue("x");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    Assert.assertTrue(componentErrors.containsKey(lastNameField));
    binder.setBean(person);
    Assert.assertFalse(componentErrors.containsKey(nameField));
    Assert.assertFalse(componentErrors.containsKey(lastNameField));
}
Also used : HasValue(com.vaadin.flow.component.HasValue) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Label(com.vaadin.flow.component.html.Label) HashMap(java.util.HashMap) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) 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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test) 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) 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) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 10 with TestTextField

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

the class BinderConverterValidatorTest method validatorForSuperTypeCanBeUsed.

@Test
public void validatorForSuperTypeCanBeUsed() {
    // Validates that a validator for a super type can be used, e.g.
    // validator for Number can be used on a Double
    TestTextField salaryField = new TestTextField();
    Validator<Number> positiveNumberValidator = (value, context) -> {
        if (value.doubleValue() >= 0) {
            return ValidationResult.ok();
        } else {
            return ValidationResult.error(NEGATIVE_ERROR_MESSAGE);
        }
    };
    binder.forField(salaryField).withConverter(Double::valueOf, String::valueOf).withValidator(positiveNumberValidator).bind(Person::getSalaryDouble, Person::setSalaryDouble);
    Person person = new Person();
    binder.setBean(person);
    salaryField.setValue("10");
    assertEquals(10, person.getSalaryDouble(), 0);
    // Does not pass validator
    salaryField.setValue("-1");
    assertEquals(10, person.getSalaryDouble(), 0);
    assertInvalidField(NEGATIVE_ERROR_MESSAGE, salaryField);
}
Also used : HasValue(com.vaadin.flow.component.HasValue) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Label(com.vaadin.flow.component.html.Label) HashMap(java.util.HashMap) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) 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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test) 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) 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) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) 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