]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Fix some quality flaws and update coverage
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 7 Apr 2014 15:12:26 +0000 (17:12 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 7 Apr 2014 15:12:26 +0000 (17:12 +0200)
16 files changed:
sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java
sonar-runner-api/src/main/java/org/sonar/runner/api/package-info.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java
sonar-runner-batch/src/main/java/org/sonar/runner/batch/package-info.java
sonar-runner-dist/src/main/java/org/sonar/runner/Main.java
sonar-runner-dist/src/main/java/org/sonar/runner/package-info.java
sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module2.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_1/sonar-project.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_2/Sample.js [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_3/sonar-project.properties [new file with mode: 0644]
sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/sonar-project.properties [new file with mode: 0644]
sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java
sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java
sonar-runner-impl/src/main/java/org/sonar/runner/impl/package-info.java

index 9dff8ed9a62ac3b911db392ef520bc47afe41f6d..02ae3331df9b891dd1622342fc4cc7496475b07d 100644 (file)
@@ -194,8 +194,7 @@ public class ForkedRunner extends Runner<ForkedRunner> {
     if (status != 0) {
       if (processMonitor != null && processMonitor.stop()) {
         stdOut.consumeLine(String.format("SonarQube Runner was stopped [status=%s]", status));
-      }
-      else {
+      } else {
         throw new IllegalStateException("Error status [command: " + forkCommand.command + "]: " + status);
       }
     }
index e5a52c7c59ad645b08b6149164e1eba70248ef98..2aa3b97f888571af8e76642ad28b6927b99ca57b 100644 (file)
@@ -32,10 +32,6 @@ public enum RunnerVersion {
 
   private String version;
 
-  public static String version() {
-    return INSTANCE.version;
-  }
-
   private RunnerVersion() {
     Scanner scanner = new Scanner(getClass().getResourceAsStream("/org/sonar/runner/api/version.txt"), "UTF-8");
     try {
@@ -44,4 +40,9 @@ public enum RunnerVersion {
       scanner.close();
     }
   }
+
+  public static String version() {
+    return INSTANCE.version;
+  }
+
 }
index b12772c5fb76ca194a3fa54e877b4ef252a096d7..cebd22d3ede43341e5bab42966f44cd78001cb68 100644 (file)
@@ -20,4 +20,5 @@
 @ParametersAreNonnullByDefault
 package org.sonar.runner.api;
 
-import javax.annotation.ParametersAreNonnullByDefault;
\ No newline at end of file
+import javax.annotation.ParametersAreNonnullByDefault;
+
index 26fcf4c53f48561a5d2a7d171e08dee620648a3b..a451dbd6a0ab1f58fc1a00e502ee2b2b353bff51 100644 (file)
@@ -40,6 +40,10 @@ import java.util.Properties;
  */
 public class IsolatedLauncher {
 
+  private static final String WARN = "WARN";
+  private static final String DEBUG = "DEBUG";
+  private static final String FALSE = "false";
+
   public void execute(String sonarVersion, Properties properties, List<Object> extensions) {
     createBatch(sonarVersion, properties, extensions).execute();
   }
@@ -60,7 +64,7 @@ public class IsolatedLauncher {
     jc.setContext(context);
     context.reset();
     InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
-    System.setProperty("ROOT_LOGGER_LEVEL", isDebug(props) ? "DEBUG" : "INFO");
+    System.setProperty("ROOT_LOGGER_LEVEL", isDebug(props) ? DEBUG : "INFO");
     context.putProperty("SQL_LOGGER_LEVEL", getSqlLevel(props));
     context.putProperty("SQL_RESULTS_LOGGER_LEVEL", getSqlResultsLevel(props));
     try {
@@ -76,18 +80,18 @@ public class IsolatedLauncher {
 
   @VisibleForTesting
   protected boolean isDebug(Properties props) {
-    return Boolean.parseBoolean(props.getProperty("sonar.verbose", "false"));
+    return Boolean.parseBoolean(props.getProperty("sonar.verbose", FALSE));
   }
 
   @VisibleForTesting
   protected static String getSqlLevel(Properties props) {
-    boolean showSql = "true".equals(props.getProperty("sonar.showSql", "false"));
-    return showSql ? "DEBUG" : "WARN";
+    boolean showSql = "true".equals(props.getProperty("sonar.showSql", FALSE));
+    return showSql ? DEBUG : WARN;
   }
 
   @VisibleForTesting
   protected static String getSqlResultsLevel(Properties props) {
-    boolean showSql = "true".equals(props.getProperty("sonar.showSqlResults", "false"));
-    return showSql ? "DEBUG" : "WARN";
+    boolean showSql = "true".equals(props.getProperty("sonar.showSqlResults", FALSE));
+    return showSql ? DEBUG : WARN;
   }
 }
index 70b5c1f64d406ebf10e2b87f95be4ac12531a27c..33773c8c0cb598ebd049cf4e00ae7a4d6bdc74c9 100644 (file)
@@ -24,4 +24,5 @@
 @ParametersAreNonnullByDefault
 package org.sonar.runner.batch;
 
-import javax.annotation.ParametersAreNonnullByDefault;
\ No newline at end of file
+import javax.annotation.ParametersAreNonnullByDefault;
+
index 67d24c4bbf29d071be6d4fe8011d031a9fd1c107..aae79157728166c15b8e99ae24ef96d80b1d21a8 100644 (file)
@@ -35,12 +35,6 @@ import org.sonar.runner.impl.Logs;
  */
 public class Main {
 
-  public static void main(String[] args) {
-    Cli cli = new Cli().parse(args);
-    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;
@@ -53,6 +47,12 @@ public class Main {
     this.runnerFactory = runnerFactory;
   }
 
+  public static void main(String[] args) {
+    Cli cli = new Cli().parse(args);
+    Main main = new Main(new Exit(), cli, new Conf(cli), new RunnerFactory());
+    main.execute();
+  }
+
   void execute() {
     SystemInfo.print();
     if (!cli.isDisplayVersionOnly()) {
@@ -99,8 +99,8 @@ public class Main {
         Logs.error(e.getMessage());
         String previousMsg = "";
         for (Throwable cause = e.getCause(); cause != null
-            && cause.getMessage() != null
-            && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
+          && cause.getMessage() != null
+          && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
           Logs.error("Caused by: " + cause.getMessage());
           previousMsg = cause.getMessage();
         }
@@ -117,5 +117,4 @@ public class Main {
     Logs.error("Re-run SonarQube Runner using the -X switch to enable full debug logging.");
   }
 
-
 }
index 19bdfab50b90a39b099b64e8cc15aaa66f1d1183..9dbe260c3717051d7bb2d0b6bfff13d167a13a2e 100644 (file)
@@ -20,4 +20,5 @@
 @ParametersAreNonnullByDefault
 package org.sonar.runner;
 
-import javax.annotation.ParametersAreNonnullByDefault;
\ No newline at end of file
+import javax.annotation.ParametersAreNonnullByDefault;
+
index 323be3babab12cac05b748370b4e161b25592403..162c5f71c8966c0f7f84556f7da63ea947943c8e 100644 (file)
@@ -91,6 +91,18 @@ public class ConfTest {
     assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
   }
 
+  @Test
+  public void shouldLoadModuleConfigurationOverrideBasedir() throws Exception {
+    File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
+    args.setProperty("project.home", projectHome.getCanonicalPath());
+
+    Properties properties = conf.properties();
+
+    assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
+    assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
+    assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
+  }
+
   @Test
   public void shouldSupportSettingBaseDirFromCli() throws Exception {
     File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadModuleConfiguration/project").toURI());
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module2.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module2.properties
new file mode 100644 (file)
index 0000000..4df820c
--- /dev/null
@@ -0,0 +1,2 @@
+sonar.projectName=Module 2
+sonar.projectBaseDir=module_2
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_1/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_1/sonar-project.properties
new file mode 100644 (file)
index 0000000..6803263
--- /dev/null
@@ -0,0 +1 @@
+sonar.projectName=Module 1
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_2/Sample.js b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_2/Sample.js
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_3/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_3/sonar-project.properties
new file mode 100644 (file)
index 0000000..c074231
--- /dev/null
@@ -0,0 +1 @@
+sonar.projectName=Module 3
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/sonar-project.properties
new file mode 100644 (file)
index 0000000..999b04c
--- /dev/null
@@ -0,0 +1,4 @@
+sonar.modules=module1,module2,module3
+module1.sonar.projectBaseDir=module_1
+module2.sonar.projectConfigFile=module2.properties
+module3.sonar.projectConfigFile=module_3/sonar-project.properties
index 74d0a30ebd45cba062cd4d6fa588231c5df7d2c3..49383ce584fbc03c75e0322ee78eb1bf1c5eceb3 100644 (file)
@@ -66,7 +66,7 @@ public class BatchLauncher {
    * @return the {@link org.sonar.runner.batch.IsolatedLauncher} instance for unit tests
    */
   Object doExecute(final JarDownloader jarDownloader, final ServerVersion serverVersion, final Properties props, final List<Object> extensions) {
-    Object launcher = AccessController.doPrivileged(new PrivilegedAction<Object>() {
+    return AccessController.doPrivileged(new PrivilegedAction<Object>() {
       public Object run() {
         List<File> jarFiles = jarDownloader.checkVersionAndDownload();
         String[][] maskRules = getMaskRules(props);
@@ -97,7 +97,6 @@ public class BatchLauncher {
         }
       }
     });
-    return launcher;
   }
 
 }
index 116ba4fa545926278b872a0bf21f59b5e53e508d..b68e480a1c337931b5821c03bb2dc6b1739d87d1 100644 (file)
@@ -27,12 +27,15 @@ import java.io.IOException;
 import java.net.ConnectException;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.text.MessageFormat;
 import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 class ServerConnection {
 
+  private static final String SONAR_SERVER_CAN_NOT_BE_REACHED = "Sonar server ''{0}'' can not be reached";
+  private static final String STATUS_RETURNED_BY_URL_IS_INVALID = "Status returned by url : ''{0}'' is invalid : {1}";
   static final int CONNECT_TIMEOUT_MILLISECONDS = 30000;
   static final int READ_TIMEOUT_MILLISECONDS = 60000;
   private static final Pattern CHARSET_PATTERN = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
@@ -65,13 +68,13 @@ class ServerConnection {
       Logs.debug("Download " + fullUrl + " to " + toFile.getAbsolutePath());
       HttpRequest httpRequest = newHttpRequest(new URL(fullUrl));
       if (!httpRequest.ok()) {
-        throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + httpRequest.code());
+        throw new IOException(MessageFormat.format(STATUS_RETURNED_BY_URL_IS_INVALID, fullUrl, httpRequest.code()));
       }
       httpRequest.receive(toFile);
 
     } catch (Exception e) {
       if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
-        Logs.error("Sonar server '" + serverUrl + "' can not be reached");
+        Logs.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
       }
       FileUtils.deleteQuietly(toFile);
       throw new IllegalStateException("Fail to download: " + fullUrl, e);
@@ -88,13 +91,13 @@ class ServerConnection {
         charset = "UTF-8";
       }
       if (!httpRequest.ok()) {
-        throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + httpRequest.code());
+        throw new IOException(MessageFormat.format(STATUS_RETURNED_BY_URL_IS_INVALID, fullUrl, httpRequest.code()));
       }
       return httpRequest.body(charset);
 
     } catch (HttpRequest.HttpRequestException e) {
       if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
-        Logs.error("Sonar server '" + serverUrl + "' can not be reached");
+        Logs.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
       }
       throw e;
 
index 6bdeba83eaa64bbfbb4c5208cf3cc60cb9e10175..eff9ce5fef7f86b932026c3edf389c849aae144a 100644 (file)
@@ -18,4 +18,5 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
  */
 @javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.runner.impl;
\ No newline at end of file
+package org.sonar.runner.impl;
+