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.

PluginClassloaderFactoryTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.sonarsource.plugins.license.api.FooBar;
  22. import java.io.File;
  23. import java.util.Map;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.junit.Test;
  26. import org.sonar.api.server.rule.RulesDefinition;
  27. import static java.util.Arrays.asList;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. public class PluginClassloaderFactoryTest {
  30. static final String BASE_PLUGIN_CLASSNAME = "org.sonar.plugins.base.BasePlugin";
  31. static final String DEPENDENT_PLUGIN_CLASSNAME = "org.sonar.plugins.dependent.DependentPlugin";
  32. static final String BASE_PLUGIN_KEY = "base";
  33. static final String DEPENDENT_PLUGIN_KEY = "dependent";
  34. PluginClassloaderFactory factory = new PluginClassloaderFactory();
  35. @Test
  36. public void create_isolated_classloader() {
  37. PluginClassLoaderDef def = basePluginDef();
  38. Map<PluginClassLoaderDef, ClassLoader> map = factory.create(asList(def));
  39. assertThat(map).containsOnlyKeys(def);
  40. ClassLoader classLoader = map.get(def);
  41. // plugin can access to API classes, and of course to its own classes !
  42. assertThat(canLoadClass(classLoader, RulesDefinition.class.getCanonicalName())).isTrue();
  43. assertThat(canLoadClass(classLoader, BASE_PLUGIN_CLASSNAME)).isTrue();
  44. // plugin can not access to core classes
  45. assertThat(canLoadClass(classLoader, PluginClassloaderFactory.class.getCanonicalName())).isFalse();
  46. assertThat(canLoadClass(classLoader, Test.class.getCanonicalName())).isFalse();
  47. assertThat(canLoadClass(classLoader, StringUtils.class.getCanonicalName())).isFalse();
  48. }
  49. @Test
  50. public void classloader_exports_resources_to_other_classloaders() {
  51. PluginClassLoaderDef baseDef = basePluginDef();
  52. PluginClassLoaderDef dependentDef = dependentPluginDef();
  53. Map<PluginClassLoaderDef, ClassLoader> map = factory.create(asList(baseDef, dependentDef));
  54. ClassLoader baseClassloader = map.get(baseDef);
  55. ClassLoader dependentClassloader = map.get(dependentDef);
  56. // base-plugin exports its API package to other plugins
  57. assertThat(canLoadClass(dependentClassloader, "org.sonar.plugins.base.api.BaseApi")).isTrue();
  58. assertThat(canLoadClass(dependentClassloader, BASE_PLUGIN_CLASSNAME)).isFalse();
  59. assertThat(canLoadClass(dependentClassloader, DEPENDENT_PLUGIN_CLASSNAME)).isTrue();
  60. // dependent-plugin does not export its classes
  61. assertThat(canLoadClass(baseClassloader, DEPENDENT_PLUGIN_CLASSNAME)).isFalse();
  62. assertThat(canLoadClass(baseClassloader, BASE_PLUGIN_CLASSNAME)).isTrue();
  63. }
  64. @Test
  65. public void classloader_exposes_license_api_from_main_classloader() {
  66. PluginClassLoaderDef def = basePluginDef();
  67. Map<PluginClassLoaderDef, ClassLoader> map = factory.create(asList(def));
  68. assertThat(map).containsOnlyKeys(def);
  69. ClassLoader classLoader = map.get(def);
  70. assertThat(canLoadClass(classLoader, FooBar.class.getCanonicalName())).isTrue();
  71. }
  72. private static PluginClassLoaderDef basePluginDef() {
  73. PluginClassLoaderDef def = new PluginClassLoaderDef(BASE_PLUGIN_KEY);
  74. def.addMainClass(BASE_PLUGIN_KEY, BASE_PLUGIN_CLASSNAME);
  75. def.getExportMask().addInclusion("org/sonar/plugins/base/api/");
  76. def.addFiles(asList(fakePluginJar("base-plugin/target/base-plugin-0.1-SNAPSHOT.jar")));
  77. return def;
  78. }
  79. private static PluginClassLoaderDef dependentPluginDef() {
  80. PluginClassLoaderDef def = new PluginClassLoaderDef(DEPENDENT_PLUGIN_KEY);
  81. def.addMainClass(DEPENDENT_PLUGIN_KEY, DEPENDENT_PLUGIN_CLASSNAME);
  82. def.getExportMask().addInclusion("org/sonar/plugins/dependent/api/");
  83. def.addFiles(asList(fakePluginJar("dependent-plugin/target/dependent-plugin-0.1-SNAPSHOT.jar")));
  84. return def;
  85. }
  86. private static File fakePluginJar(String path) {
  87. // Maven way
  88. File file = new File("src/test/projects/" + path);
  89. if (!file.exists()) {
  90. // Intellij way
  91. file = new File("sonar-core/src/test/projects/" + path);
  92. if (!file.exists()) {
  93. throw new IllegalArgumentException("Fake projects are not built: " + path);
  94. }
  95. }
  96. return file;
  97. }
  98. private static boolean canLoadClass(ClassLoader classloader, String classname) {
  99. try {
  100. classloader.loadClass(classname);
  101. return true;
  102. } catch (ClassNotFoundException e) {
  103. return false;
  104. }
  105. }
  106. }