You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanBinderTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package com.vaadin.data;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertSame;
  4. import java.lang.reflect.Method;
  5. import java.util.List;
  6. import java.util.Set;
  7. import org.junit.Assert;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import com.vaadin.data.BeanBinder.BeanBindingBuilder;
  11. import com.vaadin.data.Binder.BindingBuilder;
  12. import com.vaadin.tests.data.bean.BeanToValidate;
  13. import com.vaadin.ui.CheckBoxGroup;
  14. public class BeanBinderTest
  15. extends BinderTestBase<BeanBinder<BeanToValidate>, BeanToValidate> {
  16. private enum TestEnum {
  17. }
  18. private class TestClass {
  19. private CheckBoxGroup<TestEnum> enums;
  20. }
  21. private class TestBean {
  22. private Set<TestEnum> enums;
  23. public Set<TestEnum> getEnums() {
  24. return enums;
  25. }
  26. public void setEnums(Set<TestEnum> enums) {
  27. this.enums = enums;
  28. }
  29. }
  30. @Before
  31. public void setUp() {
  32. binder = new BeanBinder<>(BeanToValidate.class);
  33. item = new BeanToValidate();
  34. item.setFirstname("Johannes");
  35. item.setAge(32);
  36. }
  37. @Test
  38. public void bindInstanceFields_parameters_type_erased() {
  39. BeanBinder<TestBean> otherBinder = new BeanBinder<>(TestBean.class);
  40. TestClass testClass = new TestClass();
  41. otherBinder.bindInstanceFields(testClass);
  42. }
  43. @Test
  44. public void fieldBound_bindBean_fieldValueUpdated() {
  45. binder.bind(nameField, "firstname");
  46. binder.setBean(item);
  47. assertEquals("Johannes", nameField.getValue());
  48. }
  49. @Test
  50. public void beanBound_bindField_fieldValueUpdated() {
  51. binder.setBean(item);
  52. binder.bind(nameField, "firstname");
  53. assertEquals("Johannes", nameField.getValue());
  54. }
  55. @Test(expected = IllegalArgumentException.class)
  56. public void bindInvalidPropertyName_throws() {
  57. binder.bind(nameField, "firstnaem");
  58. }
  59. @Test(expected = NullPointerException.class)
  60. public void bindNullPropertyName_throws() {
  61. binder.bind(nameField, null);
  62. }
  63. @Test(expected = IllegalArgumentException.class)
  64. public void bindNonReadableProperty_throws() {
  65. binder.bind(nameField, "writeOnlyProperty");
  66. }
  67. @Test
  68. public void beanBound_setValidFieldValue_propertyValueChanged() {
  69. binder.setBean(item);
  70. binder.bind(nameField, "firstname");
  71. nameField.setValue("Henri");
  72. assertEquals("Henri", item.getFirstname());
  73. }
  74. @Test
  75. public void readOnlyPropertyBound_setFieldValue_ignored() {
  76. binder.bind(nameField, "readOnlyProperty");
  77. binder.setBean(item);
  78. String propertyValue = item.getReadOnlyProperty();
  79. nameField.setValue("Foo");
  80. assertEquals(propertyValue, item.getReadOnlyProperty());
  81. }
  82. @Test
  83. public void beanBound_setInvalidFieldValue_validationError() {
  84. binder.setBean(item);
  85. binder.bind(nameField, "firstname");
  86. nameField.setValue("H"); // too short
  87. assertEquals("Johannes", item.getFirstname());
  88. assertInvalid(nameField, "size must be between 3 and 16");
  89. }
  90. @Test
  91. public void beanNotBound_setInvalidFieldValue_validationError() {
  92. binder.bind(nameField, "firstname");
  93. nameField.setValue("H"); // too short
  94. assertInvalid(nameField, "size must be between 3 and 16");
  95. }
  96. @Test
  97. public void explicitValidatorAdded_setInvalidFieldValue_explicitValidatorRunFirst() {
  98. binder.forField(nameField).withValidator(name -> name.startsWith("J"),
  99. "name must start with J").bind("firstname");
  100. nameField.setValue("A");
  101. assertInvalid(nameField, "name must start with J");
  102. }
  103. @Test
  104. public void explicitValidatorAdded_setInvalidFieldValue_beanValidatorRun() {
  105. binder.forField(nameField).withValidator(name -> name.startsWith("J"),
  106. "name must start with J").bind("firstname");
  107. nameField.setValue("J");
  108. assertInvalid(nameField, "size must be between 3 and 16");
  109. }
  110. @Test(expected = ClassCastException.class)
  111. public void fieldWithIncompatibleTypeBound_bindBean_throws() {
  112. binder.bind(ageField, "age");
  113. binder.setBean(item);
  114. }
  115. @Test(expected = ClassCastException.class)
  116. public void fieldWithIncompatibleTypeBound_loadBean_throws() {
  117. binder.bind(ageField, "age");
  118. binder.readBean(item);
  119. }
  120. @Test(expected = ClassCastException.class)
  121. public void fieldWithIncompatibleTypeBound_saveBean_throws()
  122. throws Throwable {
  123. try {
  124. binder.bind(ageField, "age");
  125. binder.writeBean(item);
  126. } catch (RuntimeException e) {
  127. throw e.getCause();
  128. }
  129. }
  130. @Test
  131. public void fieldWithConverterBound_bindBean_fieldValueUpdated() {
  132. binder.forField(ageField)
  133. .withConverter(Integer::valueOf, String::valueOf).bind("age");
  134. binder.setBean(item);
  135. assertEquals("32", ageField.getValue());
  136. }
  137. @Test(expected = ClassCastException.class)
  138. public void fieldWithInvalidConverterBound_bindBean_fieldValueUpdated() {
  139. binder.forField(ageField).withConverter(Float::valueOf, String::valueOf)
  140. .bind("age");
  141. binder.setBean(item);
  142. assertEquals("32", ageField.getValue());
  143. }
  144. @Test
  145. public void beanBinderWithBoxedType() {
  146. binder.forField(ageField)
  147. .withConverter(Integer::valueOf, String::valueOf).bind("age");
  148. binder.setBean(item);
  149. ageField.setValue(String.valueOf(20));
  150. assertEquals(20, item.getAge());
  151. }
  152. private void assertInvalid(HasValue<?> field, String message) {
  153. BinderValidationStatus<?> status = binder.validate();
  154. List<ValidationStatus<?>> errors = status.getFieldValidationErrors();
  155. assertEquals(1, errors.size());
  156. assertSame(field, errors.get(0).getField());
  157. assertEquals(message, errors.get(0).getMessage().get());
  158. }
  159. @Test
  160. public void beanBindingChainingMethods() {
  161. Method[] methods = BeanBindingBuilder.class.getMethods();
  162. for (int i = 0; i < methods.length; i++) {
  163. Method method = methods[i];
  164. try {
  165. Method actualMethod = BeanBindingBuilder.class.getMethod(
  166. method.getName(), method.getParameterTypes());
  167. Assert.assertNotSame(
  168. actualMethod + " should be overridden in "
  169. + BeanBindingBuilder.class
  170. + " with more specific return type ",
  171. BindingBuilder.class, actualMethod.getReturnType());
  172. } catch (NoSuchMethodException | SecurityException e) {
  173. throw new RuntimeException(e);
  174. }
  175. }
  176. }
  177. }