]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10591 Remove unused code in sonar-home
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 30 Apr 2018 20:53:02 +0000 (22:53 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 11 May 2018 18:20:46 +0000 (20:20 +0200)
sonar-home/src/main/java/org/sonar/home/cache/DirectoryLock.java
sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java
sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java
sonar-home/src/test/java/org/sonar/home/cache/FileCacheBuilderTest.java
sonar-home/src/test/java/org/sonar/home/cache/FileCacheTest.java
sonar-home/src/test/java/org/sonar/home/cache/FileHashesTest.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/FileCacheProvider.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginJarExploderTest.java

index e7b88a7f33db82dbdeab7ef38b17008d3f02cc33..db61bac9bcd8244f1a05e32270d81036dcac4197 100644 (file)
@@ -39,20 +39,6 @@ public class DirectoryLock {
     this.lockFilePath = directory.resolve(LOCK_FILE_NAME).toAbsolutePath();
   }
 
-  public String getFileLockName() {
-    return LOCK_FILE_NAME;
-  }
-  
-  public void lock() {
-    try {
-      lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
-      lockChannel = lockRandomAccessFile.getChannel();
-      lockFile = lockChannel.lock(0, 1024, false);
-    } catch (IOException e) {
-      throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
-    }
-  }
-  
   public boolean tryLock() {
     try {
       lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
index 89bb416e0485d2dc0a2fbb242faa2c4144bd6535..bc42ed0335cb5ce480288577dc2955d8005d5b47 100644 (file)
@@ -29,7 +29,6 @@ import java.nio.file.Path;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Pack200;
 import java.util.zip.GZIPInputStream;
-import javax.annotation.CheckForNull;
 
 /**
  * This class is responsible for managing Sonar batch file cache. You can put file into cache and
@@ -62,24 +61,15 @@ public class FileCache {
     return cacheDir;
   }
 
-  /**
-   * Look for a file in the cache by its filename and md5 checksum. If the file is not
-   * present then return null.
-   */
-  @CheckForNull
-  public File get(String filename, String hash) {
-    File cachedFile = new File(new File(cacheDir, hash), filename);
-    if (cachedFile.exists()) {
-      return cachedFile;
-    }
-    logger.debug(String.format("No file found in the cache with name %s and hash %s", filename, hash));
-    return null;
-  }
-
   public interface Downloader {
     void download(String filename, File toFile) throws IOException;
   }
 
+  /**
+   * Look for a file in the cache by its filename and md5 checksum. If the file is not
+   * present then try to download it. In case of error or if the file is not found
+   * an exception is thrown. {@code null} is never returned.
+   */
   public File get(String filename, String hash, Downloader downloader) {
     // Does not fail if another process tries to create the directory at the same time.
     File hashDir = hashDir(hash);
index 0734a28c9dacd03d640f75293e11ebe20d892576..0bb78aada302622d697cdcddb72e973d3c82f32e 100644 (file)
@@ -31,13 +31,8 @@ public class FileCacheBuilder {
     this.logger = logger;
   }
 
-  public FileCacheBuilder setUserHome(File d) {
-    this.userHome = d;
-    return this;
-  }
-
-  public FileCacheBuilder setUserHome(@Nullable String path) {
-    this.userHome = (path == null) ? null : new File(path);
+  public FileCacheBuilder setUserHome(@Nullable File dir) {
+    this.userHome = dir;
     return this;
   }
 
index 2a04cb6aa7716a1f8797b5e8ff847a36c2bbaf3a..36815b3c2b1df22dd880e1c40845c494ff332c50 100644 (file)
  */
 package org.sonar.home.cache;
 
-import static org.mockito.Mockito.mock;
-import static org.assertj.core.api.Assertions.assertThat;
-
-import org.junit.rules.ExpectedException;
-
-import java.nio.channels.OverlappingFileLockException;
 import java.nio.file.Paths;
-
-import org.junit.Test;
 import org.junit.Before;
 import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
 public class DirectoryLockTest {
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
   @Rule
-  public ExpectedException exception = ExpectedException.none();
+  public ExpectedException expectedException = ExpectedException.none();
   private DirectoryLock lock;
 
   @Before
@@ -44,14 +41,6 @@ public class DirectoryLockTest {
     lock = new DirectoryLock(temp.getRoot().toPath(), mock(Logger.class));
   }
 
-  @Test
-  public void lock() {
-    assertThat(temp.getRoot().list()).isEmpty();
-    lock.lock();
-    assertThat(temp.getRoot().toPath().resolve(".sonar_lock")).exists();
-    lock.unlock();
-  }
-
   @Test
   public void tryLock() {
     assertThat(temp.getRoot().list()).isEmpty();
@@ -60,33 +49,18 @@ public class DirectoryLockTest {
     lock.unlock();
   }
 
-  @Test(expected = OverlappingFileLockException.class)
-  public void error_2locks() {
-    assertThat(temp.getRoot().list()).isEmpty();
-    lock.lock();
-    lock.lock();
-  }
-
   @Test
   public void unlockWithoutLock() {
     lock.unlock();
   }
 
-  @Test
-  public void errorCreatingLock() {
-    lock = new DirectoryLock(Paths.get("non", "existing", "path"), mock(Logger.class));
-
-    exception.expect(IllegalStateException.class);
-    exception.expectMessage("Failed to create lock");
-    lock.lock();
-  }
-
   @Test
   public void errorTryLock() {
     lock = new DirectoryLock(Paths.get("non", "existing", "path"), mock(Logger.class));
 
-    exception.expect(IllegalStateException.class);
-    exception.expectMessage("Failed to create lock");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Failed to create lock");
+
     lock.tryLock();
   }
 }
index 623d407868e1936456782c057cfab9621bf5322d..8e144c9ed14c8057c5130127b21f5a364067298c 100644 (file)
@@ -43,7 +43,7 @@ public class FileCacheBuilderTest {
 
   @Test
   public void user_home_property_can_be_null() {
-    FileCache cache = new FileCacheBuilder(mock(Logger.class)).setUserHome((String) null).build();
+    FileCache cache = new FileCacheBuilder(mock(Logger.class)).setUserHome(null).build();
 
     // does not fail. It uses default path or env variable
     assertThat(cache.getDir()).isDirectory().exists();
index 0e88e88b1f644d165c9aa1835775cadde65a3b81..63d4d85458f4bdb9b02aa2690bbe63744eadce1f 100644 (file)
@@ -35,54 +35,37 @@ import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
 public class FileCacheTest {
-  private FileHashes fileHashes;
-  private FileCache cache;
-
-  @Before
-  public void setUp() throws IOException {
-    fileHashes = mock(FileHashes.class);
-    cache = new FileCache(tempFolder.getRoot(), fileHashes, mock(Logger.class));
-  }
 
   @Rule
   public TemporaryFolder tempFolder = new TemporaryFolder();
-
   @Rule
-  public ExpectedException thrown = ExpectedException.none();
-
-  @Test
-  public void not_in_cache() throws IOException {
-    assertThat(cache.get("sonar-foo-plugin-1.5.jar", "ABCDE")).isNull();
-  }
+  public ExpectedException expectedException = ExpectedException.none();
 
-  @Test
-  public void found_in_cache() throws IOException {
-    // populate the cache. Assume that hash is correct.
-    File cachedFile = new File(new File(cache.getDir(), "ABCDE"), "sonar-foo-plugin-1.5.jar");
-    FileUtils.write(cachedFile, "body");
+  private FileHashes fileHashes = mock(FileHashes.class);
+  private FileCache cache;
 
-    assertThat(cache.get("sonar-foo-plugin-1.5.jar", "ABCDE")).isNotNull().exists().isEqualTo(cachedFile);
+  @Before
+  public void setUp() {
+    cache = new FileCache(tempFolder.getRoot(), fileHashes, mock(Logger.class));
   }
 
   @Test
   public void fail_to_download() {
     when(fileHashes.of(any(File.class))).thenReturn("ABCDE");
 
-    FileCache.Downloader downloader = new FileCache.Downloader() {
-      public void download(String filename, File toFile) throws IOException {
-        throw new IOException("fail");
-      }
+    FileCache.Downloader downloader = (filename, toFile) -> {
+      throw new IOException("fail");
     };
-    thrown.expect(IllegalStateException.class);
-    thrown.expectMessage("Fail to download");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to download");
     cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader);
   }
 
   @Test
   public void fail_create_hash_dir() throws IOException {
     File file = tempFolder.newFile();
-    thrown.expect(IllegalStateException.class);
-    thrown.expectMessage("Unable to create user cache");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Unable to create user cache");
     cache = new FileCache(file, fileHashes, mock(Logger.class));
   }
 
@@ -92,8 +75,8 @@ public class FileCacheTest {
 
     File hashDir = new File(cache.getDir(), "ABCDE");
     hashDir.createNewFile();
-    thrown.expect(IllegalStateException.class);
-    thrown.expectMessage("Fail to create cache directory");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to create cache directory");
     cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", mock(FileCache.Downloader.class));
   }
 
@@ -101,11 +84,7 @@ public class FileCacheTest {
   public void download_and_add_to_cache() throws IOException {
     when(fileHashes.of(any(File.class))).thenReturn("ABCDE");
 
-    FileCache.Downloader downloader = new FileCache.Downloader() {
-      public void download(String filename, File toFile) throws IOException {
-        FileUtils.write(toFile, "body");
-      }
-    };
+    FileCache.Downloader downloader = (filename, toFile) -> FileUtils.write(toFile, "body");
     File cachedFile = cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader);
     assertThat(cachedFile).isNotNull().exists().isFile();
     assertThat(cachedFile.getName()).isEqualTo("sonar-foo-plugin-1.5.jar");
@@ -114,14 +93,10 @@ public class FileCacheTest {
   }
 
   @Test
-  public void download_and_add_to_cache_compressed_file() throws IOException {
+  public void download_and_add_to_cache_compressed_file() {
     when(fileHashes.of(any(File.class))).thenReturn("ABCDE");
 
-    FileCache.Downloader downloader = new FileCache.Downloader() {
-      public void download(String filename, File toFile) throws IOException {
-        FileUtils.copyFile(compressedFile(), toFile);
-      }
-    };
+    FileCache.Downloader downloader = (filename, toFile) -> FileUtils.copyFile(compressedFile(), toFile);
     File cachedFile = cache.getCompressed("sonar-foo-plugin-1.5.pack.gz", "ABCDE", downloader);
     assertThat(cachedFile).isNotNull().exists().isFile();
 
@@ -168,17 +143,13 @@ public class FileCacheTest {
   }
 
   @Test
-  public void download_corrupted_file() throws IOException {
-    thrown.expect(IllegalStateException.class);
-    thrown.expectMessage("INVALID HASH");
+  public void download_corrupted_file() {
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("INVALID HASH");
 
     when(fileHashes.of(any(File.class))).thenReturn("VWXYZ");
 
-    FileCache.Downloader downloader = new FileCache.Downloader() {
-      public void download(String filename, File toFile) throws IOException {
-        FileUtils.write(toFile, "corrupted body");
-      }
-    };
+    FileCache.Downloader downloader = (filename, toFile) -> FileUtils.write(toFile, "corrupted body");
     cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", downloader);
   }
 
@@ -186,14 +157,12 @@ public class FileCacheTest {
   public void concurrent_download() throws IOException {
     when(fileHashes.of(any(File.class))).thenReturn("ABCDE");
 
-    FileCache.Downloader downloader = new FileCache.Downloader() {
-      public void download(String filename, File toFile) throws IOException {
-        // Emulate a concurrent download that adds file to cache before
-        File cachedFile = new File(new File(cache.getDir(), "ABCDE"), "sonar-foo-plugin-1.5.jar");
-        FileUtils.write(cachedFile, "downloaded by other");
+    FileCache.Downloader downloader = (filename, toFile) -> {
+      // Emulate a concurrent download that adds file to cache before
+      File cachedFile = new File(new File(cache.getDir(), "ABCDE"), "sonar-foo-plugin-1.5.jar");
+      FileUtils.write(cachedFile, "downloaded by other");
 
-        FileUtils.write(toFile, "downloaded by me");
-      }
+      FileUtils.write(toFile, "downloaded by me");
     };
 
     // do not fail
index 975d0e65218847ee1aeee73e9fee343c336d971d..35bb977675d817d0cd2b2d233617499f243a33f1 100644 (file)
@@ -45,10 +45,10 @@ import static org.mockito.Mockito.when;
 
 public class FileHashesTest {
 
-  SecureRandom secureRandom = new SecureRandom();
+  private SecureRandom secureRandom = new SecureRandom();
 
   @Rule
-  public ExpectedException thrown = ExpectedException.none();
+  public ExpectedException expectedException = ExpectedException.none();
 
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
@@ -92,16 +92,16 @@ public class FileHashesTest {
     File file = temp.newFile("does_not_exist");
     FileUtils.forceDelete(file);
 
-    thrown.expect(IllegalStateException.class);
-    thrown.expectMessage("Fail to compute hash of: " + file.getAbsolutePath());
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to compute hash of: " + file.getAbsolutePath());
 
     new FileHashes().of(file);
   }
 
   @Test
   public void fail_if_stream_is_closed() throws Exception {
-    thrown.expect(IllegalStateException.class);
-    thrown.expectMessage("Fail to compute hash");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to compute hash");
 
     InputStream input = mock(InputStream.class);
     when(input.read(any(byte[].class), anyInt(), anyInt())).thenThrow(new IllegalThreadStateException());
index b2946c960719664e35084370546d48daaed52dcb..7deea6e36d6a56a7cfa646f9d30f919bda786c41 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.scanner.bootstrap;
 
+import java.io.File;
 import org.picocontainer.injectors.ProviderAdapter;
 import org.sonar.api.config.Configuration;
 import org.sonar.home.cache.FileCache;
@@ -29,7 +30,9 @@ public class FileCacheProvider extends ProviderAdapter {
 
   public FileCache provide(Configuration settings) {
     if (cache == null) {
-      String home = settings.get("sonar.userHome").orElse(null);
+      File home = settings.get("sonar.userHome")
+        .map(File::new)
+        .orElse(null);
       cache = new FileCacheBuilder(new Slf4jLogger()).setUserHome(home).build();
     }
     return cache;
index 00e7bdfc39899ed4fe0bf885e10abcc26e71922b..c8a92c3a3ca1c387f144066dd0a8872e5190de79 100644 (file)
@@ -38,8 +38,8 @@ public class ScannerPluginJarExploderTest {
   @ClassRule
   public static TemporaryFolder temp = new TemporaryFolder();
 
-  File userHome;
-  ScannerPluginJarExploder underTest;
+  private File userHome;
+  private ScannerPluginJarExploder underTest;
 
   @Before
   public void setUp() throws IOException {
@@ -55,7 +55,7 @@ public class ScannerPluginJarExploderTest {
 
     assertThat(exploded.getKey()).isEqualTo("checkstyle");
     assertThat(exploded.getMain()).isFile().exists();
-    assertThat(exploded.getLibs()).extracting("name").containsOnly("antlr-2.7.6.jar", "checkstyle-5.1.jar", "commons-cli-1.0.jar");
+    assertThat(exploded.getLibs()).extracting(File::getName).containsExactlyInAnyOrder("antlr-2.7.6.jar", "checkstyle-5.1.jar", "commons-cli-1.0.jar");
     assertThat(new File(fileFromCache.getParent(), "sonar-checkstyle-plugin-2.8.jar")).exists();
     assertThat(new File(fileFromCache.getParent(), "sonar-checkstyle-plugin-2.8.jar_unzip/META-INF/lib/checkstyle-5.1.jar")).exists();
   }
@@ -70,7 +70,7 @@ public class ScannerPluginJarExploderTest {
     assertThat(new File(fileFromCache.getParent(), "sonar-checkstyle-plugin-2.8.jar_unzip/org/sonar/plugins/checkstyle/CheckstyleVersion.class")).doesNotExist();
   }
 
-  File getFileFromCache(String filename) throws IOException {
+  private File getFileFromCache(String filename) throws IOException {
     File src = FileUtils.toFile(getClass().getResource(this.getClass().getSimpleName() + "/" + filename));
     File destFile = new File(new File(userHome, "" + filename.hashCode()), filename);
     FileUtils.copyFile(src, destFile);