]> source.dussan.org Git - vaadin-framework.git/blob
cae04a28f556bee42b6b0dd5f607932e38b2960c
[vaadin-framework.git] /
1 package com.vaadin.v7.tests.server.component.fieldgroup;
2
3 import static org.junit.Assert.assertEquals;
4
5 import org.junit.Assert;
6 import org.junit.Test;
7
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;
16
17 public class BeanFieldGroupTest {
18
19     private static final String DEFAULT_FOR_BASIC_FIELD = "default";
20
21     public static class MyBean {
22
23         private String basicField = DEFAULT_FOR_BASIC_FIELD;
24
25         private String anotherField;
26
27         private MyNestedBean nestedBean = new MyNestedBean();
28
29         public MyNestedBean getNestedBean() {
30             return nestedBean;
31         }
32
33         /**
34          * @return the basicField
35          */
36         public String getBasicField() {
37             return basicField;
38         }
39
40         /**
41          * @param basicField
42          *            the basicField to set
43          */
44         public void setBasicField(String basicField) {
45             this.basicField = basicField;
46         }
47
48         /**
49          * @return the anotherField
50          */
51         public String getAnotherField() {
52             return anotherField;
53         }
54
55         /**
56          * @param anotherField
57          *            the anotherField to set
58          */
59         public void setAnotherField(String anotherField) {
60             this.anotherField = anotherField;
61         }
62     }
63
64     public static class MyNestedBean {
65
66         private String hello = "Hello world";
67
68         public String getHello() {
69             return hello;
70         }
71     }
72
73     public static class ViewStub {
74
75         TextField basicField = new TextField();
76
77         @PropertyId("anotherField")
78         TextField boundWithAnnotation = new TextField();
79     }
80
81     @SuppressWarnings("unchecked")
82     @Test
83     public void testStaticBindingHelper() {
84         MyBean myBean = new MyBean();
85
86         ViewStub viewStub = new ViewStub();
87         BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
88                 .bindFieldsUnbuffered(myBean, viewStub);
89
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);
94
95         field = (Field<String>) bindFields.getField("anotherField");
96         field.setValue("Foo");
97         Assert.assertEquals("Foo", myBean.anotherField);
98     }
99
100     @SuppressWarnings("unchecked")
101     @Test
102     public void testStaticBufferedBindingHelper() throws CommitException {
103         MyBean myBean = new MyBean();
104
105         ViewStub viewStub = new ViewStub();
106         BeanFieldGroup<MyBean> bindFields = BeanFieldGroup
107                 .bindFieldsBuffered(myBean, viewStub);
108
109         Field<String> basicField = (Field<String>) bindFields
110                 .getField("basicField");
111         basicField.setValue("Foo");
112         Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField);
113
114         Field<String> anotherField = (Field<String>) bindFields
115                 .getField("anotherField");
116         anotherField.setValue("Foo");
117         Assert.assertNull(myBean.anotherField);
118
119         bindFields.commit();
120
121         Assert.assertEquals("Foo", myBean.basicField);
122         Assert.assertEquals("Foo", myBean.anotherField);
123
124     }
125
126     @Test
127     public void buildAndBindNestedProperty() {
128
129         MyBean bean = new MyBean();
130
131         BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
132         bfg.setItemDataSource(bean);
133
134         com.vaadin.v7.ui.Field<?> helloField = bfg.buildAndBind("Hello string",
135                 "nestedBean.hello");
136         assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
137     }
138
139     @Test
140     public void buildAndBindNestedRichTextAreaProperty() {
141
142         MyBean bean = new MyBean();
143
144         BeanFieldGroup<MyBean> bfg = new BeanFieldGroup<MyBean>(MyBean.class);
145         bfg.setItemDataSource(bean);
146
147         RichTextArea helloField = bfg.buildAndBind("Hello string",
148                 "nestedBean.hello", RichTextArea.class);
149         assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
150     }
151
152     @Test
153     public void setDataSource_nullBean_nullBeanIsSetInDataSource() {
154         BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
155
156         group.setItemDataSource((MyBean) null);
157
158         BeanItem<MyBean> dataSource = group.getItemDataSource();
159         Assert.assertNull("Data source is null for null bean", dataSource);
160     }
161
162     @Test
163     public void setDataSource_nullItem_nullDataSourceIsSet() {
164         BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
165
166         group.setItemDataSource((Item) null);
167         BeanItem<MyBean> dataSource = group.getItemDataSource();
168         Assert.assertNull("Group returns not null data source", dataSource);
169     }
170
171 }