diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-02-20 13:37:23 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-02-20 13:37:40 +0100 |
commit | 12a8d26ceb6b46e87fbbd0d683630fa5463bd848 (patch) | |
tree | c4abad403a2ae1be3969a5b83b2faef10adca89c /sonar-server | |
parent | 3b170aea5f340ac1c4d7497412ef1cdd99d97f3a (diff) | |
download | sonarqube-12a8d26ceb6b46e87fbbd0d683630fa5463bd848.tar.gz sonarqube-12a8d26ceb6b46e87fbbd0d683630fa5463bd848.zip |
Add the possibility to download a release from a file
Diffstat (limited to 'sonar-server')
3 files changed, 185 insertions, 14 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java index ace67889f2d..8aed2d2ad10 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java @@ -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); + } } } diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java index d8886e2650c..9c7ef917535 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterMatrixFactory.java @@ -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 index 00000000000..d8be3d54397 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/plugins/PluginDownloaderTest.java @@ -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); + } + } + +} |