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.

MetadataLoaderTest.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.api.internal;
  21. import java.io.File;
  22. import java.net.MalformedURLException;
  23. import org.junit.Test;
  24. import org.sonar.api.SonarEdition;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.api.utils.Version;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. import static org.mockito.ArgumentMatchers.anyString;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. public class MetadataLoaderTest {
  33. private System2 system = mock(System2.class);
  34. @Test
  35. public void load_version_from_file_in_classpath() {
  36. Version version = MetadataLoader.loadVersion(System2.INSTANCE);
  37. assertThat(version).isNotNull();
  38. assertThat(version.major()).isGreaterThanOrEqualTo(5);
  39. }
  40. @Test
  41. public void load_edition_from_file_in_classpath() {
  42. SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE);
  43. assertThat(edition).isNotNull();
  44. }
  45. @Test
  46. public void load_edition_defaults_to_community_if_file_not_found() throws MalformedURLException {
  47. when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL());
  48. SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE);
  49. assertThat(edition).isEqualTo(SonarEdition.COMMUNITY);
  50. }
  51. @Test
  52. public void throw_ISE_if_edition_is_invalid() {
  53. assertThatThrownBy(() -> MetadataLoader.parseEdition("trash"))
  54. .isInstanceOf(IllegalStateException.class)
  55. .hasMessage("Invalid edition found in '/sonar-edition.txt': 'TRASH'");
  56. }
  57. @Test
  58. public void throw_ISE_if_fail_to_load_version() throws Exception {
  59. when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL());
  60. assertThatThrownBy(() -> MetadataLoader.loadVersion(system))
  61. .isInstanceOf(IllegalStateException.class)
  62. .hasMessageContaining("Can not load /sq-version.txt from classpath");
  63. }
  64. }