Search in sources :

Example 91 with Person

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

the class BinderTest method binding_with_null_representation_value_not_null.

@Test
public void binding_with_null_representation_value_not_null() {
    String nullRepresentation = "Some arbitrary text";
    binder.forField(nameField).withNullRepresentation(nullRepresentation).bind(Person::getFirstName, Person::setFirstName);
    assertFalse("First name in item should not be null", Objects.isNull(item.getFirstName()));
    binder.setBean(item);
    Assert.assertEquals("Field value was not set correctly", item.getFirstName(), 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 92 with Person

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

the class BinderTest method binding_with_default_null_representation.

@Test
public void binding_with_default_null_representation() {
    TestTextField nullTextField = new TestTextField() {

        @Override
        public String getEmptyValue() {
            return "null";
        }
    };
    Person namelessPerson = new Person(null, "Doe", "", 25, Sex.UNKNOWN, null);
    binder.bind(nullTextField, Person::getFirstName, Person::setFirstName);
    binder.setBean(namelessPerson);
    assertTrue(nullTextField.isEmpty());
    Assert.assertEquals("null", namelessPerson.getFirstName());
    // Change value, see that textfield is not empty and bean is updated.
    nullTextField.setValue("");
    assertFalse(nullTextField.isEmpty());
    Assert.assertEquals("First name of person was not properly updated", "", namelessPerson.getFirstName());
    // Verify that default null representation does not map back to null
    nullTextField.setValue("null");
    assertTrue(nullTextField.isEmpty());
    Assert.assertEquals("Default one-way null representation failed.", "null", namelessPerson.getFirstName());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 93 with Person

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

the class BinderStatusChangeTest method setUp.

@Before
public void setUp() {
    binder = new Binder<>();
    item = new Person();
    event = new AtomicReference<>();
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Before(org.junit.Before)

Example 94 with Person

use of com.vaadin.flow.tests.data.bean.Person 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)

Example 95 with Person

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

the class BinderValidationStatusTest method bindingWithStatusLabel_setAfterHandler.

@Test(expected = IllegalStateException.class)
public void bindingWithStatusLabel_setAfterHandler() {
    TestLabel label = new TestLabel();
    BindingBuilder<Person, String> binding = binder.forField(nameField);
    binding.withValidationStatusHandler(NOOP);
    binding.withStatusLabel(label);
}
Also used : TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) 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