You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandTest.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.api;
  21. import org.junit.Test;
  22. import java.util.Arrays;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import static org.fest.assertions.Assertions.assertThat;
  26. import static org.fest.assertions.Fail.fail;
  27. import static org.fest.assertions.MapAssert.entry;
  28. public class CommandTest {
  29. @Test
  30. public void test_simple_build() throws Exception {
  31. Command command = Command.builder().setExecutable("java").build();
  32. assertThat(command.executable()).isEqualTo("java");
  33. assertThat(command.envVariables()).isEmpty();
  34. assertThat(command.arguments()).isEmpty();
  35. assertThat(command.toStrings()).containsOnly("java");
  36. assertThat(command.toString()).isEqualTo("java");
  37. }
  38. @Test
  39. public void test_arguments_and_env_variables() throws Exception {
  40. Map<String, String> env = new HashMap<String, String>();
  41. env.put("USER_HOME", "/user");
  42. Command command = Command.builder()
  43. .setExecutable("java")
  44. .addArguments("-Dfoo=bar", "-Djava.io.tmpdir=/tmp")
  45. .addArguments(Arrays.asList("-Xmx512m"))
  46. .setEnvVariable("JAVA_HOME", "/path/to/jdk")
  47. .addEnvVariables(env)
  48. .build();
  49. assertThat(command.executable()).isEqualTo("java");
  50. assertThat(command.envVariables()).hasSize(2).includes(
  51. entry("JAVA_HOME", "/path/to/jdk"),
  52. entry("USER_HOME", "/user")
  53. );
  54. assertThat(command.arguments()).containsSequence("-Dfoo=bar", "-Djava.io.tmpdir=/tmp", "-Xmx512m");
  55. assertThat(command.toStrings()).containsOnly("java", "-Dfoo=bar", "-Djava.io.tmpdir=/tmp", "-Xmx512m");
  56. assertThat(command.toString()).isEqualTo("java -Dfoo=bar -Djava.io.tmpdir=/tmp -Xmx512m");
  57. }
  58. @Test
  59. public void executable_should_be_required() {
  60. try {
  61. Command.builder().build();
  62. fail();
  63. } catch (IllegalArgumentException e) {
  64. // success
  65. }
  66. }
  67. }