Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MainTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact 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.sonarsource.scanner.cli;
  21. import java.io.BufferedReader;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.Properties;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.mockito.ArgumentCaptor;
  30. import org.mockito.InOrder;
  31. import org.mockito.Mock;
  32. import org.mockito.Mockito;
  33. import org.mockito.MockitoAnnotations;
  34. import org.sonar.runner.api.EmbeddedRunner;
  35. import static org.fest.assertions.Assertions.assertThat;
  36. import static org.mockito.Matchers.any;
  37. import static org.mockito.Mockito.doThrow;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.times;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.when;
  42. public class MainTest {
  43. @Mock
  44. private Shutdown shutdown;
  45. @Mock
  46. private Cli cli;
  47. @Mock
  48. private Conf conf;
  49. @Mock
  50. private Properties properties;
  51. @Mock
  52. private RunnerFactory runnerFactory;
  53. @Mock
  54. private EmbeddedRunner runner;
  55. @Mock
  56. private Logs logs;
  57. @Before
  58. public void setUp() throws IOException {
  59. MockitoAnnotations.initMocks(this);
  60. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  61. when(conf.properties()).thenReturn(properties);
  62. }
  63. @Test
  64. public void should_execute_runner() {
  65. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  66. main.execute();
  67. verify(shutdown).exit(Exit.SUCCESS);
  68. verify(runnerFactory).create(properties);
  69. verify(runner, times(1)).start();
  70. verify(runner, times(1)).runAnalysis(properties);
  71. verify(runner, times(1)).stop();
  72. }
  73. @Test
  74. public void should_stop_on_error() {
  75. EmbeddedRunner runner = mock(EmbeddedRunner.class);
  76. Exception e = new NullPointerException("NPE");
  77. e = new IllegalStateException("Error", e);
  78. doThrow(e).when(runner).runAnalysis(any(Properties.class));
  79. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  80. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  81. main.execute();
  82. verify(runner).stop();
  83. verify(shutdown).exit(Exit.ERROR);
  84. verify(logs).error("Caused by: NPE");
  85. }
  86. @Test
  87. public void show_error_stacktrace() {
  88. Exception e = new NullPointerException("NPE");
  89. e = new IllegalStateException("Error", e);
  90. when(cli.isDisplayStackTrace()).thenReturn(true);
  91. EmbeddedRunner runner = mock(EmbeddedRunner.class);
  92. doThrow(e).when(runner).runAnalysis(any(Properties.class));
  93. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  94. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  95. main.execute();
  96. verify(runner).stop();
  97. verify(shutdown).exit(Exit.ERROR);
  98. verify(logs).error("Error during SonarQube Scanner execution", e);
  99. }
  100. @Test
  101. public void should_not_stop_on_error_in_interactive_mode() throws Exception {
  102. EmbeddedRunner runner = mock(EmbeddedRunner.class);
  103. doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class));
  104. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  105. when(cli.isInteractive()).thenReturn(true);
  106. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  107. BufferedReader inputReader = mock(BufferedReader.class);
  108. when(inputReader.readLine()).thenReturn("");
  109. when(shutdown.shouldExit()).thenReturn(false).thenReturn(true);
  110. main.setInputReader(inputReader);
  111. main.execute();
  112. verify(runner, times(2)).runAnalysis(any(Properties.class));
  113. verify(runner).stop();
  114. verify(shutdown).exit(Exit.SUCCESS);
  115. }
  116. @Test
  117. public void should_only_display_version() throws IOException {
  118. Properties p = new Properties();
  119. when(cli.isDisplayVersionOnly()).thenReturn(true);
  120. when(conf.properties()).thenReturn(p);
  121. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  122. main.execute();
  123. InOrder inOrder = Mockito.inOrder(shutdown, runnerFactory);
  124. inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
  125. inOrder.verify(runnerFactory, times(1)).create(p);
  126. inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
  127. }
  128. @Test
  129. public void shouldLogServerVersion() throws IOException {
  130. when(runner.serverVersion()).thenReturn("5.5");
  131. Properties p = new Properties();
  132. when(cli.isDisplayVersionOnly()).thenReturn(true);
  133. when(conf.properties()).thenReturn(p);
  134. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  135. main.execute();
  136. verify(logs).info("SonarQube server 5.5");
  137. }
  138. @Test
  139. public void should_configure_logging() throws IOException {
  140. Properties p = new Properties();
  141. p.put("sonar.verbose", "true");
  142. when(conf.properties()).thenReturn(p);
  143. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  144. main.execute();
  145. // Logger used for callback should have debug enabled
  146. verify(logs).setDebugEnabled(true);
  147. verify(logs).setDisplayStackTrace(true);
  148. ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
  149. verify(runner).runAnalysis(propertiesCapture.capture());
  150. Properties analysisProps = propertiesCapture.getValue();
  151. assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true");
  152. }
  153. @Test
  154. public void should_configure_logging_trace() throws IOException {
  155. Properties p = new Properties();
  156. p.put("sonar.log.level", "TRACE");
  157. when(conf.properties()).thenReturn(p);
  158. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  159. main.execute();
  160. // Logger used for callback should have debug enabled
  161. verify(logs).setDebugEnabled(true);
  162. verify(logs).setDisplayStackTrace(true);
  163. ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
  164. verify(runner).runAnalysis(propertiesCapture.capture());
  165. Properties analysisProps = propertiesCapture.getValue();
  166. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
  167. }
  168. @Test(timeout = 30000)
  169. public void test_interactive_mode() throws IOException {
  170. String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator();
  171. InputStream input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
  172. System.setIn(input);
  173. input.close();
  174. when(cli.isInteractive()).thenReturn(true);
  175. when(cli.isDebugEnabled()).thenReturn(true);
  176. when(cli.isDisplayStackTrace()).thenReturn(true);
  177. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  178. main.execute();
  179. verify(runner, times(1)).start();
  180. verify(runner, times(3)).runAnalysis(any(Properties.class));
  181. verify(runner, times(1)).stop();
  182. }
  183. }