Search in sources :

Example 6 with Person

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

the class BinderConverterValidatorTest method binderHasChanges.

@Test
public void binderHasChanges() throws ValidationException {
    binder.forField(nameField).withValidator(Validator.from(name -> !"".equals(name), "Name can't be empty")).bind(Person::getFirstName, Person::setFirstName);
    assertFalse(binder.hasChanges());
    binder.setBean(item);
    assertFalse(binder.hasChanges());
    // Bound binder + valid user changes: hasChanges == false
    nameField.setValue("foo");
    assertFalse(binder.hasChanges());
    nameField.setValue("bar");
    binder.writeBeanIfValid(new Person());
    assertFalse(binder.hasChanges());
    // Bound binder + invalid user changes: hasChanges() == true
    nameField.setValue("");
    binder.writeBeanIfValid(new Person());
    assertTrue(binder.hasChanges());
    // Read bean resets hasChanges
    binder.readBean(item);
    assertFalse(binder.hasChanges());
    // Removing a bound bean resets hasChanges
    nameField.setValue("");
    assertTrue(binder.hasChanges());
    binder.removeBean();
    assertFalse(binder.hasChanges());
    // Unbound binder + valid user changes: hasChanges() == true
    nameField.setValue("foo");
    assertTrue(binder.hasChanges());
    // successful writeBean resets hasChanges to false
    binder.writeBeanIfValid(new Person());
    assertFalse(binder.hasChanges());
    // Unbound binder + invalid user changes: hasChanges() == true
    nameField.setValue("");
    assertTrue(binder.hasChanges());
    // unsuccessful writeBean doesn't affect hasChanges
    nameField.setValue("");
    binder.writeBeanIfValid(new Person());
    assertTrue(binder.hasChanges());
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 7 with Person

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

Example 8 with Person

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

the class BinderConverterValidatorTest method save_beanValidationErrors.

@Test(expected = ValidationException.class)
public void save_beanValidationErrors() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(person -> false, "b"));
    Person person = new Person();
    nameField.setValue("foo");
    try {
        binder.writeBean(person);
    } finally {
        // Bean should have been updated for item validation but reverted
        assertNull(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) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 9 with Person

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

the class BinderConverterValidatorTest method validate_failedBothBeanValidatorAndFieldValidator.

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

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

the class BinderInstanceFieldTest method bindInstanceFields_bindNotHasValueField_fieldIsNotReplaced.

@Test
public void bindInstanceFields_bindNotHasValueField_fieldIsNotReplaced() {
    BindFieldHasWrongType form = new BindFieldHasWrongType();
    Binder<Person> binder = new Binder<>(Person.class);
    String name = "foo";
    form.firstName = name;
    Person person = new Person();
    person.setFirstName("foo");
    binder.setBean(person);
    Assert.assertEquals(name, form.firstName);
}
Also used : 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