From a9bf502a052d2edc80526521de7407b43d751973 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 7 Apr 2014 17:12:26 +0200 Subject: [PATCH] Fix some quality flaws and update coverage --- .../java/org/sonar/runner/api/ForkedRunner.java | 3 +-- .../org/sonar/runner/api/RunnerVersion.java | 9 +++++---- .../java/org/sonar/runner/api/package-info.java | 3 ++- .../sonar/runner/batch/IsolatedLauncher.java | 16 ++++++++++------ .../org/sonar/runner/batch/package-info.java | 3 ++- .../src/main/java/org/sonar/runner/Main.java | 17 ++++++++--------- .../java/org/sonar/runner/package-info.java | 3 ++- .../test/java/org/sonar/runner/ConfTest.java | 12 ++++++++++++ .../project/module2.properties | 2 ++ .../project/module_1/sonar-project.properties | 1 + .../project/module_2/Sample.js | 0 .../project/module_3/sonar-project.properties | 1 + .../project/sonar-project.properties | 4 ++++ .../org/sonar/runner/impl/BatchLauncher.java | 3 +-- .../org/sonar/runner/impl/ServerConnection.java | 11 +++++++---- .../org/sonar/runner/impl/package-info.java | 3 ++- 16 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module2.properties create mode 100644 sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_1/sonar-project.properties create mode 100644 sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_2/Sample.js create mode 100644 sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_3/sonar-project.properties create mode 100644 sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/sonar-project.properties diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index 9dff8ed..02ae333 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -194,8 +194,7 @@ public class ForkedRunner extends Runner { 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); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java index e5a52c7..2aa3b97 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerVersion.java @@ -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; + } + } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/package-info.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/package-info.java index b12772c..cebd22d 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/package-info.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/package-info.java @@ -20,4 +20,5 @@ @ParametersAreNonnullByDefault package org.sonar.runner.api; -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java index 26fcf4c..a451dbd 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/IsolatedLauncher.java @@ -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 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; } } diff --git a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/package-info.java b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/package-info.java index 70b5c1f..33773c8 100644 --- a/sonar-runner-batch/src/main/java/org/sonar/runner/batch/package-info.java +++ b/sonar-runner-batch/src/main/java/org/sonar/runner/batch/package-info.java @@ -24,4 +24,5 @@ @ParametersAreNonnullByDefault package org.sonar.runner.batch; -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java index 67d24c4..aae7915 100644 --- a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java @@ -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."); } - } diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/package-info.java b/sonar-runner-dist/src/main/java/org/sonar/runner/package-info.java index 19bdfab..9dbe260 100644 --- a/sonar-runner-dist/src/main/java/org/sonar/runner/package-info.java +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/package-info.java @@ -20,4 +20,5 @@ @ParametersAreNonnullByDefault package org.sonar.runner; -import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java index 323be3b..162c5f7 100644 --- a/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java +++ b/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java @@ -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 index 0000000..4df820c --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module2.properties @@ -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 index 0000000..6803263 --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_1/sonar-project.properties @@ -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 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 index 0000000..c074231 --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/module_3/sonar-project.properties @@ -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 index 0000000..999b04c --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project/sonar-project.properties @@ -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 diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java index 74d0a30..49383ce 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/BatchLauncher.java @@ -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 extensions) { - Object launcher = AccessController.doPrivileged(new PrivilegedAction() { + return AccessController.doPrivileged(new PrivilegedAction() { public Object run() { List jarFiles = jarDownloader.checkVersionAndDownload(); String[][] maskRules = getMaskRules(props); @@ -97,7 +97,6 @@ public class BatchLauncher { } } }); - return launcher; } } diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java index 116ba4f..b68e480 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java @@ -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; diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/package-info.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/package-info.java index 6bdeba8..eff9ce5 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/package-info.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/package-info.java @@ -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; + -- 2.39.5