Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PluginDownloaderTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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 com.google.common.base.Optional;
  22. import java.io.File;
  23. import java.net.URI;
  24. import org.junit.After;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.mockito.ArgumentMatcher;
  31. import org.mockito.invocation.InvocationOnMock;
  32. import org.mockito.stubbing.Answer;
  33. import org.sonar.api.utils.HttpDownloader;
  34. import org.sonar.core.platform.PluginInfo;
  35. import org.sonar.server.exceptions.BadRequestException;
  36. import org.sonar.server.platform.ServerFileSystem;
  37. import org.sonar.updatecenter.common.Plugin;
  38. import org.sonar.updatecenter.common.Release;
  39. import org.sonar.updatecenter.common.UpdateCenter;
  40. import org.sonar.updatecenter.common.Version;
  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.ArgumentMatchers.any;
  48. import static org.mockito.ArgumentMatchers.anyBoolean;
  49. import static org.mockito.ArgumentMatchers.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. @Rule
  61. public ExpectedException expectedException = ExpectedException.none();
  62. private File downloadDir;
  63. private UpdateCenterMatrixFactory updateCenterMatrixFactory;
  64. private UpdateCenter updateCenter;
  65. private HttpDownloader httpDownloader;
  66. private PluginDownloader pluginDownloader;
  67. @Before
  68. public void before() throws Exception {
  69. updateCenterMatrixFactory = mock(UpdateCenterMatrixFactory.class);
  70. updateCenter = mock(UpdateCenter.class);
  71. when(updateCenterMatrixFactory.getUpdateCenter(anyBoolean())).thenReturn(Optional.of(updateCenter));
  72. httpDownloader = mock(HttpDownloader.class);
  73. doAnswer(new Answer<Void>() {
  74. @Override
  75. public Void answer(InvocationOnMock inv) throws Throwable {
  76. File toFile = (File) inv.getArguments()[1];
  77. touch(toFile);
  78. return null;
  79. }
  80. }).when(httpDownloader).download(any(URI.class), any(File.class));
  81. ServerFileSystem fs = mock(ServerFileSystem.class);
  82. downloadDir = testFolder.newFolder("downloads");
  83. when(fs.getDownloadedPluginsDir()).thenReturn(downloadDir);
  84. pluginDownloader = new PluginDownloader(updateCenterMatrixFactory, httpDownloader, fs);
  85. }
  86. @After
  87. public void stop() {
  88. pluginDownloader.stop();
  89. }
  90. @Test
  91. public void clean_temporary_files_at_startup() throws Exception {
  92. touch(new File(downloadDir, "sonar-php.jar"));
  93. touch(new File(downloadDir, "sonar-js.jar.tmp"));
  94. assertThat(downloadDir.listFiles()).hasSize(2);
  95. pluginDownloader.start();
  96. File[] files = downloadDir.listFiles();
  97. assertThat(files).hasSize(1);
  98. assertThat(files[0].getName()).isEqualTo("sonar-php.jar");
  99. }
  100. @Test
  101. public void download_from_url() {
  102. Plugin test = Plugin.factory("test");
  103. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
  104. test.addRelease(test10);
  105. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  106. pluginDownloader.start();
  107. pluginDownloader.download("foo", create("1.0"));
  108. // SONAR-4523: do not corrupt JAR files when restarting the server while a plugin is being downloaded.
  109. // The JAR file is downloaded in a temp file
  110. verify(httpDownloader).download(any(URI.class), argThat(new HasFileName("test-1.0.jar.tmp")));
  111. assertThat(new File(downloadDir, "test-1.0.jar")).exists();
  112. assertThat(new File(downloadDir, "test-1.0.jar.tmp")).doesNotExist();
  113. }
  114. @Test
  115. public void download_when_update_center_is_unavailable_with_no_exception_thrown() {
  116. when(updateCenterMatrixFactory.getUpdateCenter(anyBoolean())).thenReturn(Optional.absent());
  117. Plugin test = Plugin.factory("test");
  118. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
  119. test.addRelease(test10);
  120. pluginDownloader.start();
  121. pluginDownloader.download("foo", create("1.0"));
  122. }
  123. /**
  124. * SONAR-4685
  125. */
  126. @Test
  127. public void download_from_redirect_url() {
  128. Plugin test = Plugin.factory("plugintest");
  129. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/redirect?r=release&g=test&a=test&v=1.0&e=jar");
  130. test.addRelease(test10);
  131. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  132. pluginDownloader.start();
  133. pluginDownloader.download("foo", create("1.0"));
  134. // SONAR-4523: do not corrupt JAR files when restarting the server while a plugin is being downloaded.
  135. // The JAR file is downloaded in a temp file
  136. verify(httpDownloader).download(any(URI.class), argThat(new HasFileName("plugintest-1.0.jar.tmp")));
  137. assertThat(new File(downloadDir, "plugintest-1.0.jar")).exists();
  138. assertThat(new File(downloadDir, "plugintest-1.0.jar.tmp")).doesNotExist();
  139. }
  140. @Test
  141. public void throw_exception_if_download_dir_is_invalid() throws Exception {
  142. ServerFileSystem fs = mock(ServerFileSystem.class);
  143. // download dir is a file instead of being a directory
  144. File downloadDir = testFolder.newFile();
  145. when(fs.getDownloadedPluginsDir()).thenReturn(downloadDir);
  146. pluginDownloader = new PluginDownloader(updateCenterMatrixFactory, httpDownloader, fs);
  147. try {
  148. pluginDownloader.start();
  149. fail();
  150. } catch (IllegalStateException e) {
  151. // ok
  152. }
  153. }
  154. @Test
  155. public void fail_if_no_compatible_plugin_found() {
  156. expectedException.expect(BadRequestException.class);
  157. pluginDownloader.download("foo", create("1.0"));
  158. }
  159. @Test
  160. public void download_from_file() throws Exception {
  161. Plugin test = Plugin.factory("test");
  162. File file = testFolder.newFile("test-1.0.jar");
  163. file.createNewFile();
  164. Release test10 = new Release(test, "1.0").setDownloadUrl("file://" + separatorsToUnix(file.getCanonicalPath()));
  165. test.addRelease(test10);
  166. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  167. pluginDownloader.start();
  168. pluginDownloader.download("foo", create("1.0"));
  169. verify(httpDownloader, never()).download(any(URI.class), any(File.class));
  170. assertThat(noDownloadedFiles()).isGreaterThan(0);
  171. }
  172. @Test
  173. public void throw_exception_if_could_not_download() {
  174. Plugin test = Plugin.factory("test");
  175. Release test10 = new Release(test, "1.0").setDownloadUrl("file://not_found");
  176. test.addRelease(test10);
  177. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  178. pluginDownloader.start();
  179. try {
  180. pluginDownloader.download("foo", create("1.0"));
  181. fail();
  182. } catch (IllegalStateException e) {
  183. // ok
  184. }
  185. }
  186. @Test
  187. public void throw_exception_if_download_fail() {
  188. Plugin test = Plugin.factory("test");
  189. Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar");
  190. test.addRelease(test10);
  191. when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10));
  192. doThrow(new RuntimeException()).when(httpDownloader).download(any(URI.class), any(File.class));
  193. pluginDownloader.start();
  194. try {
  195. pluginDownloader.download("foo", create("1.0"));
  196. fail();
  197. } catch (IllegalStateException e) {
  198. // ok
  199. }
  200. }
  201. @Test
  202. public void read_download_folder() throws Exception {
  203. pluginDownloader.start();
  204. assertThat(noDownloadedFiles()).isZero();
  205. copyFileToDirectory(TestProjectUtils.jarOf("test-base-plugin"), downloadDir);
  206. assertThat(pluginDownloader.getDownloadedPlugins()).hasSize(1);
  207. PluginInfo info = pluginDownloader.getDownloadedPlugins().iterator().next();
  208. assertThat(info.getKey()).isEqualTo("testbase");
  209. assertThat(info.getName()).isEqualTo("Base Plugin");
  210. assertThat(info.getVersion()).isEqualTo(Version.create("0.1-SNAPSHOT"));
  211. assertThat(info.getMainClass()).isEqualTo("BasePlugin");
  212. }
  213. @Test
  214. public void getDownloadedPluginFilenames_reads_plugin_info_of_files_in_download_folder() throws Exception {
  215. pluginDownloader.start();
  216. assertThat(pluginDownloader.getDownloadedPlugins()).hasSize(0);
  217. File file1 = new File(downloadDir, "file1.jar");
  218. file1.createNewFile();
  219. File file2 = new File(downloadDir, "file2.jar");
  220. file2.createNewFile();
  221. assertThat(noDownloadedFiles()).isEqualTo(2);
  222. }
  223. @Test
  224. public void cancel_downloads() throws Exception {
  225. File file1 = new File(downloadDir, "file1.jar");
  226. file1.createNewFile();
  227. File file2 = new File(downloadDir, "file2.jar");
  228. file2.createNewFile();
  229. pluginDownloader.start();
  230. assertThat(noDownloadedFiles()).isGreaterThan(0);
  231. pluginDownloader.cancelDownloads();
  232. assertThat(noDownloadedFiles()).isZero();
  233. }
  234. private int noDownloadedFiles() {
  235. return downloadDir.listFiles((file, name) -> name.endsWith(".jar")).length;
  236. }
  237. // SONAR-5011
  238. @Test
  239. public void download_common_transitive_dependency() {
  240. Plugin test1 = Plugin.factory("test1");
  241. Release test1R = new Release(test1, "1.0").setDownloadUrl("http://server/test1-1.0.jar");
  242. test1.addRelease(test1R);
  243. Plugin test2 = Plugin.factory("test2");
  244. Release test2R = new Release(test2, "1.0").setDownloadUrl("http://server/test2-1.0.jar");
  245. test2.addRelease(test2R);
  246. Plugin testDep = Plugin.factory("testdep");
  247. Release testDepR = new Release(testDep, "1.0").setDownloadUrl("http://server/testdep-1.0.jar");
  248. testDep.addRelease(testDepR);
  249. when(updateCenter.findInstallablePlugins("test1", create("1.0"))).thenReturn(newArrayList(test1R, testDepR));
  250. when(updateCenter.findInstallablePlugins("test2", create("1.0"))).thenReturn(newArrayList(test2R, testDepR));
  251. pluginDownloader.start();
  252. pluginDownloader.download("test1", create("1.0"));
  253. pluginDownloader.download("test2", create("1.0"));
  254. assertThat(new File(downloadDir, "test1-1.0.jar")).exists();
  255. assertThat(new File(downloadDir, "test2-1.0.jar")).exists();
  256. assertThat(new File(downloadDir, "testdep-1.0.jar")).exists();
  257. }
  258. class HasFileName implements ArgumentMatcher<File> {
  259. private final String name;
  260. HasFileName(String name) {
  261. this.name = name;
  262. }
  263. @Override
  264. public boolean matches(File file) {
  265. return file.getName().equals(name);
  266. }
  267. }
  268. }