aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/sonarsource/scanner/cli/MainTest.java
blob: 80847bea595a95c3a2ac2934a17cdc1a2ec3d54b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * SonarScanner CLI
 * Copyright (C) 2011-2022 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarsource.scanner.cli;

import java.util.Map;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.sonar.api.utils.MessageException;
import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.ScanProperties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class MainTest {

  @Mock
  private Exit exit;
  @Mock
  private Cli cli;
  @Mock
  private Conf conf;
  @Mock
  private Properties properties;
  @Mock
  private ScannerFactory scannerFactory;
  @Mock
  private EmbeddedScanner scanner;
  @Mock
  private Logs logs;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(scanner);
    when(conf.properties()).thenReturn(properties);
  }

  @Test
  public void should_execute_runner() {
    when(cli.getInvokedFrom()).thenReturn("");
    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    verify(exit).exit(Exit.SUCCESS);
    verify(scannerFactory).create(properties, "");

    verify(scanner, times(1)).start();
    verify(scanner, times(1)).execute((Map) properties);
  }

  @Test
  public void should_exit_with_error_on_error_during_analysis() {
    EmbeddedScanner runner = mock(EmbeddedScanner.class);
    Exception e = new NullPointerException("NPE");
    e = new IllegalStateException("Error", e);
    doThrow(e).when(runner).execute(any());
    when(cli.getInvokedFrom()).thenReturn("");
    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(runner);
    when(cli.isDebugEnabled()).thenReturn(true);
    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    verify(exit).exit(Exit.INTERNAL_ERROR);
    verify(logs).error("Error during SonarScanner execution", e);
  }

  @Test
  public void should_exit_with_error_on_error_during_start() {
    EmbeddedScanner runner = mock(EmbeddedScanner.class);
    Exception e = new NullPointerException("NPE");
    e = new IllegalStateException("Error", e);
    doThrow(e).when(runner).start();
    when(cli.getInvokedFrom()).thenReturn("");
    when(cli.isDebugEnabled()).thenReturn(true);
    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(runner);

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    verify(runner).start();
    verify(runner, never()).execute(any());
    verify(exit).exit(Exit.INTERNAL_ERROR);
    verify(logs).error("Error during SonarScanner execution", e);
  }

  @Test
  public void show_stacktrace() {
    Exception e = createException(false);
    testException(e, false, false, Exit.INTERNAL_ERROR);

    verify(logs).error("Error during SonarScanner execution", e);
    verify(logs).error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  }

  @Test
  public void dont_show_MessageException_stacktrace() {
    Exception e = createException(true);
    testException(e, false, false, Exit.USER_ERROR);

    verify(logs, times(5)).error(anyString());
    verify(logs).error("Error during SonarScanner execution");
    verify(logs).error("my message");
    verify(logs).error("Caused by: A functional cause");
    verify(logs).error("");
    verify(logs).error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  }

  @Test
  public void dont_show_MessageException_stacktrace_embedded() {
    Exception e = createException(true);
    testException(e, false, true, Exit.USER_ERROR);

    verify(logs, times(4)).error(anyString());
    verify(logs).error("Error during SonarScanner execution");
    verify(logs).error("my message");
    verify(logs).error("Caused by: A functional cause");
    verify(logs).error("");
  }

  @Test
  public void show_MessageException_stacktrace_in_debug() {
    Exception e = createException(true);
    testException(e, true, false, Exit.USER_ERROR);

    verify(logs, times(1)).error(anyString(), any(Throwable.class));
    verify(logs).error("Error during SonarScanner execution", e);
  }

  @Test
  public void show_MessageException_stacktrace_in_debug_embedded() {
    Exception e = createException(true);
    testException(e, true, true, Exit.USER_ERROR);

    verify(logs, times(1)).error(anyString(), any(Throwable.class));
    verify(logs).error("Error during SonarScanner execution", e);
  }

  @Test
  public void show_stacktrace_in_debug() {
    Exception e = createException(false);
    testException(e, true, false, Exit.INTERNAL_ERROR);

    verify(logs).error("Error during SonarScanner execution", e);
    verify(logs, never()).error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  }

  private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) {
    when(cli.isDebugEnabled()).thenReturn(debugEnabled);
    when(cli.isEmbedded()).thenReturn(isEmbedded);
    when(cli.getInvokedFrom()).thenReturn("");

    EmbeddedScanner runner = mock(EmbeddedScanner.class);
    doThrow(e).when(runner).execute(any());

    when(scannerFactory.create(any(Properties.class), any(String.class))).thenReturn(runner);

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    verify(exit).exit(expectedExitCode);
  }

  private Exception createException(boolean messageException) {
    Exception e;
    if (messageException) {
      e = new MessageException("my message", new IllegalStateException("A functional cause"));
    } else {
      e = new IllegalStateException("Error", new NullPointerException("NPE"));
    }

    return e;
  }

  @Test
  public void should_only_display_version() {
    Properties p = new Properties();
    when(cli.isDisplayVersionOnly()).thenReturn(true);
    when(cli.getInvokedFrom()).thenReturn("");
    when(conf.properties()).thenReturn(p);

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    InOrder inOrder = Mockito.inOrder(exit, scannerFactory);

    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
    inOrder.verify(scannerFactory, times(1)).create(p, "");
    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  }

  @Test
  public void should_skip() {
    Properties p = new Properties();
    p.setProperty(ScanProperties.SKIP, "true");
    when(conf.properties()).thenReturn(p);
    when(cli.getInvokedFrom()).thenReturn("");

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    verify(logs).info("SonarScanner analysis skipped");
    InOrder inOrder = Mockito.inOrder(exit, scannerFactory);

    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
    inOrder.verify(scannerFactory, times(1)).create(p, "");
    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  }

  @Test
  public void shouldLogServerVersion() {
    when(scanner.serverVersion()).thenReturn("5.5");
    Properties p = new Properties();
    when(cli.isDisplayVersionOnly()).thenReturn(true);
    when(cli.getInvokedFrom()).thenReturn("");
    when(conf.properties()).thenReturn(p);

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();
    verify(logs).info("Analyzing on SonarQube server 5.5");
  }

  @Test
  public void should_log_SonarCloud_server() {
    Properties p = new Properties();
    p.setProperty("sonar.host.url", "https://sonarcloud.io");
    when(conf.properties()).thenReturn(p);
    when(cli.getInvokedFrom()).thenReturn("");

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();
    verify(logs).info("Analyzing on SonarCloud");
  }

  // SQSCANNER-57
  @Test
  public void should_return_true_is_sonar_cloud() {

    Properties properties = new Properties();
    properties.setProperty("sonar.host.url", "https://sonarcloud.io");

    assertThat(Main.isSonarCloud(properties)).isTrue();
  }

  // SQSCANNER-57
  @Test
  public void should_return_false_is_sonar_cloud() {
    Properties properties = new Properties();
    properties.setProperty("sonar.host.url", "https://mysonarqube.com:9000/");

    assertThat(Main.isSonarCloud(properties)).isFalse();
  }

  // SQSCANNER-57
  @Test
  public void should_return_false_is_sonar_cloud_host_is_null() {
    assertThat(Main.isSonarCloud(new Properties())).isFalse();
  }

  @Test
  public void should_configure_logging() {
    Properties analysisProps = testLogging("sonar.verbose", "true");
    assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true");
  }

  @Test
  public void should_configure_logging_trace() {
    Properties analysisProps = testLogging("sonar.log.level", "TRACE");
    assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
  }

  @Test
  public void should_configure_logging_debug() {
    Properties analysisProps = testLogging("sonar.log.level", "DEBUG");
    assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG");
  }

  private Properties testLogging(String propKey, String propValue) {
    Properties p = new Properties();
    p.put(propKey, propValue);
    when(conf.properties()).thenReturn(p);
    when(cli.getInvokedFrom()).thenReturn("");

    Main main = new Main(exit, cli, conf, scannerFactory, logs);
    main.execute();

    // Logger used for callback should have debug enabled
    verify(logs).setDebugEnabled(true);

    ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
    verify(scanner).execute((Map) propertiesCapture.capture());

    return propertiesCapture.getValue();
  }

}