From 49dfb95a5ac0f6c11682b99456ce2da0d4e60176 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 29 Sep 2015 11:29:59 +0200 Subject: [PATCH] base on changes in sonar-home and improve coverage --- .../org/sonar/runner/api/StdOutLogOutput.java | 13 ++- .../runner/impl/IsolatedLauncherFactory.java | 3 + .../sonar/runner/impl/ServerConnection.java | 2 +- .../sonar/runner/api/StdOutLogOutputTest.java | 40 +++++++++ .../runner/impl/ServerConnectionTest.java | 17 ++-- sonar-runner-batch/pom.xml | 10 +++ .../sonar/runner/batch/CompatibilityTest.java | 72 ++++++++++++++++ .../main/java/org/sonar/runner/cli/Logs.java | 21 +++-- .../main/java/org/sonar/runner/cli/Main.java | 2 +- .../java/org/sonar/runner/cli/CliTest.java | 2 +- .../java/org/sonar/runner/cli/ConfTest.java | 2 +- .../java/org/sonar/runner/cli/LogsTest.java | 83 +++++++++++++++++++ .../java/org/sonar/runner/cli/StatsTest.java | 31 +++---- 13 files changed, 259 insertions(+), 39 deletions(-) create mode 100644 sonar-runner-api/src/test/java/org/sonar/runner/api/StdOutLogOutputTest.java create mode 100644 sonar-runner-batch/src/test/java/org/sonar/runner/batch/CompatibilityTest.java create mode 100644 sonar-runner-cli/src/test/java/org/sonar/runner/cli/LogsTest.java diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/StdOutLogOutput.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/StdOutLogOutput.java index eac27ce..880a27e 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/StdOutLogOutput.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/StdOutLogOutput.java @@ -19,10 +19,21 @@ */ package org.sonar.runner.api; +import java.io.PrintStream; + public class StdOutLogOutput implements LogOutput { + private PrintStream stdOut; + + public StdOutLogOutput() { + this(System.out); + } + + StdOutLogOutput(PrintStream stdOut) { + this.stdOut = stdOut; + } @Override public void log(String formattedMessage, org.sonar.runner.api.LogOutput.Level level) { - System.out.println(level.name() + ": " + formattedMessage); + stdOut.println(level.name() + ": " + formattedMessage); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java index 44682fd..59d416c 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java @@ -52,7 +52,10 @@ public class IsolatedLauncherFactory { private PersistentCache getCache(Properties props) { PersistentCacheBuilder builder = new PersistentCacheBuilder(logger); + String serverUrl = props.getProperty("sonar.host.url"); String home = props.getProperty("sonar.userHome"); + + builder.setAreaForGlobal(serverUrl, null); if (home != null) { builder.setSonarHome(Paths.get(home)); } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java index 22081d3..dce9180 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java @@ -157,7 +157,7 @@ class ServerConnection { logger.info(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + ", trying cache", serverUrl)); try { - String cached = wsCache.getString(fullUrl, null); + String cached = wsCache.getString(fullUrl); if (cached != null) { return cached; } diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/StdOutLogOutputTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/StdOutLogOutputTest.java new file mode 100644 index 0000000..ead9645 --- /dev/null +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/StdOutLogOutputTest.java @@ -0,0 +1,40 @@ +/* + * SonarQube Runner - API + * Copyright (C) 2011 SonarSource + * sonarqube@googlegroups.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.api; + +import org.junit.Test; +import org.sonar.runner.api.LogOutput.Level; + +import java.io.PrintStream; + +import static org.mockito.Mockito.verify; + +import static org.mockito.Mockito.mock; + +public class StdOutLogOutputTest { + private PrintStream stdOut = mock(PrintStream.class); + private StdOutLogOutput logOutput = new StdOutLogOutput(stdOut); + + @Test + public void test() { + logOutput.log("msg", Level.INFO); + verify(stdOut).println("INFO: msg"); + } +} diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java index 8723c97..db9e814 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java @@ -19,8 +19,6 @@ */ package org.sonar.runner.impl; -import org.sonar.home.cache.PersistentCacheLoader; - import com.github.kevinsawicki.http.HttpRequest; import java.io.File; @@ -60,7 +58,10 @@ public class ServerConnectionTest { @Before public void setUp() { - cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).build(); + cache = new PersistentCacheBuilder(mock(Logger.class)) + .setAreaForGlobal("server", "5.2") + .setSonarHome(temp.getRoot().toPath()) + .build(); logger = mock(Logger.class); } @@ -101,14 +102,14 @@ public class ServerConnectionTest { connection.downloadStringCache("/batch/index.txt"); fail(); } catch (HttpRequest.HttpRequestException e) { - verify(cache).getString(anyString(), any(PersistentCacheLoader.class)); + verify(cache).getString(anyString()); assertThat(e.getCause()).isInstanceOf(ConnectException.class); } } @Test public void should_cache_jar_list() throws Exception { - File cacheDir = new File(temp.getRoot(), "ws_cache"); + File cacheDir = cache.getDirectory().toFile(); ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null, true); @@ -116,7 +117,7 @@ public class ServerConnectionTest { String str = connection.downloadStringCache("/batch/index.txt"); assertThat(str).isEqualTo("abcde"); - assertThat(cacheDir.list().length).isEqualTo(2); + assertThat(cacheDir.list().length).isEqualTo(1); httpServer.after(); str = connection.downloadStringCache("/batch/index.txt"); @@ -125,7 +126,7 @@ public class ServerConnectionTest { @Test public void should_throw_connection_exception_() throws IOException { - File cacheDir = new File(temp.getRoot(), "ws_cache"); + File cacheDir = cache.getDirectory().toFile(); ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null); assertThat(cacheDir.list().length).isEqualTo(0); @@ -149,7 +150,7 @@ public class ServerConnectionTest { @Test public void should_not_cache_not_issues_mode() throws Exception { - File cacheDir = new File(temp.getRoot(), "ws_cache"); + File cacheDir = cache.getDirectory().toFile(); ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null); assertThat(cacheDir.list().length).isEqualTo(0); diff --git a/sonar-runner-batch/pom.xml b/sonar-runner-batch/pom.xml index e2fd475..20e039d 100644 --- a/sonar-runner-batch/pom.xml +++ b/sonar-runner-batch/pom.xml @@ -44,6 +44,16 @@ org.easytesting fest-assert test + + + junit + junit + test + + + org.mockito + mockito-all + test diff --git a/sonar-runner-batch/src/test/java/org/sonar/runner/batch/CompatibilityTest.java b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/CompatibilityTest.java new file mode 100644 index 0000000..6bfa09c --- /dev/null +++ b/sonar-runner-batch/src/test/java/org/sonar/runner/batch/CompatibilityTest.java @@ -0,0 +1,72 @@ +/* + * SonarQube Runner - Batch + * Copyright (C) 2011 SonarSource + * sonarqube@googlegroups.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.batch; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.mockito.ArgumentCaptor; +import org.junit.Test; + +public class CompatibilityTest { + @Test + public void test() { + IssueListener issueListener = mock(IssueListener.class); + + org.sonar.batch.bootstrapper.IssueListener.Issue issue = new org.sonar.batch.bootstrapper.IssueListener.Issue(); + setIssue(issue); + + org.sonar.batch.bootstrapper.IssueListener adaptedIssueListener = Compatibility.getBatchIssueListener(issueListener); + + adaptedIssueListener.handle(issue); + + ArgumentCaptor arg = ArgumentCaptor.forClass(IssueListener.Issue.class); + verify(issueListener).handle(arg.capture()); + assertIssue(arg.getValue()); + } + + private static void setIssue(org.sonar.batch.bootstrapper.IssueListener.Issue issue) { + issue.setAssigneeName("name"); + issue.setRuleName("rule"); + issue.setRuleKey("key"); + issue.setMessage("msg"); + issue.setAssigneeLogin("login"); + issue.setLine(10); + issue.setComponentKey("component"); + issue.setSeverity("severity"); + issue.setNew(true); + issue.setStatus("status"); + } + + private static void assertIssue(IssueListener.Issue issue) { + assertThat(issue.getAssigneeName()).isEqualTo("name"); + assertThat(issue.getRuleName()).isEqualTo("rule"); + assertThat(issue.getRuleKey()).isEqualTo("key"); + assertThat(issue.getMessage()).isEqualTo("msg"); + assertThat(issue.getAssigneeLogin()).isEqualTo("login"); + assertThat(issue.getLine()).isEqualTo(10); + assertThat(issue.getComponentKey()).isEqualTo("component"); + assertThat(issue.getSeverity()).isEqualTo("severity"); + assertThat(issue.isNew()).isEqualTo(true); + assertThat(issue.getStatus()).isEqualTo("status"); + } +} 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 6577a81..69c2a23 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 @@ -19,9 +19,18 @@ */ package org.sonar.runner.cli; +import java.io.PrintStream; + public class Logs { private boolean debugEnabled = false; private boolean displayStackTrace = false; + private PrintStream stdOut; + private PrintStream stdErr; + + public Logs(PrintStream stdOut, PrintStream stdErr) { + this.stdErr = stdErr; + this.stdOut = stdOut; + } public void setDebugEnabled(boolean debugEnabled) { this.debugEnabled = debugEnabled; @@ -37,26 +46,26 @@ public class Logs { public void debug(String message) { if (isDebugEnabled()) { - System.out.println("DEBUG: " + message); + stdOut.println("DEBUG: " + message); } } public void info(String message) { - System.out.println("INFO: " + message); + stdOut.println("INFO: " + message); } public void warn(String message) { - System.out.println("WARN: " + message); + stdOut.println("WARN: " + message); } public void error(String message) { - System.err.println("ERROR: " + message); + stdErr.println("ERROR: " + message); } public void error(String message, Throwable t) { - System.err.println("ERROR: " + message); + stdErr.println("ERROR: " + message); if (t != null && displayStackTrace) { - t.printStackTrace(System.err); + t.printStackTrace(stdErr); } } } 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 457380b..bceda70 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 @@ -58,7 +58,7 @@ public class Main { public static void main(String[] args) { Exit exit = new Exit(); - Logs logs = new Logs(); + Logs logs = new Logs(System.out, System.err); Cli cli = new Cli(exit, logs).parse(args); cli.verify(); Shutdown shutdown = new Shutdown(exit, cli.isInteractive()); diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java index 52f46e0..f01f47c 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java @@ -27,7 +27,7 @@ import static org.mockito.Mockito.verify; public class CliTest { Exit exit = mock(Exit.class); - Logs logs = new Logs(); + Logs logs = new Logs(System.out, System.err); Cli cli = new Cli(exit, logs); @Test diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java index 50a53e5..c13d751 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java @@ -36,7 +36,7 @@ public class ConfTest { public TemporaryFolder temp = new TemporaryFolder(); Properties args = new Properties(); - Logs logs = new Logs(); + Logs logs = new Logs(System.out, System.err); Cli cli = mock(Cli.class); Conf conf = new Conf(cli, logs); diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/LogsTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/LogsTest.java new file mode 100644 index 0000000..4b1d51f --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/LogsTest.java @@ -0,0 +1,83 @@ +/* + * SonarQube Runner - CLI - Distribution + * Copyright (C) 2011 SonarSource + * sonarqube@googlegroups.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.cli; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.PrintStream; + +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verify; + +public class LogsTest { + @Mock + private PrintStream stdOut; + + @Mock + private PrintStream stdErr; + + private Logs logs; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + logs = new Logs(stdOut, stdErr); + } + + @Test + public void testInfo() { + logs.info("info"); + verify(stdOut).println("INFO: info"); + verifyNoMoreInteractions(stdOut, stdErr); + } + + @Test + public void testError() { + Exception e = new NullPointerException("exception"); + logs.setDisplayStackTrace(false); + logs.error("error1"); + verify(stdErr).println("ERROR: error1"); + + logs.error("error2", e); + verify(stdErr).println("ERROR: error2"); + + verifyNoMoreInteractions(stdOut, stdErr); + + logs.setDisplayStackTrace(true); + logs.error("error3", e); + verify(stdErr).println("ERROR: error3"); + // other interactions to print the exception.. + } + + @Test + public void testDebug() { + logs.setDebugEnabled(true); + + logs.debug("debug"); + verify(stdOut).println("DEBUG: debug"); + + logs.setDebugEnabled(false); + logs.debug("debug"); + verifyNoMoreInteractions(stdOut, stdErr); + } +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java index 1037483..6522fb6 100644 --- a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java @@ -19,36 +19,27 @@ */ package org.sonar.runner.cli; -import java.io.ByteArrayOutputStream; +import org.mockito.Mockito; + import java.io.PrintStream; import java.io.UnsupportedEncodingException; -import org.junit.Test; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.mock; +import org.junit.Test; import static org.fest.assertions.Assertions.assertThat; public class StatsTest { - Logs logs = new Logs(); + private PrintStream stdOut = mock(PrintStream.class); + private PrintStream stdErr; + private Logs logs = new Logs(stdOut, stdErr); @Test public void shouldPrintStats() throws UnsupportedEncodingException { + new Stats(logs).start().stop(); - PrintStream backupOut = System.out; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - System.setOut(new PrintStream(baos)); - - new Stats(logs).start().stop(); - - String out = baos.toString(); - String[] lines = out.split(System.lineSeparator()); - - assertThat(lines).hasSize(2); - - assertThat(lines[0]).contains("Total time: "); - assertThat(lines[1]).contains("Final Memory: "); - } finally { - System.setOut(backupOut); - } + verify(stdOut).println(Mockito.contains("Total time: ")); + verify(stdOut).println(Mockito.contains("Final Memory: ")); } @Test -- 2.39.5