]> source.dussan.org Git - sonarqube.git/commitdiff
Add the possibility to download a release from a file
authorJulien Lancelot <julien.lancelot@gmail.com>
Wed, 20 Feb 2013 12:37:23 +0000 (13:37 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Wed, 20 Feb 2013 12:37:40 +0000 (13:37 +0100)
sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java
sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java
sonar-server/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java [new file with mode: 0644]

index ace67889f2dfbd7d710b20b8b7c9df7e4f744df7..8aed2d2ad10269c80800f5ff91d57234fb0649f7 100644 (file)
@@ -56,15 +56,6 @@ public class PluginDownloader implements ServerComponent {
     }
   }
 
-  /**
-   * for unit tests
-   */
-  PluginDownloader(UpdateCenterMatrixFactory updateCenterMatrixFactory, HttpDownloader downloader, File downloadDir) {
-    this.updateCenterMatrixFactory = updateCenterMatrixFactory;
-    this.downloader = downloader;
-    this.downloadDir = downloadDir;
-  }
-
   public void cancelDownloads() {
     try {
       if (downloadDir.exists()) {
@@ -102,9 +93,16 @@ public class PluginDownloader implements ServerComponent {
     }
   }
 
-  private void downloadRelease(Release release) throws URISyntaxException {
-    URI uri = new URI(release.getDownloadUrl());
-    String filename = StringUtils.substringAfterLast(uri.getPath(), "/");
-    downloader.download(uri, new File(downloadDir, filename));
+  private void downloadRelease(Release release) throws URISyntaxException, IOException {
+    String url = release.getDownloadUrl();
+    if (!url.startsWith("file:")) {
+      URI uri = new URI(url);
+      String filename = StringUtils.substringAfterLast(uri.getPath(), "/");
+      downloader.download(uri, new File(downloadDir, filename));
+    } else {
+      String filePath = url.replaceFirst("file:", "");
+      File file = new File(filePath);
+      FileUtils.copyFileToDirectory(file, downloadDir);
+    }
   }
 }
index d8886e2650ccf01518ef0ef35c336757e301af22..9c7ef917535029617b6151c02c587a4827ac16e7 100644 (file)
@@ -27,7 +27,7 @@ import org.sonar.updatecenter.common.Version;
 /**
  * @since 2.4
  */
-public final class UpdateCenterMatrixFactory implements ServerComponent {
+public class UpdateCenterMatrixFactory implements ServerComponent {
 
   private UpdateCenterClient centerClient;
   private Version sonarVersion;
diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java
new file mode 100644 (file)
index 0000000..d8be3d5
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.server.plugins;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.mockito.ArgumentMatcher;
+import org.sonar.api.utils.HttpDownloader;
+import org.sonar.api.utils.SonarException;
+import org.sonar.server.platform.DefaultServerFileSystem;
+import org.sonar.updatecenter.common.Plugin;
+import org.sonar.updatecenter.common.Release;
+import org.sonar.updatecenter.common.UpdateCenter;
+import org.sonar.updatecenter.common.Version;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class PluginDownloaderTest {
+
+  @Rule
+  public TemporaryFolder testFolder = new TemporaryFolder();
+  private File downloadDir;
+
+  private UpdateCenterMatrixFactory updateCenterMatrixFactory;
+  private UpdateCenter updateCenter;
+  private HttpDownloader httpDownloader;
+
+  private PluginDownloader pluginDownloader;
+
+  @Before
+  public void before() throws IOException {
+    updateCenterMatrixFactory = mock(UpdateCenterMatrixFactory.class);
+    updateCenter = mock(UpdateCenter.class);
+    when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(updateCenter);
+
+    httpDownloader = mock(HttpDownloader.class);
+
+    DefaultServerFileSystem defaultServerFileSystem = mock(DefaultServerFileSystem.class);
+    downloadDir = testFolder.newFolder("downloads");
+    when(defaultServerFileSystem.getDownloadedPluginsDir()).thenReturn(downloadDir);
+
+    pluginDownloader = new PluginDownloader(updateCenterMatrixFactory, httpDownloader, defaultServerFileSystem);
+  }
+
+  @Test
+  public void should_download_from_url() throws Exception {
+    Plugin test = new Plugin("test");
+    Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
+    test.addRelease(test10);
+
+    when(updateCenter.findInstallablePlugins("foo", Version.create("1.0"))).thenReturn(newArrayList(test10));
+
+    pluginDownloader.download("foo", Version.create("1.0"));
+    verify(httpDownloader).download(any(URI.class), argThat(new HasFileName("test-1.0.jar")));
+  }
+
+  @Test(expected = SonarException.class)
+  public void should_throw_exception_if_download_dir_is_invalid() throws Exception {
+    DefaultServerFileSystem defaultServerFileSystem = mock(DefaultServerFileSystem.class);
+    // download dir is a file instead of being a directory
+    File downloadDir = testFolder.newFile();
+    when(defaultServerFileSystem.getDownloadedPluginsDir()).thenReturn(downloadDir);
+    new PluginDownloader(updateCenterMatrixFactory, httpDownloader, defaultServerFileSystem);
+  }
+
+  @Test
+  public void should_download_from_file() throws Exception {
+    Plugin test = new Plugin("test");
+    File file = testFolder.newFile("test-1.0.jar");
+    file.createNewFile();
+    Release test10 = new Release(test, "1.0").setDownloadUrl("file://"+ file.getAbsoluteFile());
+    test.addRelease(test10);
+
+    when(updateCenter.findInstallablePlugins("foo", Version.create("1.0"))).thenReturn(newArrayList(test10));
+
+    pluginDownloader.download("foo", Version.create("1.0"));
+    verify(httpDownloader, never()).download(any(URI.class), any(File.class));
+    assertThat(pluginDownloader.hasDownloads()).isTrue();
+  }
+
+  @Test(expected = SonarException.class)
+  public void should_throw_exception_if_could_not_download() throws Exception {
+    Plugin test = new Plugin("test");
+    Release test10 = new Release(test, "1.0").setDownloadUrl("file://not_found");
+    test.addRelease(test10);
+
+    when(updateCenter.findInstallablePlugins("foo", Version.create("1.0"))).thenReturn(newArrayList(test10));
+
+    pluginDownloader.download("foo", Version.create("1.0"));
+  }
+
+  @Test(expected = SonarException.class)
+  public void should_throw_exception_if_download_fail() throws Exception {
+    Plugin test = new Plugin("test");
+    Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
+    test.addRelease(test10);
+    when(updateCenter.findInstallablePlugins("foo", Version.create("1.0"))).thenReturn(newArrayList(test10));
+
+    doThrow(new RuntimeException()).when(httpDownloader).download(any(URI.class), any(File.class));
+
+    pluginDownloader.download("foo", Version.create("1.0"));
+  }
+
+  @Test
+  public void should_read_download_folder() throws Exception {
+    assertThat(pluginDownloader.getDownloads()).hasSize(0);
+
+    File file1 = new File(downloadDir, "file1.jar");
+    file1.createNewFile();
+    File file2 = new File(downloadDir, "file2.jar");
+    file2.createNewFile();
+
+    assertThat(pluginDownloader.getDownloads()).hasSize(2);
+  }
+
+  @Test
+  public void should_cancel_downloads() throws Exception {
+    File file1 = new File(downloadDir, "file1.jar");
+    file1.createNewFile();
+    File file2 = new File(downloadDir, "file2.jar");
+    file2.createNewFile();
+
+    assertThat(pluginDownloader.hasDownloads()).isTrue();
+    pluginDownloader.cancelDownloads();
+    assertThat(pluginDownloader.hasDownloads()).isFalse();
+  }
+
+  class HasFileName extends ArgumentMatcher<File> {
+
+    private final String name;
+
+    HasFileName(String name) {
+      this.name = name;
+    }
+
+    public boolean matches(Object obj) {
+      File file = (File) obj;
+      return file.getName().equals(name);
+    }
+  }
+
+}