aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/test
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-01 13:56:05 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-09-01 13:56:05 +0200
commitabde4bd08f8bf901dba878fc99c8f22c8ca1b071 (patch)
treedb7ad1bb3c805a01a43af271c7ecf6ddcee0f3a8 /sonar-runner-api/src/test
parent38d8b26a2aafe04e478e91e46511cd37e55ea47b (diff)
downloadsonar-scanner-cli-abde4bd08f8bf901dba878fc99c8f22c8ca1b071.tar.gz
sonar-scanner-cli-abde4bd08f8bf901dba878fc99c8f22c8ca1b071.zip
Improve unit tests
Diffstat (limited to 'sonar-runner-api/src/test')
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java96
-rw-r--r--sonar-runner-api/src/test/java/org/sonar/runner/impl/SimulatedLauncherTest.java120
2 files changed, 180 insertions, 36 deletions
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 4698576..8723c97 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,6 +19,8 @@
*/
package org.sonar.runner.impl;
+import org.sonar.home.cache.PersistentCacheLoader;
+
import com.github.kevinsawicki.http.HttpRequest;
import java.io.File;
@@ -27,6 +29,11 @@ import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.Properties;
+import static org.mockito.Matchers.startsWith;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
import static org.junit.Assert.*;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
@@ -48,20 +55,29 @@ public class ServerConnectionTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- private PersistentCache cache = null;
+ private PersistentCache cache;
+ private Logger logger;
@Before
public void setUp() {
cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).build();
+ logger = mock(Logger.class);
}
@Test
- public void should_download_to_string() throws Exception {
- httpServer.setMockResponseData("abcde");
- Properties props = new Properties();
- props.setProperty("sonar.host.url", httpServer.url());
+ public void continue_if_cache_put_fails() throws Exception {
+ cache = mock(PersistentCache.class);
+ doThrow(IOException.class).when(cache).put(anyString(), any(byte[].class));
+ ServerConnection connection = createSimpleServerConnection(httpServer.url(), null, true);
+ String response = connection.downloadStringCache("/batch/index.txt");
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
+ assertThat(response).isEqualTo("abcde");
+ verify(logger).warn(startsWith("Failed to cache WS call:"));
+ }
+
+ @Test
+ public void should_download_to_string() throws Exception {
+ ServerConnection connection = createSimpleServerConnection(httpServer.url(), null);
String response = connection.downloadStringCache("/batch/index.txt");
assertThat(response).isEqualTo("abcde");
@@ -69,11 +85,7 @@ public class ServerConnectionTest {
@Test
public void should_download_to_file() throws Exception {
- httpServer.setMockResponseData("abcde");
- Properties props = new Properties();
- props.setProperty("sonar.host.url", httpServer.url());
-
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
+ ServerConnection connection = createSimpleServerConnection(httpServer.url(), null);
File toFile = temp.newFile();
connection.download("/batch/index.txt", toFile);
@@ -81,15 +93,26 @@ public class ServerConnectionTest {
}
@Test
+ public void should_throw_original_exception_fallback() throws IOException {
+ cache = mock(PersistentCache.class);
+ ServerConnection connection = createSimpleServerConnection("http://localhost", NetworkUtil.getNextAvailablePort(), true);
+
+ try {
+ connection.downloadStringCache("/batch/index.txt");
+ fail();
+ } catch (HttpRequest.HttpRequestException e) {
+ verify(cache).getString(anyString(), any(PersistentCacheLoader.class));
+ assertThat(e.getCause()).isInstanceOf(ConnectException.class);
+ }
+ }
+
+ @Test
public void should_cache_jar_list() throws Exception {
File cacheDir = new File(temp.getRoot(), "ws_cache");
- httpServer.setMockResponseData("abcde");
- Properties props = new Properties();
- props.setProperty("sonar.host.url", httpServer.url() + "/");
- props.setProperty("sonar.analysis.mode", "issues");
+
+ ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null, true);
assertThat(cacheDir.list().length).isEqualTo(0);
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
String str = connection.downloadStringCache("/batch/index.txt");
assertThat(str).isEqualTo("abcde");
@@ -103,12 +126,9 @@ public class ServerConnectionTest {
@Test
public void should_throw_connection_exception_() throws IOException {
File cacheDir = new File(temp.getRoot(), "ws_cache");
- httpServer.setMockResponseData("abcde");
- Properties props = new Properties();
- props.setProperty("sonar.host.url", httpServer.url() + "/");
+ ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null);
assertThat(cacheDir.list().length).isEqualTo(0);
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
String str = connection.downloadStringCache("/batch/index.txt");
assertThat(str).isEqualTo("abcde");
@@ -128,14 +148,11 @@ public class ServerConnectionTest {
}
@Test
- public void should_not_cache_not_preview() throws Exception {
+ public void should_not_cache_not_issues_mode() throws Exception {
File cacheDir = new File(temp.getRoot(), "ws_cache");
- httpServer.setMockResponseData("abcde");
- Properties props = new Properties();
- props.setProperty("sonar.host.url", httpServer.url() + "/");
+ ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null);
assertThat(cacheDir.list().length).isEqualTo(0);
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
String str = connection.downloadStringCache("/batch/index.txt");
assertThat(str).isEqualTo("abcde");
@@ -149,11 +166,7 @@ public class ServerConnectionTest {
// SONARPLUGINS-3061
@Test
public void should_support_trailing_slash() throws Exception {
- httpServer.setMockResponseData("abcde");
- Properties props = new Properties();
- props.setProperty("sonar.host.url", httpServer.url() + "/");
-
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
+ ServerConnection connection = createSimpleServerConnection(httpServer.url() + "/", null);
File toFile = temp.newFile();
connection.download("/batch/index.txt", toFile);
@@ -162,10 +175,8 @@ public class ServerConnectionTest {
@Test
public void should_not_download_file_when_host_is_down() throws Exception {
- Properties props = new Properties();
- props.setProperty("sonar.host.url", "http://localhost:" + NetworkUtil.getNextAvailablePort());
+ ServerConnection connection = createSimpleServerConnection("http://localhost", NetworkUtil.getNextAvailablePort());
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
File toFile = temp.newFile();
try {
connection.download("/batch/index.txt", toFile);
@@ -177,10 +188,8 @@ public class ServerConnectionTest {
@Test
public void should_not_download_string_when_host_is_down() throws Exception {
- Properties props = new Properties();
- props.setProperty("sonar.host.url", "http://localhost:" + NetworkUtil.getNextAvailablePort());
+ ServerConnection connection = createSimpleServerConnection("http://localhost", NetworkUtil.getNextAvailablePort());
- ServerConnection connection = ServerConnection.create(props, cache, mock(Logger.class));
try {
connection.downloadStringCache("/batch/index.txt");
fail();
@@ -188,4 +197,19 @@ public class ServerConnectionTest {
// success
}
}
+
+ private ServerConnection createSimpleServerConnection(String url, Integer port) {
+ return createSimpleServerConnection(url, port, false);
+ }
+
+ private ServerConnection createSimpleServerConnection(String url, Integer port, boolean issuesMode) {
+ httpServer.setMockResponseData("abcde");
+ String fullUrl = port == null ? url : url + ":" + port;
+ Properties props = new Properties();
+ props.setProperty("sonar.host.url", fullUrl);
+ if (issuesMode) {
+ props.setProperty("sonar.analysis.mode", "issues");
+ }
+ return ServerConnection.create(props, cache, logger);
+ }
}
diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/impl/SimulatedLauncherTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/impl/SimulatedLauncherTest.java
new file mode 100644
index 0000000..5f34023
--- /dev/null
+++ b/sonar-runner-api/src/test/java/org/sonar/runner/impl/SimulatedLauncherTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.impl;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.home.cache.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class SimulatedLauncherTest {
+ private static final String VERSION = "5.2";
+ private SimulatedLauncher launcher;
+ private Logger logger;
+ private String filename;
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Before
+ public void setUp() {
+ logger = mock(Logger.class);
+ launcher = new SimulatedLauncher(VERSION, logger);
+ filename = new File(temp.getRoot(), "props").getAbsolutePath();
+ }
+
+ @Test
+ public void testDump() throws IOException {
+ Properties global = new Properties();
+ global.putAll(createProperties(true));
+ Properties analysis = new Properties();
+ analysis.putAll(createProperties(false));
+
+ launcher.start(global, null, false);
+ launcher.execute(analysis);
+ assertDump(global, analysis);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void error_if_no_dump_file() {
+ launcher.execute(new Properties());
+ }
+
+ @Test
+ public void no_ops() {
+ launcher.syncProject(null);
+ }
+
+ @Test
+ public void testOldExecute() {
+ Properties global = new Properties();
+ global.putAll(createProperties(true));
+ Properties analysis = new Properties();
+ analysis.putAll(createProperties(false));
+
+ launcher.start(global, null, false);
+ launcher.executeOldVersion(analysis, null);
+
+ }
+
+ private Properties createProperties(boolean global) {
+ Properties prop = new Properties();
+ prop.put("key1_" + global, "value1");
+ prop.put("key2_" + global, "value2");
+ prop.put(InternalProperties.RUNNER_DUMP_TO_FILE, filename);
+ return prop;
+ }
+
+ @Test
+ public void version() {
+ assertThat(launcher.getVersion()).isEqualTo(VERSION);
+ }
+
+ private void assertDump(Properties global, Properties analysis) throws IOException {
+ if (analysis != null) {
+ String content = FileUtils.readFileToString(new File(filename));
+ for (Map.Entry<Object, Object> e : analysis.entrySet()) {
+ assertThat(content).contains(e.getKey() + "=" + e.getValue());
+ }
+ } else {
+ assertThat(new File(filename)).doesNotExist();
+ }
+
+ if (global != null) {
+ String content = FileUtils.readFileToString(new File(filename + ".global"));
+ for (Map.Entry<Object, Object> e : global.entrySet()) {
+ assertThat(content).contains(e.getKey() + "=" + e.getValue());
+ }
+ } else {
+ assertThat(new File(filename + ".global")).doesNotExist();
+ }
+ }
+
+}