Search in sources :

Example 6 with StringToIntegerConverter

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

the class BinderTest method multiple_calls_to_same_binding_builder.

@Test
public void multiple_calls_to_same_binding_builder() {
    String stringLength = "String length failure";
    String conversion = "Conversion failed";
    String ageLimit = "Age not in valid range";
    BindingValidationStatus validation;
    binder = new Binder<>(Person.class);
    BindingBuilder builder = binder.forField(ageField);
    builder.withValidator(new StringLengthValidator(stringLength, 0, 3));
    builder.withConverter(new StringToIntegerConverter(conversion));
    builder.withValidator(new IntegerRangeValidator(ageLimit, 3, 150));
    Binding<Person, ?> bind = builder.bind("age");
    binder.setBean(item);
    ageField.setValue("123123");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(stringLength, validation.getMessage().get());
    ageField.setValue("age");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(conversion, validation.getMessage().get());
    ageField.setValue("256");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(ageLimit, validation.getMessage().get());
    ageField.setValue("30");
    validation = bind.validate();
    Assert.assertFalse(validation.isError());
    Assert.assertEquals(30, item.getAge());
}
Also used : IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) 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 7 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 8 with StringToIntegerConverter

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

the class BeanBinderTest method bindInstanceFields_does_not_automatically_bind_incomplete_forField_bindings.

@Test(expected = IllegalStateException.class)
public void bindInstanceFields_does_not_automatically_bind_incomplete_forField_bindings() {
    Binder<TestBean> otherBinder = new Binder<>(TestBean.class);
    TestClass testClass = new TestClass();
    otherBinder.forField(testClass.number).withConverter(new StringToIntegerConverter(""));
    // Should throw an IllegalStateException since the binding for number is
    // not completed with bind
    otherBinder.bindInstanceFields(testClass);
}
Also used : StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Test(org.junit.Test)

Example 9 with StringToIntegerConverter

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

the class BinderInstanceFieldTest method bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced.

@Test
public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
    BindWithNoFieldInPerson form = new BindWithNoFieldInPerson();
    Binder<Person> binder = new Binder<>(Person.class);
    TestTextField name = new TestTextField();
    form.firstName = name;
    binder.forField(form.firstName).withValidator(new StringLengthValidator("Name is invalid", 3, 10)).bind("firstName");
    TestTextField ageField = new TestTextField();
    form.noFieldInPerson = ageField;
    binder.forField(form.noFieldInPerson).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    binder.bindInstanceFields(form);
    Person person = new Person();
    String personName = "foo";
    int age = 11;
    person.setFirstName(personName);
    person.setAge(age);
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
    Assert.assertEquals(String.valueOf(person.getAge()), form.noFieldInPerson.getValue());
    // the instances are not overridden
    Assert.assertEquals(name, form.firstName);
    Assert.assertEquals(ageField, form.noFieldInPerson);
    // Test correct age
    age += 56;
    form.noFieldInPerson.setValue(String.valueOf(age));
    Assert.assertEquals(form.noFieldInPerson.getValue(), String.valueOf(person.getAge()));
    // Test incorrect name
    form.firstName.setValue("aa");
    Assert.assertEquals(personName, person.getFirstName());
    Assert.assertFalse(binder.validate().isOk());
}
Also used : StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 10 with StringToIntegerConverter

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

the class BinderTest method withConverter_disablesDefaulNullRepresentation.

@Test
public void withConverter_disablesDefaulNullRepresentation() {
    Integer customNullConverter = 0;
    binder.forField(ageField).withNullRepresentation("foo").withConverter(new StringToIntegerConverter("")).withConverter(age -> age, age -> age == null ? customNullConverter : age).bind(Person::getSalary, Person::setSalary);
    binder.setBean(item);
    Assert.assertEquals(customNullConverter.toString(), ageField.getValue());
    Integer salary = 11;
    ageField.setValue(salary.toString());
    Assert.assertEquals(11, salary.intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HasValue(com.vaadin.flow.component.HasValue) Person(com.vaadin.flow.tests.data.bean.Person) HashMap(java.util.HashMap) Assert.assertSame(org.junit.Assert.assertSame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) 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) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Objects(java.util.Objects) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) 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) Sex(com.vaadin.flow.tests.data.bean.Sex) Matchers.containsString(org.hamcrest.Matchers.containsString) 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)

Aggregations

StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)12 Test (org.junit.Test)12 Person (com.vaadin.flow.tests.data.bean.Person)10 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)6 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)4 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)4 HasValue (com.vaadin.flow.component.HasValue)3 Binding (com.vaadin.flow.data.binder.Binder.Binding)3 IntegerRangeValidator (com.vaadin.flow.data.validator.IntegerRangeValidator)3 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Assert (org.junit.Assert)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 Assert.assertNotNull (org.junit.Assert.assertNotNull)3 Assert.assertNull (org.junit.Assert.assertNull)3