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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
package com.vaadin.data.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import junit.framework.Assert;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver;
public class BeanContainerTest extends AbstractBeanContainerTest {
protected static class PersonNameResolver implements
BeanIdResolver<String, Person> {
@Override
public String getIdForBean(Person bean) {
return bean != null ? bean.getName() : null;
}
}
protected static class NullResolver implements
BeanIdResolver<String, Person> {
@Override
public String getIdForBean(Person bean) {
return null;
}
}
private Map<String, ClassName> nameToBean = new LinkedHashMap<String, ClassName>();
private BeanContainer<String, ClassName> getContainer() {
return new BeanContainer<String, ClassName>(ClassName.class);
}
@Override
public void setUp() {
nameToBean.clear();
for (int i = 0; i < sampleData.length; i++) {
ClassName className = new ClassName(sampleData[i], i);
nameToBean.put(sampleData[i], className);
}
}
@Override
@SuppressWarnings("unchecked")
protected void initializeContainer(Container container) {
BeanContainer<String, ClassName> beanItemContainer = (BeanContainer<String, ClassName>) container;
beanItemContainer.removeAllItems();
for (Entry<String, ClassName> entry : nameToBean.entrySet()) {
beanItemContainer.addItem(entry.getKey(), entry.getValue());
}
}
@Override
protected boolean isFilteredOutItemNull() {
return false;
}
public void testBasicOperations() {
testBasicContainerOperations(getContainer());
}
public void testFiltering() {
testContainerFiltering(getContainer());
}
public void testSorting() {
testContainerSorting(getContainer());
}
public void testSortingAndFiltering() {
testContainerSortingAndFiltering(getContainer());
}
// duplicated from parent class and modified - adding items to
// BeanContainer differs from other containers
public void testContainerOrdered() {
BeanContainer<String, String> container = new BeanContainer<String, String>(
String.class);
String id = "test1";
Item item = container.addItem(id, "value");
assertNotNull(item);
assertEquals(id, container.firstItemId());
assertEquals(id, container.lastItemId());
// isFirstId
assertTrue(container.isFirstId(id));
assertTrue(container.isFirstId(container.firstItemId()));
// isLastId
assertTrue(container.isLastId(id));
assertTrue(container.isLastId(container.lastItemId()));
// Add a new item before the first
// addItemAfter
String newFirstId = "newFirst";
item = container.addItemAfter(null, newFirstId, "newFirstValue");
assertNotNull(item);
assertNotNull(container.getItem(newFirstId));
// isFirstId
assertTrue(container.isFirstId(newFirstId));
assertTrue(container.isFirstId(container.firstItemId()));
// isLastId
assertTrue(container.isLastId(id));
assertTrue(container.isLastId(container.lastItemId()));
// nextItemId
assertEquals(id, container.nextItemId(newFirstId));
assertNull(container.nextItemId(id));
assertNull(container.nextItemId("not-in-container"));
// prevItemId
assertEquals(newFirstId, container.prevItemId(id));
assertNull(container.prevItemId(newFirstId));
assertNull(container.prevItemId("not-in-container"));
// addItemAfter(IDTYPE, IDTYPE, BT)
String newSecondItemId = "newSecond";
item = container.addItemAfter(newFirstId, newSecondItemId,
"newSecondValue");
// order is now: newFirstId, newSecondItemId, id
assertNotNull(item);
assertNotNull(container.getItem(newSecondItemId));
assertEquals(id, container.nextItemId(newSecondItemId));
assertEquals(newFirstId, container.prevItemId(newSecondItemId));
// addItemAfter(IDTYPE, IDTYPE, BT)
String fourthId = "id of the fourth item";
Item fourth = container.addItemAfter(newFirstId, fourthId,
"fourthValue");
// order is now: newFirstId, fourthId, newSecondItemId, id
assertNotNull(fourth);
assertEquals(fourth, container.getItem(fourthId));
assertEquals(newSecondItemId, container.nextItemId(fourthId));
assertEquals(newFirstId, container.prevItemId(fourthId));
// addItemAfter(IDTYPE, IDTYPE, BT)
String fifthId = "fifth";
Item fifth = container.addItemAfter(null, fifthId, "fifthValue");
// order is now: fifthId, newFirstId, fourthId, newSecondItemId, id
assertNotNull(fifth);
assertEquals(fifth, container.getItem(fifthId));
assertEquals(newFirstId, container.nextItemId(fifthId));
assertNull(container.prevItemId(fifthId));
}
// TODO test Container.Indexed interface operation - testContainerIndexed()?
public void testAddItemAt() {
BeanContainer<String, String> container = new BeanContainer<String, String>(
String.class);
container.addItem("id1", "value1");
// id1
container.addItemAt(0, "id2", "value2");
// id2, id1
container.addItemAt(1, "id3", "value3");
// id2, id3, id1
container.addItemAt(container.size(), "id4", "value4");
// id2, id3, id1, id4
assertNull(container.addItemAt(-1, "id5", "value5"));
assertNull(container.addItemAt(container.size() + 1, "id6", "value6"));
assertEquals(4, container.size());
assertEquals("id2", container.getIdByIndex(0));
assertEquals("id3", container.getIdByIndex(1));
assertEquals("id1", container.getIdByIndex(2));
assertEquals("id4", container.getIdByIndex(3));
}
public void testUnsupportedMethods() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
container.addItem("John", new Person("John"));
try {
container.addItem();
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
try {
container.addItem(null);
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
try {
container.addItemAfter(null, null);
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
try {
container.addItemAfter(new Person("Jane"));
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
try {
container.addItemAt(0);
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
try {
container.addItemAt(0, new Person("Jane"));
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
try {
container.addContainerProperty("lastName", String.class, "");
Assert.fail();
} catch (UnsupportedOperationException e) {
// should get exception
}
assertEquals(1, container.size());
}
public void testRemoveContainerProperty() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
container.setBeanIdResolver(new PersonNameResolver());
container.addBean(new Person("John"));
Assert.assertEquals("John",
container.getContainerProperty("John", "name").getValue());
Assert.assertTrue(container.removeContainerProperty("name"));
Assert.assertNull(container.getContainerProperty("John", "name"));
Assert.assertNotNull(container.getItem("John"));
// property removed also from item
Assert.assertNull(container.getItem("John").getItemProperty("name"));
}
public void testAddNullBeans() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
assertNull(container.addItem("id1", null));
assertNull(container.addItemAfter(null, "id2", null));
assertNull(container.addItemAt(0, "id3", null));
assertEquals(0, container.size());
}
public void testAddNullId() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
Person john = new Person("John");
assertNull(container.addItem(null, john));
assertNull(container.addItemAfter(null, null, john));
assertNull(container.addItemAt(0, null, john));
assertEquals(0, container.size());
}
public void testEmptyContainer() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
assertNull(container.firstItemId());
assertNull(container.lastItemId());
assertEquals(0, container.size());
// could test more about empty container
}
public void testAddBeanWithoutResolver() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
try {
container.addBean(new Person("John"));
Assert.fail();
} catch (IllegalStateException e) {
// should get exception
}
try {
container.addBeanAfter(null, new Person("Jane"));
Assert.fail();
} catch (IllegalStateException e) {
// should get exception
}
try {
container.addBeanAt(0, new Person("Jack"));
Assert.fail();
} catch (IllegalStateException e) {
// should get exception
}
try {
container
.addAll(Arrays.asList(new Person[] { new Person("Jack") }));
Assert.fail();
} catch (IllegalStateException e) {
// should get exception
}
assertEquals(0, container.size());
}
public void testAddAllWithNullItemId() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
// resolver that returns null as item id
container
.setBeanIdResolver(new BeanIdResolver<String, AbstractBeanContainerTest.Person>() {
@Override
public String getIdForBean(Person bean) {
return bean.getName();
}
});
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("John"));
persons.add(new Person("Marc"));
persons.add(new Person(null));
persons.add(new Person("foo"));
try {
container.addAll(persons);
fail();
} catch (IllegalArgumentException e) {
// should get exception
}
container.removeAllItems();
persons.remove(2);
container.addAll(persons);
assertEquals(3, container.size());
}
public void testAddBeanWithNullResolver() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
// resolver that returns null as item id
container.setBeanIdResolver(new NullResolver());
try {
container.addBean(new Person("John"));
Assert.fail();
} catch (IllegalArgumentException e) {
// should get exception
}
try {
container.addBeanAfter(null, new Person("Jane"));
Assert.fail();
} catch (IllegalArgumentException e) {
// should get exception
}
try {
container.addBeanAt(0, new Person("Jack"));
Assert.fail();
} catch (IllegalArgumentException e) {
// should get exception
}
assertEquals(0, container.size());
}
public void testAddBeanWithResolver() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
container.setBeanIdResolver(new PersonNameResolver());
assertNotNull(container.addBean(new Person("John")));
assertNotNull(container.addBeanAfter(null, new Person("Jane")));
assertNotNull(container.addBeanAt(0, new Person("Jack")));
container.addAll(Arrays.asList(new Person[] { new Person("Jill"),
new Person("Joe") }));
assertTrue(container.containsId("John"));
assertTrue(container.containsId("Jane"));
assertTrue(container.containsId("Jack"));
assertTrue(container.containsId("Jill"));
assertTrue(container.containsId("Joe"));
assertEquals(3, container.indexOfId("Jill"));
assertEquals(4, container.indexOfId("Joe"));
assertEquals(5, container.size());
}
public void testAddNullBeansWithResolver() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
container.setBeanIdResolver(new PersonNameResolver());
assertNull(container.addBean(null));
assertNull(container.addBeanAfter(null, null));
assertNull(container.addBeanAt(0, null));
assertEquals(0, container.size());
}
public void testAddBeanWithPropertyResolver() {
BeanContainer<String, Person> container = new BeanContainer<String, Person>(
Person.class);
container.setBeanIdProperty("name");
assertNotNull(container.addBean(new Person("John")));
assertNotNull(container.addBeanAfter(null, new Person("Jane")));
assertNotNull(container.addBeanAt(0, new Person("Jack")));
container.addAll(Arrays.asList(new Person[] { new Person("Jill"),
new Person("Joe") }));
assertTrue(container.containsId("John"));
assertTrue(container.containsId("Jane"));
assertTrue(container.containsId("Jack"));
assertTrue(container.containsId("Jill"));
assertTrue(container.containsId("Joe"));
assertEquals(3, container.indexOfId("Jill"));
assertEquals(4, container.indexOfId("Joe"));
assertEquals(5, container.size());
}
public void testAddNestedContainerProperty() {
BeanContainer<String, NestedMethodPropertyTest.Person> container = new BeanContainer<String, NestedMethodPropertyTest.Person>(
NestedMethodPropertyTest.Person.class);
container.setBeanIdProperty("name");
container.addBean(new NestedMethodPropertyTest.Person("John",
new NestedMethodPropertyTest.Address("Ruukinkatu 2-4", 20540)));
assertTrue(container.addNestedContainerProperty("address.street"));
assertEquals("Ruukinkatu 2-4",
container.getContainerProperty("John", "address.street")
.getValue());
}
}
|