Search in sources :

Example 1 with StringToIntegerConverter

use of com.vaadin.flow.data.converter.StringToIntegerConverter 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) 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) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 2 with StringToIntegerConverter

use of com.vaadin.flow.data.converter.StringToIntegerConverter 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 3 with StringToIntegerConverter

use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.

the class BinderTest method setReadOnly_binderLoadedByReadBean.

@Test
public void setReadOnly_binderLoadedByReadBean() {
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    binder.readBean(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 4 with StringToIntegerConverter

use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.

the class BinderTest method removed_binding_not_updates_value.

@Test
public void removed_binding_not_updates_value() {
    Binding<Person, Integer> binding = binder.forField(ageField).withConverter(new StringToIntegerConverter("Can't convert")).bind(Person::getAge, Person::setAge);
    binder.setBean(item);
    String modifiedAge = String.valueOf(item.getAge() + 10);
    String ageBeforeUnbind = String.valueOf(item.getAge());
    binder.removeBinding(binding);
    ageField.setValue(modifiedAge);
    assertEquals("Binding still affects bean even after unbind", ageBeforeUnbind, String.valueOf(item.getAge()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 5 with StringToIntegerConverter

use of com.vaadin.flow.data.converter.StringToIntegerConverter in project flow by vaadin.

the class BinderTest method conversionWithLocaleBasedErrorMessage.

@Test
public void conversionWithLocaleBasedErrorMessage() {
    TestTextField ageField = new TestTextField();
    String fiError = "VIRHE";
    String otherError = "ERROR";
    StringToIntegerConverter converter = new StringToIntegerConverter(context -> context.getLocale().map(Locale::getLanguage).orElse("en").equals("fi") ? fiError : otherError);
    binder.forField(ageField).withConverter(converter).bind(Person::getAge, Person::setAge);
    binder.setBean(item);
    UI testUI = new UI();
    UI.setCurrent(testUI);
    testUI.add(ageField);
    ageField.setValue("not a number");
    assertEquals(otherError, ageField.getErrorMessage());
    testUI.setLocale(new Locale("fi", "FI"));
    // Re-validate to get the error message with correct locale
    binder.validate();
    assertEquals(fiError, ageField.getErrorMessage());
}
Also used : Locale(java.util.Locale) UI(com.vaadin.flow.component.UI) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) 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)

Aggregations

StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)16 Test (org.junit.Test)16 Person (com.vaadin.flow.tests.data.bean.Person)14 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)10 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)7 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)7 HasValue (com.vaadin.flow.component.HasValue)6 UI (com.vaadin.flow.component.UI)6 Binding (com.vaadin.flow.data.binder.Binder.Binding)6 IntegerRangeValidator (com.vaadin.flow.data.validator.IntegerRangeValidator)6 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)6 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)6 Serializable (java.io.Serializable)6 HashMap (java.util.HashMap)6 Locale (java.util.Locale)6 Map (java.util.Map)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6