From: Simon Brandhof Date: Thu, 24 Apr 2014 22:01:03 +0000 (+0200) Subject: SONAR-5234 new org.sonar.api.utils.command.TimeoutException X-Git-Tag: 4.4-RC1~1400 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9889c094f169beff6c9b9104f594b8f9ac20f0c8;p=sonarqube.git SONAR-5234 new org.sonar.api.utils.command.TimeoutException --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java index 562f0e4488b..211031ee01c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java @@ -19,13 +19,11 @@ */ 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; } 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 d6f5ea64b92..bdb9cd4b3ef 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 @@ -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 index 00000000000..74eb4436fe5 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/TimeoutException.java @@ -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); + } +} 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 b3891832993..3eaca6e0072 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 @@ -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 (??)