]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-1619 Support the properties sonar.showSql and sonar.showSqlResults
authorFabrice Bellingard <bellingard@gmail.com>
Thu, 3 May 2012 09:14:29 +0000 (09:14 +0000)
committerFabrice Bellingard <bellingard@gmail.com>
Thu, 3 May 2012 09:14:29 +0000 (09:14 +0000)
src/main/java/org/sonar/runner/Launcher.java
src/test/java/org/sonar/runner/LauncherTest.java

index 3567126c9fc40a748eb915e8516198f30c5e4f61..a429d0f15e9f8150ba83ba427f2a7c4c5a4dcc80 100644 (file)
 
 package org.sonar.runner;
 
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.joran.JoranConfigurator;
 import ch.qos.logback.core.joran.spi.JoranException;
-import org.apache.commons.configuration.*;
+import org.apache.commons.configuration.CompositeConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.EnvironmentConfiguration;
+import org.apache.commons.configuration.MapConfiguration;
+import org.apache.commons.configuration.SystemConfiguration;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.filefilter.AndFileFilter;
 import org.apache.commons.io.filefilter.FileFileFilter;
@@ -42,6 +40,12 @@ import org.sonar.batch.bootstrapper.EnvironmentInformation;
 import org.sonar.batch.bootstrapper.ProjectDefinition;
 import org.sonar.batch.bootstrapper.Reactor;
 
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
 public class Launcher {
 
   private Runner runner;
@@ -54,24 +58,27 @@ public class Launcher {
    * This method invoked from {@link Main}. Do not rename it.
    */
   public void execute() {
-    initLogging();
-    executeBatch();
+    ProjectDefinition project = defineProject();
+    Configuration initialConfiguration = getInitialConfiguration(project);
+    initLogging(initialConfiguration);
+    executeBatch(project, initialConfiguration);
   }
 
-  private void executeBatch() {
-    ProjectDefinition project = defineProject();
+  private void executeBatch(ProjectDefinition project, Configuration initialConfiguration) {
     Reactor reactor = new Reactor(project);
-    Batch batch = new Batch(getInitialConfiguration(project), new EnvironmentInformation("Runner", runner.getRunnerVersion()), reactor);
+    Batch batch = new Batch(initialConfiguration, new EnvironmentInformation("Runner", runner.getRunnerVersion()), reactor);
     batch.execute();
   }
 
-  private void initLogging() {
+  private void initLogging(Configuration initialConfiguration) {
     LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
     JoranConfigurator jc = new JoranConfigurator();
     jc.setContext(context);
     context.reset();
     InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
     System.setProperty("ROOT_LOGGER_LEVEL", runner.isDebug() ? "DEBUG" : "INFO");
+    context.putProperty("SQL_LOGGER_LEVEL", getSqlLevel(initialConfiguration));// since 2.14. Ignored on previous versions.
+    context.putProperty("SQL_RESULTS_LOGGER_LEVEL", getSqlResultsLevel(initialConfiguration));// since 2.14. Ignored on previous versions.
     try {
       jc.doConfigure(input);
 
@@ -83,6 +90,16 @@ public class Launcher {
     }
   }
 
+  protected static String getSqlLevel(Configuration config) {
+    boolean showSql = config.getBoolean("sonar.showSql", false);
+    return showSql ? "DEBUG" : "WARN";
+  }
+
+  protected static String getSqlResultsLevel(Configuration config) {
+    boolean showSql = config.getBoolean("sonar.showSqlResults", false);
+    return showSql ? "DEBUG" : "WARN";
+  }
+
   private ProjectDefinition defineProject() {
     File baseDir = runner.getProjectDir();
     Properties properties = runner.getProperties();
index f8574412661ad0480a60575fc4b049ed99423637..ea3de33831a1e24f26e56ebc7267dedac4610801 100644 (file)
  */
 package org.sonar.runner;
 
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.junit.Test;
+
+import java.io.File;
+
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.startsWith;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 
-import java.io.File;
-import org.junit.Test;
-
 public class LauncherTest {
 
   @Test
@@ -56,4 +59,30 @@ public class LauncherTest {
     }
   }
 
+  @Test
+  public void testGetSqlLevel() throws Exception {
+    Configuration conf = new BaseConfiguration();
+
+    assertThat(Launcher.getSqlLevel(conf), is("WARN"));
+
+    conf.setProperty("sonar.showSql", "true");
+    assertThat(Launcher.getSqlLevel(conf), is("DEBUG"));
+
+    conf.setProperty("sonar.showSql", "false");
+    assertThat(Launcher.getSqlLevel(conf), is("WARN"));
+  }
+
+  @Test
+  public void testGetSqlResultsLevel() throws Exception {
+    Configuration conf = new BaseConfiguration();
+
+    assertThat(Launcher.getSqlResultsLevel(conf), is("WARN"));
+
+    conf.setProperty("sonar.showSqlResults", "true");
+    assertThat(Launcher.getSqlResultsLevel(conf), is("DEBUG"));
+
+    conf.setProperty("sonar.showSqlResults", "false");
+    assertThat(Launcher.getSqlResultsLevel(conf), is("WARN"));
+  }
+
 }