You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginDownloaderTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.plugins;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.TemporaryFolder;
  26. import org.mockito.ArgumentMatcher;
  27. import org.mockito.invocation.InvocationOnMock;
  28. import org.mockito.stubbing.Answer;
  29. import org.sonar.api.utils.HttpDownloader;
  30. import org.sonar.api.utils.SonarException;
  31. import org.sonar.core.platform.PluginInfo;
  32. import org.sonar.server.platform.DefaultServerFileSystem;
  33. import org.sonar.updatecenter.common.Plugin;
  34. import org.sonar.updatecenter.common.Release;
  35. import org.sonar.updatecenter.common.UpdateCenter;
  36. import org.sonar.updatecenter.common.Version;
  37. import java.io.File;
  38. import java.net.URI;
  39. import java.net.URISyntaxException;
  40. import java.net.URL;
  41. import static com.google.common.collect.Lists.newArrayList;
  42. import static org.apache.commons.io.FileUtils.copyFileToDirectory;
  43. import static org.apache.commons.io.FileUtils.touch;
  44. import static org.apache.commons.io.FilenameUtils.separatorsToUnix;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. import static org.junit.Assert.fail;
  47. import static org.mockito.Matchers.any;
  48. import static org.mockito.Matchers.anyBoolean;
  49. import static org.mockito.Matchers.argThat;
  50. import static org.mockito.Mockito.doAnswer;
  51. import static org.mockito.Mockito.doThrow;
  52. import static org.mockito.Mockito.mock;
  53. import static org.mockito.Mockito.never;
  54. import static org.mockito.Mockito.verify;
  55. import static org.mockito.Mockito.when;
  56. import static org.sonar.updatecenter.common.Version.create;
  57. public class PluginDownloaderTest {
  58. @Rule
  59. public TemporaryFolder testFolder = new TemporaryFolder();
  60. File downloadDir;
  61. UpdateCenterMatrixFactory updateCenterMatrixFactory;
  62. UpdateCenter updateCenter;
  63. HttpDownloader httpDownloader;
  64. PluginDownloader pluginDownloader;
  65. @Before
  66. public void before() throws Exception {
  67. updateCenterMatrixFactory = mock(UpdateCenterMatrixFactory.class);
  68. updateCenter = mock(UpdateCenter.class);
  69. when(updateCenterMatrixFactory.getUpdateCenter(anyBoolean())).thenReturn(updateCenter);
  70. httpDownloader = mock(HttpDownloader.class);
  71. doAnswer(new Answer() {
  72. @Override
  73. public Object answer(InvocationOnMock inv) throws Throwable {
  74. File toFile = (File) inv.getArguments()[1];
  75. touch(toFile);
  76. return null;
  77. }
  78. }).when(httpDownloader).download(any(URI.class), any(File.class));
  79. DefaultServerFileSystem defaultServerFileSystem = mock(DefaultServerFileSystem.class);
  80. downloadDir = testFolder.newFolder("downloads");
  81. when(defaultServerFileSystem.getDownloadedPluginsDir()).thenReturn(downloadDir);
  82. pluginDownloader = new PluginDownloader(updateCenterMatrixFactory, httpDownloader, defaultServerFileSystem);
  83. }
  84. @After
  85. public void stop() {
  86. pluginDownloader.stop();
  87. }
  88. @Test
  89. public void clean_temporary_files_at_startup() throws Exception {
  90. touch(new File(downloadDir, "sonar-php.jar"));
  91. touch(new File(downloadDir, "sonar-js.jar.tmp"));
  92. assertThat(downloadDir.listFiles()).hasSize(2);
  93. pluginDownloader.start();
  94. File[] files = downloadDir.listFiles();
  95. assertThat(files).hasSize(1);
  96. assertThat(files[0].getName()).isEqualTo("sonar-php.jar");
  97. }
  98. @Test
  99. public void download_from_url() {
  100. Plugin test = new Plugin("test");
  101. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
  102. test.addRelease(test10);
  103. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  104. pluginDownloader.start();
  105. pluginDownloader.download("foo", create("1.0"));
  106. // SONAR-4523: do not corrupt JAR files when restarting the server while a plugin is being downloaded.
  107. // The JAR file is downloaded in a temp file
  108. verify(httpDownloader).download(any(URI.class), argThat(new HasFileName("test-1.0.jar.tmp")));
  109. assertThat(new File(downloadDir, "test-1.0.jar")).exists();
  110. assertThat(new File(downloadDir, "test-1.0.jar.tmp")).doesNotExist();
  111. }
  112. /**
  113. * SONAR-4685
  114. */
  115. @Test
  116. public void download_from_redirect_url() {
  117. Plugin test = new Plugin("plugin-test");
  118. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/redirect?r=release&g=test&a=test&v=1.0&e=jar");
  119. test.addRelease(test10);
  120. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  121. pluginDownloader.start();
  122. pluginDownloader.download("foo", create("1.0"));
  123. // SONAR-4523: do not corrupt JAR files when restarting the server while a plugin is being downloaded.
  124. // The JAR file is downloaded in a temp file
  125. verify(httpDownloader).download(any(URI.class), argThat(new HasFileName("plugin-test-1.0.jar.tmp")));
  126. assertThat(new File(downloadDir, "plugin-test-1.0.jar")).exists();
  127. assertThat(new File(downloadDir, "plugin-test-1.0.jar.tmp")).doesNotExist();
  128. }
  129. @Test
  130. public void throw_exception_if_download_dir_is_invalid() throws Exception {
  131. DefaultServerFileSystem defaultServerFileSystem = mock(DefaultServerFileSystem.class);
  132. // download dir is a file instead of being a directory
  133. File downloadDir = testFolder.newFile();
  134. when(defaultServerFileSystem.getDownloadedPluginsDir()).thenReturn(downloadDir);
  135. pluginDownloader = new PluginDownloader(updateCenterMatrixFactory, httpDownloader, defaultServerFileSystem);
  136. try {
  137. pluginDownloader.start();
  138. fail();
  139. } catch (IllegalStateException e) {
  140. // ok
  141. }
  142. }
  143. @Test
  144. public void download_from_file() throws Exception {
  145. Plugin test = new Plugin("test");
  146. File file = testFolder.newFile("test-1.0.jar");
  147. file.createNewFile();
  148. Release test10 = new Release(test, "1.0").setDownloadUrl("file://" + separatorsToUnix(file.getCanonicalPath()));
  149. test.addRelease(test10);
  150. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  151. pluginDownloader.start();
  152. pluginDownloader.download("foo", create("1.0"));
  153. verify(httpDownloader, never()).download(any(URI.class), any(File.class));
  154. assertThat(pluginDownloader.hasDownloads()).isTrue();
  155. }
  156. @Test
  157. public void throw_exception_if_could_not_download() {
  158. Plugin test = new Plugin("test");
  159. Release test10 = new Release(test, "1.0").setDownloadUrl("file://not_found");
  160. test.addRelease(test10);
  161. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  162. pluginDownloader.start();
  163. try {
  164. pluginDownloader.download("foo", create("1.0"));
  165. fail();
  166. } catch (SonarException e) {
  167. // ok
  168. }
  169. }
  170. @Test
  171. public void throw_exception_if_download_fail() {
  172. Plugin test = new Plugin("test");
  173. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
  174. test.addRelease(test10);
  175. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  176. doThrow(new RuntimeException()).when(httpDownloader).download(any(URI.class), any(File.class));
  177. pluginDownloader.start();
  178. try {
  179. pluginDownloader.download("foo", create("1.0"));
  180. fail();
  181. } catch (SonarException e) {
  182. // ok
  183. }
  184. }
  185. @Test
  186. public void read_download_folder() throws Exception {
  187. pluginDownloader.start();
  188. assertThat(pluginDownloader.getDownloadedPluginFilenames()).hasSize(0);
  189. copyFileToDirectory(TestProjectUtils.jarOf("test-base-plugin"), downloadDir);
  190. assertThat(pluginDownloader.getDownloadedPlugins()).hasSize(1);
  191. PluginInfo info = pluginDownloader.getDownloadedPlugins().iterator().next();
  192. assertThat(info.getKey()).isEqualTo("testbase");
  193. assertThat(info.getName()).isEqualTo("Base Plugin");
  194. assertThat(info.getVersion()).isEqualTo(Version.create("0.1-SNAPSHOT"));
  195. assertThat(info.getMainClass()).isEqualTo("BasePlugin");
  196. }
  197. @Test
  198. public void getDownloadedPluginFilenames_reads_plugin_info_of_files_in_download_folder() throws Exception {
  199. pluginDownloader.start();
  200. assertThat(pluginDownloader.getDownloadedPlugins()).hasSize(0);
  201. File file1 = new File(downloadDir, "file1.jar");
  202. file1.createNewFile();
  203. File file2 = new File(downloadDir, "file2.jar");
  204. file2.createNewFile();
  205. assertThat(pluginDownloader.getDownloadedPluginFilenames()).hasSize(2);
  206. }
  207. @Test
  208. public void cancel_downloads() throws Exception {
  209. File file1 = new File(downloadDir, "file1.jar");
  210. file1.createNewFile();
  211. File file2 = new File(downloadDir, "file2.jar");
  212. file2.createNewFile();
  213. pluginDownloader.start();
  214. assertThat(pluginDownloader.hasDownloads()).isTrue();
  215. pluginDownloader.cancelDownloads();
  216. assertThat(pluginDownloader.hasDownloads()).isFalse();
  217. }
  218. // SONAR-5011
  219. @Test
  220. public void download_common_transitive_dependency() {
  221. Plugin test1 = new Plugin("test1");
  222. Release test1R = new Release(test1, "1.0").setDownloadUrl("http://server/test1-1.0.jar");
  223. test1.addRelease(test1R);
  224. Plugin test2 = new Plugin("test2");
  225. Release test2R = new Release(test2, "1.0").setDownloadUrl("http://server/test2-1.0.jar");
  226. test2.addRelease(test2R);
  227. Plugin testDep = new Plugin("testdep");
  228. Release testDepR = new Release(testDep, "1.0").setDownloadUrl("http://server/testdep-1.0.jar");
  229. testDep.addRelease(testDepR);
  230. when(updateCenter.findInstallablePlugins("test1", create("1.0"))).thenReturn(newArrayList(test1R, testDepR));
  231. when(updateCenter.findInstallablePlugins("test2", create("1.0"))).thenReturn(newArrayList(test2R, testDepR));
  232. pluginDownloader.start();
  233. pluginDownloader.download("test1", create("1.0"));
  234. pluginDownloader.download("test2", create("1.0"));
  235. assertThat(new File(downloadDir, "test1-1.0.jar")).exists();
  236. assertThat(new File(downloadDir, "test2-1.0.jar")).exists();
  237. assertThat(new File(downloadDir, "testdep-1.0.jar")).exists();
  238. }
  239. class HasFileName extends ArgumentMatcher<File> {
  240. private final String name;
  241. HasFileName(String name) {
  242. this.name = name;
  243. }
  244. public boolean matches(Object obj) {
  245. File file = (File) obj;
  246. return file.getName().equals(name);
  247. }
  248. }
  249. }