diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2018-04-30 22:53:02 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-05-11 20:20:46 +0200 |
commit | 045a34deadfa9e0aed6d1dc3cc0059f3e0b390ad (patch) | |
tree | 102fdf6e243de349571b9af3ab99fb0ed2644667 /sonar-home/src | |
parent | 75c553337901c4e09aa53f07f1659d8608ad5181 (diff) | |
download | sonarqube-045a34deadfa9e0aed6d1dc3cc0059f3e0b390ad.tar.gz sonarqube-045a34deadfa9e0aed6d1dc3cc0059f3e0b390ad.zip |
SONAR-10591 Remove unused code in sonar-home
Diffstat (limited to 'sonar-home/src')
7 files changed, 49 insertions, 135 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/DirectoryLock.java b/sonar-home/src/main/java/org/sonar/home/cache/DirectoryLock.java index e7b88a7f33d..db61bac9bcd 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/DirectoryLock.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/DirectoryLock.java @@ -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"); diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java index 89bb416e048..bc42ed0335c 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java @@ -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); diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java index 0734a28c9da..0bb78aada30 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java @@ -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; } diff --git a/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java b/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java index 2a04cb6aa77..36815b3c2b1 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/DirectoryLockTest.java @@ -19,24 +19,21 @@ */ 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 @@ -45,14 +42,6 @@ public class DirectoryLockTest { } @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(); lock.tryLock(); @@ -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(); } } diff --git a/sonar-home/src/test/java/org/sonar/home/cache/FileCacheBuilderTest.java b/sonar-home/src/test/java/org/sonar/home/cache/FileCacheBuilderTest.java index 623d407868e..8e144c9ed14 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/FileCacheBuilderTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/FileCacheBuilderTest.java @@ -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(); diff --git a/sonar-home/src/test/java/org/sonar/home/cache/FileCacheTest.java b/sonar-home/src/test/java/org/sonar/home/cache/FileCacheTest.java index 0e88e88b1f6..63d4d85458f 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/FileCacheTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/FileCacheTest.java @@ -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 diff --git a/sonar-home/src/test/java/org/sonar/home/cache/FileHashesTest.java b/sonar-home/src/test/java/org/sonar/home/cache/FileHashesTest.java index 975d0e65218..35bb977675d 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/FileHashesTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/FileHashesTest.java @@ -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()); |