aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorGodin <mandrikov@gmail.com>2010-11-01 14:26:13 +0000
committerGodin <mandrikov@gmail.com>2010-11-01 14:26:13 +0000
commit96df46c9dfd44d731c14a95751e94b1cd59ca421 (patch)
treee2df40bc28fb560db2c79c3137191eb178c732a2 /sonar-plugin-api/src
parent61a84575510a2cdde57ce7b2139c52458903df52 (diff)
downloadsonarqube-96df46c9dfd44d731c14a95751e94b1cd59ca421.tar.gz
sonarqube-96df46c9dfd44d731c14a95751e94b1cd59ca421.zip
HttpDownloader should not create file if fail to download
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java7
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java17
2 files changed, 21 insertions, 3 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java
index ab56f296d26..670707c3f0c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java
@@ -19,6 +19,7 @@
*/
package org.sonar.api.utils;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
@@ -34,7 +35,7 @@ import java.net.URI;
/**
* Simple class to download a file from a HTTP repository.
- *
+ *
* @since 2.2
*/
public class HttpDownloader implements BatchComponent, ServerComponent {
@@ -60,8 +61,10 @@ public class HttpDownloader implements BatchComponent, ServerComponent {
IOUtils.copy(input, output);
} catch (Exception e) {
+ IOUtils.closeQuietly(output);
+ FileUtils.deleteQuietly(toFile);
throw new SonarException("Fail to download the file: " + uri, e);
-
+
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java
index 90e65e01f2b..9b89c49ce72 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java
@@ -66,7 +66,7 @@ public class HttpDownloaderTest {
assertThat(bytes.length, greaterThan(10));
}
- @Test(expected=SonarException.class)
+ @Test(expected = SonarException.class)
public void failIfServerDown() throws URISyntaxException {
// I hope that the port 1 is not used !
new HttpDownloader().download(new URI("http://localhost:1/unknown"));
@@ -85,6 +85,21 @@ public class HttpDownloaderTest {
}
@Test
+ public void shouldNotCreateFileIfFailToDownload() throws Exception {
+ File toDir = new File("target/test-tmp/org/sonar/api/utils/DownloaderTest/");
+ FileUtils.forceMkdir(toDir);
+ FileUtils.cleanDirectory(toDir);
+ File toFile = new File(toDir, "downloadToFile.txt");
+
+ try {
+ // I hope that the port 1 is not used !
+ new HttpDownloader().download(new URI("http://localhost:1/unknown"), toFile);
+ } catch (SonarException e) {
+ assertThat(toFile.exists(), is(false));
+ }
+ }
+
+ @Test
public void userAgentIsSonarVersion() throws URISyntaxException, IOException {
Server server = mock(Server.class);
when(server.getVersion()).thenReturn("2.2");