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.

AppReloaderImplTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.application;
  21. import java.io.IOException;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.application.config.AppSettings;
  26. import org.sonar.application.config.AppSettingsLoader;
  27. import org.sonar.application.config.TestAppSettings;
  28. import org.sonar.process.MessageException;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.assertj.core.data.MapEntry.entry;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.verify;
  33. import static org.mockito.Mockito.verifyZeroInteractions;
  34. import static org.mockito.Mockito.when;
  35. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED;
  36. import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
  37. import static org.sonar.process.ProcessProperties.Property.PATH_LOGS;
  38. import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
  39. import static org.sonar.process.ProcessProperties.Property.PATH_WEB;
  40. public class AppReloaderImplTest {
  41. @Rule
  42. public ExpectedException expectedException = ExpectedException.none();
  43. private AppSettingsLoader settingsLoader = mock(AppSettingsLoader.class);
  44. private FileSystem fs = mock(FileSystem.class);
  45. private AppState state = mock(AppState.class);
  46. private AppLogging logging = mock(AppLogging.class);
  47. private AppReloaderImpl underTest = new AppReloaderImpl(settingsLoader, fs, state, logging);
  48. @Test
  49. public void reload_configuration_then_reset_all() throws IOException {
  50. AppSettings settings = new TestAppSettings().set("foo", "bar");
  51. AppSettings newSettings = new TestAppSettings()
  52. .set("foo", "newBar")
  53. .set("newProp", "newVal");
  54. when(settingsLoader.load()).thenReturn(newSettings);
  55. underTest.reload(settings);
  56. assertThat(settings.getProps().rawProperties())
  57. .contains(entry("foo", "newBar"))
  58. .contains(entry("newProp", "newVal"));
  59. verify(logging).configure();
  60. verify(state).reset();
  61. verify(fs).reset();
  62. }
  63. @Test
  64. public void throw_ISE_if_cluster_is_enabled() throws IOException {
  65. AppSettings settings = new TestAppSettings().set(CLUSTER_ENABLED.getKey(), "true");
  66. expectedException.expect(IllegalStateException.class);
  67. expectedException.expectMessage("Restart is not possible with cluster mode");
  68. underTest.reload(settings);
  69. verifyZeroInteractions(logging);
  70. verifyZeroInteractions(state);
  71. verifyZeroInteractions(fs);
  72. }
  73. @Test
  74. public void throw_MessageException_if_path_properties_are_changed() throws IOException {
  75. verifyFailureIfPropertyValueChanged(PATH_DATA.getKey());
  76. verifyFailureIfPropertyValueChanged(PATH_LOGS.getKey());
  77. verifyFailureIfPropertyValueChanged(PATH_TEMP.getKey());
  78. verifyFailureIfPropertyValueChanged(PATH_WEB.getKey());
  79. }
  80. @Test
  81. public void throw_MessageException_if_cluster_mode_changed() throws IOException {
  82. verifyFailureIfPropertyValueChanged(CLUSTER_ENABLED.getKey());
  83. }
  84. private void verifyFailureIfPropertyValueChanged(String propertyKey) throws IOException {
  85. AppSettings settings = new TestAppSettings().set(propertyKey, "val1");
  86. AppSettings newSettings = new TestAppSettings()
  87. .set(propertyKey, "val2");
  88. when(settingsLoader.load()).thenReturn(newSettings);
  89. expectedException.expect(MessageException.class);
  90. expectedException.expectMessage("Property [" + propertyKey + "] cannot be changed on restart: [val1] => [val2]");
  91. underTest.reload(settings);
  92. verifyZeroInteractions(logging);
  93. verifyZeroInteractions(state);
  94. verifyZeroInteractions(fs);
  95. }
  96. }