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.

PluginLoaderTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.core.platform;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.assertj.core.data.MapEntry;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.sonar.api.utils.log.LogTester;
  31. import org.sonar.api.utils.log.LoggerLevel;
  32. import org.sonar.updatecenter.common.Version;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.entry;
  35. import static org.mockito.Mockito.mock;
  36. public class PluginLoaderTest {
  37. @Rule
  38. public TemporaryFolder temp = new TemporaryFolder();
  39. @Rule
  40. public LogTester logTester = new LogTester();
  41. private PluginClassloaderFactory classloaderFactory = mock(PluginClassloaderFactory.class);
  42. private PluginLoader underTest = new PluginLoader(new FakePluginExploder(), classloaderFactory);
  43. @Test
  44. public void define_classloader() throws Exception {
  45. File jarFile = temp.newFile();
  46. PluginInfo info = new PluginInfo("foo")
  47. .setJarFile(jarFile)
  48. .setMainClass("org.foo.FooPlugin")
  49. .setMinimalSqVersion(Version.create("5.2"));
  50. Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of("foo", info));
  51. assertThat(defs).hasSize(1);
  52. PluginClassLoaderDef def = defs.iterator().next();
  53. assertThat(def.getBasePluginKey()).isEqualTo("foo");
  54. assertThat(def.isSelfFirstStrategy()).isFalse();
  55. assertThat(def.getFiles()).containsOnly(jarFile);
  56. assertThat(def.getMainClassesByPluginKey()).containsOnly(MapEntry.entry("foo", "org.foo.FooPlugin"));
  57. // TODO test mask - require change in sonar-classloader
  58. }
  59. /**
  60. * A plugin (the "base" plugin) can be extended by other plugins. In this case they share the same classloader.
  61. */
  62. @Test
  63. public void test_plugins_sharing_the_same_classloader() throws Exception {
  64. File baseJarFile = temp.newFile();
  65. File extensionJar1 = temp.newFile();
  66. File extensionJar2 = temp.newFile();
  67. PluginInfo base = new PluginInfo("foo")
  68. .setJarFile(baseJarFile)
  69. .setMainClass("org.foo.FooPlugin")
  70. .setUseChildFirstClassLoader(false);
  71. PluginInfo extension1 = new PluginInfo("fooExtension1")
  72. .setJarFile(extensionJar1)
  73. .setMainClass("org.foo.Extension1Plugin")
  74. .setBasePlugin("foo");
  75. // This extension tries to change the classloader-ordering strategy of base plugin
  76. // (see setUseChildFirstClassLoader(true)).
  77. // That is not allowed and should be ignored -> strategy is still the one
  78. // defined on base plugin (parent-first in this example)
  79. PluginInfo extension2 = new PluginInfo("fooExtension2")
  80. .setJarFile(extensionJar2)
  81. .setMainClass("org.foo.Extension2Plugin")
  82. .setBasePlugin("foo")
  83. .setUseChildFirstClassLoader(true);
  84. Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of(
  85. base.getKey(), base, extension1.getKey(), extension1, extension2.getKey(), extension2));
  86. assertThat(defs).hasSize(1);
  87. PluginClassLoaderDef def = defs.iterator().next();
  88. assertThat(def.getBasePluginKey()).isEqualTo("foo");
  89. assertThat(def.isSelfFirstStrategy()).isFalse();
  90. assertThat(def.getFiles()).containsOnly(baseJarFile, extensionJar1, extensionJar2);
  91. assertThat(def.getMainClassesByPluginKey()).containsOnly(
  92. entry("foo", "org.foo.FooPlugin"),
  93. entry("fooExtension1", "org.foo.Extension1Plugin"),
  94. entry("fooExtension2", "org.foo.Extension2Plugin"));
  95. // TODO test mask - require change in sonar-classloader
  96. }
  97. @Test
  98. public void log_warning_if_plugin_is_built_with_api_5_2_or_lower() throws Exception {
  99. File jarFile = temp.newFile();
  100. PluginInfo info = new PluginInfo("foo")
  101. .setJarFile(jarFile)
  102. .setMainClass("org.foo.FooPlugin")
  103. .setMinimalSqVersion(Version.create("4.5.2"));
  104. Collection<PluginClassLoaderDef> defs = underTest.defineClassloaders(ImmutableMap.of("foo", info));
  105. assertThat(defs).extracting(PluginClassLoaderDef::getBasePluginKey).containsExactly("foo");
  106. List<String> warnings = logTester.logs(LoggerLevel.WARN);
  107. assertThat(warnings).contains("API compatibility mode is no longer supported. In case of error, plugin foo [foo] should package its dependencies.");
  108. }
  109. /**
  110. * Does not unzip jar file. It directly returns the JAR file defined on PluginInfo.
  111. */
  112. private static class FakePluginExploder extends PluginJarExploder {
  113. @Override
  114. public ExplodedPlugin explode(PluginInfo info) {
  115. return new ExplodedPlugin(info.getKey(), info.getNonNullJarFile(), Collections.emptyList());
  116. }
  117. }
  118. }