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.

MainTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Sonar Standalone Runner
  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;
  21. import org.junit.Test;
  22. import java.io.File;
  23. import java.util.Properties;
  24. import static org.fest.assertions.Assertions.assertThat;
  25. public class MainTest {
  26. @Test
  27. public void shouldParseEmptyArguments() {
  28. Properties props = new Main().parseArguments(new String[]{});
  29. assertThat(props).isEmpty();
  30. }
  31. @Test
  32. public void shouldParseArguments() {
  33. Properties props = new Main().parseArguments(new String[]{"-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
  34. assertThat(props).hasSize(3);
  35. assertThat(props.getProperty("foo")).isEqualTo("bar");
  36. assertThat(props.getProperty("hello")).isEqualTo("world");
  37. assertThat(props.getProperty("boolean")).isEqualTo("true");
  38. }
  39. @Test
  40. public void shouldEnableDebugMode() {
  41. Properties props = new Main().parseArguments(new String[]{"-X"});
  42. assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isEqualTo("true");
  43. }
  44. @Test
  45. public void shouldDisableDebugModeByDefault() {
  46. Properties props = new Main().parseArguments(new String[]{});
  47. assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull();
  48. }
  49. @Test
  50. public void shouldLoadRunnerSettingsByHome() throws Exception {
  51. File home = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/").toURI());
  52. Properties args = new Properties();
  53. args.setProperty("runner.home", home.getCanonicalPath());
  54. Properties props = new Main().loadRunnerProperties(args);
  55. assertThat(props.getProperty("sonar.host.url")).isEqualTo("http://moon/sonar");
  56. }
  57. @Test
  58. public void shouldNotFailIfNoHome() throws Exception {
  59. Properties args = new Properties();
  60. Properties props = new Main().loadRunnerProperties(args);
  61. assertThat(props).isEmpty();
  62. }
  63. @Test
  64. public void shouldLoadRunnerSettingsByDirectPath() throws Exception {
  65. File settings = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
  66. Properties args = new Properties();
  67. args.setProperty("runner.settings", settings.getCanonicalPath());
  68. Properties props = new Main().loadRunnerProperties(args);
  69. assertThat(props.getProperty("sonar.host.url")).isEqualTo("http://other/sonar");
  70. }
  71. @Test
  72. public void shouldLoadCompleteConfiguration() throws Exception {
  73. File runnerHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner").toURI());
  74. File projectHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project").toURI());
  75. Properties props = new Main().loadProperties(new String[]{
  76. "-D", "runner.home=" + runnerHome.getCanonicalPath(),
  77. "-D", "project.home=" + projectHome.getCanonicalPath()
  78. });
  79. assertThat(props.getProperty("project.prop")).isEqualTo("foo");
  80. assertThat(props.getProperty("overridden.prop")).isEqualTo("project scope");
  81. assertThat(props.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar");
  82. }
  83. }