]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5234 new org.sonar.api.utils.command.TimeoutException
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 24 Apr 2014 22:01:03 +0000 (00:01 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 24 Apr 2014 22:02:10 +0000 (00:02 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java

index 562f0e4488b0d480a1b7abfca695d0a3f4bccd77..211031ee01c5517ac81e8a34a903e9ef6bb07a3a 100644 (file)
  */
 package org.sonar.api.utils.command;
 
-import javax.annotation.Nullable;
-
-public final class CommandException extends RuntimeException {
+public class CommandException extends RuntimeException {
 
   private final Command command;
 
-  public CommandException(Command command, String message, @Nullable Throwable throwable) {
+  public CommandException(Command command, String message, Throwable throwable) {
     super(message + " [command: " + command + "]", throwable);
     this.command = command;
   }
index d6f5ea64b92de6c023e22a1ba7b8c489158fd4cd..bdb9cd4b3ef98a9bd2c099a522a037f7885435ab 100644 (file)
@@ -50,7 +50,8 @@ public class CommandExecutor {
   }
 
   /**
-   * @throws CommandException
+   * @throws org.sonar.api.utils.command.TimeoutException on timeout, since 4.4
+   * @throws CommandException on any other error
    * @since 3.0
    */
   public int execute(Command command, StreamConsumer stdOut, StreamConsumer stdErr, long timeoutMilliseconds) {
@@ -86,9 +87,9 @@ public class CommandExecutor {
       verifyGobbler(command, errorGobbler, "stdErr");
       return exitCode;
 
-    } catch (TimeoutException te) {
+    } catch (java.util.concurrent.TimeoutException te) {
       process.destroy();
-      throw new CommandException(command, "Timeout exceeded: " + timeoutMilliseconds + " ms", te);
+      throw new TimeoutException(command, "Timeout exceeded: " + timeoutMilliseconds + " ms", te);
 
     } catch (CommandException e) {
       throw e;
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java
new file mode 100644 (file)
index 0000000..74eb443
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.api.utils.command;
+
+/**
+ * Unchecked version of {@link java.util.concurrent.TimeoutException}
+ *
+ * @since 4.4
+ */
+public class TimeoutException extends CommandException {
+
+  public TimeoutException(Command command, String message, Throwable throwable) {
+    super(command, message, throwable);
+  }
+}
index b38918329938de15b734711032366fe0b96094e5..3eaca6e0072cb22767f384e381998a55d30c7c55 100644 (file)
@@ -110,9 +110,9 @@ public class CommandExecutorTest {
   @Test
   public void should_use_working_directory_to_store_argument_and_environment_variable() throws Exception {
     Command command = Command.create(getScript("echo"))
-        .setDirectory(workDir)
-        .addArgument("1")
-        .setEnvironmentVariable("ENVVAR", "2");
+      .setDirectory(workDir)
+      .addArgument("1")
+      .setEnvironmentVariable("ENVVAR", "2");
     int exitCode = CommandExecutor.create().execute(command, 1000L);
     assertThat(exitCode).isEqualTo(0);
     File logFile = new File(workDir, "echo.log");
@@ -130,7 +130,7 @@ public class CommandExecutorTest {
     try {
       CommandExecutor.create().execute(Command.create(executable).setDirectory(workDir), 300L);
       fail();
-    } catch (CommandException e) {
+    } catch (TimeoutException e) {
       long duration = System.currentTimeMillis() - start;
       // should test >= 300 but it strangly fails during build on windows.
       // The timeout is raised after 297ms (??)