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.1KB

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