]> source.dussan.org Git - sonarqube.git/commitdiff
Minor improvements on org.sonar.api.utils.command
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 10 Jul 2012 15:34:00 +0000 (17:34 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 10 Jul 2012 15:47:03 +0000 (17:47 +0200)
* shorten the method CommandExecutor#execute()
* add javadoc

sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java

index 461c82f11f3554498ec61cd0df8af789198d581d..04614bd2bc3afd92e68cfd2963920d96f5d4a399 100644 (file)
@@ -80,6 +80,9 @@ public final class Command {
   }
 
   /**
+   * Adds or overrides an environment variable. The initial values are a copy of the environment
+   * of the current process.
+   *
    * @since 3.2
    */
   public Command setEnvironmentVariable(String name, String value) {
index 05de3914947912d49a4cdd15a44d92bebf6c3afe..0384b5251cb3fdfdfb4a1fb3c743f80ed221db35 100644 (file)
@@ -76,23 +76,17 @@ public final class CommandExecutor {
       errorGobbler.start();
 
       final Process finalProcess = process;
-      Callable<Integer> call = new Callable<Integer>() {
+      executorService = Executors.newSingleThreadExecutor();
+      Future<Integer> ft = executorService.submit(new Callable<Integer>() {
         public Integer call() throws Exception {
           return finalProcess.waitFor();
         }
-      };
-
-      executorService = Executors.newSingleThreadExecutor();
-      Future<Integer> ft = executorService.submit(call);
+      });
       int exitCode = ft.get(timeoutMilliseconds, TimeUnit.MILLISECONDS);
       waitUntilFinish(outputGobbler);
       waitUntilFinish(errorGobbler);
-      if (outputGobbler.getException() != null) {
-        throw new CommandException(command, "Error inside stdOut parser", outputGobbler.getException());
-      }
-      if (errorGobbler.getException() != null) {
-        throw new CommandException(command, "Error inside stdErr parser", errorGobbler.getException());
-      }
+      verifyGobbler(command, outputGobbler, "stdOut");
+      verifyGobbler(command, errorGobbler, "stdErr");
       return exitCode;
 
     } catch (TimeoutException te) {
@@ -116,6 +110,12 @@ public final class CommandExecutor {
     }
   }
 
+  private void verifyGobbler(Command command, StreamGobbler gobbler, String type) {
+    if (gobbler.getException() != null) {
+      throw new CommandException(command, "Error inside " + type + " stream", gobbler.getException());
+    }
+  }
+
   /**
    * Execute command and display error and output streams in log.
    * Method {@link #execute(Command, StreamConsumer, StreamConsumer, long)} is preferable,
index b06194bbbd86f63d3ed7ae19248162d2fdf1c264..391573f8057176814b2f5443d1c4deffb9ac1e12 100644 (file)
@@ -82,7 +82,7 @@ public class CommandExecutorTest {
   public void stdOut_consumer_can_throw_exception() throws Exception {
     Command command = Command.create(getScript("output")).setDirectory(workDir);
     thrown.expect(CommandException.class);
-    thrown.expectMessage("Error inside stdOut parser");
+    thrown.expectMessage("Error inside stdOut stream");
     CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L);
   }
 
@@ -90,7 +90,7 @@ public class CommandExecutorTest {
   public void stdErr_consumer_can_throw_exception() throws Exception {
     Command command = Command.create(getScript("output")).setDirectory(workDir);
     thrown.expect(CommandException.class);
-    thrown.expectMessage("Error inside stdErr parser");
+    thrown.expectMessage("Error inside stdErr stream");
     CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1000L);
   }