Search in sources :

Example 1 with Converter

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

the class BinderTest method setRequired_withCustomValidator_modelConverterBeforeValidator.

@Test
public void setRequired_withCustomValidator_modelConverterBeforeValidator() {
    TestTextField textField = new TestTextField();
    assertFalse(textField.isRequiredIndicatorVisible());
    Converter<String, String> stringBasicPreProcessingConverter = new Converter<String, String>() {

        @Override
        public Result<String> convertToModel(String value, ValueContext context) {
            if (StringUtils.isBlank(value)) {
                return Result.ok(null);
            }
            return Result.ok(StringUtils.trim(value));
        }

        @Override
        public String convertToPresentation(String value, ValueContext context) {
            if (value == null) {
                return "";
            }
            return value;
        }
    };
    AtomicInteger invokes = new AtomicInteger();
    Validator<String> customRequiredValidator = (value, context) -> {
        invokes.incrementAndGet();
        if (value == null) {
            return ValidationResult.error("Input required.");
        }
        return ValidationResult.ok();
    };
    binder.forField(textField).withConverter(stringBasicPreProcessingConverter).asRequired(customRequiredValidator).bind(Person::getFirstName, Person::setFirstName);
    binder.setBean(item);
    assertThat(textField.getErrorMessage(), isEmptyString());
    assertEquals(1, invokes.get());
    textField.setValue(" ");
    assertNotNull(textField.getErrorMessage());
    assertEquals("Input required.", componentErrors.get(textField));
    // validation is done for all changed bindings once.
    assertEquals(2, invokes.get());
    textField.setValue("value");
    assertFalse(textField.isInvalid());
    assertTrue(textField.isRequiredIndicatorVisible());
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) StringToDoubleConverter(com.vaadin.flow.data.converter.StringToDoubleConverter) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) StringToBigDecimalConverter(com.vaadin.flow.data.converter.StringToBigDecimalConverter) Converter(com.vaadin.flow.data.converter.Converter) 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 2 with Converter

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

the class BinderTest method bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() {
    TestTextField testField = new TestTextField();
    setExceptionHandler();
    binder.forField(testField).withConverter(Converter.<String, String>from(name -> Result.ok(name), name -> {
        throw new NullPointerException();
    })).bind(Person::getFirstName, Person::setFirstName).read(new Person());
}
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) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 3 with Converter

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

the class BinderTest method readBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void readBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() {
    TestTextField testField = new TestTextField();
    setExceptionHandler();
    binder.forField(testField).withConverter(Converter.<String, String>from(name -> {
        throw new NullPointerException();
    }, name -> name)).bind(Person::getFirstName, Person::setFirstName).read(new Person());
}
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) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 4 with Converter

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

the class RequiredFieldConfiguratorUtil method testConvertedDefaultValue.

/**
 * Tests the converted default value of the provided binding builder if
 * possible.
 *
 * @param binding
 *            the binding builder to test
 * @param predicate
 *            predicate for testing the converted default value
 * @return <code>true</code> if a converted default value is available and
 *         it passes the test; <code>false</code> if no converted default
 *         value is available or if it doesn't pass the test
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean testConvertedDefaultValue(BindingBuilder<?, ?> binding, Predicate<Object> predicate) {
    if (binding instanceof BindingBuilderImpl<?, ?, ?>) {
        HasValue<?, ?> field = binding.getField();
        Converter converter = ((BindingBuilderImpl<?, ?, ?>) binding).getConverterValidatorChain();
        Result<?> result = converter.convertToModel(field.getEmptyValue(), BindingImpl.createValueContext(field));
        if (!result.isError()) {
            Object convertedEmptyValue = result.getOrThrow(IllegalStateException::new);
            return predicate.test(convertedEmptyValue);
        }
    }
    return false;
}
Also used : BindingBuilderImpl(com.vaadin.flow.data.binder.Binder.BindingBuilderImpl) Converter(com.vaadin.flow.data.converter.Converter)

Aggregations

Converter (com.vaadin.flow.data.converter.Converter)4 HasValue (com.vaadin.flow.component.HasValue)3 UI (com.vaadin.flow.component.UI)3 Binding (com.vaadin.flow.data.binder.Binder.Binding)3 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)3 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)3 StringToBigDecimalConverter (com.vaadin.flow.data.converter.StringToBigDecimalConverter)3 StringToDoubleConverter (com.vaadin.flow.data.converter.StringToDoubleConverter)3 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)3 IntegerRangeValidator (com.vaadin.flow.data.validator.IntegerRangeValidator)3 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)3 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)3 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 Person (com.vaadin.flow.tests.data.bean.Person)3 Sex (com.vaadin.flow.tests.data.bean.Sex)3 Serializable (java.io.Serializable)3 BigDecimal (java.math.BigDecimal)3 DecimalFormat (java.text.DecimalFormat)3 NumberFormat (java.text.NumberFormat)3 HashMap (java.util.HashMap)3