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
|
/*
* SonarQube Scanner
* Copyright (C) 2011-2016 SonarSource SA
* mailto:contact 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.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.sonar.runner.api.EmbeddedRunner;
import org.sonarsource.scanner.cli.Cli;
import org.sonarsource.scanner.cli.Conf;
import org.sonarsource.scanner.cli.Exit;
import org.sonarsource.scanner.cli.Logs;
import org.sonarsource.scanner.cli.Main;
import org.sonarsource.scanner.cli.RunnerFactory;
import org.sonarsource.scanner.cli.Shutdown;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class MainTest {
@Mock
private Shutdown shutdown;
@Mock
private Cli cli;
@Mock
private Conf conf;
@Mock
private Properties properties;
@Mock
private RunnerFactory runnerFactory;
@Mock
private EmbeddedRunner runner;
@Mock
private Logs logs;
@Before
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
when(conf.properties()).thenReturn(properties);
}
@Test
public void should_execute_runner() {
Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
main.execute();
verify(shutdown).exit(Exit.SUCCESS);
verify(runnerFactory).create(properties);
verify(runner, times(1)).start();
verify(runner, times(1)).runAnalysis(properties);
verify(runner, times(1)).stop();
}
@Test
public void should_stop_on_error() {
EmbeddedRunner runner = mock(EmbeddedRunner.class);
Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e);
doThrow(e).when(runner).runAnalysis(any(Properties.class));
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
main.execute();
verify(runner).stop();
verify(shutdown).exit(Exit.ERROR);
verify(logs).error("Caused by: NPE");
}
@Test
public void show_error_stacktrace() {
Exception e = new NullPointerException("NPE");
e = new IllegalStateException("Error", e);
when(cli.isDisplayStackTrace()).thenReturn(true);
EmbeddedRunner runner = mock(EmbeddedRunner.class);
doThrow(e).when(runner).runAnalysis(any(Properties.class));
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
main.execute();
verify(runner).stop();
verify(shutdown).exit(Exit.ERROR);
verify(logs).error("Error during Sonar runner execution", e);
}
@Test
public void should_not_stop_on_error_in_interactive_mode() throws Exception {
EmbeddedRunner runner = mock(EmbeddedRunner.class);
doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class));
when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
when(cli.isInteractive()).thenReturn(true);
Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
BufferedReader inputReader = mock(BufferedReader.class);
when(inputReader.readLine()).thenReturn("");
when(shutdown.shouldExit()).thenReturn(false).thenReturn(true);
main.setInputReader(inputReader);
main.execute();
verify(runner, times(2)).runAnalysis(any(Properties.class));
verify(runner).stop();
verify(shutdown).exit(Exit.SUCCESS);
}
@Test
public void should_only_display_version() throws IOException {
Properties p = new Properties();
when(cli.isDisplayVersionOnly()).thenReturn(true);
when(conf.properties()).thenReturn(p);
Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
main.execute();
InOrder inOrder = Mockito.inOrder(shutdown, runnerFactory);
inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
inOrder.verify(runnerFactory, times(1)).create(p);
inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
}
@Test(timeout = 30000)
public void test_interactive_mode() throws IOException {
String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator();
InputStream input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
System.setIn(input);
input.close();
when(cli.isInteractive()).thenReturn(true);
when(cli.isDebugMode()).thenReturn(true);
when(cli.isDisplayStackTrace()).thenReturn(true);
Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
main.execute();
verify(runner, times(1)).start();
verify(runner, times(3)).runAnalysis(any(Properties.class));
verify(runner, times(1)).stop();
}
}
|