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.

DefaultLanguagesRepositoryTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.repository.language;
  21. import java.io.InputStreamReader;
  22. import java.io.StringReader;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.slf4j.event.Level;
  27. import org.sonar.api.config.Configuration;
  28. import org.sonar.api.testfixtures.log.LogTester;
  29. import org.sonar.scanner.WsTestUtil;
  30. import org.sonar.scanner.http.DefaultScannerWsClient;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.AssertionsForClassTypes.catchThrowableOfType;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. public class DefaultLanguagesRepositoryTest {
  36. @Rule
  37. public LogTester logTester = new LogTester();
  38. private final DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
  39. private final Configuration properties = mock(Configuration.class);
  40. private final LanguagesLoader languagesLoader = new DefaultLanguagesLoader(wsClient, properties);
  41. private final DefaultLanguagesRepository underTest = new DefaultLanguagesRepository(languagesLoader);
  42. private static final String[] JAVA_SUFFIXES = new String[] { ".java", ".jav" };
  43. private static final String[] XOO_SUFFIXES = new String[] { ".xoo" };
  44. private static final String[] XOO_PATTERNS = new String[] { "Xoofile" };
  45. private static final String[] PYTHON_SUFFIXES = new String[] { ".py" };
  46. @Before
  47. public void setup() {
  48. logTester.setLevel(Level.DEBUG);
  49. }
  50. @Test
  51. public void should_load_languages_from_ws() {
  52. WsTestUtil.mockReader(wsClient, "/api/languages/list",
  53. new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
  54. when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
  55. when(properties.getStringArray("sonar.xoo.file.suffixes")).thenReturn(XOO_SUFFIXES);
  56. when(properties.getStringArray("sonar.python.file.suffixes")).thenReturn(PYTHON_SUFFIXES);
  57. underTest.start();
  58. assertThat(underTest.all()).hasSize(3);
  59. assertThat(underTest.get("java")).isNotNull();
  60. assertThat(underTest.get("java").fileSuffixes()).containsExactlyInAnyOrder(JAVA_SUFFIXES);
  61. assertThat(underTest.get("java").isPublishAllFiles()).isTrue();
  62. assertThat(underTest.get("xoo")).isNotNull();
  63. assertThat(underTest.get("xoo").fileSuffixes()).containsExactlyInAnyOrder(XOO_SUFFIXES);
  64. assertThat(underTest.get("xoo").isPublishAllFiles()).isTrue();
  65. assertThat(underTest.get("python")).isNotNull();
  66. assertThat(underTest.get("python").fileSuffixes()).containsExactlyInAnyOrder(PYTHON_SUFFIXES);
  67. assertThat(underTest.get("python").isPublishAllFiles()).isTrue();
  68. }
  69. @Test
  70. public void should_throw_error_on_invalid_ws_response() {
  71. WsTestUtil.mockReader(wsClient, "api/languages/list", new StringReader("not json"));
  72. IllegalStateException e = catchThrowableOfType(underTest::start, IllegalStateException.class);
  73. assertThat(e).hasMessage("Fail to parse response of /api/languages/list");
  74. }
  75. @Test
  76. public void should_return_null_when_language_not_found() {
  77. WsTestUtil.mockReader(wsClient, "/api/languages/list",
  78. new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
  79. when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
  80. when(properties.getStringArray("sonar.xoo.file.suffixes")).thenReturn(XOO_SUFFIXES);
  81. when(properties.getStringArray("sonar.python.file.suffixes")).thenReturn(PYTHON_SUFFIXES);
  82. assertThat(underTest.get("k1")).isNull();
  83. }
  84. @Test
  85. public void publishAllFiles_by_default() {
  86. WsTestUtil.mockReader(wsClient, "/api/languages/list",
  87. new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
  88. underTest.start();
  89. assertThat(underTest.get("java").isPublishAllFiles()).isTrue();
  90. assertThat(underTest.get("xoo").isPublishAllFiles()).isTrue();
  91. assertThat(underTest.get("python").isPublishAllFiles()).isTrue();
  92. }
  93. @Test
  94. public void get_find_language_by_key() {
  95. WsTestUtil.mockReader(wsClient, "/api/languages/list",
  96. new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
  97. when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
  98. underTest.start();
  99. assertThat(underTest.get("java"))
  100. .extracting("key", "name", "fileSuffixes", "publishAllFiles")
  101. .containsOnly("java", "Java", JAVA_SUFFIXES, true);
  102. }
  103. @Test
  104. public void should_log_if_language_has_no_suffixes_or_patterns() {
  105. WsTestUtil.mockReader(wsClient, "/api/languages/list",
  106. new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
  107. when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
  108. when(properties.getStringArray("sonar.xoo.file.patterns")).thenReturn(XOO_PATTERNS);
  109. underTest.start();
  110. assertThat(logTester.logs(Level.DEBUG)).contains("Language 'Python' cannot be detected as it has neither suffixes nor patterns.");
  111. }
  112. }