Search in sources :

Example 11 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 12 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) CoreMatchers(org.hamcrest.CoreMatchers) 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) StringToDoubleConverter(com.vaadin.flow.data.converter.StringToDoubleConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) Assert.assertSame(org.junit.Assert.assertSame) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) After(org.junit.After) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) UI(com.vaadin.flow.component.UI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) DecimalFormat(java.text.DecimalFormat) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StringToBigDecimalConverter(com.vaadin.flow.data.converter.StringToBigDecimalConverter) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Serializable(java.io.Serializable) Objects(java.util.Objects) Converter(com.vaadin.flow.data.converter.Converter) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) 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)

Example 13 with StringToIntegerConverter

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

the class BinderTest method do_test_save_bound_beanAsDraft.

private void do_test_save_bound_beanAsDraft(boolean setBean) {
    Binder<Person> binder = new Binder<>();
    binder.forField(nameField).withValidator((value, context) -> {
        if (value.equals("Mike")) {
            return ValidationResult.ok();
        } else {
            return ValidationResult.error("value must be Mike");
        }
    }).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    Person person = new Person();
    if (setBean) {
        binder.setBean(person);
    }
    String fieldValue = "John";
    nameField.setValue(fieldValue);
    int age = 10;
    ageField.setValue("10");
    person.setFirstName("Mark");
    binder.writeBeanAsDraft(person);
    // name is not written to draft as validation / conversion
    // does not pass
    assertNotEquals(fieldValue, person.getFirstName());
    // age is written to draft even if firstname validation
    // fails
    assertEquals(age, person.getAge());
    binder.writeBeanAsDraft(person, true);
    // name is now written despite validation as write was forced
    assertEquals(fieldValue, person.getFirstName());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) 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) StringToDoubleConverter(com.vaadin.flow.data.converter.StringToDoubleConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) Assert.assertSame(org.junit.Assert.assertSame) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) After(org.junit.After) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) UI(com.vaadin.flow.component.UI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) DecimalFormat(java.text.DecimalFormat) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StringToBigDecimalConverter(com.vaadin.flow.data.converter.StringToBigDecimalConverter) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Serializable(java.io.Serializable) Objects(java.util.Objects) Converter(com.vaadin.flow.data.converter.Converter) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) 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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) Person(com.vaadin.flow.tests.data.bean.Person)

Example 14 with StringToIntegerConverter

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

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

the class BinderValueChangeTest method userOriginatedUpdate_unbound_singleEventOnSetValue.

@Test
public void userOriginatedUpdate_unbound_singleEventOnSetValue() {
    TestTextField field = new TestTextField();
    binder.forField(field).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    binder.addValueChangeListener(this::statusChanged);
    Assert.assertNull(event.get());
    field.getElement().getNode().getFeature(ElementPropertyMap.class).setProperty("value", "foo", false);
    verifyEvent(field, true);
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) 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