diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-03-13 19:18:32 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-03-13 19:18:32 +0100 |
commit | 8d4fab85509a8864cecb5781fd4bc8d407a5e964 (patch) | |
tree | bd2790c27180103b68fd46c9f4028e326e2b5c92 /sonar-plugin-api/src/test | |
parent | 287e3997ac3e71b986093033c2cf742925d2893a (diff) | |
download | sonarqube-8d4fab85509a8864cecb5781fd4bc8d407a5e964.tar.gz sonarqube-8d4fab85509a8864cecb5781fd4bc8d407a5e964.zip |
SONAR-2274 API: add utility class to execute command-lines
Diffstat (limited to 'sonar-plugin-api/src/test')
6 files changed, 132 insertions, 0 deletions
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 new file mode 100644 index 00000000000..b0f7bb7ef7b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java @@ -0,0 +1,70 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.utils.command; + +import org.apache.commons.lang.SystemUtils; +import org.junit.Test; + +import java.io.File; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.number.OrderingComparisons.greaterThanOrEqualTo; +import static org.hamcrest.number.OrderingComparisons.lessThan; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +public class CommandExecutorTest { + + @Test + public void shouldEchoArguments() { + String executable = getScript("echo"); + int exitCode = CommandExecutor.create().execute(Command.create(executable), 1000L); + assertThat(exitCode, is(0)); + } + + @Test + public void shouldStopWithTimeout() { + String executable = getScript("forever"); + long start = System.currentTimeMillis(); + try { + CommandExecutor.create().execute(Command.create(executable), 100L); + fail(); + } catch (CommandException e) { + long duration = System.currentTimeMillis()-start; + assertThat(e.getMessage(), duration, greaterThanOrEqualTo(100L)); + assertThat(e.getMessage(), duration, lessThan(1000L)); + } + } + + @Test(expected = CommandException.class) + public void shouldFailIfScriptNotFound() { + CommandExecutor.create().execute(Command.create("notfound"), 1000L); + } + + private String getScript(String name) { + String filename; + if (SystemUtils.IS_OS_WINDOWS) { + filename = name + ".bat"; + } else { + filename = name + ".sh"; + } + return new File("src/test/scripts/" + filename).getPath(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java new file mode 100644 index 00000000000..b32d8946440 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java @@ -0,0 +1,48 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.utils.command; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class CommandTest { + + @Test(expected = IllegalArgumentException.class) + public void shouldFailWhenBlankExecutable() throws Exception { + Command.create(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldFailWhenNullExecutable() throws Exception { + Command.create(null); + } + + @Test + public void shouldCreateCommand() throws Exception { + Command command = Command.create("java"); + command.addArgument("-Xmx512m"); + command.addArgument("-Dfoo=bar"); + assertThat(command.getExecutable(), is("java")); + assertThat(command.getArguments().size(), is(2)); + assertThat(command.toCommandLine(), is("java -Xmx512m -Dfoo=bar")); + } +} diff --git a/sonar-plugin-api/src/test/scripts/echo.bat b/sonar-plugin-api/src/test/scripts/echo.bat new file mode 100755 index 00000000000..4c84e2dc8a9 --- /dev/null +++ b/sonar-plugin-api/src/test/scripts/echo.bat @@ -0,0 +1,2 @@ +@ECHO OFF +@ECHO "Parameter: " + %1 diff --git a/sonar-plugin-api/src/test/scripts/echo.sh b/sonar-plugin-api/src/test/scripts/echo.sh new file mode 100755 index 00000000000..eebd2bd2fe6 --- /dev/null +++ b/sonar-plugin-api/src/test/scripts/echo.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo "Parameter: " + $1
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/scripts/forever.bat b/sonar-plugin-api/src/test/scripts/forever.bat new file mode 100755 index 00000000000..0ecf4e9215a --- /dev/null +++ b/sonar-plugin-api/src/test/scripts/forever.bat @@ -0,0 +1,5 @@ +@ECHO OFF + +:LOOP + @ping 127.0.0.1 -n 2 -w 1000 > nul +GOTO LOOP diff --git a/sonar-plugin-api/src/test/scripts/forever.sh b/sonar-plugin-api/src/test/scripts/forever.sh new file mode 100755 index 00000000000..c7f008117e2 --- /dev/null +++ b/sonar-plugin-api/src/test/scripts/forever.sh @@ -0,0 +1,5 @@ +#!/bin/sh +while test "notempty" +do + sleep 1 +done
\ No newline at end of file |