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.

BinderBookOfVaadinTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.util.List;
  18. import org.junit.Assert;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import com.vaadin.data.Binder.Binding;
  22. import com.vaadin.data.util.converter.StringToIntegerConverter;
  23. import com.vaadin.data.validator.EmailValidator;
  24. import com.vaadin.server.AbstractErrorMessage;
  25. import com.vaadin.ui.AbstractField;
  26. import com.vaadin.ui.Slider;
  27. /**
  28. * Book of Vaadin tests.
  29. *
  30. * @author Vaadin Ltd
  31. *
  32. */
  33. public class BinderBookOfVaadinTest {
  34. static class TextField extends AbstractField<String> {
  35. String value = "";
  36. @Override
  37. public String getValue() {
  38. return value;
  39. }
  40. @Override
  41. protected void doSetValue(String value) {
  42. this.value = value;
  43. }
  44. }
  45. private static class BookPerson {
  46. private String lastName;
  47. private String email;
  48. private int yearOfBirth, salaryLevel;
  49. public String getLastName() {
  50. return lastName;
  51. }
  52. public void setLastName(String lastName) {
  53. this.lastName = lastName;
  54. }
  55. public BookPerson(int yearOfBirth, int salaryLevel) {
  56. this.yearOfBirth = yearOfBirth;
  57. this.salaryLevel = salaryLevel;
  58. }
  59. public int getYearOfBirth() {
  60. return yearOfBirth;
  61. }
  62. public void setYearOfBirth(int yearOfBirth) {
  63. this.yearOfBirth = yearOfBirth;
  64. }
  65. public int getSalaryLevel() {
  66. return salaryLevel;
  67. }
  68. public void setSalaryLevel(int salaryLevel) {
  69. this.salaryLevel = salaryLevel;
  70. }
  71. public String getEmail() {
  72. return email;
  73. }
  74. public void setEmail(String email) {
  75. this.email = email;
  76. }
  77. }
  78. private Binder<BookPerson> binder;
  79. private TextField field;
  80. @Before
  81. public void setUp() {
  82. binder = new Binder<>();
  83. field = new TextField();
  84. }
  85. @Test
  86. public void simpleEmailValidator() {
  87. binder.forField(field)
  88. // Explicit validator instance
  89. .withValidator(new EmailValidator(
  90. "This doesn't look like a valid email address"))
  91. .bind(BookPerson::getEmail, BookPerson::setEmail);
  92. field.setValue("not-email");
  93. List<ValidationError<?>> errors = binder.validate();
  94. Assert.assertEquals(1, errors.size());
  95. Assert.assertEquals("This doesn't look like a valid email address",
  96. errors.get(0).getMessage());
  97. Assert.assertEquals("This doesn't look like a valid email address",
  98. ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
  99. field.setValue("abc@vaadin.com");
  100. errors = binder.validate();
  101. Assert.assertEquals(0, errors.size());
  102. Assert.assertNull(field.getErrorMessage());
  103. }
  104. @Test
  105. public void nameLengthTest() {
  106. binder.forField(field)
  107. // Validator defined based on a lambda and an error message
  108. .withValidator(name -> name.length() >= 3,
  109. "Last name must contain at least three characters")
  110. .bind(BookPerson::getLastName, BookPerson::setLastName);
  111. field.setValue("a");
  112. List<ValidationError<?>> errors = binder.validate();
  113. Assert.assertEquals(1, errors.size());
  114. Assert.assertEquals("Last name must contain at least three characters",
  115. errors.get(0).getMessage());
  116. Assert.assertEquals("Last name must contain at least three characters",
  117. ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
  118. field.setValue("long last name");
  119. errors = binder.validate();
  120. Assert.assertEquals(0, errors.size());
  121. Assert.assertNull(field.getErrorMessage());
  122. }
  123. @Test
  124. public void chainedEmailValidator() {
  125. binder.forField(field)
  126. // Explicit validator instance
  127. .withValidator(new EmailValidator(
  128. "This doesn't look like a valid email address"))
  129. .withValidator(email -> email.endsWith("@acme.com"),
  130. "Only acme.com email addresses are allowed")
  131. .bind(BookPerson::getEmail, BookPerson::setEmail);
  132. field.setValue("not-email");
  133. List<ValidationError<?>> errors = binder.validate();
  134. // Only one error per field should be reported
  135. Assert.assertEquals(1, errors.size());
  136. Assert.assertEquals("This doesn't look like a valid email address",
  137. errors.get(0).getMessage());
  138. Assert.assertEquals("This doesn't look like a valid email address",
  139. ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
  140. field.setValue("abc@vaadin.com");
  141. errors = binder.validate();
  142. Assert.assertEquals(1, errors.size());
  143. Assert.assertEquals("Only acme.com email addresses are allowed",
  144. errors.get(0).getMessage());
  145. Assert.assertEquals("Only acme.com email addresses are allowed",
  146. ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
  147. field.setValue("abc@acme.com");
  148. errors = binder.validate();
  149. Assert.assertEquals(0, errors.size());
  150. Assert.assertNull(field.getErrorMessage());
  151. }
  152. @Test
  153. public void converterBookOfVaadinExample1() {
  154. TextField yearOfBirthField = new TextField();
  155. // Slider for integers between 1 and 10
  156. Slider salaryLevelField = new Slider("Salary level", 1, 10);
  157. Binding<BookPerson, String, String> b1 = binder
  158. .forField(yearOfBirthField);
  159. Binding<BookPerson, String, Integer> b2 = b1.withConverter(
  160. new StringToIntegerConverter("Must enter a number"));
  161. b2.bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
  162. Binding<BookPerson, Double, Double> salaryBinding1 = binder
  163. .forField(salaryLevelField);
  164. Binding<BookPerson, Double, Integer> salaryBinding2 = salaryBinding1
  165. .withConverter(Double::intValue, Integer::doubleValue);
  166. salaryBinding2.bind(BookPerson::getSalaryLevel,
  167. BookPerson::setSalaryLevel);
  168. // Test that the book code works
  169. BookPerson bookPerson = new BookPerson(1972, 4);
  170. binder.bind(bookPerson);
  171. Assert.assertEquals(4.0, salaryLevelField.getValue().doubleValue(), 0);
  172. Assert.assertEquals("1,972", yearOfBirthField.getValue());
  173. bookPerson.setSalaryLevel(8);
  174. binder.load(bookPerson);
  175. Assert.assertEquals(8.0, salaryLevelField.getValue().doubleValue(), 0);
  176. bookPerson.setYearOfBirth(123);
  177. binder.load(bookPerson);
  178. Assert.assertEquals("123", yearOfBirthField.getValue());
  179. yearOfBirthField.setValue("2016");
  180. salaryLevelField.setValue(1.0);
  181. Assert.assertEquals(2016, bookPerson.getYearOfBirth());
  182. Assert.assertEquals(1, bookPerson.getSalaryLevel());
  183. }
  184. @Test
  185. public void converterBookOfVaadinExample2() {
  186. TextField yearOfBirthField = new TextField();
  187. binder.forField(yearOfBirthField)
  188. .withConverter(Integer::valueOf, String::valueOf,
  189. // Text to use instead of the NumberFormatException
  190. // message
  191. "Please enter a number")
  192. .bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
  193. binder.bind(new BookPerson(1900, 5));
  194. yearOfBirthField.setValue("abc");
  195. binder.validate();
  196. Assert.assertEquals("Please&#32;enter&#32;a&#32;number",
  197. yearOfBirthField.getComponentError().getFormattedHtmlMessage());
  198. }
  199. }