2323import java .io .IOException ;
2424import java .nio .charset .StandardCharsets ;
2525import java .nio .file .Files ;
26+ import org .junit .Before ;
2627import org .junit .Rule ;
2728import org .junit .Test ;
2829import org .junit .rules .ExpectedException ;
3435import static org .mockito .Mockito .when ;
3536
3637public class FileCacheTest {
38+ private FileHashes fileHashes ;
39+ private FileCache cache ;
40+
41+ @ Before
42+ public void setUp () throws IOException {
43+ fileHashes = mock (FileHashes .class );
44+ cache = new FileCache (temp .getRoot ().toPath (), fileHashes , mock (Logger .class ));
45+ }
46+
3747 @ Rule
3848 public TemporaryFolder temp = new TemporaryFolder ();
3949
@@ -42,26 +52,54 @@ public class FileCacheTest {
4252
4353 @ Test
4454 public void not_in_cache () throws IOException {
45- FileCache cache = FileCache .create (temp .newFolder ().toPath (), mock (Logger .class ));
4655 assertThat (cache .get ("sonar-foo-plugin-1.5.jar" , "ABCDE" )).isNull ();
4756 }
4857
4958 @ Test
5059 public void found_in_cache () throws IOException {
51- FileCache cache = FileCache .create (temp .newFolder ().toPath (), mock (Logger .class ));
52-
5360 // populate the cache. Assume that hash is correct.
5461 File cachedFile = new File (new File (cache .getDir (), "ABCDE" ), "sonar-foo-plugin-1.5.jar" );
5562 write (cachedFile , "body" );
5663
5764 assertThat (cache .get ("sonar-foo-plugin-1.5.jar" , "ABCDE" )).isNotNull ().exists ().isEqualTo (cachedFile );
5865 }
5966
67+ @ Test
68+ public void fail_to_download () {
69+ when (fileHashes .of (any (File .class ))).thenReturn ("ABCDE" );
70+
71+ FileCache .Downloader downloader = new FileCache .Downloader () {
72+ public void download (String filename , File toFile ) throws IOException {
73+ throw new IOException ("fail" );
74+ }
75+ };
76+ thrown .expect (IllegalStateException .class );
77+ thrown .expectMessage ("Fail to download" );
78+ cache .get ("sonar-foo-plugin-1.5.jar" , "ABCDE" , downloader );
79+ }
80+
81+ @ Test
82+ public void fail_create_hash_dir () throws IOException {
83+ File file = temp .newFile ();
84+ thrown .expect (IllegalStateException .class );
85+ thrown .expectMessage ("Unable to create user cache" );
86+ cache = new FileCache (file .toPath (), fileHashes , mock (Logger .class ));
87+ }
88+
89+ @ Test
90+ public void fail_to_create_hash_dir () throws IOException {
91+ when (fileHashes .of (any (File .class ))).thenReturn ("ABCDE" );
92+
93+ File hashDir = new File (cache .getDir (), "ABCDE" );
94+ hashDir .createNewFile ();
95+ thrown .expect (IllegalStateException .class );
96+ thrown .expectMessage ("Fail to create cache directory" );
97+ cache .get ("sonar-foo-plugin-1.5.jar" , "ABCDE" , mock (FileCache .Downloader .class ));
98+ }
99+
60100 @ Test
61101 public void download_and_add_to_cache () throws IOException {
62- FileHashes hashes = mock (FileHashes .class );
63- FileCache cache = new FileCache (temp .newFolder ().toPath (), hashes , mock (Logger .class ));
64- when (hashes .of (any (File .class ))).thenReturn ("ABCDE" );
102+ when (fileHashes .of (any (File .class ))).thenReturn ("ABCDE" );
65103
66104 FileCache .Downloader downloader = new FileCache .Downloader () {
67105 boolean single = false ;
@@ -92,9 +130,7 @@ public void download_corrupted_file() throws IOException {
92130 thrown .expect (IllegalStateException .class );
93131 thrown .expectMessage ("INVALID HASH" );
94132
95- FileHashes hashes = mock (FileHashes .class );
96- FileCache cache = new FileCache (temp .newFolder ().toPath (), hashes , mock (Logger .class ));
97- when (hashes .of (any (File .class ))).thenReturn ("VWXYZ" );
133+ when (fileHashes .of (any (File .class ))).thenReturn ("VWXYZ" );
98134
99135 FileCache .Downloader downloader = new FileCache .Downloader () {
100136 public void download (String filename , File toFile ) throws IOException {
@@ -106,9 +142,7 @@ public void download(String filename, File toFile) throws IOException {
106142
107143 @ Test
108144 public void concurrent_download () throws IOException {
109- FileHashes hashes = mock (FileHashes .class );
110- when (hashes .of (any (File .class ))).thenReturn ("ABCDE" );
111- final FileCache cache = new FileCache (temp .newFolder ().toPath (), hashes , mock (Logger .class ));
145+ when (fileHashes .of (any (File .class ))).thenReturn ("ABCDE" );
112146
113147 FileCache .Downloader downloader = new FileCache .Downloader () {
114148 public void download (String filename , File toFile ) throws IOException {
0 commit comments