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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 org.easymock.EasyMock;
  9. import org.junit.Test;
  10. import com.vaadin.shared.Registration;
  11. import com.vaadin.util.CurrentInstance;
  12. public class VaadinServiceTest {
  13. private class TestSessionDestroyListener implements SessionDestroyListener {
  14. int callCount = 0;
  15. @Override
  16. public void sessionDestroy(SessionDestroyEvent event) {
  17. callCount++;
  18. }
  19. }
  20. private class TestServiceDestroyListener implements ServiceDestroyListener {
  21. int callCount = 0;
  22. @Override
  23. public void serviceDestroy(ServiceDestroyEvent event) {
  24. callCount++;
  25. }
  26. }
  27. private String createCriticalNotification(String caption, String message,
  28. String details, String url) {
  29. return VaadinService.createCriticalNotificationJSON(caption, message,
  30. details, url);
  31. }
  32. @Test
  33. public void testFireSessionDestroy() throws ServletException {
  34. VaadinService service = createService();
  35. TestSessionDestroyListener listener = new TestSessionDestroyListener();
  36. service.addSessionDestroyListener(listener);
  37. MockVaadinSession vaadinSession = new MockVaadinSession(service);
  38. service.fireSessionDestroy(vaadinSession);
  39. assertEquals(
  40. "'fireSessionDestroy' method doesn't call 'close' for the session",
  41. 1, vaadinSession.getCloseCount());
  42. vaadinSession.valueUnbound(
  43. EasyMock.createMock(HttpSessionBindingEvent.class));
  44. assertEquals(
  45. "'fireSessionDestroy' method may not call 'close' "
  46. + "method for closing session",
  47. 1, vaadinSession.getCloseCount());
  48. assertEquals("SessionDestroyListeners not called exactly once", 1,
  49. listener.callCount);
  50. }
  51. @Test
  52. public void captionIsSetToACriticalNotification() {
  53. String notification = createCriticalNotification("foobar", "message",
  54. "details", "url");
  55. assertThat(notification, containsString("\"caption\":\"foobar\""));
  56. }
  57. @Test
  58. public void nullCaptionIsSetToACriticalNotification() {
  59. String notification = createCriticalNotification(null, "message",
  60. "details", "url");
  61. assertThat(notification, containsString("\"caption\":null"));
  62. }
  63. @Test
  64. public void messageWithDetailsIsSetToACriticalNotification() {
  65. String notification = createCriticalNotification("caption", "foo",
  66. "bar", "url");
  67. assertThat(notification, containsString("\"details\":\"bar\""));
  68. }
  69. @Test
  70. public void nullMessageSentAsNullInACriticalNotification() {
  71. String notification = createCriticalNotification("caption", null,
  72. "foobar", "url");
  73. assertThat(notification, containsString("\"message\":null"));
  74. }
  75. @Test
  76. public void nullMessageIsSetToACriticalNotification() {
  77. String notification = createCriticalNotification("caption", null, null,
  78. "url");
  79. assertThat(notification, containsString("\"message\":null"));
  80. }
  81. @Test
  82. public void messageSetToACriticalNotification() {
  83. String notification = createCriticalNotification("caption", "foobar",
  84. null, "url");
  85. assertThat(notification, containsString("\"message\":\"foobar\""));
  86. }
  87. @Test
  88. public void urlIsSetToACriticalNotification() {
  89. String notification = createCriticalNotification("caption", "message",
  90. "details", "foobar");
  91. assertThat(notification, containsString("\"url\":\"foobar\""));
  92. }
  93. @Test
  94. public void nullUrlIsSetToACriticalNotification() {
  95. String notification = createCriticalNotification("caption", "message",
  96. "details", null);
  97. assertThat(notification, containsString("\"url\":null"));
  98. }
  99. @Test
  100. public void currentInstancesAfterPendingAccessTasks() {
  101. VaadinService service = createService();
  102. MockVaadinSession session = new MockVaadinSession(service);
  103. session.lock();
  104. service.accessSession(session,
  105. () -> CurrentInstance.set(String.class, "Set in task"));
  106. CurrentInstance.set(String.class, "Original value");
  107. service.runPendingAccessTasks(session);
  108. assertEquals(
  109. "Original CurrentInstance should be set after the task has been run",
  110. "Original value", CurrentInstance.get(String.class));
  111. }
  112. private static VaadinService createService() {
  113. ServletConfig servletConfig = new MockServletConfig();
  114. VaadinServlet servlet = new VaadinServlet();
  115. try {
  116. servlet.init(servletConfig);
  117. } catch (ServletException e) {
  118. throw new RuntimeException(e);
  119. }
  120. VaadinService service = servlet.getService();
  121. return service;
  122. }
  123. @Test
  124. public void fireServiceDestroy() {
  125. VaadinService service = createService();
  126. TestServiceDestroyListener listener = new TestServiceDestroyListener();
  127. TestServiceDestroyListener listener2 = new TestServiceDestroyListener();
  128. service.addServiceDestroyListener(listener);
  129. Registration remover2 = service.addServiceDestroyListener(listener2);
  130. service.destroy();
  131. assertEquals(1, listener.callCount);
  132. assertEquals(1, listener2.callCount);
  133. service.removeServiceDestroyListener(listener);
  134. remover2.remove();
  135. service.destroy();
  136. assertEquals(1, listener.callCount);
  137. assertEquals(1, listener2.callCount);
  138. }
  139. }