]> source.dussan.org Git - sonarqube.git/commitdiff
LICENSE-99 backward support of plugins depending on license plugin
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 4 Jun 2018 11:20:32 +0000 (13:20 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 12 Jun 2018 18:21:01 +0000 (20:21 +0200)
both on server side (web server & CE & scanner) and in SonarLint

server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java
sonar-core/src/main/java/org/sonar/core/platform/PluginClassloaderFactory.java
sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java
sonar-core/src/test/java/com/sonarsource/plugins/license/api/FooBar.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/platform/PluginClassloaderFactoryTest.java
sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java

index a5755a839d389cf4b8c514224f6d82f6b8bc95de..75d15421ba20e40016a16b98a558112926e9990b 100644 (file)
 package org.sonar.server.platform.platformlevel;
 
 import org.sonar.api.utils.Durations;
+import org.sonar.core.extension.CoreExtensionRepositoryImpl;
+import org.sonar.core.extension.CoreExtensionsLoader;
 import org.sonar.core.i18n.RuleI18nManager;
 import org.sonar.core.platform.PluginClassloaderFactory;
 import org.sonar.core.platform.PluginLoader;
-import org.sonar.core.extension.CoreExtensionRepositoryImpl;
-import org.sonar.core.extension.CoreExtensionsLoader;
-import org.sonar.server.l18n.ServerI18n;
 import org.sonar.server.es.MigrationEsClientImpl;
+import org.sonar.server.l18n.ServerI18n;
 import org.sonar.server.platform.DatabaseServerCompatibility;
 import org.sonar.server.platform.DefaultServerUpgradeStatus;
 import org.sonar.server.platform.StartupMetadataProvider;
index fe5c33300ed87ec941db12a0afecfa0f989dd85c..0706050feaddbf67735b0c395f1ca4bd675be4c9 100644 (file)
@@ -178,6 +178,7 @@ public class PluginClassloaderFactory {
       .addInclusion("org/sonar/core/persistence/")
       .addInclusion("org/sonar/core/properties/")
       .addInclusion("org/sonar/server/views/")
+      .addInclusion("com/sonarsource/plugins/license/api/")
 
       // API exclusions
       .addExclusion("org/sonar/api/internal/");
index cdaff422d187c50f2b808db92ff3d79057ec928a..e888ff1447e2d86bea25a6282187a3872e5cd136 100644 (file)
@@ -25,6 +25,7 @@ import com.google.common.collect.ComparisonChain;
 import com.google.common.collect.Ordering;
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.regex.Pattern;
@@ -429,9 +430,10 @@ public class PluginInfo implements Comparable<PluginInfo> {
     info.setImplementationBuild(manifest.getImplementationBuild());
     String[] requiredPlugins = manifest.getRequirePlugins();
     if (requiredPlugins != null) {
-      for (String s : requiredPlugins) {
-        info.addRequiredPlugin(RequiredPlugin.parse(s));
-      }
+      Arrays.stream(requiredPlugins)
+        .map(RequiredPlugin::parse)
+        .filter(t -> !"license".equals(t.key))
+        .forEach(info::addRequiredPlugin);
     }
     return info;
   }
diff --git a/sonar-core/src/test/java/com/sonarsource/plugins/license/api/FooBar.java b/sonar-core/src/test/java/com/sonarsource/plugins/license/api/FooBar.java
new file mode 100644 (file)
index 0000000..7c44097
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package com.sonarsource.plugins.license.api;
+
+public interface FooBar {
+}
index a8542bd0fede1a8d356005723a6adbf451fc1f46..9d825b758af58f77a3a410b404e2f408cf59c125 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.core.platform;
 
+import com.sonarsource.plugins.license.api.FooBar;
 import java.io.File;
 import java.util.Map;
 import org.apache.commons.lang.StringUtils;
@@ -92,6 +93,17 @@ public class PluginClassloaderFactoryTest {
     assertThat(canLoadClass(baseClassloader, BASE_PLUGIN_CLASSNAME)).isTrue();
   }
 
+  @Test
+  public void classloader_exposes_license_api_from_main_classloader() {
+    PluginClassLoaderDef def = basePluginDef();
+    Map<PluginClassLoaderDef, ClassLoader> map = factory.create(asList(def));
+
+    assertThat(map).containsOnlyKeys(def);
+    ClassLoader classLoader = map.get(def);
+
+    assertThat(canLoadClass(classLoader, FooBar.class.getCanonicalName())).isTrue();
+  }
+
   private static PluginClassLoaderDef basePluginDef() {
     PluginClassLoaderDef def = new PluginClassLoaderDef(BASE_PLUGIN_KEY);
     def.addMainClass(BASE_PLUGIN_KEY, BASE_PLUGIN_CLASSNAME);
index d96963a6cf80dcf72dcf787d1337c75df41db9fb..72a0c5832046358e69e99afaeac9c1062d47708e 100644 (file)
@@ -19,6 +19,9 @@
  */
 package org.sonar.core.platform;
 
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
@@ -30,6 +33,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.ZipUtils;
 import org.sonar.updatecenter.common.PluginManifest;
@@ -39,6 +43,7 @@ import static com.google.common.collect.Ordering.natural;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
 
+@RunWith(DataProviderRunner.class)
 public class PluginInfoTest {
 
   @Rule
@@ -48,7 +53,7 @@ public class PluginInfoTest {
   public ExpectedException expectedException = ExpectedException.none();
 
   @Test
-  public void test_RequiredPlugin() throws Exception {
+  public void test_RequiredPlugin() {
     PluginInfo.RequiredPlugin plugin = PluginInfo.RequiredPlugin.parse("java:1.1");
     assertThat(plugin.getKey()).isEqualTo("java");
     assertThat(plugin.getMinimalVersion().getName()).isEqualTo("1.1");
@@ -105,7 +110,7 @@ public class PluginInfoTest {
    * All other build environments have unique release versions (6.3.0.12345).
    */
   @Test
-  public void test_compatibility_with_snapshot_version_of_sonarqube() throws IOException {
+  public void test_compatibility_with_snapshot_version_of_sonarqube() {
     // plugins compatible with 5.6 LTS
     assertThat(withMinSqVersion("5.6").isCompatibleWith("6.3-SNAPSHOT")).isTrue();
     assertThat(withMinSqVersion("5.6.1").isCompatibleWith("6.3-SNAPSHOT")).isTrue();
@@ -134,7 +139,7 @@ public class PluginInfoTest {
    * @see #test_compatibility_with_snapshot_version_of_sonarqube
    */
   @Test
-  public void test_compatibility_with_release_version_of_sonarqube() throws IOException {
+  public void test_compatibility_with_release_version_of_sonarqube() {
     // plugins compatible with 5.6 LTS
     assertThat(withMinSqVersion("5.6").isCompatibleWith("6.3.0.5000")).isTrue();
     assertThat(withMinSqVersion("5.6.1").isCompatibleWith("6.3.0.5000")).isTrue();
@@ -224,6 +229,44 @@ public class PluginInfoTest {
     assertThat(pluginInfo.isSonarLintSupported()).isTrue();
   }
 
+  @Test
+  @UseDataProvider("licenseVersions")
+  public void requiredPlugin_license_is_ignored_when_reading_manifest(String version) throws IOException {
+    PluginManifest manifest = new PluginManifest();
+    manifest.setKey("java");
+    manifest.setVersion("1.0");
+    manifest.setName("Java");
+    manifest.setMainClass("org.foo.FooPlugin");
+    manifest.setRequirePlugins(new String[] {"license:" + version});
+
+    File jarFile = temp.newFile();
+    PluginInfo pluginInfo = PluginInfo.create(jarFile, manifest);
+    assertThat(pluginInfo.getRequiredPlugins()).isEmpty();
+  }
+
+  @Test
+  @UseDataProvider("licenseVersions")
+  public void requiredPlugin_license_among_others_is_ignored_when_reading_manifest(String version) throws IOException {
+    PluginManifest manifest = new PluginManifest();
+    manifest.setKey("java");
+    manifest.setVersion("1.0");
+    manifest.setName("Java");
+    manifest.setMainClass("org.foo.FooPlugin");
+    manifest.setRequirePlugins(new String[] {"java:2.0", "license:" + version, "pmd:1.3"});
+
+    File jarFile = temp.newFile();
+    PluginInfo pluginInfo = PluginInfo.create(jarFile, manifest);
+    assertThat(pluginInfo.getRequiredPlugins()).extracting("key").containsOnly("java", "pmd");
+  }
+
+  @DataProvider
+  public static Object[][] licenseVersions() {
+    return new Object[][] {
+      {"0.3"},
+      {"7.2.0.1253"}
+    };
+  }
+
   @Test
   public void create_from_file() {
     File checkstyleJar = FileUtils.toFile(getClass().getResource("/org/sonar/core/platform/sonar-checkstyle-plugin-2.8.jar"));