From 08e5054fa2bbc66035fa154c406da04348d5dafa Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 16 Sep 2015 14:30:41 +0200 Subject: [PATCH] SONARUNNER-91 SonarQube Runner should honor sonar.userHome --- .../test/java/com/sonar/runner/it/CacheTest.java | 2 +- .../runner/impl/IsolatedLauncherFactory.java | 7 ++++++- .../java/org/sonar/runner/impl/JarDownloader.java | 8 ++++++-- .../src/main/java/org/sonar/runner/impl/Jars.java | 15 +++++++++++++-- .../org/sonar/runner/impl/ServerConnection.java | 2 +- .../org/sonar/runner/impl/JarDownloaderTest.java | 2 +- .../test/java/org/sonar/runner/impl/JarsTest.java | 14 ++++++++++++-- 7 files changed, 40 insertions(+), 10 deletions(-) diff --git a/it/src/test/java/com/sonar/runner/it/CacheTest.java b/it/src/test/java/com/sonar/runner/it/CacheTest.java index 09f8d20..0e99bf9 100644 --- a/it/src/test/java/com/sonar/runner/it/CacheTest.java +++ b/it/src/test/java/com/sonar/runner/it/CacheTest.java @@ -84,7 +84,7 @@ public class CacheTest extends RunnerTestCase { try { result = orchestrator.executeBuild(build); } catch (BuildFailureException e) { - assertThat(e.getResult().getLogs()).contains("Server is not accessible and data is not cached"); + assertThat(e.getResult().getLogs()).contains("and data is not cached"); } } 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 3fa467e..44682fd 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 @@ -20,6 +20,7 @@ package org.sonar.runner.impl; import java.io.File; +import java.nio.file.Paths; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.List; @@ -51,6 +52,10 @@ public class IsolatedLauncherFactory { private PersistentCache getCache(Properties props) { PersistentCacheBuilder builder = new PersistentCacheBuilder(logger); + String home = props.getProperty("sonar.userHome"); + if (home != null) { + builder.setSonarHome(Paths.get(home)); + } return builder.build(); } @@ -70,7 +75,7 @@ public class IsolatedLauncherFactory { return new SimulatedLauncher(version, logger); } ServerConnection serverConnection = ServerConnection.create(props, getCache(props), logger); - JarDownloader jarDownloader = new JarDownloader(serverConnection, logger); + JarDownloader jarDownloader = new JarDownloader(serverConnection, logger, props); return createLauncher(jarDownloader, rules); } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/JarDownloader.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/JarDownloader.java index f3f88ba..4160754 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/JarDownloader.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/JarDownloader.java @@ -21,18 +21,22 @@ package org.sonar.runner.impl; import java.io.File; import java.util.List; +import java.util.Properties; + import org.sonar.home.cache.Logger; class JarDownloader { private final ServerConnection serverConnection; private final Logger logger; + private final Properties props; - JarDownloader(ServerConnection conn, Logger logger) { + JarDownloader(ServerConnection conn, Logger logger, Properties props) { this.serverConnection = conn; this.logger = logger; + this.props = props; } List download() { - return new Jars(serverConnection, new JarExtractor(), logger).download(); + return new Jars(serverConnection, new JarExtractor(), logger, props).download(); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java b/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java index 262c859..b526df9 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/impl/Jars.java @@ -23,6 +23,8 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Properties; + import org.sonar.home.cache.FileCache; import org.sonar.home.cache.FileCacheBuilder; import org.sonar.home.cache.Logger; @@ -36,9 +38,11 @@ class Jars { private final JarExtractor jarExtractor; private final Logger logger; - Jars(ServerConnection conn, JarExtractor jarExtractor, Logger logger) { + Jars(ServerConnection conn, JarExtractor jarExtractor, Logger logger, Properties props) { this.logger = logger; - this.fileCache = new FileCacheBuilder(logger).build(); + this.fileCache = new FileCacheBuilder(logger) + .setUserHome(props.getProperty("sonar.userHome")) + .build(); this.connection = conn; this.jarExtractor = jarExtractor; } @@ -53,6 +57,13 @@ class Jars { this.jarExtractor = jarExtractor; } + /** + * For unit tests + */ + FileCache getFileCache() { + return fileCache; + } + List download() { List files = new ArrayList(); logger.debug("Extract sonar-runner-batch in temp..."); 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 7156ebc..22081d3 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 @@ -161,7 +161,7 @@ class ServerConnection { if (cached != null) { return cached; } - logger.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + " and had a cache miss", serverUrl)); + logger.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED + " and data is not cached", serverUrl)); throw originalException; } catch (IOException e) { throw new IllegalStateException("Failed to access cache", e); diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java index 8c6cb6c..284fb14 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarDownloaderTest.java @@ -35,7 +35,7 @@ public class JarDownloaderTest { ServerConnection serverConnection = mock(ServerConnection.class); Properties props = new Properties(); - JarDownloader downloader = spy(new JarDownloader(serverConnection, mock(Logger.class))); + JarDownloader downloader = spy(new JarDownloader(serverConnection, mock(Logger.class), props)); @Test public void should_download_jar_files() { diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarsTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarsTest.java index a772901..222c693 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarsTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/impl/JarsTest.java @@ -20,13 +20,15 @@ package org.sonar.runner.impl; import java.io.File; +import java.io.IOException; import java.util.List; +import java.util.Properties; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.home.cache.FileCache; import org.sonar.home.cache.Logger; - import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; import static org.mockito.Matchers.any; @@ -38,7 +40,6 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class JarsTest { - ServerConnection connection = mock(ServerConnection.class); JarExtractor jarExtractor = mock(JarExtractor.class); FileCache fileCache = mock(FileCache.class); @@ -66,6 +67,15 @@ public class JarsTest { verifyNoMoreInteractions(fileCache); } + @Test + public void should_honor_sonarUserHome() throws IOException { + Properties props = new Properties(); + File f = temp.newFolder(); + props.put("sonar.userHome", f.getAbsolutePath()); + Jars jars = new Jars(connection, jarExtractor, mock(Logger.class), props); + assertThat(jars.getFileCache().getDir()).isEqualTo(new File(f, "cache")); + } + @Test public void should_fail_to_download_files() throws Exception { File batchJar = temp.newFile("sonar-runner-batch.jar"); -- 2.39.5