1 package com.vaadin.v7.tests.server.component.fieldgroup;
3 import static org.junit.Assert.assertEquals;
5 import org.junit.Assert;
8 import com.vaadin.v7.data.Item;
9 import com.vaadin.v7.data.fieldgroup.BeanFieldGroup;
10 import com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException;
11 import com.vaadin.v7.data.fieldgroup.PropertyId;
12 import com.vaadin.v7.data.util.BeanItem;
13 import com.vaadin.v7.ui.Field;
14 import com.vaadin.v7.ui.RichTextArea;
15 import com.vaadin.v7.ui.TextField;
17 public class BeanFieldGroupTest {
19 private static final String DEFAULT_FOR_BASIC_FIELD = "default";
21 public static class MyBean {
23 private String basicField = DEFAULT_FOR_BASIC_FIELD;
25 private String anotherField;
27 private MyNestedBean nestedBean = new MyNestedBean();
29 public MyNestedBean getNestedBean() {
34 * @return the basicField
36 public String getBasicField() {
42 * the basicField to set
44 public void setBasicField(String basicField) {
45 this.basicField = basicField;
49 * @return the anotherField
51 public String getAnotherField() {
57 * the anotherField to set
59 public void setAnotherField(String anotherField) {
60 this.anotherField = anotherField;
64 public static class MyNestedBean {
66 private String hello = "Hello world";
68 public String getHello() {
73 public static class ViewStub {
75 TextField basicField = new TextField();
77 @PropertyId("anotherField")
78 TextField boundWithAnnotation = new TextField();
81 @SuppressWarnings("unchecked")
83 public void testStaticBindingHelper() {
84 MyBean myBean = new MyBean();
86 ViewStub viewStub = new ViewStub();
87 BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
88 .bindFieldsUnbuffered(myBean, viewStub);
90 Field<String> field = (Field<String>) bindFields.getField("basicField");
91 Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
92 field.setValue("Foo");
93 Assert.assertEquals("Foo", myBean.basicField);
95 field = (Field<String>) bindFields.getField("anotherField");
96 field.setValue("Foo");
97 Assert.assertEquals("Foo", myBean.anotherField);
100 @SuppressWarnings("unchecked")
102 public void testStaticBufferedBindingHelper() throws CommitException {
103 MyBean myBean = new MyBean();
105 ViewStub viewStub = new ViewStub();
106 BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
107 .bindFieldsBuffered(myBean, viewStub);
109 Field<String> basicField = (Field<String>) bindFields
110 .getField("basicField");
111 basicField.setValue("Foo");
112 Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
114 Field<String> anotherField = (Field<String>) bindFields
115 .getField("anotherField");
116 anotherField.setValue("Foo");
117 Assert.assertNull(myBean.anotherField);
121 Assert.assertEquals("Foo", myBean.basicField);
122 Assert.assertEquals("Foo", myBean.anotherField);
127 public void buildAndBindNestedProperty() {
129 MyBean bean = new MyBean();
131 BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
132 bfg.setItemDataSource(bean);
134 com.vaadin.v7.ui.Field<?> helloField = bfg.buildAndBind("Hello string",
136 assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
140 public void buildAndBindNestedRichTextAreaProperty() {
142 MyBean bean = new MyBean();
144 BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
145 bfg.setItemDataSource(bean);
147 RichTextArea helloField = bfg.buildAndBind("Hello string",
148 "nestedBean.hello", RichTextArea.class);
149 assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
153 public void setDataSource_nullBean_nullBeanIsSetInDataSource() {
154 BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
156 group.setItemDataSource((MyBean) null);
158 BeanItem<MyBean> dataSource = group.getItemDataSource();
159 Assert.assertNull("Data source is null for null bean", dataSource);
163 public void setDataSource_nullItem_nullDataSourceIsSet() {
164 BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
166 group.setItemDataSource((Item) null);
167 BeanItem<MyBean> dataSource = group.getItemDataSource();
168 Assert.assertNull("Group returns not null data source", dataSource);