Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MainTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube Runner - Distribution
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.mockito.MockitoAnnotations;
  22. import org.mockito.Mock;
  23. import org.junit.Before;
  24. import org.mockito.Mockito;
  25. import org.mockito.InOrder;
  26. import org.junit.Test;
  27. import org.sonar.runner.api.Runner;
  28. import java.io.ByteArrayInputStream;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.nio.charset.StandardCharsets;
  32. import java.util.Properties;
  33. import static org.mockito.Mockito.times;
  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.verify;
  38. import static org.mockito.Mockito.when;
  39. public class MainTest {
  40. @Mock
  41. private Shutdown exit;
  42. @Mock
  43. private Cli cli;
  44. @Mock
  45. private Conf conf;
  46. @Mock
  47. private Properties properties;
  48. @Mock
  49. private RunnerFactory runnerFactory;
  50. @Mock
  51. private Runner<?> runner;
  52. @Before
  53. public void setUp() throws IOException {
  54. MockitoAnnotations.initMocks(this);
  55. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  56. when(conf.properties()).thenReturn(properties);
  57. }
  58. @Test
  59. public void should_execute_runner() {
  60. Main main = new Main(exit, cli, conf, runnerFactory);
  61. main.execute();
  62. verify(exit).exit(Exit.SUCCESS);
  63. verify(runnerFactory).create(properties);
  64. verify(runner, times(1)).start();
  65. verify(runner, times(1)).runAnalysis(properties);
  66. verify(runner, times(1)).stop();
  67. }
  68. @Test
  69. public void should_fail_on_error() {
  70. Runner<?> runner = mock(Runner.class);
  71. doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class));
  72. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  73. Main main = new Main(exit, cli, conf, runnerFactory);
  74. main.execute();
  75. verify(exit).exit(Exit.ERROR);
  76. }
  77. @Test
  78. public void should_only_display_version() throws IOException {
  79. Properties p = new Properties();
  80. when(cli.isDisplayVersionOnly()).thenReturn(true);
  81. when(conf.properties()).thenReturn(p);
  82. Main main = new Main(exit, cli, conf, runnerFactory);
  83. main.execute();
  84. InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
  85. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  86. inOrder.verify(runnerFactory, times(1)).create(p);
  87. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  88. }
  89. @Test(timeout=30000)
  90. public void test_interactive_mode() throws IOException {
  91. String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator();
  92. InputStream input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
  93. System.setIn(input);
  94. input.close();
  95. when(cli.isInteractive()).thenReturn(true);
  96. when(cli.isDebugMode()).thenReturn(true);
  97. when(cli.isDisplayStackTrace()).thenReturn(true);
  98. Main main = new Main(exit, cli, conf, runnerFactory);
  99. main.execute();
  100. verify(runner, times(1)).start();
  101. verify(runner, times(3)).runAnalysis(any(Properties.class));
  102. verify(runner, times(1)).stop();
  103. }
  104. }