]> source.dussan.org Git - sonarqube.git/blob
01b231fbde0575f96056ce44238902e893eb1bb2
[sonarqube.git] /
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
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;
33
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;
38
39 public class DefaultLanguagesRepositoryTest {
40
41   @Rule
42   public LogTester logTester = new LogTester();
43
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);
48
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" };
53
54   @Before
55   public void setup() {
56     logTester.setLevel(Level.DEBUG);
57   }
58
59   @Test
60   public void should_load_languages_from_ws() {
61     WsTestUtil.mockReader(wsClient, "/api/languages/list",
62       new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
63
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);
67
68     underTest.start();
69     underTest.stop();
70
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();
81   }
82
83   @Test
84   public void should_throw_error_on_invalid_ws_response() {
85     WsTestUtil.mockReader(wsClient, "api/languages/list", new StringReader("not json"));
86
87     IllegalStateException e = catchThrowableOfType(underTest::start, IllegalStateException.class);
88
89     assertThat(e).hasMessage("Fail to parse response of /api/languages/list");
90   }
91
92   @Test
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")));
96
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);
100
101     assertThat(underTest.get("k1")).isNull();
102   }
103
104   @Test
105   public void publishAllFiles_by_default() {
106     WsTestUtil.mockReader(wsClient, "/api/languages/list",
107       new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
108
109     underTest.start();
110     underTest.stop();
111
112     assertThat(underTest.get("java").isPublishAllFiles()).isTrue();
113     assertThat(underTest.get("xoo").isPublishAllFiles()).isTrue();
114     assertThat(underTest.get("python").isPublishAllFiles()).isTrue();
115   }
116
117   @Test
118   public void get_find_language_by_key() {
119     WsTestUtil.mockReader(wsClient, "/api/languages/list",
120       new InputStreamReader(getClass().getResourceAsStream("DefaultLanguageRepositoryTest/languages-ws.json")));
121
122     when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
123
124     underTest.start();
125     underTest.stop();
126
127     assertThat(underTest.get("java"))
128       .extracting("key", "name", "fileSuffixes", "publishAllFiles")
129       .containsOnly("java", "Java", JAVA_SUFFIXES, true);
130   }
131
132
133   @Test
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")));
137
138     when(properties.getStringArray("sonar.java.file.suffixes")).thenReturn(JAVA_SUFFIXES);
139     when(properties.getStringArray("sonar.xoo.file.patterns")).thenReturn(XOO_PATTERNS);
140
141     underTest.start();
142
143     assertThat(logTester.logs(Level.DEBUG)).contains("Language 'Python' cannot be detected as it has neither suffixes nor patterns.");
144   }
145
146 }