From 84e2798123e242bfe786359dfff9865e1a230793 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 3 May 2012 09:14:29 +0000 Subject: [PATCH] SONARPLUGINS-1619 Support the properties sonar.showSql and sonar.showSqlResults --- src/main/java/org/sonar/runner/Launcher.java | 43 +++++++++++++------ .../java/org/sonar/runner/LauncherTest.java | 35 +++++++++++++-- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/sonar/runner/Launcher.java b/src/main/java/org/sonar/runner/Launcher.java index 3567126..a429d0f 100644 --- a/src/main/java/org/sonar/runner/Launcher.java +++ b/src/main/java/org/sonar/runner/Launcher.java @@ -20,16 +20,14 @@ 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(); diff --git a/src/test/java/org/sonar/runner/LauncherTest.java b/src/test/java/org/sonar/runner/LauncherTest.java index f857441..ea3de33 100644 --- a/src/test/java/org/sonar/runner/LauncherTest.java +++ b/src/test/java/org/sonar/runner/LauncherTest.java @@ -19,14 +19,17 @@ */ 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")); + } + } -- 2.39.5