Skip to content

Commit c08b60b

Browse files
committed
Improve coverage
1 parent 6cd1b45 commit c08b60b

2 files changed

Lines changed: 54 additions & 12 deletions

File tree

api/src/test/java/org/sonarsource/scanner/api/internal/SimulatedLauncherTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public void setUp() {
5353
filename = new File(temp.getRoot(), "props").getAbsolutePath();
5454
}
5555

56+
@Test(expected = IllegalStateException.class)
57+
public void failIfInvalidFile() {
58+
Map<String, String> props = createProperties();
59+
props.put(InternalProperties.SCANNER_DUMP_TO_FILE, temp.getRoot().getAbsolutePath());
60+
61+
launcher.execute(props, logOutput);
62+
}
63+
5664
@Test
5765
public void testDump() throws IOException {
5866
Map<String, String> props = createProperties();

api/src/test/java/org/sonarsource/scanner/api/internal/cache/FileCacheTest.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.nio.charset.StandardCharsets;
2525
import java.nio.file.Files;
26+
import org.junit.Before;
2627
import org.junit.Rule;
2728
import org.junit.Test;
2829
import org.junit.rules.ExpectedException;
@@ -34,6 +35,15 @@
3435
import static org.mockito.Mockito.when;
3536

3637
public 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

Comments
 (0)