]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Add unit tests
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 5 Apr 2013 22:59:45 +0000 (00:59 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 5 Apr 2013 22:59:45 +0000 (00:59 +0200)
19 files changed:
sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java
sonar-runner-dist/pom.xml
sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java
sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java [new file with mode: 0644]
sonar-runner-dist/src/main/java/org/sonar/runner/Main.java
sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java [new file with mode: 0644]
sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java
sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java [new file with mode: 0644]
sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java [new file with mode: 0644]
sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties [deleted file]
sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties [deleted file]
sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties [deleted file]
sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties [deleted file]

index ec148d667ce99198799c49a980703de58dbc9abb..c7769b79435785c11b139aaf6814fe2009cbe279 100644 (file)
@@ -71,6 +71,10 @@ public class ForkedRunner extends Runner<ForkedRunner> {
     return this;
   }
 
+  public List<String> jvmArguments() {
+    return new ArrayList<String>(jvmArguments);
+  }
+
   /**
    * See {@link #addJvmArguments(java.util.List)}
    */
index ee87a883ac516c03b9045e0f1d276d9588bced5b..7cfa62737c406f53e0b17ac93b3aed32f8f572fa 100644 (file)
@@ -93,6 +93,7 @@ public class ForkedRunnerTest {
     runner.setStdOut(mock(StreamConsumer.class));
     runner.setStdErr(mock(StreamConsumer.class));
 
+    assertThat(runner.jvmArguments()).contains("-Xmx512m");
     runner.execute();
 
     verify(commandExecutor).execute(argThat(new ArgumentMatcher<Command>() {
index b5e023a4b212c6e3d10e867f90554c638e61768f..54d2e6d0e80ab1b8a74c814fe3c803ad5a6a598c 100644 (file)
             <configuration>
               <rules>
                 <requireFilesSize>
-                  <minsize>200000</minsize>
-                  <maxsize>220000</maxsize>
+                  <minsize>220000</minsize>
+                  <maxsize>240000</maxsize>
                   <files>
                     <file>${pom.build.directory}/sonar-runner-${pom.version}.zip</file>
                   </files>
index 504fe3f40bd7d40774a29f2b9476bd4150c124f2..13b5239b6ce74ffd707c835de5afdda7a8b7fa13 100644 (file)
@@ -39,7 +39,7 @@ class Conf {
     this.cli = cli;
   }
 
-  Properties load() throws IOException {
+  Properties properties() throws IOException {
     Properties result = new Properties();
     result.putAll(loadGlobalProperties());
     result.putAll(loadProjectProperties());
@@ -47,12 +47,11 @@ class Conf {
     result.putAll(cli.properties());
 
     if (result.containsKey(PROJECT_HOME)) {
-      // the real property of the Sonar Runner is "sonar.projectDir"
+      // the real property of the Sonar Runner is "sonar.projectBaseDir"
       String baseDir = result.getProperty(PROJECT_HOME);
       result.remove(PROJECT_HOME);
       result.put("sonar.projectBaseDir", baseDir);
     }
-
     return result;
   }
 
diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java
new file mode 100644 (file)
index 0000000..c1b8e74
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program 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.
+ *
+ * This program 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  02
+ */
+package org.sonar.runner;
+
+class Exit {
+  static final int SUCCESS = 0;
+  static final int ERROR = 1;
+
+  void exit(int status) {
+    System.exit(status);
+  }
+}
index 8e3a83313a6862e094a613cceaf400feddca1ce0..57a16230bb73988e334b35d0a15a396a2bf7ba9e 100644 (file)
  */
 package org.sonar.runner;
 
-import org.sonar.runner.api.EmbeddedRunner;
 import org.sonar.runner.impl.Logs;
 
-import java.util.Properties;
-
 /**
  * Arguments :
  * <ul>
@@ -40,40 +37,46 @@ public class Main {
 
   public static void main(String[] args) {
     Cli cli = new Cli().parse(args);
-    new Main(cli).execute();
+    Main main = new Main(new Exit(), cli, new Conf(cli), new RunnerFactory());
+    main.execute();
   }
 
+  private final Exit exit;
   private final Cli cli;
+  private final Conf conf;
+  private final RunnerFactory runnerFactory;
 
-  Main(Cli cli) {
+  Main(Exit exit, Cli cli, Conf conf, RunnerFactory runnerFactory) {
+    this.exit = exit;
     this.cli = cli;
+    this.conf = conf;
+    this.runnerFactory = runnerFactory;
   }
 
   void execute() {
     SystemInfo.print();
     if (!cli.isDisplayVersionOnly()) {
-      int status = doExecute(new Conf(cli));
-      System.exit(status);
+      int status = executeTask();
+      exit.exit(status);
     }
   }
 
-  private int doExecute(Conf conf) {
-    if (cli.isDisplayStackTrace()) {
-      Logs.info("Error stacktraces are turned on.");
-    }
+  private int executeTask() {
     Stats stats = new Stats().start();
     try {
-      Properties properties = conf.load();
-      EmbeddedRunner.create().addProperties(properties).execute();
-     // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
+      if (cli.isDisplayStackTrace()) {
+        Logs.info("Error stacktraces are turned on.");
+      }
+      runnerFactory.create(conf.properties()).execute();
+      // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
 
     } catch (Exception e) {
       displayExecutionResult(stats, "FAILURE");
       showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace());
-      return 1;
+      return Exit.ERROR;
     }
     displayExecutionResult(stats, "SUCCESS");
-    return 0;
+    return Exit.SUCCESS;
   }
 
   private void displayExecutionResult(Stats stats, String resultMsg) {
diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java b/sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java
new file mode 100644 (file)
index 0000000..736db56
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program 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.
+ *
+ * This program 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  02
+ */
+package org.sonar.runner;
+
+import org.sonar.runner.api.EmbeddedRunner;
+import org.sonar.runner.api.ForkedRunner;
+import org.sonar.runner.api.Runner;
+
+import java.util.Properties;
+
+class RunnerFactory {
+
+  Runner create(Properties props) {
+    Runner runner;
+    if ("fork".equals(props.getProperty("sonarRunner.mode"))) {
+      String jvmArgs = props.getProperty("sonarRunner.fork.jvmArgs", "");
+      runner = ForkedRunner.create().addJvmArguments(jvmArgs.split(" "));
+
+    } else {
+      runner = EmbeddedRunner.create();
+    }
+    runner.addProperties(props);
+    return runner;
+  }
+}
index 7cb2c385078589ff9d6d82de7c7694e71fb3f914..f363c563a29deea6cbfdba76ddddb75ccbf5954f 100644 (file)
@@ -32,34 +32,33 @@ import static org.mockito.Mockito.when;
 public class ConfTest {
 
   Properties args = new Properties();
-  Conf conf;
+  Cli cli = mock(Cli.class);
+  Conf conf = new Conf(cli);
 
   @Before
   public void initConf() {
-    Cli cli = mock(Cli.class);
     when(cli.properties()).thenReturn(args);
-    conf = new Conf(cli);
   }
 
   @Test
   public void should_load_global_settings_by_home() throws Exception {
-    File home = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/").toURI());
+    File home = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/").toURI());
     args.setProperty("runner.home", home.getCanonicalPath());
 
-    assertThat(conf.load().get("sonar.host.url")).isEqualTo("http://moon/sonar");
+    assertThat(conf.properties().get("sonar.host.url")).isEqualTo("http://moon/sonar");
   }
 
   @Test
   public void should_not_fail_if_no_home() throws Exception {
-    assertThat(conf.load()).isNotEmpty();
+    assertThat(conf.properties()).isNotEmpty();
   }
 
   @Test
   public void should_load_conf_by_direct_path() throws Exception {
-    File settings = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
+    File settings = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
     args.setProperty("runner.settings", settings.getCanonicalPath());
 
-    assertThat(conf.load().get("sonar.host.url")).isEqualTo("http://other/sonar");
+    assertThat(conf.properties().get("sonar.host.url")).isEqualTo("http://other/sonar");
   }
 
 //  @Test
diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java
new file mode 100644 (file)
index 0000000..bb462ec
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program 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.
+ *
+ * This program 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  02
+ */
+package org.sonar.runner;
+
+import org.junit.Test;
+import org.sonar.runner.api.Runner;
+
+import java.util.Properties;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.RETURNS_MOCKS;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+public class MainTest {
+
+  Exit exit = mock(Exit.class);
+  Cli cli = mock(Cli.class);
+  Conf conf = mock(Conf.class);
+  RunnerFactory runnerFactory = mock(RunnerFactory.class, RETURNS_MOCKS);
+
+  @Test
+  public void should_execute_runner() {
+    Main main = new Main(exit, cli, conf, runnerFactory);
+    main.execute();
+
+    verify(exit).exit(0);
+  }
+
+  @Test
+  public void should_fail_on_error() {
+    Runner runner = mock(Runner.class);
+    doThrow(new IllegalStateException("Error")).when(runner).execute();
+    when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
+
+    Main main = new Main(exit, cli, conf, runnerFactory);
+    main.execute();
+
+    verify(exit).exit(1);
+  }
+
+  @Test
+  public void should_only_display_version() {
+    when(cli.isDisplayVersionOnly()).thenReturn(true);
+    Main main = new Main(exit, cli, conf, runnerFactory);
+    main.execute();
+    verifyZeroInteractions(runnerFactory);
+  }
+}
diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java
new file mode 100644 (file)
index 0000000..6f21c18
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program 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.
+ *
+ * This program 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  02
+ */
+package org.sonar.runner;
+
+import org.junit.Test;
+import org.sonar.runner.api.EmbeddedRunner;
+import org.sonar.runner.api.ForkedRunner;
+import org.sonar.runner.api.Runner;
+
+import java.util.Properties;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RunnerFactoryTest {
+
+  Properties props = new Properties();
+
+  @Test
+  public void should_create_embedded_runner_by_default() {
+    props.setProperty("foo", "bar");
+    Runner runner = new RunnerFactory().create(props);
+
+    assertThat(runner).isInstanceOf(EmbeddedRunner.class);
+    assertThat(runner.properties().get("foo")).isEqualTo("bar");
+  }
+
+  @Test
+  public void should_create_forked_runner() {
+    props.setProperty("foo", "bar");
+    props.setProperty("sonarRunner.mode", "fork");
+    props.setProperty("sonarRunner.fork.jvmArgs", "-Xms128m -Xmx512m");
+    Runner runner = new RunnerFactory().create(props);
+
+    assertThat(runner).isInstanceOf(ForkedRunner.class);
+    assertThat(runner.properties().get("foo")).isEqualTo("bar");
+    assertThat(((ForkedRunner)runner).jvmArguments()).contains("-Xms128m", "-Xmx512m");
+  }
+}
index 0e57284ac1d6c7e273de0adf3eb59659663273b4..ece65c5a39680ef06919287eae231e44bccd24c6 100644 (file)
@@ -33,4 +33,10 @@ public class SystemInfoTest {
   public void test_os() {
     assertThat(SystemInfo.os()).isNotEmpty();
   }
+
+  @Test
+  public void should_print() {
+    SystemInfo.print();
+    // should mock output
+  }
 }
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties
new file mode 100644 (file)
index 0000000..0d1e025
--- /dev/null
@@ -0,0 +1,4 @@
+project.prop=foo
+
+# overridden property
+overridden.prop=project scope
\ No newline at end of file
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties
new file mode 100644 (file)
index 0000000..7edfb99
--- /dev/null
@@ -0,0 +1,2 @@
+overridden.prop=runner scope
+global.prop=jdbc:mysql:localhost/sonar
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties
new file mode 100644 (file)
index 0000000..740a616
--- /dev/null
@@ -0,0 +1 @@
+sonar.host.url=http://other/sonar
\ No newline at end of file
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties
new file mode 100644 (file)
index 0000000..e7d5c09
--- /dev/null
@@ -0,0 +1,2 @@
+sonar.host.url=http://moon/sonar
+sonar.jdbc.url=jdbc:mysql:localhost/sonar
\ No newline at end of file
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties
deleted file mode 100644 (file)
index 0d1e025..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-project.prop=foo
-
-# overridden property
-overridden.prop=project scope
\ No newline at end of file
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties
deleted file mode 100644 (file)
index 7edfb99..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-overridden.prop=runner scope
-global.prop=jdbc:mysql:localhost/sonar
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties
deleted file mode 100644 (file)
index 740a616..0000000
+++ /dev/null
@@ -1 +0,0 @@
-sonar.host.url=http://other/sonar
\ No newline at end of file
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties
deleted file mode 100644 (file)
index e7d5c09..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-sonar.host.url=http://moon/sonar
-sonar.jdbc.url=jdbc:mysql:localhost/sonar
\ No newline at end of file