diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-14 14:05:32 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-09-14 15:40:11 +0200 |
commit | d37a06d8bb263713e3e2c99b977806a41dec15a6 (patch) | |
tree | b42c27ab89a4965d9f68c61dcf707eb7686bc797 /sonar-home/src/test/java | |
parent | c9975c3520d55d9f79cb04468fa5a64da8ed5dfa (diff) | |
download | sonarqube-d37a06d8bb263713e3e2c99b977806a41dec15a6.tar.gz sonarqube-d37a06d8bb263713e3e2c99b977806a41dec15a6.zip |
Improve quality
Diffstat (limited to 'sonar-home/src/test/java')
-rw-r--r-- | sonar-home/src/test/java/org/sonar/home/cache/FileCacheTest.java | 59 |
1 files changed, 47 insertions, 12 deletions
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 64815767e7f..2a7381c866a 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 @@ -22,6 +22,8 @@ package org.sonar.home.cache; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; +import org.assertj.core.util.Files; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -33,6 +35,15 @@ import static org.mockito.Mockito.mock; 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(); @@ -41,14 +52,11 @@ public class FileCacheTest { @Test public void not_in_cache() throws IOException { - FileCache cache = FileCache.create(tempFolder.newFolder(), mock(Logger.class)); assertThat(cache.get("sonar-foo-plugin-1.5.jar", "ABCDE")).isNull(); } @Test public void found_in_cache() throws IOException { - FileCache cache = FileCache.create(tempFolder.newFolder(), mock(Logger.class)); - // 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"); @@ -57,10 +65,41 @@ public class FileCacheTest { } @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"); + } + }; + thrown.expect(IllegalStateException.class); + thrown.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"); + cache = new FileCache(file, fileHashes, mock(Logger.class)); + } + + @Test + public void fail_to_create_hash_dir() throws IOException { + when(fileHashes.of(any(File.class))).thenReturn("ABCDE"); + + File hashDir = new File(cache.getDir(), "ABCDE"); + hashDir.createNewFile(); + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Fail to create cache directory"); + cache.get("sonar-foo-plugin-1.5.jar", "ABCDE", mock(FileCache.Downloader.class)); + } + + @Test public void download_and_add_to_cache() throws IOException { - FileHashes hashes = mock(FileHashes.class); - FileCache cache = new FileCache(tempFolder.newFolder(), hashes, mock(Logger.class)); - when(hashes.of(any(File.class))).thenReturn("ABCDE"); + when(fileHashes.of(any(File.class))).thenReturn("ABCDE"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { @@ -79,9 +118,7 @@ public class FileCacheTest { thrown.expect(IllegalStateException.class); thrown.expectMessage("INVALID HASH"); - FileHashes hashes = mock(FileHashes.class); - FileCache cache = new FileCache(tempFolder.newFolder(), hashes, mock(Logger.class)); - when(hashes.of(any(File.class))).thenReturn("VWXYZ"); + when(fileHashes.of(any(File.class))).thenReturn("VWXYZ"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { @@ -93,9 +130,7 @@ public class FileCacheTest { @Test public void concurrent_download() throws IOException { - FileHashes hashes = mock(FileHashes.class); - when(hashes.of(any(File.class))).thenReturn("ABCDE"); - final FileCache cache = new FileCache(tempFolder.newFolder(), hashes, mock(Logger.class)); + when(fileHashes.of(any(File.class))).thenReturn("ABCDE"); FileCache.Downloader downloader = new FileCache.Downloader() { public void download(String filename, File toFile) throws IOException { |