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.

PlatformLevelTest.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.platformlevel;
  21. import java.util.Random;
  22. import java.util.stream.IntStream;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.mockito.Mockito;
  27. import org.sonar.server.platform.WebServer;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. public class PlatformLevelTest {
  30. @Rule
  31. public ExpectedException expectedException = ExpectedException.none();
  32. private PlatformLevel underTest = new PlatformLevel("name") {
  33. @Override
  34. protected void configureLevel() {
  35. }
  36. };
  37. @Test
  38. public void addIfStartupLeader_throws_ISE_if_container_does_not_have_WebServer_object() {
  39. expectedException.expect(IllegalStateException.class);
  40. expectedException.expectMessage("WebServer not available in Pico yet");
  41. underTest.addIfStartupLeader();
  42. }
  43. @Test
  44. public void addIfStartupLeader_always_returns_the_same_instance() {
  45. underTest.add(Mockito.mock(WebServer.class));
  46. PlatformLevel.AddIfStartupLeader addIfStartupLeader = underTest.addIfStartupLeader();
  47. IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfStartupLeader()).isSameAs(addIfStartupLeader));
  48. }
  49. @Test
  50. public void addIfCluster_throws_ISE_if_container_does_not_have_WebServer_object() {
  51. expectedException.expect(IllegalStateException.class);
  52. expectedException.expectMessage("WebServer not available in Pico yet");
  53. underTest.addIfCluster();
  54. }
  55. @Test
  56. public void addIfCluster_always_returns_the_same_instance() {
  57. underTest.add(Mockito.mock(WebServer.class));
  58. PlatformLevel.AddIfCluster addIfCluster = underTest.addIfCluster();
  59. IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfCluster()).isSameAs(addIfCluster));
  60. }
  61. @Test
  62. public void addIfStandalone_throws_ISE_if_container_does_not_have_WebServer_object() {
  63. expectedException.expect(IllegalStateException.class);
  64. expectedException.expectMessage("WebServer not available in Pico yet");
  65. underTest.addIfCluster();
  66. }
  67. @Test
  68. public void addIfStandalone_always_returns_the_same_instance() {
  69. underTest.add(Mockito.mock(WebServer.class));
  70. PlatformLevel.AddIfCluster addIfCluster = underTest.addIfCluster();
  71. IntStream.range(0, 1 + new Random().nextInt(4)).forEach(i -> assertThat(underTest.addIfCluster()).isSameAs(addIfCluster));
  72. }
  73. }