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.

BeanFieldGroupTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.vaadin.tests.server.component.fieldgroup;
  2. import static org.junit.Assert.assertEquals;
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5. import com.vaadin.data.Item;
  6. import com.vaadin.data.fieldgroup.BeanFieldGroup;
  7. import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
  8. import com.vaadin.data.fieldgroup.PropertyId;
  9. import com.vaadin.data.util.BeanItem;
  10. import com.vaadin.ui.RichTextArea;
  11. import com.vaadin.v7.ui.LegacyField;
  12. import com.vaadin.v7.ui.LegacyTextField;
  13. public class BeanFieldGroupTest {
  14. private static final String DEFAULT_FOR_BASIC_FIELD = "default";
  15. public static class MyBean {
  16. private String basicField = DEFAULT_FOR_BASIC_FIELD;
  17. private String anotherField;
  18. private MyNestedBean nestedBean = new MyNestedBean();
  19. public MyNestedBean getNestedBean() {
  20. return nestedBean;
  21. }
  22. /**
  23. * @return the basicField
  24. */
  25. public String getBasicField() {
  26. return basicField;
  27. }
  28. /**
  29. * @param basicField
  30. * the basicField to set
  31. */
  32. public void setBasicField(String basicField) {
  33. this.basicField = basicField;
  34. }
  35. /**
  36. * @return the anotherField
  37. */
  38. public String getAnotherField() {
  39. return anotherField;
  40. }
  41. /**
  42. * @param anotherField
  43. * the anotherField to set
  44. */
  45. public void setAnotherField(String anotherField) {
  46. this.anotherField = anotherField;
  47. }
  48. }
  49. public static class MyNestedBean {
  50. private String hello = "Hello world";
  51. public String getHello() {
  52. return hello;
  53. }
  54. }
  55. public static class ViewStub {
  56. LegacyTextField basicField = new LegacyTextField();
  57. @PropertyId("anotherField")
  58. LegacyTextField boundWithAnnotation = new LegacyTextField();
  59. }
  60. @SuppressWarnings("unchecked")
  61. @Test
  62. public void testStaticBindingHelper() {
  63. MyBean myBean = new MyBean();
  64. ViewStub viewStub = new ViewStub();
  65. BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
  66. .bindFieldsUnbuffered(myBean, viewStub);
  67. LegacyField<String> field = (LegacyField<String>) bindFields
  68. .getField("basicField");
  69. Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
  70. field.setValue("Foo");
  71. Assert.assertEquals("Foo", myBean.basicField);
  72. field = (LegacyField<String>) bindFields.getField("anotherField");
  73. field.setValue("Foo");
  74. Assert.assertEquals("Foo", myBean.anotherField);
  75. }
  76. @SuppressWarnings("unchecked")
  77. @Test
  78. public void testStaticBufferedBindingHelper() throws CommitException {
  79. MyBean myBean = new MyBean();
  80. ViewStub viewStub = new ViewStub();
  81. BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
  82. .bindFieldsBuffered(myBean, viewStub);
  83. LegacyField<String> basicField = (LegacyField<String>) bindFields
  84. .getField("basicField");
  85. basicField.setValue("Foo");
  86. Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
  87. LegacyField<String> anotherField = (LegacyField<String>) bindFields
  88. .getField("anotherField");
  89. anotherField.setValue("Foo");
  90. Assert.assertNull(myBean.anotherField);
  91. bindFields.commit();
  92. Assert.assertEquals("Foo", myBean.basicField);
  93. Assert.assertEquals("Foo", myBean.anotherField);
  94. }
  95. @Test
  96. public void buildAndBindNestedProperty() {
  97. MyBean bean = new MyBean();
  98. BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
  99. bfg.setItemDataSource(bean);
  100. com.vaadin.v7.ui.LegacyField<?> helloField = bfg
  101. .buildAndBind("Hello string", "nestedBean.hello");
  102. assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
  103. }
  104. @Test
  105. public void buildAndBindNestedRichTextAreaProperty() {
  106. MyBean bean = new MyBean();
  107. BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
  108. bfg.setItemDataSource(bean);
  109. RichTextArea helloField = bfg.buildAndBind("Hello string",
  110. "nestedBean.hello", RichTextArea.class);
  111. assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
  112. }
  113. @Test
  114. public void setDataSource_nullBean_nullBeanIsSetInDataSource() {
  115. BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
  116. group.setItemDataSource((MyBean) null);
  117. BeanItem<MyBean> dataSource = group.getItemDataSource();
  118. Assert.assertNull("Data source is null for null bean", dataSource);
  119. }
  120. @Test
  121. public void setDataSource_nullItem_nullDataSourceIsSet() {
  122. BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
  123. group.setItemDataSource((Item) null);
  124. BeanItem<MyBean> dataSource = group.getItemDataSource();
  125. Assert.assertNull("Group returns not null data source", dataSource);
  126. }
  127. }