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.

DefaultServerTest.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scanner.platform;
  21. import org.sonar.api.SonarEdition;
  22. import org.junit.Test;
  23. import org.sonar.api.CoreProperties;
  24. import org.sonar.api.SonarQubeSide;
  25. import org.sonar.api.config.internal.Settings;
  26. import org.sonar.api.config.internal.MapSettings;
  27. import org.sonar.api.internal.SonarRuntimeImpl;
  28. import org.sonar.api.utils.Version;
  29. import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.when;
  33. public class DefaultServerTest {
  34. @Test
  35. public void shouldLoadServerProperties() {
  36. Settings settings = new MapSettings();
  37. settings.setProperty(CoreProperties.SERVER_ID, "123");
  38. settings.setProperty(CoreProperties.SERVER_STARTTIME, "2010-05-18T17:59:00+0000");
  39. DefaultScannerWsClient client = mock(DefaultScannerWsClient.class);
  40. when(client.baseUrl()).thenReturn("http://foo.com");
  41. DefaultServer metadata = new DefaultServer(((MapSettings) settings).asConfig(), client,
  42. SonarRuntimeImpl.forSonarQube(Version.parse("2.2"), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
  43. assertThat(metadata.getId()).isEqualTo("123");
  44. assertThat(metadata.getVersion()).isEqualTo("2.2");
  45. assertThat(metadata.getStartedAt()).isNotNull();
  46. assertThat(metadata.getURL()).isEqualTo("http://foo.com");
  47. assertThat(metadata.getPermanentServerId()).isEqualTo("123");
  48. assertThat(metadata.getRootDir()).isNull();
  49. assertThat(metadata.getContextPath()).isNull();
  50. assertThat(metadata.isDev()).isFalse();
  51. assertThat(metadata.isSecured()).isFalse();
  52. }
  53. @Test
  54. public void publicRootUrl() {
  55. Settings settings = new MapSettings();
  56. DefaultScannerWsClient client = mock(DefaultScannerWsClient.class);
  57. when(client.baseUrl()).thenReturn("http://foo.com/");
  58. DefaultServer metadata = new DefaultServer(((MapSettings) settings).asConfig(), client, null);
  59. settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://server.com/");
  60. assertThat(metadata.getPublicRootUrl()).isEqualTo("http://server.com");
  61. settings.removeProperty(CoreProperties.SERVER_BASE_URL);
  62. assertThat(metadata.getPublicRootUrl()).isEqualTo("http://foo.com");
  63. }
  64. @Test(expected = RuntimeException.class)
  65. public void invalid_startup_date_throws_exception() {
  66. Settings settings = new MapSettings();
  67. settings.setProperty(CoreProperties.SERVER_STARTTIME, "invalid");
  68. DefaultScannerWsClient client = mock(DefaultScannerWsClient.class);
  69. DefaultServer metadata = new DefaultServer(((MapSettings) settings).asConfig(), client, null);
  70. metadata.getStartedAt();
  71. }
  72. }