|
30 | 30 | import java.util.Map; |
31 | 31 | import java.util.Optional; |
32 | 32 | import java.util.Set; |
| 33 | +import java.util.concurrent.atomic.AtomicInteger; |
33 | 34 | import java.util.concurrent.atomic.AtomicReference; |
34 | 35 | import java.util.stream.Collectors; |
35 | 36 |
|
|
63 | 64 |
|
64 | 65 | import static org.hamcrest.CoreMatchers.containsString; |
65 | 66 | import static org.hamcrest.MatcherAssert.assertThat; |
| 67 | +import static org.junit.Assert.assertThrows; |
66 | 68 |
|
67 | 69 | /** |
68 | 70 | * |
@@ -99,6 +101,34 @@ public void sessionDestroy(SessionDestroyEvent event) { |
99 | 101 | } |
100 | 102 | } |
101 | 103 |
|
| 104 | + private class ThrowingSessionDestroyListener |
| 105 | + implements SessionDestroyListener { |
| 106 | + |
| 107 | + @Override |
| 108 | + public void sessionDestroy(SessionDestroyEvent event) { |
| 109 | + throw new RuntimeException(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private class TestServiceDestroyListener implements ServiceDestroyListener { |
| 114 | + |
| 115 | + int callCount = 0; |
| 116 | + |
| 117 | + @Override |
| 118 | + public void serviceDestroy(ServiceDestroyEvent event) { |
| 119 | + callCount++; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private class ThrowingServiceDestroyListener |
| 124 | + implements ServiceDestroyListener { |
| 125 | + |
| 126 | + @Override |
| 127 | + public void serviceDestroy(ServiceDestroyEvent event) { |
| 128 | + throw new RuntimeException(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
102 | 132 | private String createCriticalNotification(String caption, String message, |
103 | 133 | String details, String url) { |
104 | 134 | return VaadinService.createCriticalNotificationJSON(caption, message, |
@@ -298,6 +328,48 @@ public void testFireSessionDestroy() |
298 | 328 | 1, listener.callCount); |
299 | 329 | } |
300 | 330 |
|
| 331 | + @Test |
| 332 | + public void testSessionDestroyListenerCalled_whenAnotherListenerThrows() |
| 333 | + throws ServiceException { |
| 334 | + VaadinService service = createService(); |
| 335 | + |
| 336 | + ThrowingSessionDestroyListener throwingListener = new ThrowingSessionDestroyListener(); |
| 337 | + TestSessionDestroyListener listener = new TestSessionDestroyListener(); |
| 338 | + |
| 339 | + service.addSessionDestroyListener(throwingListener); |
| 340 | + service.addSessionDestroyListener(listener); |
| 341 | + |
| 342 | + final AtomicInteger errorCount = new AtomicInteger(); |
| 343 | + MockVaadinSession vaadinSession = new MockVaadinSession(service); |
| 344 | + vaadinSession.lock(); |
| 345 | + vaadinSession.setErrorHandler(e -> errorCount.getAndIncrement()); |
| 346 | + vaadinSession.unlock(); |
| 347 | + service.fireSessionDestroy(vaadinSession); |
| 348 | + |
| 349 | + Assert.assertEquals("ErrorHandler not called exactly once", 1, |
| 350 | + errorCount.get()); |
| 351 | + |
| 352 | + Assert.assertEquals("SessionDestroyListener not called exactly once", 1, |
| 353 | + listener.callCount); |
| 354 | + } |
| 355 | + |
| 356 | + @Test |
| 357 | + public void testServiceDestroyListenerCalled_whenAnotherListenerThrows() |
| 358 | + throws ServletException, ServiceException { |
| 359 | + VaadinService service = createService(); |
| 360 | + |
| 361 | + ThrowingServiceDestroyListener throwingListener = new ThrowingServiceDestroyListener(); |
| 362 | + TestServiceDestroyListener listener = new TestServiceDestroyListener(); |
| 363 | + |
| 364 | + service.addServiceDestroyListener(throwingListener); |
| 365 | + service.addServiceDestroyListener(listener); |
| 366 | + |
| 367 | + assertThrows(RuntimeException.class, () -> service.destroy()); |
| 368 | + |
| 369 | + Assert.assertEquals("ServiceDestroyListener not called exactly once", 1, |
| 370 | + listener.callCount); |
| 371 | + } |
| 372 | + |
301 | 373 | @Test |
302 | 374 | public void captionIsSetToACriticalNotification() { |
303 | 375 | String notification = createCriticalNotification("foobar", "message", |
|
0 commit comments