From 6d3ab7ef3aa6672e6af3925f670fdd660225fe5f Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 10 Jul 2012 17:34:00 +0200 Subject: [PATCH] Minor improvements on org.sonar.api.utils.command * shorten the method CommandExecutor#execute() * add javadoc --- .../org/sonar/api/utils/command/Command.java | 3 +++ .../api/utils/command/CommandExecutor.java | 22 +++++++++---------- .../utils/command/CommandExecutorTest.java | 4 ++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java index 461c82f11f3..04614bd2bc3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java @@ -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) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java index 05de3914947..0384b5251cb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java @@ -76,23 +76,17 @@ public final class CommandExecutor { errorGobbler.start(); final Process finalProcess = process; - Callable call = new Callable() { + executorService = Executors.newSingleThreadExecutor(); + Future ft = executorService.submit(new Callable() { public Integer call() throws Exception { return finalProcess.waitFor(); } - }; - - executorService = Executors.newSingleThreadExecutor(); - Future 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, diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java index b06194bbbd8..391573f8057 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java @@ -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); } -- 2.39.5