]> source.dussan.org Git - sonarqube.git/commitdiff
Improve FileCache tests
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 14 Sep 2015 14:46:35 +0000 (16:46 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 14 Sep 2015 15:29:40 +0000 (17:29 +0200)
it/it-tests/src/test/java/batch/BatchTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/FileCacheProviderTest.java
sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java

index 01352f4523106fa25fad3078834029982824b2b5..a3a7edadf6cb0db70ba026f5cc329cf3aeaca61b 100644 (file)
@@ -39,11 +39,11 @@ public class BatchTest {
     .addPlugin(ItUtils.xooPlugin())
     .setContext("/")
 
-  .addPlugin(ItUtils.pluginArtifact("batch-plugin"))
+    .addPlugin(ItUtils.pluginArtifact("batch-plugin"))
     // Java is only used in convert_library_into_module test
     .setOrchestratorProperty("javaVersion", "LATEST_RELEASE").addPlugin("java")
 
-  .build();
+    .build();
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
@@ -195,6 +195,25 @@ public class BatchTest {
       "sonar.profile was set to 'unknow' but didn't match any profile for any language. Please check your configuration.");
   }
 
+  @Test
+  public void should_honor_sonarUserHome() {
+    File userHome = temp.getRoot();
+
+    orchestrator.getServer().provisionProject("sample", "xoo-sample");
+    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
+
+    SonarRunner runner = configureRunner("shared/xoo-sample",
+      "sonar.verbose", "true");
+    runner.setEnvironmentVariable("SONAR_USER_HOME", "/dev/null");
+    BuildResult buildResult = orchestrator.executeBuildQuietly(runner);
+    assertThat(buildResult.getStatus()).isEqualTo(1);
+
+    buildResult = scan("shared/xoo-sample",
+      "sonar.verbose", "true",
+      "sonar.userHome", userHome.getAbsolutePath());
+    assertThat(buildResult.isSuccess()).isTrue();
+  }
+
   @Test
   public void should_authenticate_when_needed() {
     try {
@@ -351,7 +370,7 @@ public class BatchTest {
       // message
       .contains("Error message from plugin")
 
-    // but not stacktrace
+      // but not stacktrace
       .doesNotContain("at com.sonarsource.RaiseMessageException");
   }
 
index e0408eae0a380226da493636e385f2008d50368a..babf5fd17802d8d44d01ea2c53888d9c7f247733 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.home.cache.FileCache;
-
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class FileCacheProviderTest {
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
   @Test
   public void provide() {
     FileCacheProvider provider = new FileCacheProvider();
@@ -44,4 +51,15 @@ public class FileCacheProviderTest {
 
     assertThat(cache1).isSameAs(cache2);
   }
+
+  @Test
+  public void honor_sonarUserHome() throws IOException {
+    FileCacheProvider provider = new FileCacheProvider();
+    Settings settings = new Settings();
+    File f = temp.newFolder();
+    settings.appendProperty("sonar.userHome", f.getAbsolutePath());
+    FileCache cache = provider.provide(settings);
+
+    assertThat(cache.getDir()).isEqualTo(new File(f, "cache"));
+  }
 }
index b4fd0da96d8a80f148347d556ffe6735d36aaac7..e8027081aac04bfba9ef27c0ec45fbb9274f71a4 100644 (file)
 package org.sonar.home.cache;
 
 import java.io.File;
+
 import javax.annotation.Nullable;
 
 public class FileCacheBuilder {
-
+  private final Logger logger;
   private File userHome;
-  private Logger logger;
 
   public FileCacheBuilder(Logger logger) {
     this.logger = logger;
@@ -43,14 +43,18 @@ public class FileCacheBuilder {
 
   public FileCache build() {
     if (userHome == null) {
-      String path = System.getenv("SONAR_USER_HOME");
-      if (path == null) {
-        // Default
-        path = System.getProperty("user.home") + File.separator + ".sonar";
-      }
-      userHome = new File(path);
+      userHome = findHome();
     }
     File cacheDir = new File(userHome, "cache");
     return FileCache.create(cacheDir, logger);
   }
+  
+  private File findHome() {
+    String path = System.getenv("SONAR_USER_HOME");
+    if (path == null) {
+      // Default
+      path = System.getProperty("user.home") + File.separator + ".sonar";
+    }
+    return new File(path);
+  }
 }