1 package org.djutils.event;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotEquals;
6 import static org.junit.Assert.assertNotNull;
7 import static org.junit.Assert.assertNull;
8 import static org.junit.Assert.assertTrue;
9 import static org.junit.Assert.fail;
10
11 import java.rmi.RemoteException;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.LinkedHashMap;
17 import java.util.LinkedHashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.NoSuchElementException;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
24
25 import org.djutils.event.util.EventProducingCollection;
26 import org.djutils.event.util.EventProducingIterator;
27 import org.djutils.event.util.EventProducingList;
28 import org.djutils.event.util.EventProducingListIterator;
29 import org.djutils.event.util.EventProducingMap;
30 import org.djutils.event.util.EventProducingSet;
31 import org.djutils.exceptions.Try;
32 import org.junit.Test;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class EventUtilTest
47 {
48
49
50
51 @Test
52 public void testEventProducingCollection()
53 {
54 EventProducingCollection<String> epc = new EventProducingCollection<>(new LinkedHashSet<>(), "epc");
55 TestEventListener listener = new TestEventListener();
56 epc.addListener(listener, EventProducingCollection.OBJECT_ADDED_EVENT);
57 epc.addListener(listener, EventProducingCollection.OBJECT_REMOVED_EVENT);
58 epc.addListener(listener, EventProducingCollection.OBJECT_CHANGED_EVENT);
59
60
61 listener.setExpectingNotification(true);
62 assertTrue(epc.isEmpty());
63 boolean ok = epc.add("abc");
64 assertTrue(ok);
65 assertEquals("epc", listener.getReceivedEvent().getSourceId());
66 assertEquals(EventProducingCollection.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
67 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
68 assertFalse(epc.isEmpty());
69 ok = epc.add("abc");
70 assertFalse(ok);
71 assertEquals("epc", listener.getReceivedEvent().getSourceId());
72 assertEquals(EventProducingCollection.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
73 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
74
75
76 ok = epc.remove("abc");
77 assertTrue(ok);
78 assertEquals(EventProducingCollection.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
79 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
80 assertTrue(epc.isEmpty());
81 listener.setExpectingNotification(false);
82 ok = epc.remove("def");
83 assertFalse(ok);
84 listener.setExpectingNotification(true);
85
86
87 ok = epc.addAll(Arrays.asList("a", "b", "c", "d", "e"));
88 assertTrue(ok);
89 assertEquals(EventProducingCollection.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
90 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
91 assertEquals(5, epc.size());
92 ok = epc.addAll(Arrays.asList("b", "e"));
93 assertFalse(ok);
94 assertEquals(EventProducingCollection.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
95 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
96 listener.setExpectingNotification(false);
97 ok = epc.addAll(Arrays.asList());
98 assertFalse(ok);
99 listener.setExpectingNotification(true);
100
101
102 epc.removeAll(Arrays.asList("b", "c"));
103 assertEquals(EventProducingCollection.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
104 assertEquals(Integer.valueOf(3), listener.getReceivedEvent().getContent());
105 listener.setExpectingNotification(false);
106 epc.removeAll(Arrays.asList());
107 listener.setExpectingNotification(true);
108
109
110 epc.retainAll(Arrays.asList("c", "d", "e"));
111 assertEquals(EventProducingCollection.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
112 assertEquals(Integer.valueOf(2), listener.getReceivedEvent().getContent());
113 listener.setExpectingNotification(false);
114 epc.retainAll(Arrays.asList("d", "e"));
115 listener.setExpectingNotification(true);
116
117
118 assertTrue(epc.contains("d"));
119 assertFalse(epc.contains("a"));
120 assertTrue(epc.containsAll(Arrays.asList("d", "e")));
121
122
123 Object[] arr = epc.toArray();
124 String[] stringArr = epc.toArray(new String[] {});
125 assertEquals(2, arr.length);
126 assertTrue(arr[0].equals("d") || arr[0].equals("e"));
127 assertTrue(arr[1].equals("d") || arr[1].equals("e"));
128 assertNotEquals(arr[0], arr[1]);
129 assertEquals(2, stringArr.length);
130 assertTrue(stringArr[0].equals("d") || stringArr[0].equals("e"));
131 assertTrue(stringArr[1].equals("d") || stringArr[1].equals("e"));
132 assertNotEquals(stringArr[0], stringArr[1]);
133
134
135 epc.clear();
136 assertEquals(EventProducingCollection.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
137 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
138 listener.setExpectingNotification(false);
139 epc.clear();
140 listener.setExpectingNotification(true);
141
142
143 ok = epc.addAll(Arrays.asList("a", "b", "c", "d", "e"));
144 assertTrue(ok);
145 assertEquals(EventProducingCollection.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
146 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
147 assertEquals(5, epc.size());
148 EventProducingIterator<String> eit = epc.iterator();
149 assertNotNull(eit);
150 assertTrue(eit.hasNext());
151 String firstString = eit.next();
152 assertTrue(eit.hasNext());
153 String secondString = eit.next();
154 eit.remove();
155 assertEquals(EventProducingCollection.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
156 assertEquals(Integer.valueOf(4), listener.getReceivedEvent().getContent());
157 assertEquals(4, epc.size());
158 assertTrue(epc.contains(firstString));
159 assertFalse(epc.contains(secondString));
160
161
162 epc.removeAllListeners();
163 epc.clear();
164 }
165
166
167
168
169 @Test
170 public void testEventProducingSet()
171 {
172 EventProducingSet<String> eps = new EventProducingSet<>(new LinkedHashSet<>(), "eps");
173 TestEventListener listener = new TestEventListener();
174 eps.addListener(listener, EventProducingSet.OBJECT_ADDED_EVENT);
175 eps.addListener(listener, EventProducingSet.OBJECT_REMOVED_EVENT);
176 eps.addListener(listener, EventProducingSet.OBJECT_CHANGED_EVENT);
177
178
179 listener.setExpectingNotification(true);
180 assertTrue(eps.isEmpty());
181 boolean ok = eps.add("abc");
182 assertTrue(ok);
183 assertEquals("eps", listener.getReceivedEvent().getSourceId());
184 assertEquals(EventProducingSet.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
185 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
186 assertFalse(eps.isEmpty());
187 ok = eps.add("abc");
188 assertFalse(ok);
189 assertEquals("eps", listener.getReceivedEvent().getSourceId());
190 assertEquals(EventProducingSet.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
191 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
192
193
194 ok = eps.remove("abc");
195 assertTrue(ok);
196 assertEquals(EventProducingSet.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
197 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
198 assertTrue(eps.isEmpty());
199 listener.setExpectingNotification(false);
200 ok = eps.remove("def");
201 assertFalse(ok);
202 listener.setExpectingNotification(true);
203
204
205 ok = eps.addAll(Arrays.asList("a", "b", "c", "d", "e"));
206 assertTrue(ok);
207 assertEquals(EventProducingSet.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
208 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
209 assertEquals(5, eps.size());
210 ok = eps.addAll(Arrays.asList("b", "e"));
211 assertFalse(ok);
212 assertEquals(EventProducingSet.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
213 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
214 listener.setExpectingNotification(false);
215 ok = eps.addAll(Arrays.asList());
216 assertFalse(ok);
217 listener.setExpectingNotification(true);
218
219
220 eps.removeAll(Arrays.asList("b", "c"));
221 assertEquals(EventProducingSet.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
222 assertEquals(Integer.valueOf(3), listener.getReceivedEvent().getContent());
223 listener.setExpectingNotification(false);
224 eps.removeAll(Arrays.asList());
225 listener.setExpectingNotification(true);
226
227
228 eps.retainAll(Arrays.asList("c", "d", "e"));
229 assertEquals(EventProducingSet.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
230 assertEquals(Integer.valueOf(2), listener.getReceivedEvent().getContent());
231 listener.setExpectingNotification(false);
232 eps.retainAll(Arrays.asList("d", "e"));
233 listener.setExpectingNotification(true);
234
235
236 assertTrue(eps.contains("d"));
237 assertFalse(eps.contains("a"));
238 assertTrue(eps.containsAll(Arrays.asList("d", "e")));
239
240
241 Object[] arr = eps.toArray();
242 String[] stringArr = eps.toArray(new String[] {});
243 assertEquals(2, arr.length);
244 assertTrue(arr[0].equals("d") || arr[0].equals("e"));
245 assertTrue(arr[1].equals("d") || arr[1].equals("e"));
246 assertNotEquals(arr[0], arr[1]);
247 assertEquals(2, stringArr.length);
248 assertTrue(stringArr[0].equals("d") || stringArr[0].equals("e"));
249 assertTrue(stringArr[1].equals("d") || stringArr[1].equals("e"));
250 assertNotEquals(stringArr[0], stringArr[1]);
251
252
253 eps.clear();
254 assertEquals(EventProducingSet.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
255 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
256 listener.setExpectingNotification(false);
257 eps.clear();
258 listener.setExpectingNotification(true);
259
260
261 ok = eps.addAll(Arrays.asList("a", "b", "c", "d", "e"));
262 assertTrue(ok);
263 assertEquals(EventProducingSet.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
264 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
265 assertEquals(5, eps.size());
266 EventProducingIterator<String> eit = eps.iterator();
267 assertNotNull(eit);
268 assertTrue(eit.hasNext());
269 String firstString = eit.next();
270 assertTrue(eit.hasNext());
271 String secondString = eit.next();
272 eit.remove();
273 assertEquals(EventProducingSet.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
274 assertEquals(Integer.valueOf(4), listener.getReceivedEvent().getContent());
275 assertEquals(4, eps.size());
276 assertTrue(eps.contains(firstString));
277 assertFalse(eps.contains(secondString));
278
279
280 eps.removeAllListeners();
281 eps.clear();
282 }
283
284
285
286
287 @Test
288 public void testEventProducingList()
289 {
290 EventProducingList<String> epl = new EventProducingList<>(new ArrayList<>(), "epl");
291 TestEventListener listener = new TestEventListener();
292 epl.addListener(listener, EventProducingList.OBJECT_ADDED_EVENT);
293 epl.addListener(listener, EventProducingList.OBJECT_REMOVED_EVENT);
294 epl.addListener(listener, EventProducingList.OBJECT_CHANGED_EVENT);
295
296
297 listener.setExpectingNotification(true);
298 assertTrue(epl.isEmpty());
299 assertEquals(0, epl.size());
300 boolean ok = epl.add("abc");
301 assertTrue(ok);
302 assertEquals("epl", listener.getReceivedEvent().getSourceId());
303 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
304 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
305 assertFalse(epl.isEmpty());
306 ok = epl.add("abc");
307 assertTrue(ok);
308 assertEquals("epl", listener.getReceivedEvent().getSourceId());
309 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
310 assertEquals(Integer.valueOf(2), listener.getReceivedEvent().getContent());
311 epl.remove(1);
312 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
313 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
314 assertEquals(1, epl.size());
315
316
317 ok = epl.remove("abc");
318 assertTrue(ok);
319 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
320 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
321 assertTrue(epl.isEmpty());
322 listener.setExpectingNotification(false);
323 ok = epl.remove("def");
324 assertFalse(ok);
325 listener.setExpectingNotification(true);
326
327
328 ok = epl.addAll(Arrays.asList("a", "e"));
329 assertTrue(ok);
330 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
331 assertEquals(Integer.valueOf(2), listener.getReceivedEvent().getContent());
332 assertEquals(2, epl.size());
333 ok = epl.addAll(1, Arrays.asList("b", "c", "d"));
334 assertTrue(ok);
335 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
336 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
337 assertEquals(5, epl.size());
338 assertEquals(2, epl.indexOf("c"));
339 String old = epl.set(0, "aa");
340 assertEquals("a", old);
341 assertEquals(EventProducingList.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
342 listener.setExpectingNotification(false);
343 ok = epl.addAll(Arrays.asList());
344 assertFalse(ok);
345 listener.setExpectingNotification(true);
346 epl.add(1, "z");
347 assertEquals(6, epl.size());
348 assertEquals(3, epl.indexOf("c"));
349 assertEquals(1, epl.indexOf("z"));
350 epl.add("z");
351 assertEquals(7, epl.size());
352 assertEquals(3, epl.indexOf("c"));
353 assertEquals(6, epl.lastIndexOf("z"));
354 listener.setExpectingNotification(false);
355 epl.addAll(2, Arrays.asList());
356 listener.setExpectingNotification(true);
357
358
359 List<String> subList = epl.subList(2, 5);
360 assertEquals(Arrays.asList("b", "c", "d"), subList);
361
362
363 epl.removeAll(Arrays.asList("b", "c", "z"));
364 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
365 assertEquals(Integer.valueOf(3), listener.getReceivedEvent().getContent());
366 listener.setExpectingNotification(false);
367 epl.removeAll(Arrays.asList());
368 listener.setExpectingNotification(true);
369
370
371 epl.retainAll(Arrays.asList("c", "d", "e"));
372 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
373 assertEquals(Integer.valueOf(2), listener.getReceivedEvent().getContent());
374 listener.setExpectingNotification(false);
375 epl.retainAll(Arrays.asList("d", "e"));
376 listener.setExpectingNotification(true);
377
378
379 assertTrue(epl.contains("d"));
380 assertFalse(epl.contains("a"));
381 assertTrue(epl.containsAll(Arrays.asList("d", "e")));
382 assertEquals("d", epl.get(0));
383 assertEquals("e", epl.get(1));
384
385
386 Object[] arr = epl.toArray();
387 String[] stringArr = epl.toArray(new String[] {});
388 assertEquals(2, arr.length);
389 assertTrue(arr[0].equals("d") && arr[1].equals("e"));
390 assertEquals(2, stringArr.length);
391 assertTrue(stringArr[0].equals("d") && stringArr[1].equals("e"));
392
393
394 epl.clear();
395 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
396 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
397 listener.setExpectingNotification(false);
398 epl.clear();
399 listener.setExpectingNotification(true);
400
401
402 ok = epl.addAll(Arrays.asList("a", "b", "c", "d", "e"));
403 assertTrue(ok);
404 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
405 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
406 assertEquals(5, epl.size());
407 EventProducingIterator<String> eit = epl.iterator();
408 assertNotNull(eit);
409 assertTrue(eit.hasNext());
410 String firstString = eit.next();
411 assertTrue(eit.hasNext());
412 String secondString = eit.next();
413 eit.remove();
414 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
415 assertEquals(Integer.valueOf(4), listener.getReceivedEvent().getContent());
416 assertEquals(4, epl.size());
417 assertTrue(epl.contains(firstString));
418 assertFalse(epl.contains(secondString));
419 epl.clear();
420
421
422 ok = epl.addAll(Arrays.asList("a", "b", "c", "d", "e"));
423 assertTrue(ok);
424 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
425 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
426 assertEquals(5, epl.size());
427 EventProducingListIterator<String> leit = epl.listIterator();
428 assertNotNull(leit);
429 assertTrue(leit.hasNext());
430 assertFalse(leit.hasPrevious());
431 assertEquals(-1, leit.previousIndex());
432 assertEquals(0, leit.nextIndex());
433 Try.testFail(new Try.Execution()
434 {
435 @Override
436 public void execute() throws Throwable
437 {
438 System.out.println(leit.previous());
439 }
440 }, "call to previous() should have caused exception", NoSuchElementException.class);
441 firstString = leit.next();
442 assertEquals("a", firstString);
443 assertTrue(leit.hasNext());
444 secondString = leit.next();
445 assertEquals("b", secondString);
446 assertEquals(1, leit.previousIndex());
447 assertEquals(2, leit.nextIndex());
448 leit.remove();
449 assertEquals(EventProducingList.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
450 assertEquals(Integer.valueOf(4), listener.getReceivedEvent().getContent());
451 assertEquals(4, epl.size());
452 assertTrue(epl.contains(firstString));
453 assertFalse(epl.contains(secondString));
454 assertEquals(0, leit.previousIndex());
455 assertEquals(1, leit.nextIndex());
456 String thirdString = leit.next();
457 assertEquals("c", thirdString);
458 leit.set("cc");
459 assertEquals(EventProducingList.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
460 assertEquals(4, epl.size());
461 assertEquals(Arrays.asList("a", "cc", "d", "e"), Arrays.asList(epl.toArray()));
462 leit.add("dd");
463 assertEquals(EventProducingList.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
464 assertEquals(5, epl.size());
465 assertEquals(Arrays.asList("a", "cc", "dd", "d", "e"), Arrays.asList(epl.toArray()));
466 assertEquals("dd", leit.previous());
467 assertEquals("dd", leit.next());
468 assertEquals("d", leit.next());
469 assertEquals("e", leit.next());
470 Try.testFail(new Try.Execution()
471 {
472 @Override
473 public void execute() throws Throwable
474 {
475 System.out.println(leit.next());
476 }
477 }, "call to next() should have caused exception", NoSuchElementException.class);
478 epl.clear();
479
480
481 epl.removeAllListeners();
482 epl.clear();
483 }
484
485
486
487
488 @Test
489 public void testEventProducingMap()
490 {
491 EventProducingMap<Integer, String> epm = new EventProducingMap<>(new TreeMap<>(), "epm");
492 TestEventListener listener = new TestEventListener();
493 epm.addListener(listener, EventProducingMap.OBJECT_ADDED_EVENT);
494 epm.addListener(listener, EventProducingMap.OBJECT_REMOVED_EVENT);
495 epm.addListener(listener, EventProducingMap.OBJECT_CHANGED_EVENT);
496
497
498 listener.setExpectingNotification(true);
499 assertTrue(epm.isEmpty());
500 assertEquals(0, epm.size());
501 String replaced = epm.put(1, "abc");
502 assertNull(replaced);
503 assertEquals("epm", listener.getReceivedEvent().getSourceId());
504 assertEquals(EventProducingMap.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
505 assertEquals(Integer.valueOf(1), listener.getReceivedEvent().getContent());
506 assertFalse(epm.isEmpty());
507 assertEquals(1, epm.size());
508 replaced = epm.put(1, "def");
509 assertEquals("abc", replaced);
510 assertEquals("epm", listener.getReceivedEvent().getSourceId());
511 assertEquals(EventProducingMap.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
512
513 assertEquals("payload is now the unchanged size of the map", 1, listener.getReceivedEvent().getContent());
514 assertNull(epm.get(2));
515 assertEquals("def", epm.get(1));
516 assertEquals(1, epm.size());
517 assertTrue(epm.containsKey(1));
518 assertFalse(epm.containsKey(2));
519 assertTrue(epm.containsValue("def"));
520 assertFalse(epm.containsValue("abc"));
521
522
523 String removed = epm.remove(123);
524 assertNull(removed);
525 removed = epm.remove(1);
526 assertEquals("def", removed);
527 assertEquals(EventProducingMap.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
528 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
529 assertTrue(epm.isEmpty());
530 listener.setExpectingNotification(false);
531 removed = epm.remove(234);
532 assertNull(removed);
533 listener.setExpectingNotification(true);
534 epm.clear();
535
536
537 Map<Integer, String> addMap = new LinkedHashMap<>();
538 addMap.put(1, "a");
539 addMap.put(2, "b");
540 addMap.put(3, "c");
541 addMap.put(4, "d");
542 addMap.put(5, "e");
543 epm.putAll(addMap);
544 assertEquals(EventProducingMap.OBJECT_ADDED_EVENT, listener.getReceivedEvent().getType());
545 assertEquals(Integer.valueOf(5), listener.getReceivedEvent().getContent());
546 assertEquals(5, epm.size());
547 epm.putAll(addMap);
548 assertEquals(EventProducingMap.OBJECT_CHANGED_EVENT, listener.getReceivedEvent().getType());
549
550 assertEquals("payload is now the unchanged size of the map", 5, listener.getReceivedEvent().getContent());
551 listener.setExpectingNotification(false);
552 epm.putAll(new LinkedHashMap<>());
553 assertEquals(5, epm.size());
554 listener.setExpectingNotification(true);
555
556
557 assertEquals(new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5)), new HashSet<Integer>(epm.keySet()));
558 assertEquals(new TreeSet<String>(Arrays.asList("a", "b", "c", "d", "e")), new TreeSet<String>(epm.values()));
559 Set<Map.Entry<Integer, String>> entrySet = epm.entrySet();
560 assertEquals(5, entrySet.size());
561 Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();
562 Map.Entry<Integer, String> entry = it.next();
563 assertEquals(1, entry.getKey().intValue());
564 assertEquals("a", entry.getValue());
565 entry = it.next();
566 assertEquals(2, entry.getKey().intValue());
567 assertEquals("b", entry.getValue());
568
569
570 epm.clear();
571 assertEquals(EventProducingMap.OBJECT_REMOVED_EVENT, listener.getReceivedEvent().getType());
572 assertEquals(Integer.valueOf(0), listener.getReceivedEvent().getContent());
573 listener.setExpectingNotification(false);
574 epm.clear();
575 listener.setExpectingNotification(true);
576
577
578 epm.removeAllListeners();
579 epm.clear();
580 }
581
582
583 protected static class TestEventListener implements EventListenerInterface
584 {
585
586 private static final long serialVersionUID = 20191230L;
587
588
589 private boolean expectingNotification = true;
590
591
592 private EventInterface receivedEvent;
593
594
595
596
597 public void setExpectingNotification(final boolean expectingNotification)
598 {
599 this.expectingNotification = expectingNotification;
600 }
601
602
603
604
605 public EventInterface getReceivedEvent()
606 {
607 return this.receivedEvent;
608 }
609
610
611 @Override
612 public void notify(final EventInterface event) throws RemoteException
613 {
614 if (!this.expectingNotification)
615 {
616 fail("Received event " + event + " unexpectedly");
617 }
618 this.receivedEvent = event;
619 }
620 }
621
622 }