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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations