Search in sources :

Example 56 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderTest method beanBinder_withConverter_nullRepresentationIsNotDisabled.

@Test
public void beanBinder_withConverter_nullRepresentationIsNotDisabled() {
    String customNullPointerRepresentation = "foo";
    Binder<Person> binder = new Binder<>(Person.class);
    binder.forField(nameField).withConverter(value -> value, value -> value == null ? customNullPointerRepresentation : value).bind("firstName");
    Person person = new Person();
    binder.setBean(person);
    Assert.assertEquals(customNullPointerRepresentation, nameField.getValue());
}
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) 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 57 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderTest method load_bound_fieldValueIsUpdated.

@Test
public void load_bound_fieldValueIsUpdated() {
    binder.bind(nameField, Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    String name = "bar";
    person.setFirstName(name);
    binder.readBean(person);
    Assert.assertEquals(name, nameField.getValue());
}
Also used : 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 58 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderTest method load_unbound_noChanges.

@Test
public void load_unbound_noChanges() {
    nameField.setValue("");
    Person person = new Person();
    String name = "bar";
    person.setFirstName(name);
    binder.readBean(person);
    Assert.assertEquals("", nameField.getValue());
}
Also used : 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 59 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderTest method setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator.

@Test
public void setRequired_withErrorMessageProvider_fieldGetsRequiredIndicatorAndValidator() {
    TestTextField textField = new TestTextField();
    assertFalse(textField.isRequiredIndicatorVisible());
    BindingBuilder<Person, String> binding = binder.forField(textField);
    assertFalse(textField.isRequiredIndicatorVisible());
    AtomicInteger invokes = new AtomicInteger();
    binding.asRequired(context -> {
        invokes.incrementAndGet();
        return "foobar";
    });
    assertTrue(textField.isRequiredIndicatorVisible());
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.setBean(item);
    assertThat(textField.getErrorMessage(), isEmptyString());
    Assert.assertEquals(0, invokes.get());
    textField.setValue(textField.getEmptyValue());
    Assert.assertEquals("foobar", componentErrors.get(textField));
    // validation is done for all changed bindings once.
    Assert.assertEquals(1, invokes.get());
    textField.setValue("value");
    assertFalse(textField.isInvalid());
    assertTrue(textField.isRequiredIndicatorVisible());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) 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 60 with Person

use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.

the class BinderTest method settingAsRequiredEnabledFalseWhenNoAsRequired.

@Test(expected = IllegalStateException.class)
public void settingAsRequiredEnabledFalseWhenNoAsRequired() {
    TestTextField textField = new TestTextField();
    BindingBuilder<Person, String> bindingBuilder = binder.forField(textField);
    Binding<Person, String> binding = bindingBuilder.bind(Person::getFirstName, Person::setFirstName);
    binder.readBean(item);
    // TextField input is not set required, this should trigger
    // IllegalStateExceptipon
    binding.setAsRequiredEnabled(false);
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) 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

Person (com.vaadin.flow.tests.data.bean.Person)97 Test (org.junit.Test)92 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)51 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)49 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)34 Matchers.containsString (org.hamcrest.Matchers.containsString)31 Before (org.junit.Before)30 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 HasValue (com.vaadin.flow.component.HasValue)26 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)26 Assert (org.junit.Assert)26 Binding (com.vaadin.flow.data.binder.Binder.Binding)25 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)25 Serializable (java.io.Serializable)25 HashMap (java.util.HashMap)25 Map (java.util.Map)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 Assert.assertFalse (org.junit.Assert.assertFalse)25 Assert.assertNotNull (org.junit.Assert.assertNotNull)25