From f225950c4db72aaf5eaa267261691a0240d6550f Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 5 Apr 2013 23:30:42 +0200 Subject: [PATCH] Do not log encoding when non-project task --- .../main/java/org/sonar/runner/api/Dirs.java | 2 +- .../sonar/runner/api/RunnerProperties.java | 5 ----- .../org/sonar/runner/api/ScanProperties.java | 7 +++++++ .../org/sonar/runner/api/SourceEncoding.java | 20 ++++++++++--------- .../main/java/org/sonar/runner/api/Utils.java | 5 +++++ .../sonar/runner/api/SourceEncodingTest.java | 8 ++++++++ .../java/org/sonar/runner/api/UtilsTest.java | 17 ++++++++++++++++ .../src/main/java/org/sonar/runner/Main.java | 1 - 8 files changed, 49 insertions(+), 16 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java index a831d05..b1ab426 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java @@ -26,7 +26,7 @@ import java.io.File; class Dirs { void init(Runner runner) { - boolean onProject = ScanProperties.SCAN_TASK.equals(runner.property(RunnerProperties.TASK, null)); + boolean onProject = Utils.taskRequiresProject(runner.properties()); if (onProject) { initProjectDirs(runner); } else { diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java index 80c4a0c..999ac07 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java @@ -36,11 +36,6 @@ public interface RunnerProperties { */ String TASK = "sonar.task"; - /** - * Encoding of source and test files. By default it's the platform encoding. - */ - String SOURCE_ENCODING = "sonar.sourceEncoding"; - /** * Working directory containing generated reports and temporary data. */ diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java index f41459b..91e68b8 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java @@ -29,6 +29,7 @@ public interface ScanProperties { /** * Default task + * * @see RunnerProperties#TASK */ String SCAN_TASK = "scan"; @@ -80,4 +81,10 @@ public interface ScanProperties { * Property used to specify the base directory of the project to analyse. Default is ".". */ String PROJECT_BASEDIR = "sonar.projectBaseDir"; + + /** + * Encoding of source and test files. By default it's the platform encoding. + */ + String PROJECT_SOURCE_ENCODING = "sonar.sourceEncoding"; + } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java index 8782ebd..660e929 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java @@ -27,15 +27,17 @@ import java.util.Locale; class SourceEncoding { void init(Runner runner) { - String sourceEncoding = runner.property(RunnerProperties.SOURCE_ENCODING, ""); - boolean platformDependent = false; - if ("".equals(sourceEncoding)) { - sourceEncoding = Charset.defaultCharset().name(); - platformDependent = true; - runner.setProperty(RunnerProperties.SOURCE_ENCODING, sourceEncoding); + boolean onProject = Utils.taskRequiresProject(runner.properties()); + if (onProject) { + String sourceEncoding = runner.property(ScanProperties.PROJECT_SOURCE_ENCODING, ""); + boolean platformDependent = false; + if ("".equals(sourceEncoding)) { + sourceEncoding = Charset.defaultCharset().name(); + platformDependent = true; + runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, sourceEncoding); + } + Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\"" + + (platformDependent ? " (analysis is platform dependent)" : "")); } - Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\"" - + (platformDependent ? " (analysis is platform dependent)" : "")); } - } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java index 046c1dd..a9c18eb 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java @@ -21,6 +21,7 @@ package org.sonar.runner.api; import java.util.Arrays; import java.util.Iterator; +import java.util.Properties; class Utils { private Utils() { @@ -42,4 +43,8 @@ class Utils { } return sb.toString(); } + + static boolean taskRequiresProject(Properties props) { + return ScanProperties.SCAN_TASK.equals(props.get(RunnerProperties.TASK)); + } } diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java index b7270e9..3cce220 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java @@ -32,15 +32,23 @@ public class SourceEncodingTest { @Test public void should_set_default_platform_encoding() throws Exception { + runner.setProperty("sonar.task", "scan"); encoding.init(runner); assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo(Charset.defaultCharset().name()); } @Test public void should_use_parameterized_encoding() throws Exception { + runner.setProperty("sonar.task", "scan"); runner.setProperty("sonar.sourceEncoding", "THE_ISO_1234"); encoding.init(runner); assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo("THE_ISO_1234"); } + @Test + public void should_not_init_encoding_if_not_project_task() throws Exception { + runner.setProperty("sonar.task", "views"); + encoding.init(runner); + assertThat(runner.property("sonar.sourceEncoding", null)).isNull(); + } } diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java index f956c75..8bb4ec2 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java @@ -21,6 +21,8 @@ package org.sonar.runner.api; import org.junit.Test; +import java.util.Properties; + import static org.fest.assertions.Assertions.assertThat; public class UtilsTest { @@ -30,4 +32,19 @@ public class UtilsTest { assertThat(Utils.join(new String[]{"foo"}, ",")).isEqualTo("foo"); assertThat(Utils.join(new String[]{"foo", "bar"}, ",")).isEqualTo("foo,bar"); } + + @Test + public void task_should_require_project() { + Properties props = new Properties(); + props.setProperty("sonar.task", "scan"); + assertThat(Utils.taskRequiresProject(props)).isTrue(); + } + + @Test + public void task_should_not_require_project() { + Properties props = new Properties(); + assertThat(Utils.taskRequiresProject(props)).isFalse(); + props.setProperty("sonar.task", "views"); + assertThat(Utils.taskRequiresProject(props)).isFalse(); + } } 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 a158562..69022f3 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 @@ -78,7 +78,6 @@ public class Main { // Logs.info("Server: " + runner.getSonarServerURL()); // try { // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath()); - //Logs.info("Cache directory: " + runner.getCache().getCacheLocation()); // } catch (IOException e) { // throw new RunnerException("Unable to resolve directory", e); // } -- 2.39.5