From abde4bd08f8bf901dba878fc99c8f22c8ca1b071 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 1 Sep 2015 13:56:05 +0200 Subject: Improve unit tests --- .../src/main/java/org/sonar/runner/cli/Cli.java | 34 ++++++++-------- .../src/main/java/org/sonar/runner/cli/Conf.java | 12 +++--- .../src/main/java/org/sonar/runner/cli/Logs.java | 28 ++++++-------- .../src/main/java/org/sonar/runner/cli/Main.java | 45 ++++++++++++---------- .../java/org/sonar/runner/cli/RunnerFactory.java | 42 +++++++++++--------- .../src/main/java/org/sonar/runner/cli/Stats.java | 8 ++-- .../main/java/org/sonar/runner/cli/SystemInfo.java | 10 ++--- 7 files changed, 95 insertions(+), 84 deletions(-) (limited to 'sonar-runner-cli/src/main/java') diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java index f1a038b..8a34607 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java @@ -29,10 +29,12 @@ class Cli { private boolean displayStackTrace = false; private boolean interactive = false; private Properties props = new Properties(); - private Exit exit; + private final Exit exit; + private Logs logger; - public Cli(Exit exit) { + public Cli(Exit exit, Logs logger) { this.exit = exit; + this.logger = logger; } boolean isDebugMode() { @@ -71,14 +73,14 @@ class Cli { } else if ("-e".equals(arg) || "--errors".equals(arg)) { displayStackTrace = true; - Logs.setDisplayStackTrace(true); + logger.setDisplayStackTrace(true); } else if ("-X".equals(arg) || "--debug".equals(arg)) { props.setProperty("sonar.verbose", "true"); displayStackTrace = true; debugMode = true; - Logs.setDebugEnabled(true); - Logs.setDisplayStackTrace(true); + logger.setDebugEnabled(true); + logger.setDisplayStackTrace(true); } else if ("-D".equals(arg) || "--define".equals(arg)) { i++; @@ -130,21 +132,21 @@ class Cli { } private void printError(String message) { - Logs.error(message); + logger.error(message); printUsage(); } private void printUsage() { - Logs.info(""); - Logs.info("usage: sonar-runner [options]"); - Logs.info(""); - Logs.info("Options:"); - Logs.info(" -D,--define Define property"); - Logs.info(" -e,--errors Produce execution error messages"); - Logs.info(" -h,--help Display help information"); - Logs.info(" -v,--version Display version information"); - Logs.info(" -X,--debug Produce execution debug output"); - Logs.info(" -i,--interactive Run interactively"); + logger.info(""); + logger.info("usage: sonar-runner [options]"); + logger.info(""); + logger.info("Options:"); + logger.info(" -D,--define Define property"); + logger.info(" -e,--errors Produce execution error messages"); + logger.info(" -h,--help Display help information"); + logger.info(" -v,--version Display version information"); + logger.info(" -X,--debug Produce execution debug output"); + logger.info(" -i,--interactive Run interactively"); exit.exit(Exit.SUCCESS); } } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java index 68627f7..ac7248f 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java @@ -39,9 +39,11 @@ class Conf { private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties"; private final Cli cli; + private final Logs logger; - Conf(Cli cli) { + Conf(Cli cli, Logs logger) { this.cli = cli; + this.logger = logger; } Properties properties() throws IOException { @@ -57,10 +59,10 @@ class Conf { private Properties loadGlobalProperties() throws IOException { File settingsFile = locatePropertiesFile(cli.properties(), RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS); if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { - Logs.info("Runner configuration file: " + settingsFile.getAbsolutePath()); + logger.info("Runner configuration file: " + settingsFile.getAbsolutePath()); return toProperties(settingsFile); } - Logs.info("Runner configuration file: NONE"); + logger.info("Runner configuration file: NONE"); return new Properties(); } @@ -70,7 +72,7 @@ class Conf { SONAR_PROJECT_PROPERTIES_FILENAME, PROJECT_SETTINGS); if (rootSettingsFile != null && rootSettingsFile.isFile() && rootSettingsFile.exists()) { - Logs.info("Project configuration file: " + rootSettingsFile.getAbsolutePath()); + logger.info("Project configuration file: " + rootSettingsFile.getAbsolutePath()); Properties projectProps = new Properties(); Properties rootProps = toProperties(rootSettingsFile); projectProps.putAll(rootProps); @@ -78,7 +80,7 @@ class Conf { loadModulesProperties(rootProps, projectProps, ""); return projectProps; } - Logs.info("Project configuration file: NONE"); + logger.info("Project configuration file: NONE"); return new Properties(); } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java index d69f3fd..6577a81 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java @@ -20,44 +20,40 @@ package org.sonar.runner.cli; public class Logs { + private boolean debugEnabled = false; + private boolean displayStackTrace = false; - private Logs() { + public void setDebugEnabled(boolean debugEnabled) { + this.debugEnabled = debugEnabled; } - private static boolean debugEnabled = false; - private static boolean displayStackTrace = false; - - public static void setDebugEnabled(boolean debugEnabled) { - Logs.debugEnabled = debugEnabled; - } - - public static void setDisplayStackTrace(boolean displayStackTrace) { - Logs.displayStackTrace = displayStackTrace; + public void setDisplayStackTrace(boolean displayStackTrace) { + this.displayStackTrace = displayStackTrace; } - public static boolean isDebugEnabled() { + public boolean isDebugEnabled() { return debugEnabled; } - public static void debug(String message) { + public void debug(String message) { if (isDebugEnabled()) { System.out.println("DEBUG: " + message); } } - public static void info(String message) { + public void info(String message) { System.out.println("INFO: " + message); } - public static void warn(String message) { + public void warn(String message) { System.out.println("WARN: " + message); } - public static void error(String message) { + public void error(String message) { System.err.println("ERROR: " + message); } - public static void error(String message, Throwable t) { + public void error(String message, Throwable t) { System.err.println("ERROR: " + message); if (t != null && displayStackTrace) { t.printStackTrace(System.err); diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java index 88b4510..0229cc9 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java @@ -46,25 +46,28 @@ public class Main { private EmbeddedRunner runner; private BufferedReader inputReader; private RunnerFactory runnerFactory; + private Logs logger; - Main(Shutdown shutdown, Cli cli, Conf conf, RunnerFactory runnerFactory) { + Main(Shutdown shutdown, Cli cli, Conf conf, RunnerFactory runnerFactory, Logs logger) { this.shutdown = shutdown; this.cli = cli; this.conf = conf; this.runnerFactory = runnerFactory; + this.logger = logger; } public static void main(String[] args) { Exit exit = new Exit(); Shutdown shutdown = new Shutdown(exit); - Cli cli = new Cli(exit).parse(args); + Logs logs = new Logs(); + Cli cli = new Cli(exit, logs).parse(args); cli.verify(); - Main main = new Main(shutdown, cli, new Conf(cli), new RunnerFactory()); + Main main = new Main(shutdown, cli, new Conf(cli, logs), new RunnerFactory(logs), logs); main.execute(); } void execute() { - Stats stats = new Stats().start(); + Stats stats = new Stats(logger).start(); try { Properties p = conf.properties(); @@ -88,7 +91,7 @@ public class Main { private void interactiveLoop(Properties p) throws IOException { do { - Stats stats = new Stats().start(); + Stats stats = new Stats(logger).start(); try { runAnalysis(stats, p); } catch (Exception e) { @@ -99,13 +102,13 @@ public class Main { } private void init(Properties p) throws IOException { - SystemInfo.print(); + SystemInfo.print(logger); if (cli.isDisplayVersionOnly()) { shutdown.exit(Exit.SUCCESS); } if (cli.isDisplayStackTrace()) { - Logs.info("Error stacktraces are turned on."); + logger.info("Error stacktraces are turned on."); } runner = runnerFactory.create(p); @@ -140,43 +143,43 @@ public class Main { this.inputReader = inputReader; } - private static void displayExecutionResult(Stats stats, String resultMsg) { - Logs.info("------------------------------------------------------------------------"); - Logs.info("EXECUTION " + resultMsg); - Logs.info("------------------------------------------------------------------------"); + private void displayExecutionResult(Stats stats, String resultMsg) { + logger.info("------------------------------------------------------------------------"); + logger.info("EXECUTION " + resultMsg); + logger.info("------------------------------------------------------------------------"); stats.stop(); - Logs.info("------------------------------------------------------------------------"); + logger.info("------------------------------------------------------------------------"); } private void showError(String message, Throwable e, boolean showStackTrace) { if (showStackTrace) { - Logs.error(message, e); + logger.error(message, e); if (!cli.isDebugMode()) { - Logs.error(""); + logger.error(""); suggestDebugMode(); } } else { - Logs.error(message); + logger.error(message); if (e != null) { - Logs.error(e.getMessage()); + logger.error(e.getMessage()); String previousMsg = ""; for (Throwable cause = e.getCause(); cause != null && cause.getMessage() != null && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { - Logs.error("Caused by: " + cause.getMessage()); + logger.error("Caused by: " + cause.getMessage()); previousMsg = cause.getMessage(); } } - Logs.error(""); - Logs.error("To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch."); + logger.error(""); + logger.error("To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch."); if (!cli.isDebugMode()) { suggestDebugMode(); } } } - private static void suggestDebugMode() { - Logs.error("Re-run SonarQube Runner using the -X switch to enable full debug logging."); + private void suggestDebugMode() { + logger.error("Re-run SonarQube Runner using the -X switch to enable full debug logging."); } } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java index 3abe645..69406ba 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java @@ -25,26 +25,32 @@ import org.sonar.runner.api.LogOutput; class RunnerFactory { + private final Logs logger; + + public RunnerFactory(Logs logger) { + this.logger = logger; + } + EmbeddedRunner create(Properties props) { - return EmbeddedRunner.create(new LogOutput() { + return EmbeddedRunner.create(new DefaultLogOutput()).addGlobalProperties(props); + } - @Override - public void log(String formattedMessage, Level level) { - switch (level) { - case TRACE: - case DEBUG: - Logs.debug(formattedMessage); - break; - case ERROR: - Logs.error(formattedMessage); - break; - case INFO: - case WARN: - default: - Logs.info(formattedMessage); - } + class DefaultLogOutput implements LogOutput { + @Override + public void log(String formattedMessage, Level level) { + switch (level) { + case TRACE: + case DEBUG: + logger.debug(formattedMessage); + break; + case ERROR: + logger.error(formattedMessage); + break; + case INFO: + case WARN: + default: + logger.info(formattedMessage); } - }).addGlobalProperties(props); + } } - } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java index 5173137..82fbdb5 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java @@ -20,9 +20,11 @@ package org.sonar.runner.cli; class Stats { + private final Logs logger; private long startTime; - Stats() { + Stats(Logs logger) { + this.logger = logger; } Stats start() { @@ -32,12 +34,12 @@ class Stats { Stats stop() { long stopTime = System.currentTimeMillis() - startTime; - Logs.info("Total time: " + formatTime(stopTime)); + logger.info("Total time: " + formatTime(stopTime)); System.gc(); Runtime r = Runtime.getRuntime(); long mb = 1024L * 1024; - Logs.info("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M"); + logger.info("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M"); return this; } diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java index 43b27ab..5d98720 100644 --- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java +++ b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java @@ -31,13 +31,13 @@ class SystemInfo { SystemInfo.system = system; } - static void print() { - Logs.info("SonarQube Runner " + RunnerVersion.version()); - Logs.info(java()); - Logs.info(os()); + static void print(Logs logger) { + logger.info("SonarQube Runner " + RunnerVersion.version()); + logger.info(java()); + logger.info(os()); String runnerOpts = system.getenv("SONAR_RUNNER_OPTS"); if (runnerOpts != null) { - Logs.info("SONAR_RUNNER_OPTS=" + runnerOpts); + logger.info("SONAR_RUNNER_OPTS=" + runnerOpts); } } -- cgit v1.2.3