Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BinderInstanceFieldTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.time.LocalDate;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import com.vaadin.annotations.PropertyId;
  21. import com.vaadin.data.converter.StringToIntegerConverter;
  22. import com.vaadin.data.validator.StringLengthValidator;
  23. import com.vaadin.tests.data.bean.Person;
  24. import com.vaadin.ui.AbstractField;
  25. import com.vaadin.ui.AbstractTextField;
  26. import com.vaadin.ui.DateField;
  27. import com.vaadin.ui.FormLayout;
  28. import com.vaadin.ui.TextField;
  29. public class BinderInstanceFieldTest {
  30. public static class BindAllFields extends FormLayout {
  31. private TextField firstName;
  32. private DateField birthDate;
  33. }
  34. public static class BindFieldsUsingAnnotation extends FormLayout {
  35. @PropertyId("firstName")
  36. private TextField nameField;
  37. @PropertyId("birthDate")
  38. private DateField birthDateField;
  39. }
  40. public static class BindOnlyOneField extends FormLayout {
  41. private TextField firstName;
  42. private TextField noFieldInPerson;
  43. }
  44. public static class BindNoHasValueField extends FormLayout {
  45. private String firstName;
  46. }
  47. public static class BindGenericField extends FormLayout {
  48. private CustomField<String> firstName;
  49. }
  50. public static class BindGenericWrongTypeParameterField extends FormLayout {
  51. private CustomField<Boolean> firstName;
  52. }
  53. public static class BindWrongTypeParameterField extends FormLayout {
  54. private IntegerTextField firstName;
  55. }
  56. public static class BindGeneric<T> extends FormLayout {
  57. private CustomField<T> firstName;
  58. }
  59. public static class BindRaw extends FormLayout {
  60. private CustomField firstName;
  61. }
  62. public static class BindAbstract extends FormLayout {
  63. private AbstractTextField firstName;
  64. }
  65. public static class BindNonInstantiatableType extends FormLayout {
  66. private NoDefaultCtor firstName;
  67. }
  68. public static class BindComplextHierarchyGenericType extends FormLayout {
  69. private ComplexHierarchy firstName;
  70. }
  71. public static class NoDefaultCtor extends TextField {
  72. public NoDefaultCtor(int arg) {
  73. }
  74. }
  75. public static class IntegerTextField extends CustomField<Integer> {
  76. }
  77. public static class ComplexHierarchy extends Generic<Long> {
  78. }
  79. public static class Generic<T> extends ComplexGeneric<Boolean, String, T> {
  80. }
  81. public static class ComplexGeneric<U, V, S> extends CustomField<V> {
  82. }
  83. public static class CustomField<T> extends AbstractField<T> {
  84. private T value;
  85. @Override
  86. public T getValue() {
  87. return value;
  88. }
  89. @Override
  90. protected void doSetValue(T value) {
  91. this.value = value;
  92. }
  93. }
  94. @Test
  95. public void bindInstanceFields_bindAllFields() {
  96. BindAllFields form = new BindAllFields();
  97. Binder<Person> binder = new Binder<>(Person.class);
  98. binder.bindInstanceFields(form);
  99. Person person = new Person();
  100. person.setFirstName("foo");
  101. person.setBirthDate(LocalDate.now());
  102. binder.setBean(person);
  103. Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
  104. Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
  105. form.firstName.setValue("bar");
  106. form.birthDate.setValue(person.getBirthDate().plusDays(345));
  107. Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
  108. Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
  109. }
  110. @Test(expected = IllegalStateException.class)
  111. public void bind_instanceFields_noArgsConstructor() {
  112. BindAllFields form = new BindAllFields();
  113. Binder<Person> binder = new Binder<>();
  114. binder.bindInstanceFields(form);
  115. }
  116. @Test
  117. public void bindInstanceFields_bindOnlyOneFields() {
  118. BindOnlyOneField form = new BindOnlyOneField();
  119. Binder<Person> binder = new Binder<>(Person.class);
  120. binder.bindInstanceFields(form);
  121. Person person = new Person();
  122. person.setFirstName("foo");
  123. binder.setBean(person);
  124. Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
  125. Assert.assertNull(form.noFieldInPerson);
  126. form.firstName.setValue("bar");
  127. Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
  128. }
  129. @Test
  130. public void bindInstanceFields_bindNotHasValueField_fieldIsNull() {
  131. BindNoHasValueField form = new BindNoHasValueField();
  132. Binder<Person> binder = new Binder<>(Person.class);
  133. binder.bindInstanceFields(form);
  134. Person person = new Person();
  135. person.setFirstName("foo");
  136. binder.setBean(person);
  137. Assert.assertNull(form.firstName);
  138. }
  139. @Test
  140. public void bindInstanceFields_genericField() {
  141. BindGenericField form = new BindGenericField();
  142. Binder<Person> binder = new Binder<>(Person.class);
  143. binder.bindInstanceFields(form);
  144. Person person = new Person();
  145. person.setFirstName("foo");
  146. binder.setBean(person);
  147. Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
  148. form.firstName.setValue("bar");
  149. Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
  150. }
  151. @Test(expected = IllegalStateException.class)
  152. public void bindInstanceFields_genericFieldWithWrongTypeParameter() {
  153. BindGenericWrongTypeParameterField form = new BindGenericWrongTypeParameterField();
  154. Binder<Person> binder = new Binder<>(Person.class);
  155. binder.bindInstanceFields(form);
  156. }
  157. @Test(expected = IllegalStateException.class)
  158. public void bindInstanceFields_generic() {
  159. BindGeneric<String> form = new BindGeneric<>();
  160. Binder<Person> binder = new Binder<>(Person.class);
  161. binder.bindInstanceFields(form);
  162. }
  163. @Test(expected = IllegalStateException.class)
  164. public void bindInstanceFields_rawFieldType() {
  165. BindRaw form = new BindRaw();
  166. Binder<Person> binder = new Binder<>(Person.class);
  167. binder.bindInstanceFields(form);
  168. }
  169. @Test(expected = IllegalStateException.class)
  170. public void bindInstanceFields_abstractFieldType() {
  171. BindAbstract form = new BindAbstract();
  172. Binder<Person> binder = new Binder<>(Person.class);
  173. binder.bindInstanceFields(form);
  174. }
  175. @Test(expected = IllegalStateException.class)
  176. public void bindInstanceFields_noInstantiatableFieldType() {
  177. BindNonInstantiatableType form = new BindNonInstantiatableType();
  178. Binder<Person> binder = new Binder<>(Person.class);
  179. binder.bindInstanceFields(form);
  180. }
  181. @Test(expected = IllegalStateException.class)
  182. public void bindInstanceFields_wrongFieldType() {
  183. BindWrongTypeParameterField form = new BindWrongTypeParameterField();
  184. Binder<Person> binder = new Binder<>(Person.class);
  185. binder.bindInstanceFields(form);
  186. }
  187. @Test
  188. public void bindInstanceFields_complexGenericHierarchy() {
  189. BindComplextHierarchyGenericType form = new BindComplextHierarchyGenericType();
  190. Binder<Person> binder = new Binder<>(Person.class);
  191. binder.bindInstanceFields(form);
  192. Person person = new Person();
  193. person.setFirstName("foo");
  194. binder.setBean(person);
  195. Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
  196. form.firstName.setValue("bar");
  197. Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
  198. }
  199. @Test
  200. public void bindInstanceFields_bindNotHasValueField_fieldIsNotReplaced() {
  201. BindNoHasValueField form = new BindNoHasValueField();
  202. Binder<Person> binder = new Binder<>(Person.class);
  203. String name = "foo";
  204. form.firstName = name;
  205. binder.bindInstanceFields(form);
  206. Person person = new Person();
  207. person.setFirstName("foo");
  208. binder.setBean(person);
  209. Assert.assertEquals(name, form.firstName);
  210. }
  211. @Test
  212. public void bindInstanceFields_bindAllFieldsUsingAnnotations() {
  213. BindFieldsUsingAnnotation form = new BindFieldsUsingAnnotation();
  214. Binder<Person> binder = new Binder<>(Person.class);
  215. binder.bindInstanceFields(form);
  216. Person person = new Person();
  217. person.setFirstName("foo");
  218. person.setBirthDate(LocalDate.now());
  219. binder.setBean(person);
  220. Assert.assertEquals(person.getFirstName(), form.nameField.getValue());
  221. Assert.assertEquals(person.getBirthDate(),
  222. form.birthDateField.getValue());
  223. form.nameField.setValue("bar");
  224. form.birthDateField.setValue(person.getBirthDate().plusDays(345));
  225. Assert.assertEquals(form.nameField.getValue(), person.getFirstName());
  226. Assert.assertEquals(form.birthDateField.getValue(),
  227. person.getBirthDate());
  228. }
  229. @Test
  230. public void bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced() {
  231. BindAllFields form = new BindAllFields();
  232. Binder<Person> binder = new Binder<>(Person.class);
  233. TextField name = new TextField();
  234. form.firstName = name;
  235. binder.forField(form.firstName)
  236. .withValidator(
  237. new StringLengthValidator("Name is invalid", 3, 10))
  238. .bind("firstName");
  239. binder.bindInstanceFields(form);
  240. Person person = new Person();
  241. String personName = "foo";
  242. person.setFirstName(personName);
  243. person.setBirthDate(LocalDate.now());
  244. binder.setBean(person);
  245. Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
  246. Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
  247. // the instance is not overridden
  248. Assert.assertEquals(name, form.firstName);
  249. form.firstName.setValue("aa");
  250. form.birthDate.setValue(person.getBirthDate().plusDays(345));
  251. Assert.assertEquals(personName, person.getFirstName());
  252. Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
  253. Assert.assertFalse(binder.validate().isOk());
  254. }
  255. @Test
  256. public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
  257. BindOnlyOneField form = new BindOnlyOneField();
  258. Binder<Person> binder = new Binder<>(Person.class);
  259. TextField name = new TextField();
  260. form.firstName = name;
  261. binder.forField(form.firstName)
  262. .withValidator(
  263. new StringLengthValidator("Name is invalid", 3, 10))
  264. .bind("firstName");
  265. TextField ageField = new TextField();
  266. form.noFieldInPerson = ageField;
  267. binder.forField(form.noFieldInPerson)
  268. .withConverter(new StringToIntegerConverter(""))
  269. .bind(Person::getAge, Person::setAge);
  270. binder.bindInstanceFields(form);
  271. Person person = new Person();
  272. String personName = "foo";
  273. int age = 11;
  274. person.setFirstName(personName);
  275. person.setAge(age);
  276. binder.setBean(person);
  277. Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
  278. Assert.assertEquals(String.valueOf(person.getAge()),
  279. form.noFieldInPerson.getValue());
  280. // the instances are not overridden
  281. Assert.assertEquals(name, form.firstName);
  282. Assert.assertEquals(ageField, form.noFieldInPerson);
  283. form.firstName.setValue("aa");
  284. age += 56;
  285. form.noFieldInPerson.setValue(String.valueOf(age));
  286. Assert.assertEquals(personName, person.getFirstName());
  287. Assert.assertEquals(form.noFieldInPerson.getValue(),
  288. String.valueOf(person.getAge()));
  289. Assert.assertFalse(binder.validate().isOk());
  290. }
  291. }