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.

GeneratePluginIndexTest.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.startup;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import org.apache.commons.io.FileUtils;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.TemporaryFolder;
  29. import org.sonar.core.platform.PluginInfo;
  30. import org.sonar.server.platform.ServerFileSystem;
  31. import org.sonar.server.plugins.PluginFilesAndMd5.FileAndMd5;
  32. import org.sonar.server.plugins.ServerPlugin;
  33. import org.sonar.server.plugins.ServerPluginRepository;
  34. import static java.util.Arrays.asList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. import static org.sonar.core.plugin.PluginType.BUNDLED;
  40. public class GeneratePluginIndexTest {
  41. @Rule
  42. public TemporaryFolder temp = new TemporaryFolder();
  43. private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class);
  44. private ServerPluginRepository serverPluginRepository = mock(ServerPluginRepository.class);
  45. private File index;
  46. @Before
  47. public void createIndexFile() throws IOException {
  48. index = temp.newFile();
  49. when(serverFileSystem.getPluginIndex()).thenReturn(index);
  50. }
  51. @Test
  52. public void shouldWriteIndex() throws IOException {
  53. ServerPlugin javaPlugin = newInstalledPlugin("java", true);
  54. ServerPlugin gitPlugin = newInstalledPlugin("scmgit", false);
  55. when(serverPluginRepository.getPlugins()).thenReturn(asList(javaPlugin, gitPlugin));
  56. GeneratePluginIndex underTest = new GeneratePluginIndex(serverFileSystem, serverPluginRepository);
  57. underTest.start();
  58. List<String> lines = FileUtils.readLines(index);
  59. assertThat(lines).containsExactly(
  60. "java,true," + javaPlugin.getJar().getFile().getName() + "|" + javaPlugin.getJar().getMd5(),
  61. "scmgit,false," + gitPlugin.getJar().getFile().getName() + "|" + gitPlugin.getJar().getMd5());
  62. underTest.stop();
  63. }
  64. @Test
  65. public void shouldThrowWhenUnableToWrite() throws IOException {
  66. File wrongParent = temp.newFile();
  67. wrongParent.createNewFile();
  68. File wrongIndex = new File(wrongParent, "index.txt");
  69. when(serverFileSystem.getPluginIndex()).thenReturn(wrongIndex);
  70. assertThatThrownBy(() -> new GeneratePluginIndex(serverFileSystem, serverPluginRepository).start())
  71. .isInstanceOf(IllegalStateException.class);
  72. }
  73. private ServerPlugin newInstalledPlugin(String key, boolean supportSonarLint) throws IOException {
  74. FileAndMd5 jar = new FileAndMd5(temp.newFile());
  75. PluginInfo pluginInfo = new PluginInfo(key).setJarFile(jar.getFile()).setSonarLintSupported(supportSonarLint);
  76. return new ServerPlugin(pluginInfo, BUNDLED, null, jar, null);
  77. }
  78. }