Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BeanFieldGroupTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.vaadin.v7.tests.server.component.fieldgroup;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNull;
  4. import org.junit.Test;
  5. import com.vaadin.annotations.PropertyId;
  6. import com.vaadin.v7.data.Item;
  7. import com.vaadin.v7.data.fieldgroup.BeanFieldGroup;
  8. import com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException;
  9. import com.vaadin.v7.data.util.BeanItem;
  10. import com.vaadin.v7.ui.Field;
  11. import com.vaadin.v7.ui.RichTextArea;
  12. import com.vaadin.v7.ui.TextField;
  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. TextField basicField = new TextField();
  57. @PropertyId("anotherField")
  58. TextField boundWithAnnotation = new TextField();
  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. Field<String> field = (Field<String>) bindFields.getField("basicField");
  68. assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
  69. field.setValue("Foo");
  70. assertEquals("Foo", myBean.basicField);
  71. field = (Field<String>) bindFields.getField("anotherField");
  72. field.setValue("Foo");
  73. assertEquals("Foo", myBean.anotherField);
  74. }
  75. @SuppressWarnings("unchecked")
  76. @Test
  77. public void testStaticBufferedBindingHelper() throws CommitException {
  78. MyBean myBean = new MyBean();
  79. ViewStub viewStub = new ViewStub();
  80. BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
  81. .bindFieldsBuffered(myBean, viewStub);
  82. Field<String> basicField = (Field<String>) bindFields
  83. .getField("basicField");
  84. basicField.setValue("Foo");
  85. assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
  86. Field<String> anotherField = (Field<String>) bindFields
  87. .getField("anotherField");
  88. anotherField.setValue("Foo");
  89. assertNull(myBean.anotherField);
  90. bindFields.commit();
  91. assertEquals("Foo", myBean.basicField);
  92. assertEquals("Foo", myBean.anotherField);
  93. }
  94. @Test
  95. public void buildAndBindNestedProperty() {
  96. MyBean bean = new MyBean();
  97. BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
  98. bfg.setItemDataSource(bean);
  99. Field<?> helloField = bfg.buildAndBind("Hello string",
  100. "nestedBean.hello");
  101. assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
  102. }
  103. @Test
  104. public void buildAndBindNestedRichTextAreaProperty() {
  105. MyBean bean = new MyBean();
  106. BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
  107. bfg.setItemDataSource(bean);
  108. RichTextArea helloField = bfg.buildAndBind("Hello string",
  109. "nestedBean.hello", RichTextArea.class);
  110. assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
  111. }
  112. @Test
  113. public void setDataSource_nullBean_nullBeanIsSetInDataSource() {
  114. BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
  115. group.setItemDataSource((MyBean) null);
  116. BeanItem<MyBean> dataSource = group.getItemDataSource();
  117. assertNull("Data source is null for null bean", dataSource);
  118. }
  119. @Test
  120. public void setDataSource_nullItem_nullDataSourceIsSet() {
  121. BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
  122. group.setItemDataSource((Item) null);
  123. BeanItem<MyBean> dataSource = group.getItemDataSource();
  124. assertNull("Group returns not null data source", dataSource);
  125. }
  126. }