Search in sources :

Example 6 with TestLabel

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

the class ReadOnlyHasValueTest method testBind.

@Test
public void testBind() {
    Binder<Bean> beanBinder = new Binder<>(Bean.class);
    TestLabel label = new TestLabel();
    ReadOnlyHasValue<Long> intHasValue = new ReadOnlyHasValue<>(i -> label.setText(Objects.toString(i, "")));
    beanBinder.forField(intHasValue).bind("v");
    beanBinder.readBean(new Bean(42));
    assertEquals("42", label.getText());
    assertEquals(42L, intHasValue.getValue().longValue());
    Registration registration = intHasValue.addValueChangeListener(e -> {
        assertEquals(42L, e.getOldValue().longValue());
        assertSame(intHasValue, e.getHasValue());
        assertFalse(e.isFromClient());
    });
    beanBinder.readBean(new Bean(1984));
    assertEquals("1984", label.getText());
    assertEquals(1984L, intHasValue.getValue().longValue());
    registration.remove();
    beanBinder.readBean(null);
    assertEquals("", label.getText());
    assertEquals(null, intHasValue.getValue());
}
Also used : TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 7 with TestLabel

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

the class ReadOnlyHasValueTest method testEmptyValue.

@Test
public void testEmptyValue() {
    Binder<Bean> beanBinder = new Binder<>(Bean.class);
    TestLabel label = new TestLabel();
    ReadOnlyHasValue<String> strHasValue = new ReadOnlyHasValue<>(label::setText, NO_VALUE);
    beanBinder.forField(strHasValue).withConverter(Long::parseLong, (Long i) -> "" + i).bind("v");
    beanBinder.readBean(new Bean(42));
    assertEquals("42", label.getText());
    beanBinder.readBean(null);
    assertEquals(NO_VALUE, label.getText());
    assertTrue(strHasValue.isEmpty());
}
Also used : TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) Test(org.junit.Test)

Example 8 with TestLabel

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

the class BinderConverterValidatorTest method binderBindAndLoad_clearsErrors.

@Test
public void binderBindAndLoad_clearsErrors() {
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(bean -> !bean.getFirstName().contains("error"), "error");
    Person person = new Person();
    person.setFirstName("");
    binder.setBean(person);
    // initial value is invalid but no error
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // make error show
    nameField.setValue("foo");
    nameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    // bind to another person to see that error is cleared
    person = new Person();
    person.setFirstName("");
    binder.setBean(person);
    // error has been cleared
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // make show error
    nameField.setValue("foo");
    nameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    // load should also clear error
    binder.readBean(person);
    Assert.assertFalse(componentErrors.containsKey(nameField));
    // bind a new field that has invalid value in bean
    TestTextField lastNameField = new TestTextField();
    // The test starts with a valid value as the last name of the person,
    // since the binder assumes any non-changed values to be valid.
    person.setLastName("bar");
    BindingBuilder<Person, String> binding2 = binder.forField(lastNameField).withValidator(notEmpty);
    binding2.bind(Person::getLastName, Person::setLastName);
    // should not have error shown when initialized
    assertThat(lastNameField.getErrorMessage(), isEmptyString());
    // Set a value that breaks the validation
    lastNameField.setValue("");
    assertNotNull(lastNameField.getErrorMessage());
    // add status label to show bean level error
    TestLabel statusLabel = new TestLabel();
    binder.setStatusLabel(statusLabel);
    nameField.setValue("error");
    // no error shown yet because second field validation doesn't pass
    Assert.assertEquals("", statusLabel.getText());
    // make second field validation pass to get bean validation error
    lastNameField.setValue("foo");
    Assert.assertEquals("error", statusLabel.getText());
    // reload bean to clear error
    binder.readBean(person);
    Assert.assertEquals("", statusLabel.getText());
    // reset() should clear all errors and status label
    nameField.setValue("");
    lastNameField.setValue("");
    Assert.assertTrue(componentErrors.containsKey(nameField));
    Assert.assertTrue(componentErrors.containsKey(lastNameField));
    binder.removeBean();
    Assert.assertFalse(componentErrors.containsKey(nameField));
    Assert.assertFalse(componentErrors.containsKey(lastNameField));
    Assert.assertEquals("", statusLabel.getText());
}
Also used : TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 9 with TestLabel

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

Example 10 with TestLabel

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

the class BinderValidationStatusTest method binderWithStatusLabel_setAfterHandler.

@Test(expected = IllegalStateException.class)
public void binderWithStatusLabel_setAfterHandler() {
    TestLabel label = new TestLabel();
    BindingBuilder<Person, String> binding = binder.forField(nameField);
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.setValidationStatusHandler(event -> {
    });
    binder.setStatusLabel(label);
}
Also used : TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

TestLabel (com.vaadin.flow.data.binder.testcomponents.TestLabel)13 Test (org.junit.Test)11 Person (com.vaadin.flow.tests.data.bean.Person)9 Before (org.junit.Before)2 UI (com.vaadin.flow.component.UI)1 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)1 Registration (com.vaadin.flow.shared.Registration)1 Locale (java.util.Locale)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1