Search in sources :

Example 61 with Person

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

the class BinderTest method invalidUsage_modifyFieldsInsideValidator_binderDoesNotThrow.

@Test
public void invalidUsage_modifyFieldsInsideValidator_binderDoesNotThrow() {
    TestTextField field = new TestTextField();
    AtomicBoolean validatorIsExecuted = new AtomicBoolean();
    binder.forField(field).asRequired().withValidator((val, context) -> {
        nameField.setValue("foo");
        ageField.setValue("bar");
        validatorIsExecuted.set(true);
        return ValidationResult.ok();
    }).bind(Person::getEmail, Person::setEmail);
    binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(ageField).bind(Person::getLastName, Person::setLastName);
    binder.setBean(new Person());
    field.setValue("baz");
    // mostly self control, the main check is: not exception is thrown
    Assert.assertTrue(validatorIsExecuted.get());
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 62 with Person

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

the class BinderTest method bound_bindToAnotherBean_stopsUpdatingOriginal.

@Test
public void bound_bindToAnotherBean_stopsUpdatingOriginal() {
    bindName();
    nameField.setValue("Leif");
    Person p2 = new Person();
    p2.setFirstName("Marlon");
    binder.setBean(p2);
    assertEquals("Marlon", nameField.getValue());
    assertEquals("Leif", item.getFirstName());
    assertSame(p2, binder.getBean());
    nameField.setValue("Ilia");
    assertEquals("Ilia", p2.getFirstName());
    assertEquals("Leif", item.getFirstName());
}
Also used : Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 63 with Person

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

the class BinderTest method withConverter_writeBackValueDisabled.

@Test
public void withConverter_writeBackValueDisabled() {
    TestTextField rentField = new TestTextField();
    rentField.setValue("");
    Binding<Person, BigDecimal> binding = binder.forField(rentField).withConverter(new EuroConverter("")).withNullRepresentation(BigDecimal.valueOf(0d)).bind(Person::getRent, Person::setRent);
    binder.setBean(item);
    binding.setConvertBackToPresentation(false);
    rentField.setValue("10");
    assertNotEquals("€ 10.00", rentField.getValue());
}
Also used : TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 64 with Person

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

the class BinderTest method withConverter_disablesDefaulNullRepresentation.

@Test
public void withConverter_disablesDefaulNullRepresentation() {
    Integer customNullConverter = 0;
    binder.forField(ageField).withNullRepresentation("foo").withConverter(new StringToIntegerConverter("")).withConverter(age -> age, age -> age == null ? customNullConverter : age).bind(Person::getSalary, Person::setSalary);
    binder.setBean(item);
    Assert.assertEquals(customNullConverter.toString(), ageField.getValue());
    Integer salary = 11;
    ageField.setValue(salary.toString());
    Assert.assertEquals(11, salary.intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 65 with Person

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

the class BinderTest method save_bound_beanIsUpdated.

@Test
public void save_bound_beanIsUpdated() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    binder.bind(nameField, Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    String fieldValue = "bar";
    nameField.setValue(fieldValue);
    person.setFirstName("foo");
    binder.writeBean(person);
    Assert.assertEquals(fieldValue, person.getFirstName());
}
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)

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