You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.InOrder;
  30. import org.mockito.Mock;
  31. import org.mockito.Mockito;
  32. import org.mockito.MockitoAnnotations;
  33. import org.sonar.runner.api.EmbeddedRunner;
  34. import static org.mockito.Matchers.any;
  35. import static org.mockito.Mockito.doThrow;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.times;
  38. import static org.mockito.Mockito.verify;
  39. import static org.mockito.Mockito.when;
  40. public class MainTest {
  41. @Mock
  42. private Shutdown shutdown;
  43. @Mock
  44. private Cli cli;
  45. @Mock
  46. private Conf conf;
  47. @Mock
  48. private Properties properties;
  49. @Mock
  50. private RunnerFactory runnerFactory;
  51. @Mock
  52. private EmbeddedRunner runner;
  53. @Mock
  54. private Logs logs;
  55. @Before
  56. public void setUp() throws IOException {
  57. MockitoAnnotations.initMocks(this);
  58. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  59. when(conf.properties()).thenReturn(properties);
  60. }
  61. @Test
  62. public void should_execute_runner() {
  63. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  64. main.execute();
  65. verify(shutdown).exit(Exit.SUCCESS);
  66. verify(runnerFactory).create(properties);
  67. verify(runner, times(1)).start();
  68. verify(runner, times(1)).runAnalysis(properties);
  69. verify(runner, times(1)).stop();
  70. }
  71. @Test
  72. public void should_stop_on_error() {
  73. EmbeddedRunner runner = mock(EmbeddedRunner.class);
  74. Exception e = new NullPointerException("NPE");
  75. e = new IllegalStateException("Error", e);
  76. doThrow(e).when(runner).runAnalysis(any(Properties.class));
  77. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  78. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  79. main.execute();
  80. verify(runner).stop();
  81. verify(shutdown).exit(Exit.ERROR);
  82. verify(logs).error("Caused by: NPE");
  83. }
  84. @Test
  85. public void show_error_stacktrace() {
  86. Exception e = new NullPointerException("NPE");
  87. e = new IllegalStateException("Error", e);
  88. when(cli.isDisplayStackTrace()).thenReturn(true);
  89. EmbeddedRunner runner = mock(EmbeddedRunner.class);
  90. doThrow(e).when(runner).runAnalysis(any(Properties.class));
  91. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  92. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  93. main.execute();
  94. verify(runner).stop();
  95. verify(shutdown).exit(Exit.ERROR);
  96. verify(logs).error("Error during SonarQube Scanner execution", e);
  97. }
  98. @Test
  99. public void should_not_stop_on_error_in_interactive_mode() throws Exception {
  100. EmbeddedRunner runner = mock(EmbeddedRunner.class);
  101. doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class));
  102. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  103. when(cli.isInteractive()).thenReturn(true);
  104. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  105. BufferedReader inputReader = mock(BufferedReader.class);
  106. when(inputReader.readLine()).thenReturn("");
  107. when(shutdown.shouldExit()).thenReturn(false).thenReturn(true);
  108. main.setInputReader(inputReader);
  109. main.execute();
  110. verify(runner, times(2)).runAnalysis(any(Properties.class));
  111. verify(runner).stop();
  112. verify(shutdown).exit(Exit.SUCCESS);
  113. }
  114. @Test
  115. public void should_only_display_version() throws IOException {
  116. Properties p = new Properties();
  117. when(cli.isDisplayVersionOnly()).thenReturn(true);
  118. when(conf.properties()).thenReturn(p);
  119. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  120. main.execute();
  121. InOrder inOrder = Mockito.inOrder(shutdown, runnerFactory);
  122. inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
  123. inOrder.verify(runnerFactory, times(1)).create(p);
  124. inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS);
  125. }
  126. @Test(timeout = 30000)
  127. public void test_interactive_mode() throws IOException {
  128. String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator();
  129. InputStream input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
  130. System.setIn(input);
  131. input.close();
  132. when(cli.isInteractive()).thenReturn(true);
  133. when(cli.isDebugMode()).thenReturn(true);
  134. when(cli.isDisplayStackTrace()).thenReturn(true);
  135. Main main = new Main(shutdown, cli, conf, runnerFactory, logs);
  136. main.execute();
  137. verify(runner, times(1)).start();
  138. verify(runner, times(3)).runAnalysis(any(Properties.class));
  139. verify(runner, times(1)).stop();
  140. }
  141. }