Search in sources :

Example 81 with Person

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

the class BinderConverterValidatorTest method validate_failedBeanValidatorWithoutFieldValidators.

@Test
public void validate_failedBeanValidatorWithoutFieldValidators() {
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    String msg = "foo";
    binder.withValidator(Validator.from(bean -> false, msg));
    Person person = new Person();
    binder.setBean(person);
    List<BindingValidationStatus<?>> errors = binder.validate().getFieldValidationErrors();
    assertEquals(0, errors.size());
    assertValidField(nameField);
}
Also used : 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) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) Matchers.hasSize(org.hamcrest.Matchers.hasSize) 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) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) 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) TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) 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 82 with Person

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

the class BinderConverterValidatorTest method saveIfValid_fieldValidationErrors.

@Test
public void saveIfValid_fieldValidationErrors() {
    String msg = "foo";
    binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    person.setFirstName("foo");
    nameField.setValue("");
    Assert.assertFalse(binder.writeBeanIfValid(person));
    Assert.assertEquals("foo", 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 83 with Person

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

the class BinderConverterValidatorTest method save_fieldValidationErrors.

@Test(expected = ValidationException.class)
public void save_fieldValidationErrors() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    String msg = "foo";
    binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    String firstName = "foo";
    person.setFirstName(firstName);
    nameField.setValue("");
    try {
        binder.writeBean(person);
    } finally {
        // Bean should not have been updated
        Assert.assertEquals(firstName, 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 84 with Person

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

the class BinderConverterValidatorTest method setUp.

@Before
public void setUp() {
    CurrentInstance.clearAll();
    binder = new Binder<Person>() {

        @Override
        protected void handleError(HasValue<?, ?> field, ValidationResult result) {
            super.handleError(field, result);
            componentErrors.put(field, result.getErrorMessage());
        }

        @Override
        protected void clearError(HasValue<?, ?> field) {
            super.clearError(field);
            componentErrors.remove(field);
        }
    };
    item = new Person();
    item.setFirstName("Johannes");
    item.setAge(32);
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Before(org.junit.Before)

Example 85 with Person

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

the class BinderConverterValidatorTest method save_null_beanIsUpdated.

@Test
public void save_null_beanIsUpdated() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    binder.forField(nameField).withConverter(fieldValue -> {
        if ("null".equals(fieldValue)) {
            return null;
        } else {
            return fieldValue;
        }
    }, model -> {
        return model;
    }).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    person.setFirstName("foo");
    nameField.setValue("null");
    binder.writeBean(person);
    assertNull(person.getFirstName());
}
Also used : 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) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) Matchers.hasSize(org.hamcrest.Matchers.hasSize) 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) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) 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) TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) 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) 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