Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ServerExtensionInstallerTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 java.util.Collection;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.Plugin;
  28. import org.sonar.api.SonarEdition;
  29. import org.sonar.api.SonarQubeSide;
  30. import org.sonar.api.SonarRuntime;
  31. import org.sonar.api.internal.SonarRuntimeImpl;
  32. import org.sonar.api.server.ServerSide;
  33. import org.sonar.api.utils.MessageException;
  34. import org.sonar.api.utils.Version;
  35. import org.sonar.core.platform.ComponentContainer;
  36. import org.sonar.core.platform.PluginInfo;
  37. import org.sonar.core.platform.PluginRepository;
  38. import static java.util.Collections.singleton;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static org.mockito.Mockito.mock;
  41. import static org.mockito.Mockito.when;
  42. public class ServerExtensionInstallerTest {
  43. @Rule
  44. public ExpectedException expectedException = ExpectedException.none();
  45. private SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.parse("8.0"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY);
  46. private TestPluginRepository pluginRepository = new TestPluginRepository();
  47. private TestServerExtensionInstaller underTest = new TestServerExtensionInstaller(sonarRuntime, pluginRepository);
  48. @Test
  49. public void add_plugin_to_container() {
  50. PluginInfo fooPluginInfo = newPlugin("foo", "Foo");
  51. Plugin fooPlugin = mock(Plugin.class);
  52. pluginRepository.add(fooPluginInfo, fooPlugin);
  53. ComponentContainer componentContainer = new ComponentContainer();
  54. underTest.installExtensions(componentContainer);
  55. assertThat(componentContainer.getPicoContainer().getComponents()).contains(fooPlugin);
  56. }
  57. @Test
  58. public void fail_when_detecting_github_auth_plugin() {
  59. PluginInfo foo = newPlugin("authgithub", "GitHub Auth");
  60. pluginRepository.add(foo, mock(Plugin.class));
  61. ComponentContainer componentContainer = new ComponentContainer();
  62. expectedException.expect(MessageException.class);
  63. expectedException.expectMessage("Plugins 'GitHub Auth' are no more compatible with SonarQube");
  64. underTest.installExtensions(componentContainer);
  65. }
  66. @Test
  67. public void fail_when_detecting_gitlab_auth_plugin() {
  68. PluginInfo foo = newPlugin("authgitlab", "GitLab Auth");
  69. pluginRepository.add(foo, mock(Plugin.class));
  70. ComponentContainer componentContainer = new ComponentContainer();
  71. expectedException.expect(MessageException.class);
  72. expectedException.expectMessage("Plugins 'GitLab Auth' are no more compatible with SonarQube");
  73. underTest.installExtensions(componentContainer);
  74. }
  75. @Test
  76. public void fail_when_detecting_saml_auth_plugin() {
  77. PluginInfo foo = newPlugin("authsaml", "SAML Auth");
  78. pluginRepository.add(foo, mock(Plugin.class));
  79. ComponentContainer componentContainer = new ComponentContainer();
  80. expectedException.expect(MessageException.class);
  81. expectedException.expectMessage("Plugins 'SAML Auth' are no more compatible with SonarQube");
  82. underTest.installExtensions(componentContainer);
  83. }
  84. private static PluginInfo newPlugin(String key, String name) {
  85. PluginInfo plugin = mock(PluginInfo.class);
  86. when(plugin.getKey()).thenReturn(key);
  87. when(plugin.getName()).thenReturn(name);
  88. return plugin;
  89. }
  90. private static class TestPluginRepository implements PluginRepository {
  91. private final Map<String, PluginInfo> pluginsInfoMap = new HashMap<>();
  92. private final Map<String, Plugin> pluginsMap = new HashMap<>();
  93. void add(PluginInfo pluginInfo, Plugin plugin) {
  94. pluginsInfoMap.put(pluginInfo.getKey(), pluginInfo);
  95. pluginsMap.put(pluginInfo.getKey(), plugin);
  96. }
  97. @Override
  98. public Collection<PluginInfo> getPluginInfos() {
  99. return pluginsInfoMap.values();
  100. }
  101. @Override
  102. public PluginInfo getPluginInfo(String key) {
  103. if (!pluginsMap.containsKey(key)) {
  104. throw new IllegalArgumentException();
  105. }
  106. return pluginsInfoMap.get(key);
  107. }
  108. @Override
  109. public Plugin getPluginInstance(String key) {
  110. if (!pluginsMap.containsKey(key)) {
  111. throw new IllegalArgumentException();
  112. }
  113. return pluginsMap.get(key);
  114. }
  115. @Override
  116. public boolean hasPlugin(String key) {
  117. return pluginsMap.containsKey(key);
  118. }
  119. }
  120. private static class TestServerExtensionInstaller extends ServerExtensionInstaller {
  121. protected TestServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository) {
  122. super(sonarRuntime, pluginRepository, singleton(ServerSide.class));
  123. }
  124. }
  125. }