Search in sources :

Example 71 with Person

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

the class BinderTest method addStatusListenerFromStatusListener_listenerAdded.

@Test
public void addStatusListenerFromStatusListener_listenerAdded() {
    AtomicBoolean outerListenerInvoked = new AtomicBoolean();
    AtomicBoolean innerListenerInvoked = new AtomicBoolean();
    binder.addStatusChangeListener(event -> {
        if (!outerListenerInvoked.getAndSet(true)) {
            binder.addStatusChangeListener(event2 -> {
                innerListenerInvoked.set(true);
            });
        }
    });
    // Trigger status change event
    binder.setBean(new Person());
    Assert.assertTrue("Outer listener should be invoked", outerListenerInvoked.get());
    Assert.assertFalse("Inner listener should not (yet) be invoked", innerListenerInvoked.get());
    // Trigger status change event
    binder.setBean(new Person());
    Assert.assertTrue("Inner listener should be invoked", innerListenerInvoked.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 72 with Person

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

the class BinderTest method withValidator_doesNotDisablesDefaulNullRepresentation.

@Test
public void withValidator_doesNotDisablesDefaulNullRepresentation() {
    String nullRepresentation = "foo";
    binder.forField(nameField).withNullRepresentation(nullRepresentation).withValidator(new NotEmptyValidator<>("")).bind(Person::getFirstName, Person::setFirstName);
    item.setFirstName(null);
    binder.setBean(item);
    Assert.assertEquals(nullRepresentation, nameField.getValue());
    String newValue = "bar";
    nameField.setValue(newValue);
    Assert.assertEquals(newValue, item.getFirstName());
}
Also used : NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) 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 73 with Person

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

the class BinderTest method writeBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void writeBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() throws ValidationException {
    TestTextField testField = new TestTextField();
    setExceptionHandler();
    binder.forField(testField).withConverter(Converter.<String, String>from(name -> {
        throw new NullPointerException();
    }, name -> name)).bind(Person::getFirstName, Person::setFirstName);
    binder.writeBean(new Person());
}
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)

Example 74 with Person

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

the class BinderTest method withConverter_hasChangesFalse.

// See: https://github.com/vaadin/framework/issues/9581
@Test
public void withConverter_hasChangesFalse() {
    TestTextField nameField = new TestTextField();
    nameField.setValue("");
    TestTextField rentField = new TestTextField();
    rentField.setValue("");
    rentField.addValueChangeListener(event -> {
        nameField.setValue("Name");
    });
    item.setRent(BigDecimal.valueOf(10));
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(rentField).withConverter(new EuroConverter("")).withNullRepresentation(BigDecimal.valueOf(0d)).bind(Person::getRent, Person::setRent);
    binder.readBean(item);
    assertFalse(binder.hasChanges());
    assertEquals("€ 10.00", rentField.getValue());
    assertEquals("Name", nameField.getValue());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 75 with Person

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

the class BinderTest method writeBean_getValueThrows_exceptionHandlerSet_bindingExceptionIsThrown.

@Test(expected = BindingException.class)
public void writeBean_getValueThrows_exceptionHandlerSet_bindingExceptionIsThrown() throws ValidationException {
    TestTextField testField = new ThrowingGetter();
    setExceptionHandler();
    binder.forField(testField).bind(Person::getFirstName, Person::setFirstName);
    binder.writeBean(new Person());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) 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