You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VaadinServiceTest.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package com.vaadin.server;
  2. import static org.hamcrest.CoreMatchers.containsString;
  3. import static org.hamcrest.MatcherAssert.assertThat;
  4. import static org.junit.Assert.assertEquals;
  5. import javax.servlet.ServletConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpSessionBindingEvent;
  8. import java.util.Collections;
  9. import org.easymock.EasyMock;
  10. import org.junit.Test;
  11. import org.mockito.Mockito;
  12. import com.vaadin.shared.Registration;
  13. import com.vaadin.util.CurrentInstance;
  14. public class VaadinServiceTest {
  15. private class TestSessionDestroyListener implements SessionDestroyListener {
  16. int callCount = 0;
  17. @Override
  18. public void sessionDestroy(SessionDestroyEvent event) {
  19. callCount++;
  20. }
  21. }
  22. private class TestServiceDestroyListener implements ServiceDestroyListener {
  23. int callCount = 0;
  24. @Override
  25. public void serviceDestroy(ServiceDestroyEvent event) {
  26. callCount++;
  27. }
  28. }
  29. private String createCriticalNotification(String caption, String message,
  30. String details, String url) {
  31. return VaadinService.createCriticalNotificationJSON(caption, message,
  32. details, url);
  33. }
  34. @Test
  35. public void testFireSessionDestroy() throws ServletException {
  36. VaadinService service = createService();
  37. TestSessionDestroyListener listener = new TestSessionDestroyListener();
  38. service.addSessionDestroyListener(listener);
  39. MockVaadinSession vaadinSession = new MockVaadinSession(service);
  40. service.fireSessionDestroy(vaadinSession);
  41. assertEquals(
  42. "'fireSessionDestroy' method doesn't call 'close' for the session",
  43. 1, vaadinSession.getCloseCount());
  44. vaadinSession.valueUnbound(
  45. EasyMock.createMock(HttpSessionBindingEvent.class));
  46. assertEquals(
  47. "'fireSessionDestroy' method may not call 'close' "
  48. + "method for closing session",
  49. 1, vaadinSession.getCloseCount());
  50. assertEquals("SessionDestroyListeners not called exactly once", 1,
  51. listener.callCount);
  52. }
  53. @Test
  54. public void captionIsSetToACriticalNotification() {
  55. String notification = createCriticalNotification("foobar", "message",
  56. "details", "url");
  57. assertThat(notification, containsString("\"caption\":\"foobar\""));
  58. }
  59. @Test
  60. public void nullCaptionIsSetToACriticalNotification() {
  61. String notification = createCriticalNotification(null, "message",
  62. "details", "url");
  63. assertThat(notification, containsString("\"caption\":null"));
  64. }
  65. @Test
  66. public void messageWithDetailsIsSetToACriticalNotification() {
  67. String notification = createCriticalNotification("caption", "foo",
  68. "bar", "url");
  69. assertThat(notification, containsString("\"details\":\"bar\""));
  70. }
  71. @Test
  72. public void nullMessageSentAsNullInACriticalNotification() {
  73. String notification = createCriticalNotification("caption", null,
  74. "foobar", "url");
  75. assertThat(notification, containsString("\"message\":null"));
  76. }
  77. @Test
  78. public void nullMessageIsSetToACriticalNotification() {
  79. String notification = createCriticalNotification("caption", null, null,
  80. "url");
  81. assertThat(notification, containsString("\"message\":null"));
  82. }
  83. @Test
  84. public void messageSetToACriticalNotification() {
  85. String notification = createCriticalNotification("caption", "foobar",
  86. null, "url");
  87. assertThat(notification, containsString("\"message\":\"foobar\""));
  88. }
  89. @Test
  90. public void urlIsSetToACriticalNotification() {
  91. String notification = createCriticalNotification("caption", "message",
  92. "details", "foobar");
  93. assertThat(notification, containsString("\"url\":\"foobar\""));
  94. }
  95. @Test
  96. public void nullUrlIsSetToACriticalNotification() {
  97. String notification = createCriticalNotification("caption", "message",
  98. "details", null);
  99. assertThat(notification, containsString("\"url\":null"));
  100. }
  101. @Test
  102. public void currentInstancesAfterPendingAccessTasks() {
  103. VaadinService service = createService();
  104. MockVaadinSession session = new MockVaadinSession(service);
  105. session.lock();
  106. service.accessSession(session,
  107. () -> CurrentInstance.set(String.class, "Set in task"));
  108. CurrentInstance.set(String.class, "Original value");
  109. service.runPendingAccessTasks(session);
  110. assertEquals(
  111. "Original CurrentInstance should be set after the task has been run",
  112. "Original value", CurrentInstance.get(String.class));
  113. }
  114. private static VaadinService createService() {
  115. ServletConfig servletConfig = new MockServletConfig();
  116. VaadinServlet servlet = new VaadinServlet();
  117. try {
  118. servlet.init(servletConfig);
  119. } catch (ServletException e) {
  120. throw new RuntimeException(e);
  121. }
  122. VaadinService service = servlet.getService();
  123. return service;
  124. }
  125. @Test
  126. public void fireServiceDestroy() {
  127. VaadinService service = createService();
  128. TestServiceDestroyListener listener = new TestServiceDestroyListener();
  129. TestServiceDestroyListener listener2 = new TestServiceDestroyListener();
  130. service.addServiceDestroyListener(listener);
  131. Registration remover2 = service.addServiceDestroyListener(listener2);
  132. service.destroy();
  133. assertEquals(1, listener.callCount);
  134. assertEquals(1, listener2.callCount);
  135. service.removeServiceDestroyListener(listener);
  136. remover2.remove();
  137. service.destroy();
  138. assertEquals(1, listener.callCount);
  139. assertEquals(1, listener2.callCount);
  140. }
  141. @Test
  142. public void reinitializeSession_setVaadinSessionAttriuteWithLock() {
  143. VaadinRequest request = Mockito.mock(VaadinRequest.class);
  144. VaadinSession vaadinSession = Mockito.mock(VaadinSession.class);
  145. VaadinSession newVaadinSession = Mockito.mock(VaadinSession.class);
  146. WrappedSession session = mockSession(request, vaadinSession, "foo");
  147. Mockito.doAnswer(invocation -> {
  148. mockSession(request, newVaadinSession, "bar");
  149. return null;
  150. }).when(session).invalidate();
  151. VaadinService.reinitializeSession(request);
  152. Mockito.verify(vaadinSession, Mockito.times(2)).lock();
  153. Mockito.verify(vaadinSession).setAttribute(
  154. VaadinService.PRESERVE_UNBOUND_SESSION_ATTRIBUTE, Boolean.TRUE);
  155. Mockito.verify(vaadinSession).setAttribute(
  156. VaadinService.PRESERVE_UNBOUND_SESSION_ATTRIBUTE, null);
  157. Mockito.verify(vaadinSession, Mockito.times(2)).unlock();
  158. }
  159. private WrappedSession mockSession(VaadinRequest request,
  160. VaadinSession vaadinSession, String attributeName) {
  161. WrappedSession session = Mockito.mock(WrappedSession.class);
  162. Mockito.when(request.getWrappedSession()).thenReturn(session);
  163. Mockito.when(session.getAttributeNames())
  164. .thenReturn(Collections.singleton(attributeName));
  165. Mockito.when(session.getAttribute(attributeName))
  166. .thenReturn(vaadinSession);
  167. VaadinService service = Mockito.mock(VaadinService.class);
  168. Mockito.when(vaadinSession.getService()).thenReturn(service);
  169. return session;
  170. }
  171. }