1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
package com.vaadin.data.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
/**
* Test BeanItem specific features.
*
* Only public API is tested, not the methods with package visibility.
*
* See also {@link PropertySetItemTest}, which tests the base class.
*/
public class BeanItemTest extends TestCase {
@SuppressWarnings("unused")
protected static class MySuperClass {
private int superPrivate = 1;
private int superPrivate2 = 2;
protected double superProtected = 3.0;
private double superProtected2 = 4.0;
public boolean superPublic = true;
private boolean superPublic2 = true;
public int getSuperPrivate() {
return superPrivate;
}
public void setSuperPrivate(int superPrivate) {
this.superPrivate = superPrivate;
}
public double getSuperProtected() {
return superProtected;
}
public void setSuperProtected(double superProtected) {
this.superProtected = superProtected;
}
public boolean isSuperPublic() {
return superPublic;
}
public void setSuperPublic(boolean superPublic) {
this.superPublic = superPublic;
}
}
protected static class MyClass extends MySuperClass {
private String name;
public int value = 123;
public MyClass(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setNoField(String name) {
}
public String getNoField() {
return "no field backing this setter";
}
public String getName2() {
return name;
}
}
protected static class MyClass2 extends MyClass {
public MyClass2(String name) {
super(name);
}
@Override
public void setName(String name) {
super.setName(name + "2");
}
@Override
public String getName() {
return super.getName() + "2";
}
@Override
public String getName2() {
return super.getName();
}
public void setName2(String name) {
super.setName(name);
}
}
protected static interface MySuperInterface {
public int getSuper1();
public void setSuper1(int i);
public int getOverride();
}
protected static interface MySuperInterface2 {
public int getSuper2();
}
protected static interface MySubInterface extends MySuperInterface,
MySuperInterface2 {
public int getSub();
public void setSub(int i);
@Override
public int getOverride();
public void setOverride(int i);
}
public void testGetProperties() {
BeanItem<MySuperClass> item = new BeanItem<MySuperClass>(
new MySuperClass());
Collection<?> itemPropertyIds = item.getItemPropertyIds();
Assert.assertEquals(3, itemPropertyIds.size());
Assert.assertTrue(itemPropertyIds.contains("superPrivate"));
Assert.assertTrue(itemPropertyIds.contains("superProtected"));
Assert.assertTrue(itemPropertyIds.contains("superPublic"));
}
public void testGetSuperClassProperties() {
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"));
Collection<?> itemPropertyIds = item.getItemPropertyIds();
Assert.assertEquals(6, itemPropertyIds.size());
Assert.assertTrue(itemPropertyIds.contains("superPrivate"));
Assert.assertTrue(itemPropertyIds.contains("superProtected"));
Assert.assertTrue(itemPropertyIds.contains("superPublic"));
Assert.assertTrue(itemPropertyIds.contains("name"));
Assert.assertTrue(itemPropertyIds.contains("noField"));
Assert.assertTrue(itemPropertyIds.contains("name2"));
}
public void testOverridingProperties() {
BeanItem<MyClass2> item = new BeanItem<MyClass2>(new MyClass2("bean2"));
Collection<?> itemPropertyIds = item.getItemPropertyIds();
Assert.assertEquals(6, itemPropertyIds.size());
Assert.assertTrue(MyClass2.class.equals(item.getBean().getClass()));
// check that name2 accessed via MyClass2, not MyClass
Assert.assertFalse(item.getItemProperty("name2").isReadOnly());
}
public void testGetInterfaceProperties() throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
Method method = BeanItem.class.getDeclaredMethod(
"getPropertyDescriptors", Class.class);
method.setAccessible(true);
LinkedHashMap<String, VaadinPropertyDescriptor<Class>> propertyDescriptors = (LinkedHashMap<String, VaadinPropertyDescriptor<Class>>) method
.invoke(null, MySuperInterface.class);
Assert.assertEquals(2, propertyDescriptors.size());
Assert.assertTrue(propertyDescriptors.containsKey("super1"));
Assert.assertTrue(propertyDescriptors.containsKey("override"));
MethodProperty<?> property = (MethodProperty<?>) propertyDescriptors
.get("override").createProperty(getClass());
Assert.assertTrue(property.isReadOnly());
}
public void testGetSuperInterfaceProperties() throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
Method method = BeanItem.class.getDeclaredMethod(
"getPropertyDescriptors", Class.class);
method.setAccessible(true);
LinkedHashMap<String, VaadinPropertyDescriptor<Class>> propertyDescriptors = (LinkedHashMap<String, VaadinPropertyDescriptor<Class>>) method
.invoke(null, MySubInterface.class);
Assert.assertEquals(4, propertyDescriptors.size());
Assert.assertTrue(propertyDescriptors.containsKey("sub"));
Assert.assertTrue(propertyDescriptors.containsKey("super1"));
Assert.assertTrue(propertyDescriptors.containsKey("super2"));
Assert.assertTrue(propertyDescriptors.containsKey("override"));
MethodProperty<?> property = (MethodProperty<?>) propertyDescriptors
.get("override").createProperty(getClass());
Assert.assertFalse(property.isReadOnly());
}
public void testPropertyExplicitOrder() {
Collection<String> ids = new ArrayList<String>();
ids.add("name");
ids.add("superPublic");
ids.add("name2");
ids.add("noField");
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"),
ids);
Iterator<?> it = item.getItemPropertyIds().iterator();
Assert.assertEquals("name", it.next());
Assert.assertEquals("superPublic", it.next());
Assert.assertEquals("name2", it.next());
Assert.assertEquals("noField", it.next());
Assert.assertFalse(it.hasNext());
}
public void testPropertyExplicitOrder2() {
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"),
new String[] { "name", "superPublic", "name2", "noField" });
Iterator<?> it = item.getItemPropertyIds().iterator();
Assert.assertEquals("name", it.next());
Assert.assertEquals("superPublic", it.next());
Assert.assertEquals("name2", it.next());
Assert.assertEquals("noField", it.next());
Assert.assertFalse(it.hasNext());
}
public void testPropertyBadPropertyName() {
Collection<String> ids = new ArrayList<String>();
ids.add("name3");
ids.add("name");
// currently silently ignores non-existent properties
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"),
ids);
Iterator<?> it = item.getItemPropertyIds().iterator();
Assert.assertEquals("name", it.next());
Assert.assertFalse(it.hasNext());
}
public void testRemoveProperty() {
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"));
Collection<?> itemPropertyIds = item.getItemPropertyIds();
Assert.assertEquals(6, itemPropertyIds.size());
item.removeItemProperty("name2");
Assert.assertEquals(5, itemPropertyIds.size());
Assert.assertFalse(itemPropertyIds.contains("name2"));
}
public void testRemoveSuperProperty() {
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"));
Collection<?> itemPropertyIds = item.getItemPropertyIds();
Assert.assertEquals(6, itemPropertyIds.size());
item.removeItemProperty("superPrivate");
Assert.assertEquals(5, itemPropertyIds.size());
Assert.assertFalse(itemPropertyIds.contains("superPrivate"));
}
public void testPropertyTypes() {
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"));
Assert.assertTrue(Integer.class.equals(item.getItemProperty(
"superPrivate").getType()));
Assert.assertTrue(Double.class.equals(item.getItemProperty(
"superProtected").getType()));
Assert.assertTrue(Boolean.class.equals(item.getItemProperty(
"superPublic").getType()));
Assert.assertTrue(String.class.equals(item.getItemProperty("name")
.getType()));
}
public void testPropertyReadOnly() {
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"));
Assert.assertFalse(item.getItemProperty("name").isReadOnly());
Assert.assertTrue(item.getItemProperty("name2").isReadOnly());
}
public void testCustomProperties() throws Exception {
LinkedHashMap<String, VaadinPropertyDescriptor<MyClass>> propertyDescriptors = new LinkedHashMap<String, VaadinPropertyDescriptor<MyClass>>();
propertyDescriptors.put(
"myname",
new MethodPropertyDescriptor<BeanItemTest.MyClass>("myname",
MyClass.class, MyClass.class
.getDeclaredMethod("getName"), MyClass.class
.getDeclaredMethod("setName", String.class)));
MyClass instance = new MyClass("bean1");
Constructor<BeanItem> constructor = BeanItem.class
.getDeclaredConstructor(Object.class, Map.class);
constructor.setAccessible(true);
BeanItem<MyClass> item = constructor.newInstance(instance,
propertyDescriptors);
Assert.assertEquals(1, item.getItemPropertyIds().size());
Assert.assertEquals("bean1", item.getItemProperty("myname").getValue());
}
public void testAddRemoveProperty() throws Exception {
MethodPropertyDescriptor<BeanItemTest.MyClass> pd = new MethodPropertyDescriptor<BeanItemTest.MyClass>(
"myname", MyClass.class,
MyClass.class.getDeclaredMethod("getName"),
MyClass.class.getDeclaredMethod("setName", String.class));
BeanItem<MyClass> item = new BeanItem<MyClass>(new MyClass("bean1"));
Assert.assertEquals(6, item.getItemPropertyIds().size());
Assert.assertEquals(null, item.getItemProperty("myname"));
item.addItemProperty("myname", pd.createProperty(item.getBean()));
Assert.assertEquals(7, item.getItemPropertyIds().size());
Assert.assertEquals("bean1", item.getItemProperty("myname").getValue());
item.removeItemProperty("myname");
Assert.assertEquals(6, item.getItemPropertyIds().size());
Assert.assertEquals(null, item.getItemProperty("myname"));
}
}
|