package com.vaadin.data; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.vaadin.tests.data.bean.Person; public class BinderTest extends BinderTestBase, Person> { @Before public void setUp() { binder = new Binder<>(); item = new Person(); item.setFirstName("Johannes"); item.setAge(32); } @Test(expected = NullPointerException.class) public void bindNullBean_throws() { binder.bind(null); } @Test(expected = NullPointerException.class) public void bindNullField_throws() { binder.forField(null); } @Test(expected = NullPointerException.class) public void bindNullGetter_throws() { binder.bind(nameField, null, Person::setFirstName); } @Test public void fieldBound_bindItem_fieldValueUpdated() { binder.forField(nameField).bind(Person::getFirstName, Person::setFirstName); binder.bind(item); assertEquals("Johannes", nameField.getValue()); } @Test public void fieldBoundWithShortcut_bindBean_fieldValueUpdated() { bindName(); assertEquals("Johannes", nameField.getValue()); } @Test public void beanBound_updateFieldValue_beanValueUpdated() { binder.bind(item); binder.bind(nameField, Person::getFirstName, Person::setFirstName); assertEquals("Johannes", nameField.getValue()); nameField.setValue("Artur"); assertEquals("Artur", item.getFirstName()); } @Test public void bound_getBean_returnsBoundBean() { assertFalse(binder.getBean().isPresent()); binder.bind(item); assertSame(item, binder.getBean().get()); } @Test public void unbound_getBean_returnsNothing() { binder.bind(item); binder.unbind(); assertFalse(binder.getBean().isPresent()); } @Test public void bound_changeFieldValue_beanValueUpdated() { bindName(); nameField.setValue("Henri"); assertEquals("Henri", item.getFirstName()); } @Test public void unbound_changeFieldValue_beanValueNotUpdated() { bindName(); nameField.setValue("Henri"); binder.unbind(); nameField.setValue("Aleksi"); assertEquals("Henri", item.getFirstName()); } @Test public void bindNullSetter_valueChangesIgnored() { binder.bind(nameField, Person::getFirstName, null); binder.bind(item); nameField.setValue("Artur"); assertEquals(item.getFirstName(), "Johannes"); } @Test public void bound_bindToAnotherBean_stopsUpdatingOriginal() { bindName(); nameField.setValue("Leif"); Person p2 = new Person(); p2.setFirstName("Marlon"); binder.bind(p2); assertEquals("Marlon", nameField.getValue()); assertEquals("Leif", item.getFirstName()); assertSame(p2, binder.getBean().get()); nameField.setValue("Ilia"); assertEquals("Ilia", p2.getFirstName()); assertEquals("Leif", item.getFirstName()); } @Test public void save_unbound_noChanges() throws ValidationException { Binder binder = new Binder<>(); Person person = new Person(); int age = 10; person.setAge(age); binder.save(person); Assert.assertEquals(age, person.getAge()); } @Test public void save_bound_beanIsUpdated() throws ValidationException { Binder binder = new Binder<>(); binder.bind(nameField, Person::getFirstName, Person::setFirstName); Person person = new Person(); String fieldValue = "bar"; nameField.setValue(fieldValue); person.setFirstName("foo"); binder.save(person); Assert.assertEquals(fieldValue, person.getFirstName()); } @Test public void load_bound_fieldValueIsUpdated() { binder.bind(nameField, Person::getFirstName, Person::setFirstName); Person person = new Person(); String name = "bar"; person.setFirstName(name); binder.load(person); Assert.assertEquals(name, nameField.getValue()); } @Test public void load_unbound_noChanges() { nameField.setValue(""); Person person = new Person(); String name = "bar"; person.setFirstName(name); binder.load(person); Assert.assertEquals("", nameField.getValue()); } protected void bindName() { binder.bind(nameField, Person::getFirstName, Person::setFirstName); binder.bind(item); } }