Browse Source

base on changes in sonar-home and improve coverage

tags/2.5-rc1
Duarte Meneses 8 years ago
parent
commit
49dfb95a5a

+ 12
- 1
sonar-runner-api/src/main/java/org/sonar/runner/api/StdOutLogOutput.java View File

@@ -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);
}
}

+ 3
- 0
sonar-runner-api/src/main/java/org/sonar/runner/impl/IsolatedLauncherFactory.java View File

@@ -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));
}

+ 1
- 1
sonar-runner-api/src/main/java/org/sonar/runner/impl/ServerConnection.java View File

@@ -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;
}

+ 40
- 0
sonar-runner-api/src/test/java/org/sonar/runner/api/StdOutLogOutputTest.java View File

@@ -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");
}
}

+ 9
- 8
sonar-runner-api/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java View File

@@ -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);

+ 10
- 0
sonar-runner-batch/pom.xml View File

@@ -44,6 +44,16 @@
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


+ 72
- 0
sonar-runner-batch/src/test/java/org/sonar/runner/batch/CompatibilityTest.java View File

@@ -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<IssueListener.Issue> 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");
}
}

+ 15
- 6
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java View File

@@ -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);
}
}
}

+ 1
- 1
sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java View File

@@ -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());

+ 1
- 1
sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java View File

@@ -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

+ 1
- 1
sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java View File

@@ -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);


+ 83
- 0
sonar-runner-cli/src/test/java/org/sonar/runner/cli/LogsTest.java View File

@@ -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);
}
}

+ 11
- 20
sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java View File

@@ -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

Loading…
Cancel
Save