Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SonarQube Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.impl;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.TemporaryFolder;
  24. import org.sonar.runner.cache.FileCache;
  25. import org.sonar.runner.cache.Logger;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.List;
  29. import java.util.Properties;
  30. import static org.fest.assertions.Assertions.assertThat;
  31. import static org.fest.assertions.Fail.fail;
  32. import static org.mockito.Matchers.any;
  33. import static org.mockito.Matchers.eq;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.times;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.verifyNoMoreInteractions;
  38. import static org.mockito.Mockito.when;
  39. public class JarsTest {
  40. ServerConnection connection = mock(ServerConnection.class);
  41. JarExtractor jarExtractor = mock(JarExtractor.class);
  42. FileCache fileCache = mock(FileCache.class);
  43. @Rule
  44. public TemporaryFolder temp = new TemporaryFolder();
  45. @Test
  46. public void should_download_jar_files() throws Exception {
  47. File batchJar = temp.newFile("sonar-runner-batch.jar");
  48. when(jarExtractor.extractToTemp("sonar-runner-batch")).thenReturn(batchJar);
  49. // index of the files to download
  50. when(connection.loadString("/batch_bootstrap/index")).thenReturn(
  51. "cpd.jar|CA124VADFSDS\n" +
  52. "squid.jar|34535FSFSDF\n");
  53. Jars jars35 = new Jars(fileCache, connection, jarExtractor, mock(Logger.class));
  54. List<File> files = jars35.download();
  55. assertThat(files).isNotNull();
  56. verify(connection, times(1)).loadString("/batch_bootstrap/index");
  57. verifyNoMoreInteractions(connection);
  58. verify(fileCache, times(1)).get(eq("cpd.jar"), eq("CA124VADFSDS"), any(FileCache.Downloader.class));
  59. verify(fileCache, times(1)).get(eq("squid.jar"), eq("34535FSFSDF"), any(FileCache.Downloader.class));
  60. verifyNoMoreInteractions(fileCache);
  61. }
  62. @Test
  63. public void should_honor_sonarUserHome() throws IOException {
  64. Properties props = new Properties();
  65. File f = temp.newFolder();
  66. props.put("sonar.userHome", f.getAbsolutePath());
  67. Jars jars = new Jars(connection, jarExtractor, mock(Logger.class), props);
  68. assertThat(jars.getFileCache().getDir()).isEqualTo(new File(f, "cache"));
  69. }
  70. @Test
  71. public void should_fail_to_download_files() throws Exception {
  72. File batchJar = temp.newFile("sonar-runner-batch.jar");
  73. when(jarExtractor.extractToTemp("sonar-runner-batch")).thenReturn(batchJar);
  74. // index of the files to download
  75. when(connection.loadString("/batch_bootstrap/index")).thenThrow(new IllegalStateException());
  76. Jars jars35 = new Jars(fileCache, connection, jarExtractor, mock(Logger.class));
  77. try {
  78. jars35.download();
  79. fail();
  80. } catch (RuntimeException e) {
  81. assertThat(e).hasMessage("Fail to download libraries from server");
  82. }
  83. }
  84. @Test
  85. public void test_jar_downloader() throws Exception {
  86. Jars.BatchFileDownloader downloader = new Jars.BatchFileDownloader(connection);
  87. File toFile = temp.newFile();
  88. downloader.download("squid.jar", toFile);
  89. verify(connection).download("/batch/squid.jar", toFile);
  90. }
  91. }