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.

ServerLifecycleNotifierTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.platform;
  21. import java.io.File;
  22. import java.util.Date;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.sonar.api.platform.Server;
  26. import org.sonar.api.platform.ServerStartHandler;
  27. import org.sonar.api.platform.ServerStopHandler;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.never;
  30. import static org.mockito.Mockito.verify;
  31. public class ServerLifecycleNotifierTest {
  32. private Server server;
  33. private ServerStartHandler start1;
  34. private ServerStartHandler start2;
  35. private ServerStopHandler stop1;
  36. private ServerStopHandler stop2;
  37. @Before
  38. public void before() {
  39. server = new FakeServer();
  40. start1 = mock(ServerStartHandler.class);
  41. start2 = mock(ServerStartHandler.class);
  42. stop1 = mock(ServerStopHandler.class);
  43. stop2 = mock(ServerStopHandler.class);
  44. }
  45. /**
  46. * see the explanation in the method ServerLifecycleNotifier.start()
  47. */
  48. @Test
  49. public void doNotNotifyWithTheStartMethod() {
  50. ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop2});
  51. notifier.start();
  52. verify(start1, never()).onServerStart(server);
  53. verify(start2, never()).onServerStart(server);
  54. verify(stop1, never()).onServerStop(server);
  55. }
  56. @Test
  57. public void notifyOnStart() {
  58. ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop2});
  59. notifier.notifyStart();
  60. verify(start1).onServerStart(server);
  61. verify(start2).onServerStart(server);
  62. verify(stop1, never()).onServerStop(server);
  63. }
  64. @Test
  65. public void notifyOnStop() {
  66. ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop1, stop2});
  67. notifier.stop();
  68. verify(start1, never()).onServerStart(server);
  69. verify(start2, never()).onServerStart(server);
  70. verify(stop1).onServerStop(server);
  71. verify(stop2).onServerStop(server);
  72. }
  73. }
  74. class FakeServer extends Server {
  75. @Override
  76. public String getId() {
  77. return null;
  78. }
  79. @Override
  80. public String getVersion() {
  81. return null;
  82. }
  83. @Override
  84. public Date getStartedAt() {
  85. return null;
  86. }
  87. @Override
  88. public String getContextPath() {
  89. return null;
  90. }
  91. @Override
  92. public String getPublicRootUrl() {
  93. return null;
  94. }
  95. @Override
  96. public boolean isSecured() {
  97. return false;
  98. }
  99. @Override
  100. public String getPermanentServerId() {
  101. return null;
  102. }
  103. }