Search in sources :

Example 26 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField 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 27 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField in project flow by vaadin.

the class BinderTestBase method setUpBase.

@Before
public void setUpBase() {
    UI ui = new UI() {

        @Override
        public Locale getLocale() {
            return Locale.US;
        }
    };
    nameField = new TestTextField();
    ageField = new TestTextField();
    ui.add(nameField, ageField);
}
Also used : UI(com.vaadin.flow.component.UI) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Before(org.junit.Before)

Example 28 with TestTextField

use of com.vaadin.flow.data.binder.testcomponents.TestTextField 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

TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)28 Test (org.junit.Test)26 Person (com.vaadin.flow.tests.data.bean.Person)13 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)7 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)6 Before (org.junit.Before)5 HasValue (com.vaadin.flow.component.HasValue)4 SubConstraint (com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubConstraint)4 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Assert (org.junit.Assert)4 Label (com.vaadin.flow.component.html.Label)3 SubSubConstraint (com.vaadin.flow.data.binder.BeanBinderTest.RequiredConstraints.SubSubConstraint)3 Binding (com.vaadin.flow.data.binder.Binder.Binding)3 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)3 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertFalse (org.junit.Assert.assertFalse)3