Search in sources :

Example 76 with Person

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

the class BinderTest method setReadOnly_boundBinder.

@Test
public void setReadOnly_boundBinder() {
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    binder.setBean(new Person());
    binder.setReadOnly(true);
    assertTrue(nameField.isReadOnly());
    assertTrue(ageField.isReadOnly());
    binder.setReadOnly(false);
    assertFalse(nameField.isReadOnly());
    assertFalse(ageField.isReadOnly());
}
Also used : StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 77 with Person

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

the class BinderConverterValidatorTest method bound_validatorsFail_errors.

@Test
public void bound_validatorsFail_errors() {
    BindingBuilder<Person, String> binding = binder.forField(nameField);
    binding.withValidator(Validator.alwaysPass());
    String errorMessage = "foo";
    binding.withValidator((String value, ValueContext context) -> ValidationResult.error(errorMessage));
    binding.withValidator(value -> false, "bar");
    binding.bind(Person::getFirstName, Person::setFirstName);
    BinderValidationStatus<Person> status = binder.validate();
    List<BindingValidationStatus<?>> errors = status.getFieldValidationErrors();
    assertEquals(1, errors.size());
    BindingValidationStatus<?> validationStatus = errors.stream().findFirst().get();
    assertEquals(errorMessage, validationStatus.getMessage().get());
    HasValue<?, ?> field = validationStatus.getField();
    assertEquals(nameField, field);
    assertEquals(errorMessage, componentErrors.get(nameField));
    assertInvalidField(errorMessage, nameField);
}
Also used : Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 78 with Person

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

the class BinderConverterValidatorTest method validate_failedBeanValidatorWithFieldValidator.

@Test
public void validate_failedBeanValidatorWithFieldValidator() {
    String msg = "foo";
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg));
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(bean -> false, msg));
    Person person = new Person();
    binder.setBean(person);
    List<BindingValidationStatus<?>> errors = binder.validate().getFieldValidationErrors();
    assertEquals(1, errors.size());
    BindingValidationStatus<?> error = errors.get(0);
    assertEquals(msg, error.getMessage().get());
    assertEquals(nameField, error.getField());
    assertInvalidField(msg, 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 79 with Person

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

the class BinderConverterValidatorTest method save_validationErrors_exceptionContainsErrors.

@Test
public void save_validationErrors_exceptionContainsErrors() throws ValidationException {
    String msg = "foo";
    BindingBuilder<Person, String> nameBinding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg));
    nameBinding.bind(Person::getFirstName, Person::setFirstName);
    BindingBuilder<Person, Integer> ageBinding = binder.forField(ageField).withConverter(stringToInteger).withValidator(notNegative);
    ageBinding.bind(Person::getAge, Person::setAge);
    Person person = new Person();
    nameField.setValue("");
    ageField.setValue("-1");
    try {
        binder.writeBean(person);
        Assert.fail();
    } catch (ValidationException exception) {
        List<BindingValidationStatus<?>> validationErrors = exception.getFieldValidationErrors();
        Assert.assertEquals(2, validationErrors.size());
        BindingValidationStatus<?> error = validationErrors.get(0);
        Assert.assertEquals(nameField, error.getField());
        Assert.assertEquals(msg, error.getMessage().get());
        error = validationErrors.get(1);
        Assert.assertEquals(ageField, error.getField());
        Assert.assertEquals(NEGATIVE_ERROR_MESSAGE, error.getMessage().get());
    }
}
Also used : List(java.util.List) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 80 with Person

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

the class BinderConverterValidatorTest method save_fieldsAndBeanLevelValidation.

@Test
public void save_fieldsAndBeanLevelValidation() throws ValidationException {
    binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(person -> person.getLastName() != null, "b"));
    Person person = new Person();
    person.setLastName("bar");
    nameField.setValue("foo");
    binder.writeBean(person);
    Assert.assertEquals(nameField.getValue(), person.getFirstName());
    Assert.assertEquals("bar", person.getLastName());
}
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) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) 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