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.

CommandExecutorTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Sonar Runner - API
  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.api;
  21. import org.apache.commons.io.FileUtils;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.junit.rules.TemporaryFolder;
  27. import org.junit.rules.TestName;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import static org.fest.assertions.Assertions.assertThat;
  31. import static org.junit.Assert.fail;
  32. public class CommandExecutorTest {
  33. @Rule
  34. public TemporaryFolder tempFolder = new TemporaryFolder();
  35. @Rule
  36. public TestName testName = new TestName();
  37. @Rule
  38. public ExpectedException thrown = ExpectedException.none();
  39. PrintStreamConsumer stdout = new PrintStreamConsumer(System.out);
  40. PrintStreamConsumer stderr = new PrintStreamConsumer(System.err);
  41. File workDir;
  42. @Before
  43. public void setUp() throws IOException {
  44. workDir = tempFolder.newFolder(testName.getMethodName());
  45. }
  46. @Test
  47. public void should_consume_StdOut_and_StdErr() throws Exception {
  48. final StringBuilder stdOutBuilder = new StringBuilder();
  49. StreamConsumer stdOutConsumer = new StreamConsumer() {
  50. public void consumeLine(String line) {
  51. stdOutBuilder.append(line).append(System.getProperty("line.separator"));
  52. }
  53. };
  54. final StringBuilder stdErrBuilder = new StringBuilder();
  55. StreamConsumer stdErrConsumer = new StreamConsumer() {
  56. public void consumeLine(String line) {
  57. stdErrBuilder.append(line).append(System.getProperty("line.separator"));
  58. }
  59. };
  60. Command command = Command.builder().setExecutable(getScript("output")).setDirectory(workDir).build();
  61. int exitCode = CommandExecutor.create().execute(command, stdOutConsumer, stdErrConsumer, 1000L);
  62. assertThat(exitCode).isEqualTo(0);
  63. String stdOut = stdOutBuilder.toString();
  64. String stdErr = stdErrBuilder.toString();
  65. assertThat(stdOut).contains("stdOut: first line");
  66. assertThat(stdOut).contains("stdOut: second line");
  67. assertThat(stdErr).contains("stdErr: first line");
  68. assertThat(stdErr).contains("stdErr: second line");
  69. }
  70. @Test
  71. public void stdOut_consumer_can_throw_exception() throws Exception {
  72. Command command = Command.builder().setExecutable(getScript("output")).setDirectory(workDir).build();
  73. thrown.expect(CommandException.class);
  74. thrown.expectMessage("Error inside stdOut stream");
  75. CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L);
  76. }
  77. @Test
  78. public void stdErr_consumer_can_throw_exception() throws Exception {
  79. Command command = Command.builder().setExecutable(getScript("output")).setDirectory(workDir).build();
  80. thrown.expect(CommandException.class);
  81. thrown.expectMessage("Error inside stdErr stream");
  82. CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1000L);
  83. }
  84. private static final StreamConsumer NOP_CONSUMER = new StreamConsumer() {
  85. public void consumeLine(String line) {
  86. // nop
  87. }
  88. };
  89. private static final StreamConsumer BAD_CONSUMER = new StreamConsumer() {
  90. public void consumeLine(String line) {
  91. throw new RuntimeException();
  92. }
  93. };
  94. @Test
  95. public void should_use_working_directory_to_store_argument_and_environment_variable() throws Exception {
  96. Command command = Command.builder()
  97. .setDirectory(workDir)
  98. .setExecutable(getScript("echo"))
  99. .addArguments("1")
  100. .setEnvVariable("ENVVAR", "2")
  101. .build();
  102. int exitCode = CommandExecutor.create().execute(command, stdout, stderr, 1000L);
  103. assertThat(exitCode).isEqualTo(0);
  104. File logFile = new File(workDir, "echo.log");
  105. assertThat(logFile).exists();
  106. String log = FileUtils.readFileToString(logFile);
  107. assertThat(log).contains(workDir.getAbsolutePath());
  108. assertThat(log).contains("Parameter: 1");
  109. assertThat(log).contains("Environment variable: 2");
  110. }
  111. @Test
  112. public void should_stop_after_timeout() throws IOException {
  113. String executable = getScript("forever");
  114. long start = System.currentTimeMillis();
  115. try {
  116. Command command = Command.builder().setExecutable(executable).setDirectory(workDir).build();
  117. CommandExecutor.create().execute(command, stdout, stderr, 300L);
  118. fail();
  119. } catch (CommandException e) {
  120. long duration = System.currentTimeMillis() - start;
  121. // should test >= 300 but it strangly fails during build on windows.
  122. // The timeout is raised after 297ms (??)
  123. assertThat(duration).as(e.getMessage()).isGreaterThan(290L);
  124. }
  125. }
  126. @Test
  127. public void should_fail_if_script_not_found() {
  128. thrown.expect(CommandException.class);
  129. Command command = Command.builder().setExecutable("notfound").setDirectory(workDir).build();
  130. CommandExecutor.create().execute(command, stdout, stderr, 1000L);
  131. }
  132. private static String getScript(String name) throws IOException {
  133. String filename;
  134. if (new Os().isWindows()) {
  135. filename = name + ".bat";
  136. } else {
  137. filename = name + ".sh";
  138. }
  139. return new File("src/test/scripts/" + filename).getCanonicalPath();
  140. }
  141. }