Search in sources :

Example 1 with Person

use of com.vaadin.flow.tests.data.bean.Person 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 2 with Person

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

the class BinderConverterValidatorTest method save_beanValidationErrorsWithConverter.

@Test(expected = ValidationException.class)
public void save_beanValidationErrorsWithConverter() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    binder.forField(ageField).withConverter(new StringToIntegerConverter("Can't convert")).bind(Person::getAge, Person::setAge);
    binder.withValidator(Validator.from(person -> false, "b"));
    Person person = new Person();
    ageField.setValue("1");
    try {
        binder.writeBean(person);
    } finally {
        // Bean should have been updated for item validation but reverted
        Assert.assertEquals(0, person.getAge());
    }
}
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) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 3 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 4 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) 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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 5 with Person

use of com.vaadin.flow.tests.data.bean.Person 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)

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