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
|
package com.vaadin.data.util;
import java.util.Iterator;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.easymock.EasyMock;
import com.vaadin.data.Item.PropertySetChangeEvent;
import com.vaadin.data.Item.PropertySetChangeListener;
public class PropertySetItemTest extends TestCase {
private static final String ID1 = "id1";
private static final String ID2 = "id2";
private static final String ID3 = "id3";
private static final String VALUE1 = "value1";
private static final String VALUE2 = "value2";
private static final String VALUE3 = "value3";
private ObjectProperty<String> prop1;
private ObjectProperty<String> prop2;
private ObjectProperty<String> prop3;
private PropertySetChangeListener propertySetListenerMock;
private PropertySetChangeListener propertySetListenerMock2;
@Override
protected void setUp() throws Exception {
prop1 = new ObjectProperty<String>(VALUE1, String.class);
prop2 = new ObjectProperty<String>(VALUE2, String.class);
prop3 = new ObjectProperty<String>(VALUE3, String.class);
propertySetListenerMock = EasyMock
.createStrictMock(PropertySetChangeListener.class);
propertySetListenerMock2 = EasyMock
.createMock(PropertySetChangeListener.class);
}
@Override
protected void tearDown() throws Exception {
prop1 = null;
prop2 = null;
prop3 = null;
propertySetListenerMock = null;
propertySetListenerMock2 = null;
}
private PropertysetItem createPropertySetItem() {
return new PropertysetItem();
}
public void testEmptyItem() {
PropertysetItem item = createPropertySetItem();
Assert.assertNotNull(item.getItemPropertyIds());
Assert.assertEquals(0, item.getItemPropertyIds().size());
}
public void testGetProperty() {
PropertysetItem item = createPropertySetItem();
Assert.assertNull(item.getItemProperty(ID1));
item.addItemProperty(ID1, prop1);
Assert.assertEquals(prop1, item.getItemProperty(ID1));
Assert.assertNull(item.getItemProperty(ID2));
}
public void testAddSingleProperty() {
PropertysetItem item = createPropertySetItem();
item.addItemProperty(ID1, prop1);
Assert.assertEquals(1, item.getItemPropertyIds().size());
Object firstValue = item.getItemPropertyIds().iterator().next();
Assert.assertEquals(ID1, firstValue);
Assert.assertEquals(prop1, item.getItemProperty(ID1));
}
public void testAddMultipleProperties() {
PropertysetItem item = createPropertySetItem();
item.addItemProperty(ID1, prop1);
Assert.assertEquals(1, item.getItemPropertyIds().size());
Assert.assertEquals(prop1, item.getItemProperty(ID1));
item.addItemProperty(ID2, prop2);
Assert.assertEquals(2, item.getItemPropertyIds().size());
Assert.assertEquals(prop1, item.getItemProperty(ID1));
Assert.assertEquals(prop2, item.getItemProperty(ID2));
item.addItemProperty(ID3, prop3);
Assert.assertEquals(3, item.getItemPropertyIds().size());
}
public void testAddedPropertyOrder() {
PropertysetItem item = createPropertySetItem();
item.addItemProperty(ID1, prop1);
item.addItemProperty(ID2, prop2);
item.addItemProperty(ID3, prop3);
Iterator<?> it = item.getItemPropertyIds().iterator();
Assert.assertEquals(ID1, it.next());
Assert.assertEquals(ID2, it.next());
Assert.assertEquals(ID3, it.next());
}
public void testAddPropertyTwice() {
PropertysetItem item = createPropertySetItem();
Assert.assertTrue(item.addItemProperty(ID1, prop1));
Assert.assertFalse(item.addItemProperty(ID1, prop1));
Assert.assertEquals(1, item.getItemPropertyIds().size());
Assert.assertEquals(prop1, item.getItemProperty(ID1));
}
public void testCannotChangeProperty() {
PropertysetItem item = createPropertySetItem();
Assert.assertTrue(item.addItemProperty(ID1, prop1));
Assert.assertEquals(prop1, item.getItemProperty(ID1));
Assert.assertFalse(item.addItemProperty(ID1, prop2));
Assert.assertEquals(1, item.getItemPropertyIds().size());
Assert.assertEquals(prop1, item.getItemProperty(ID1));
}
public void testRemoveProperty() {
PropertysetItem item = createPropertySetItem();
item.addItemProperty(ID1, prop1);
item.removeItemProperty(ID1);
Assert.assertEquals(0, item.getItemPropertyIds().size());
Assert.assertNull(item.getItemProperty(ID1));
}
public void testRemovePropertyOrder() {
PropertysetItem item = createPropertySetItem();
item.addItemProperty(ID1, prop1);
item.addItemProperty(ID2, prop2);
item.addItemProperty(ID3, prop3);
item.removeItemProperty(ID2);
Iterator<?> it = item.getItemPropertyIds().iterator();
Assert.assertEquals(ID1, it.next());
Assert.assertEquals(ID3, it.next());
}
public void testRemoveNonExistentListener() {
PropertysetItem item = createPropertySetItem();
item.removeListener(propertySetListenerMock);
}
public void testRemoveListenerTwice() {
PropertysetItem item = createPropertySetItem();
item.addListener(propertySetListenerMock);
item.removeListener(propertySetListenerMock);
item.removeListener(propertySetListenerMock);
}
public void testAddPropertyNotification() {
// exactly one notification each time
PropertysetItem item = createPropertySetItem();
// Expectations and start test
propertySetListenerMock.itemPropertySetChange(EasyMock
.isA(PropertySetChangeEvent.class));
EasyMock.replay(propertySetListenerMock);
// Add listener and add a property -> should end up in listener once
item.addListener(propertySetListenerMock);
item.addItemProperty(ID1, prop1);
// Ensure listener was called once
EasyMock.verify(propertySetListenerMock);
// Remove the listener -> should not end up in listener when adding a
// property
item.removeListener(propertySetListenerMock);
item.addItemProperty(ID2, prop2);
// Ensure listener still has been called only once
EasyMock.verify(propertySetListenerMock);
}
public void testRemovePropertyNotification() {
// exactly one notification each time
PropertysetItem item = createPropertySetItem();
item.addItemProperty(ID1, prop1);
item.addItemProperty(ID2, prop2);
// Expectations and start test
propertySetListenerMock.itemPropertySetChange(EasyMock
.isA(PropertySetChangeEvent.class));
EasyMock.replay(propertySetListenerMock);
// Add listener and add a property -> should end up in listener once
item.addListener(propertySetListenerMock);
item.removeItemProperty(ID1);
// Ensure listener was called once
EasyMock.verify(propertySetListenerMock);
// Remove the listener -> should not end up in listener
item.removeListener(propertySetListenerMock);
item.removeItemProperty(ID2);
// Ensure listener still has been called only once
EasyMock.verify(propertySetListenerMock);
}
public void testItemEqualsNull() {
PropertysetItem item = createPropertySetItem();
Assert.assertFalse(item.equals(null));
}
public void testEmptyItemEquals() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
Assert.assertTrue(item1.equals(item2));
}
public void testItemEqualsSingleProperty() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
item2.addItemProperty(ID1, prop1);
PropertysetItem item3 = createPropertySetItem();
item3.addItemProperty(ID1, prop1);
PropertysetItem item4 = createPropertySetItem();
item4.addItemProperty(ID1, prop2);
PropertysetItem item5 = createPropertySetItem();
item5.addItemProperty(ID2, prop2);
Assert.assertFalse(item1.equals(item2));
Assert.assertFalse(item1.equals(item3));
Assert.assertFalse(item1.equals(item4));
Assert.assertFalse(item1.equals(item5));
Assert.assertTrue(item2.equals(item3));
Assert.assertFalse(item2.equals(item4));
Assert.assertFalse(item2.equals(item5));
Assert.assertFalse(item3.equals(item4));
Assert.assertFalse(item3.equals(item5));
Assert.assertFalse(item4.equals(item5));
Assert.assertFalse(item2.equals(item1));
}
public void testItemEqualsMultipleProperties() {
PropertysetItem item1 = createPropertySetItem();
item1.addItemProperty(ID1, prop1);
PropertysetItem item2 = createPropertySetItem();
item2.addItemProperty(ID1, prop1);
item2.addItemProperty(ID2, prop2);
PropertysetItem item3 = createPropertySetItem();
item3.addItemProperty(ID1, prop1);
item3.addItemProperty(ID2, prop2);
Assert.assertFalse(item1.equals(item2));
Assert.assertTrue(item2.equals(item3));
}
public void testItemEqualsPropertyOrder() {
PropertysetItem item1 = createPropertySetItem();
item1.addItemProperty(ID1, prop1);
item1.addItemProperty(ID2, prop2);
PropertysetItem item2 = createPropertySetItem();
item2.addItemProperty(ID2, prop2);
item2.addItemProperty(ID1, prop1);
Assert.assertFalse(item1.equals(item2));
}
public void testEqualsSingleListener() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
item1.addListener(propertySetListenerMock);
Assert.assertFalse(item1.equals(item2));
Assert.assertFalse(item2.equals(item1));
item2.addListener(propertySetListenerMock);
Assert.assertTrue(item1.equals(item2));
Assert.assertTrue(item2.equals(item1));
}
public void testEqualsMultipleListeners() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
item1.addListener(propertySetListenerMock);
item1.addListener(propertySetListenerMock2);
item2.addListener(propertySetListenerMock);
Assert.assertFalse(item1.equals(item2));
Assert.assertFalse(item2.equals(item1));
item2.addListener(propertySetListenerMock2);
Assert.assertTrue(item1.equals(item2));
Assert.assertTrue(item2.equals(item1));
}
public void testEqualsAddRemoveListener() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
item1.addListener(propertySetListenerMock);
item1.removeListener(propertySetListenerMock);
Assert.assertTrue(item1.equals(item2));
Assert.assertTrue(item2.equals(item1));
}
public void testItemHashCodeEmpty() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
Assert.assertEquals(item1.hashCode(), item2.hashCode());
}
public void testItemHashCodeAddProperties() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
Assert.assertEquals(item1.hashCode(), item2.hashCode());
item1.addItemProperty(ID1, prop1);
item1.addItemProperty(ID2, prop2);
// hashCodes can be equal even if items are different
item2.addItemProperty(ID1, prop1);
item2.addItemProperty(ID2, prop2);
// but here hashCodes must be equal
Assert.assertEquals(item1.hashCode(), item2.hashCode());
}
public void testItemHashCodeAddListeners() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
Assert.assertEquals(item1.hashCode(), item2.hashCode());
item1.addListener(propertySetListenerMock);
// hashCodes can be equal even if items are different
item2.addListener(propertySetListenerMock);
// but here hashCodes must be equal
Assert.assertEquals(item1.hashCode(), item2.hashCode());
}
public void testItemHashCodeAddRemoveProperty() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
item1.addItemProperty(ID1, prop1);
item1.removeItemProperty(ID1);
Assert.assertEquals(item1.hashCode(), item2.hashCode());
}
public void testItemHashCodeAddRemoveListener() {
PropertysetItem item1 = createPropertySetItem();
PropertysetItem item2 = createPropertySetItem();
item1.addListener(propertySetListenerMock);
item1.removeListener(propertySetListenerMock);
Assert.assertEquals(item1.hashCode(), item2.hashCode());
}
public void testToString() {
// toString() behavior is specified in the class javadoc
PropertysetItem item = createPropertySetItem();
Assert.assertEquals("", item.toString());
item.addItemProperty(ID1, prop1);
Assert.assertEquals(String.valueOf(prop1.getValue()), item.toString());
item.addItemProperty(ID2, prop2);
Assert.assertEquals(
String.valueOf(prop1.getValue()) + " "
+ String.valueOf(prop2.getValue()), item.toString());
}
}
|