3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.repository.language;
22 import java.io.InputStreamReader;
23 import java.io.StringReader;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.slf4j.event.Level;
28 import org.sonar.api.config.Configuration;
29 import org.sonar.api.resources.Languages;
30 import org.sonar.api.testfixtures.log.LogTester;
31 import org.sonar.scanner.WsTestUtil;
32 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.AssertionsForClassTypes.catchThrowableOfType;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class DefaultLanguagesRepositoryTest {
42 public LogTester logTester = new LogTester();
44 private final DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
45 private final Configuration properties = mock(Configuration.class);
46 private final Languages languages = mock(Languages.class);
47 private final DefaultLanguagesRepository underTest = new DefaultLanguagesRepository(wsClient, properties);
49 private static final String[] JAVA_SUFFIXES = new String[] { ".java", ".jav" };
50 private static final String[] XOO_SUFFIXES = new String[] { ".xoo" };
51 private static final String[] XOO_PATTERNS = new String[] { "Xoofile" };
52 private static final String[] PYTHON_SUFFIXES = new String[] { ".py" };
56 logTester.setLevel(Level.DEBUG);
60 public void should_load_languages_from_ws() {
61 WsTestUtil.mockReader(wsClient, "/api/languages/list",
62 new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
64 when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
65 when(properties.getStringArray("sonar.xoo.file.suffixes")).thenReturn(XOO_SUFFIXES);
66 when(properties.getStringArray("sonar.python.file.suffixes")).thenReturn(PYTHON_SUFFIXES);
71 assertThat(underTest.all()).hasSize(3);
72 assertThat(underTest.get("java")).isNotNull();
73 assertThat(underTest.get("java").fileSuffixes()).containsExactlyInAnyOrder(JAVA_SUFFIXES);
74 assertThat(underTest.get("java").isPublishAllFiles()).isTrue();
75 assertThat(underTest.get("xoo")).isNotNull();
76 assertThat(underTest.get("xoo").fileSuffixes()).containsExactlyInAnyOrder(XOO_SUFFIXES);
77 assertThat(underTest.get("xoo").isPublishAllFiles()).isTrue();
78 assertThat(underTest.get("python")).isNotNull();
79 assertThat(underTest.get("python").fileSuffixes()).containsExactlyInAnyOrder(PYTHON_SUFFIXES);
80 assertThat(underTest.get("python").isPublishAllFiles()).isTrue();
84 public void should_throw_error_on_invalid_ws_response() {
85 WsTestUtil.mockReader(wsClient, "api/languages/list", new StringReader("not json"));
87 IllegalStateException e = catchThrowableOfType(underTest::start, IllegalStateException.class);
89 assertThat(e).hasMessage("Fail to parse response of /api/languages/list");
93 public void should_return_null_when_language_not_found() {
94 WsTestUtil.mockReader(wsClient, "/api/languages/list",
95 new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
97 when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
98 when(properties.getStringArray("sonar.xoo.file.suffixes")).thenReturn(XOO_SUFFIXES);
99 when(properties.getStringArray("sonar.python.file.suffixes")).thenReturn(PYTHON_SUFFIXES);
101 assertThat(underTest.get("k1")).isNull();
105 public void publishAllFiles_by_default() {
106 WsTestUtil.mockReader(wsClient, "/api/languages/list",
107 new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
112 assertThat(underTest.get("java").isPublishAllFiles()).isTrue();
113 assertThat(underTest.get("xoo").isPublishAllFiles()).isTrue();
114 assertThat(underTest.get("python").isPublishAllFiles()).isTrue();
118 public void get_find_language_by_key() {
119 WsTestUtil.mockReader(wsClient, "/api/languages/list",
120 new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
122 when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
127 assertThat(underTest.get("java"))
128 .extracting("key", "name", "fileSuffixes", "publishAllFiles")
129 .containsOnly("java", "Java", JAVA_SUFFIXES, true);
134 public void should_log_if_language_has_no_suffixes_or_patterns() {
135 WsTestUtil.mockReader(wsClient, "/api/languages/list",
136 new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
138 when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
139 when(properties.getStringArray("sonar.xoo.file.patterns")).thenReturn(XOO_PATTERNS);
143 assertThat(logTester.logs(Level.DEBUG)).contains("Language 'Python' cannot be detected as it has neither suffixes nor patterns.");