Search in sources :

Example 26 with Person

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

the class BinderTest method execute_binding_status_handler_from_binder_status_handler.

@Test
public void execute_binding_status_handler_from_binder_status_handler() {
    MyBindingHandler bindingHandler = new MyBindingHandler();
    binder.forField(nameField).withValidator(t -> !t.isEmpty(), "No empty values.").withValidationStatusHandler(bindingHandler).bind(Person::getFirstName, Person::setFirstName);
    String ageError = "CONVERSIONERROR";
    binder.forField(ageField).withConverter(new StringToIntegerConverter(ageError)).bind(Person::getAge, Person::setAge);
    binder.setValidationStatusHandler(BinderValidationStatus::notifyBindingValidationStatusHandlers);
    String initialName = item.getFirstName();
    int initialAge = item.getAge();
    binder.setBean(item);
    // Test specific error handling.
    bindingHandler.expectingError = true;
    nameField.setValue("");
    // Test default error handling.
    ageField.setValue("foo");
    assertThat("Error message is not what was expected", ageField.getErrorMessage(), containsString(ageError));
    // Restore values and test no errors.
    ageField.setValue(String.valueOf(initialAge));
    assertFalse("The field should be valid", ageField.isInvalid());
    bindingHandler.expectingError = false;
    nameField.setValue(initialName);
    // Assert that the handler was called.
    assertEquals("Unexpected callCount to binding validation status handler", 6, bindingHandler.callCount);
}
Also used : StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 27 with Person

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

the class BinderValidationStatusTest method binderWithStatusLabel_addAfterBound.

@Test(expected = IllegalStateException.class)
public void binderWithStatusLabel_addAfterBound() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binding.withStatusLabel(label);
}
Also used : Label(com.vaadin.flow.component.html.Label) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 28 with Person

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

the class BinderValidationStatusTest method bindingWithStatusLabel_addAfterBound.

@Test(expected = IllegalStateException.class)
public void bindingWithStatusLabel_addAfterBound() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binding.withStatusLabel(label);
}
Also used : Label(com.vaadin.flow.component.html.Label) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 29 with Person

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

the class BinderValidationStatusTest method setUp.

@Before
public void setUp() {
    binder = new Binder<Person>() {

        @Override
        protected void handleError(HasValue<?, ?> field, ValidationResult 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 30 with Person

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

the class BinderValidationStatusTest method bindingWithStatusHandler_setAfterLabel.

@Test(expected = IllegalStateException.class)
public void bindingWithStatusHandler_setAfterLabel() {
    Label label = new Label();
    BindingBuilder<Person, String> binding = binder.forField(nameField);
    binding.withStatusLabel(label);
    binding.withValidationStatusHandler(NOOP);
}
Also used : Label(com.vaadin.flow.component.html.Label) 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