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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import static org.hamcrest.CoreMatchers.containsString;
  18. import static org.hamcrest.MatcherAssert.assertThat;
  19. import static org.junit.Assert.assertEquals;
  20. import javax.servlet.ServletConfig;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.http.HttpSessionBindingEvent;
  23. import org.easymock.EasyMock;
  24. import org.junit.Test;
  25. import com.vaadin.shared.Registration;
  26. import com.vaadin.util.CurrentInstance;
  27. /**
  28. *
  29. * @author Vaadin Ltd
  30. */
  31. public class VaadinServiceTest {
  32. private class TestSessionDestroyListener implements SessionDestroyListener {
  33. int callCount = 0;
  34. @Override
  35. public void sessionDestroy(SessionDestroyEvent event) {
  36. callCount++;
  37. }
  38. }
  39. private class TestServiceDestroyListener implements ServiceDestroyListener {
  40. int callCount = 0;
  41. @Override
  42. public void serviceDestroy(ServiceDestroyEvent event) {
  43. callCount++;
  44. }
  45. }
  46. private String createCriticalNotification(String caption, String message,
  47. String details, String url) {
  48. return VaadinService.createCriticalNotificationJSON(caption, message,
  49. details, url);
  50. }
  51. @Test
  52. public void testFireSessionDestroy() throws ServletException {
  53. VaadinService service = createService();
  54. TestSessionDestroyListener listener = new TestSessionDestroyListener();
  55. service.addSessionDestroyListener(listener);
  56. MockVaadinSession vaadinSession = new MockVaadinSession(service);
  57. service.fireSessionDestroy(vaadinSession);
  58. assertEquals(
  59. "'fireSessionDestroy' method doesn't call 'close' for the session",
  60. 1, vaadinSession.getCloseCount());
  61. vaadinSession.valueUnbound(
  62. EasyMock.createMock(HttpSessionBindingEvent.class));
  63. assertEquals(
  64. "'fireSessionDestroy' method may not call 'close' "
  65. + "method for closing session",
  66. 1, vaadinSession.getCloseCount());
  67. assertEquals("SessionDestroyListeners not called exactly once", 1,
  68. listener.callCount);
  69. }
  70. @Test
  71. public void captionIsSetToACriticalNotification() {
  72. String notification = createCriticalNotification("foobar", "message",
  73. "details", "url");
  74. assertThat(notification, containsString("\"caption\":\"foobar\""));
  75. }
  76. @Test
  77. public void nullCaptionIsSetToACriticalNotification() {
  78. String notification = createCriticalNotification(null, "message",
  79. "details", "url");
  80. assertThat(notification, containsString("\"caption\":null"));
  81. }
  82. @Test
  83. public void messageWithDetailsIsSetToACriticalNotification() {
  84. String notification = createCriticalNotification("caption", "foo",
  85. "bar", "url");
  86. assertThat(notification, containsString("\"details\":\"bar\""));
  87. }
  88. @Test
  89. public void nullMessageSentAsNullInACriticalNotification() {
  90. String notification = createCriticalNotification("caption", null,
  91. "foobar", "url");
  92. assertThat(notification, containsString("\"message\":null"));
  93. }
  94. @Test
  95. public void nullMessageIsSetToACriticalNotification() {
  96. String notification = createCriticalNotification("caption", null, null,
  97. "url");
  98. assertThat(notification, containsString("\"message\":null"));
  99. }
  100. @Test
  101. public void messageSetToACriticalNotification() {
  102. String notification = createCriticalNotification("caption", "foobar",
  103. null, "url");
  104. assertThat(notification, containsString("\"message\":\"foobar\""));
  105. }
  106. @Test
  107. public void urlIsSetToACriticalNotification() {
  108. String notification = createCriticalNotification("caption", "message",
  109. "details", "foobar");
  110. assertThat(notification, containsString("\"url\":\"foobar\""));
  111. }
  112. @Test
  113. public void nullUrlIsSetToACriticalNotification() {
  114. String notification = createCriticalNotification("caption", "message",
  115. "details", null);
  116. assertThat(notification, containsString("\"url\":null"));
  117. }
  118. @Test
  119. public void currentInstancesAfterPendingAccessTasks() {
  120. VaadinService service = createService();
  121. MockVaadinSession session = new MockVaadinSession(service);
  122. session.lock();
  123. service.accessSession(session,
  124. () -> CurrentInstance.set(String.class, "Set in task"));
  125. CurrentInstance.set(String.class, "Original value");
  126. service.runPendingAccessTasks(session);
  127. assertEquals(
  128. "Original CurrentInstance should be set after the task has been run",
  129. "Original value", CurrentInstance.get(String.class));
  130. }
  131. private static VaadinService createService() {
  132. ServletConfig servletConfig = new MockServletConfig();
  133. VaadinServlet servlet = new VaadinServlet();
  134. try {
  135. servlet.init(servletConfig);
  136. } catch (ServletException e) {
  137. throw new RuntimeException(e);
  138. }
  139. VaadinService service = servlet.getService();
  140. return service;
  141. }
  142. @Test
  143. public void fireServiceDestroy() {
  144. VaadinService service = createService();
  145. TestServiceDestroyListener listener = new TestServiceDestroyListener();
  146. TestServiceDestroyListener listener2 = new TestServiceDestroyListener();
  147. service.addServiceDestroyListener(listener);
  148. Registration remover2 = service.addServiceDestroyListener(listener2);
  149. service.destroy();
  150. assertEquals(1, listener.callCount);
  151. assertEquals(1, listener2.callCount);
  152. service.removeServiceDestroyListener(listener);
  153. remover2.remove();
  154. service.destroy();
  155. assertEquals(1, listener.callCount);
  156. assertEquals(1, listener2.callCount);
  157. }
  158. }