Search in sources :

Example 6 with ValueContext

use of com.vaadin.flow.data.binder.ValueContext in project linkki by linkki-framework.

the class BindLabelValueAspectDefinitionTest method testCreateComponentValueSetter_UsesCustomConverter.

@Test
void testCreateComponentValueSetter_UsesCustomConverter() {
    LinkkiText label = new LinkkiText();
    Consumer<Object> valueSetter = new BindLabelValueAspectDefinition().createComponentValueSetter(new NoLabelComponentWrapper(label));
    LinkkiConverterRegistry converterRegistry = LinkkiConverterRegistry.DEFAULT.with(new Converter<String, FooBar>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Result<FooBar> convertToModel(String value, ValueContext context) {
            return Result.ok(FooBar.valueOf(value));
        }

        @Override
        public String convertToPresentation(FooBar value, ValueContext context) {
            return value == FooBar.FOO ? "Foo" : "Bar";
        }
    });
    VaadinSession vaadinSession = mock(VaadinSession.class);
    when(vaadinSession.getAttribute(LinkkiConverterRegistry.class)).thenReturn(converterRegistry);
    VaadinSession.setCurrent(vaadinSession);
    valueSetter.accept(FooBar.FOO);
    assertThat(label.getText(), is("Foo"));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) LinkkiText(org.linkki.core.vaadin.component.base.LinkkiText) VaadinSession(com.vaadin.flow.server.VaadinSession) LinkkiConverterRegistry(org.linkki.core.ui.converters.LinkkiConverterRegistry) Result(com.vaadin.flow.data.binder.Result) NoLabelComponentWrapper(org.linkki.core.ui.wrapper.NoLabelComponentWrapper) Test(org.junit.jupiter.api.Test)

Example 7 with ValueContext

use of com.vaadin.flow.data.binder.ValueContext in project linkki by linkki-framework.

the class FormattedDoubleToStringConverterTest method testConvertToModel_Null.

@Test
public void testConvertToModel_Null() {
    FormattedDoubleToStringConverter converter = new FormattedDoubleToStringConverter("0.00");
    ValueContext context = new ValueContext(Locale.GERMAN);
    assertThat(converter.convertToModel("", context).getOrThrow(s -> new AssertionError(s)), is(nullValue()));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) Test(org.junit.jupiter.api.Test) Locale(java.util.Locale) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Matchers.is(org.hamcrest.Matchers.is) ValueContext(com.vaadin.flow.data.binder.ValueContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Test(org.junit.jupiter.api.Test)

Example 8 with ValueContext

use of com.vaadin.flow.data.binder.ValueContext in project linkki by linkki-framework.

the class FormattedDoubleToStringConverterTest method testConvertToPresentation.

@Test
public void testConvertToPresentation() {
    FormattedDoubleToStringConverter converter = new FormattedDoubleToStringConverter("0.00");
    ValueContext context = new ValueContext(Locale.GERMAN);
    assertThat(converter.convertToPresentation(0.2, context), is("0,20"));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) Test(org.junit.jupiter.api.Test)

Example 9 with ValueContext

use of com.vaadin.flow.data.binder.ValueContext in project linkki by linkki-framework.

the class FormattedDoubleToStringConverterTest method testConvertToModel.

@Test
public void testConvertToModel() {
    FormattedDoubleToStringConverter converter = new FormattedDoubleToStringConverter("0.00");
    ValueContext context = new ValueContext(Locale.GERMAN);
    assertThat(converter.convertToModel("1,23", context).getOrThrow(s -> new AssertionError(s)), is(1.23));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) Test(org.junit.jupiter.api.Test) Locale(java.util.Locale) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Matchers.is(org.hamcrest.Matchers.is) ValueContext(com.vaadin.flow.data.binder.ValueContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Test(org.junit.jupiter.api.Test)

Example 10 with ValueContext

use of com.vaadin.flow.data.binder.ValueContext in project linkki by linkki-framework.

the class FormattedIntegerToStringConverterTest method testConvertToModel_Null.

@Test
public void testConvertToModel_Null() {
    FormattedIntegerToStringConverter converter = new FormattedIntegerToStringConverter("#,##0");
    ValueContext context = new ValueContext(Locale.GERMAN);
    assertThat(converter.convertToModel("", context).getOrThrow(s -> new AssertionError(s)), is(nullValue()));
}
Also used : ValueContext(com.vaadin.flow.data.binder.ValueContext) Test(org.junit.jupiter.api.Test) Locale(java.util.Locale) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Matchers.is(org.hamcrest.Matchers.is) ValueContext(com.vaadin.flow.data.binder.ValueContext) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Test(org.junit.jupiter.api.Test)

Aggregations

ValueContext (com.vaadin.flow.data.binder.ValueContext)31 Test (org.junit.jupiter.api.Test)24 Locale (java.util.Locale)6 ValidationResult (com.vaadin.flow.data.binder.ValidationResult)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 LocalDate (java.time.LocalDate)4 LocalDateTime (java.time.LocalDateTime)4 Matchers.is (org.hamcrest.Matchers.is)4 Matchers.nullValue (org.hamcrest.Matchers.nullValue)4 Test (org.junit.Test)3 Result (com.vaadin.flow.data.binder.Result)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Key (com.vaadin.flow.component.Key)1 UI (com.vaadin.flow.component.UI)1 Button (com.vaadin.flow.component.button.Button)1 ButtonVariant (com.vaadin.flow.component.button.ButtonVariant)1 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)1