From: Sébastien Lesaint Date: Thu, 7 Jun 2018 15:24:49 +0000 (+0200) Subject: SONAR-10690 make fake-billing-plugin a core extension X-Git-Tag: 7.5~996 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7de21fcb50fd438fbb847c57397b142bb538872b;p=sonarqube.git SONAR-10690 make fake-billing-plugin a core extension --- diff --git a/settings.gradle b/settings.gradle index 10d2f730941..c94cf11a2d1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -40,7 +40,7 @@ include 'tests:plugins:batch-plugin' include 'tests:plugins:blue-green-plugin-v1' include 'tests:plugins:blue-green-plugin-v2' include 'tests:plugins:extension-lifecycle-plugin' -include 'tests:plugins:fake-billing-plugin' +include 'tests:plugins:core-extension-fake-billing' include 'tests:plugins:core-extension-it-tests' include 'tests:plugins:foo-plugin-v1' include 'tests:plugins:foo-plugin-v2' diff --git a/tests/build.gradle b/tests/build.gradle index da55a4f03cf..fc5da403ddb 100644 --- a/tests/build.gradle +++ b/tests/build.gradle @@ -19,7 +19,7 @@ def pluginsForITs = [ ':tests:plugins:blue-green-plugin-v1', ':tests:plugins:blue-green-plugin-v2', ':tests:plugins:extension-lifecycle-plugin', - ':tests:plugins:fake-billing-plugin', + ':tests:plugins:core-extension-fake-billing', ':tests:plugins:core-extension-it-tests', ':tests:plugins:foo-plugin-v1', ':tests:plugins:foo-plugin-v2', diff --git a/tests/plugins/core-extension-fake-billing/build.gradle b/tests/plugins/core-extension-fake-billing/build.gradle new file mode 100644 index 00000000000..3dc8a9030bb --- /dev/null +++ b/tests/plugins/core-extension-fake-billing/build.gradle @@ -0,0 +1,4 @@ +dependencies { + compileOnly project(path: ':sonar-plugin-api', configuration: 'shadow') + compileOnly project(':server:sonar-server') +} diff --git a/tests/plugins/core-extension-fake-billing/src/main/java/FakeBillingCoreExtension.java b/tests/plugins/core-extension-fake-billing/src/main/java/FakeBillingCoreExtension.java new file mode 100644 index 00000000000..6ef26f790f3 --- /dev/null +++ b/tests/plugins/core-extension-fake-billing/src/main/java/FakeBillingCoreExtension.java @@ -0,0 +1,62 @@ +/* + * 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. + */ + +/* + * SonarQube + * Copyright (C) 2009-2017 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. + */ + +import org.sonar.api.SonarQubeSide; +import org.sonar.core.extension.CoreExtension; + +import static org.sonar.api.SonarQubeSide.COMPUTE_ENGINE; +import static org.sonar.api.SonarQubeSide.SERVER; + +public class FakeBillingCoreExtension implements CoreExtension { + @Override + public String getName() { + return "fake-billing"; + } + + @Override + public void load(Context context) { + SonarQubeSide sonarQubeSide = context.getRuntime().getSonarQubeSide(); + // Nothing should be loaded when the plugin is running within by the scanner + if (sonarQubeSide == SERVER || sonarQubeSide == COMPUTE_ENGINE) { + context.addExtension(FakeBillingValidations.class); + } + } + +} diff --git a/tests/plugins/core-extension-fake-billing/src/main/java/FakeBillingValidations.java b/tests/plugins/core-extension-fake-billing/src/main/java/FakeBillingValidations.java new file mode 100644 index 00000000000..16c178c4233 --- /dev/null +++ b/tests/plugins/core-extension-fake-billing/src/main/java/FakeBillingValidations.java @@ -0,0 +1,85 @@ +/* + * 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. + */ + +/* + * SonarQube + * Copyright (C) 2009-2017 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. + */ + +import org.sonar.api.config.Settings; +import org.sonar.server.organization.BillingValidationsExtension; + +import static java.lang.String.format; + +public class FakeBillingValidations implements BillingValidationsExtension { + + private static final String PREVENT_PROJECT_ANALYSIS_SETTING = "sonar.billing.preventProjectAnalysis"; + private static final String PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING = "sonar.billing.preventUpdatingProjectsVisibilityToPrivate"; + + private final Settings settings; + + public FakeBillingValidations(Settings settings) { + this.settings = settings; + } + + @Override + public void checkOnProjectAnalysis(Organization organization) { + boolean preventProjectAnalysis = settings.getBoolean(PREVENT_PROJECT_ANALYSIS_SETTING); + if (preventProjectAnalysis) { + throw new BillingValidationsException(format("Organization %s cannot perform analysis", organization.getKey())); + } + } + + @Override + public void checkCanUpdateProjectVisibility(Organization organization, boolean updateToPrivate) { + boolean preventUpdatingProjectsToPrivate = settings.getBoolean(PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING); + if (preventUpdatingProjectsToPrivate) { + throw new BillingValidationsException(format("Organization %s cannot use private project", organization.getKey())); + } + } + + @Override + public boolean canUpdateProjectVisibilityToPrivate(Organization organization) { + if (!settings.hasKey(PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING)) { + return true; + } + return !settings.getBoolean(PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING); + } + + @Override + public void onDelete(Organization organization) { + // do nothing + } +} diff --git a/tests/plugins/core-extension-fake-billing/src/main/resources/META-INF/services/org.sonar.core.extension.CoreExtension b/tests/plugins/core-extension-fake-billing/src/main/resources/META-INF/services/org.sonar.core.extension.CoreExtension new file mode 100644 index 00000000000..f8adf894e60 --- /dev/null +++ b/tests/plugins/core-extension-fake-billing/src/main/resources/META-INF/services/org.sonar.core.extension.CoreExtension @@ -0,0 +1 @@ +FakeBillingCoreExtension diff --git a/tests/plugins/core-extension-fake-billing/src/main/resources/org/sonar/l10n/billing.properties b/tests/plugins/core-extension-fake-billing/src/main/resources/org/sonar/l10n/billing.properties new file mode 100644 index 00000000000..f8ac8fcaef5 --- /dev/null +++ b/tests/plugins/core-extension-fake-billing/src/main/resources/org/sonar/l10n/billing.properties @@ -0,0 +1,2 @@ +billing.upgrade_box.header=The fake billing plugin is installed +billing.upgrade_box.text=It shows how to change the wording and hide the "Upgrade" button. \ No newline at end of file diff --git a/tests/plugins/fake-billing-plugin/build.gradle b/tests/plugins/fake-billing-plugin/build.gradle deleted file mode 100644 index 47f55fe4d63..00000000000 --- a/tests/plugins/fake-billing-plugin/build.gradle +++ /dev/null @@ -1,19 +0,0 @@ -dependencies { - compileOnly project(path: ':sonar-plugin-api', configuration: 'shadow') - compileOnly project(':server:sonar-server') -} - -jar { - manifest { - attributes( - 'Plugin-Key': 'billing', - 'Plugin-Version': version, - 'Plugin-Class': 'FakeBillingPlugin', - 'Plugin-ChildFirstClassLoader': 'false', - 'Sonar-Version': version, - 'SonarLint-Supported': 'false', - 'Plugin-Name': 'Plugins :: Fake Billing Plugin', - 'Plugin-License': 'GNU LGPL 3' - ) - } -} diff --git a/tests/plugins/fake-billing-plugin/src/main/java/FakeBillingPlugin.java b/tests/plugins/fake-billing-plugin/src/main/java/FakeBillingPlugin.java deleted file mode 100644 index 97a4e8053d6..00000000000 --- a/tests/plugins/fake-billing-plugin/src/main/java/FakeBillingPlugin.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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. - */ - -/* - * SonarQube - * Copyright (C) 2009-2017 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. - */ - -import org.sonar.api.Plugin; - -public class FakeBillingPlugin implements Plugin { - - @Override - public void define(Context context) { - // Nothing should be loaded when the plugin is running within by the scanner - if (isRunningInSQ()) { - context.addExtension(FakeBillingValidations.class); - } - } - - private static boolean isRunningInSQ() { - try { - Class.forName("org.sonar.server.plugins.privileged.CoreExtensionBridge"); - return true; - } catch (ClassNotFoundException e) { - return false; - } - } -} diff --git a/tests/plugins/fake-billing-plugin/src/main/java/FakeBillingValidations.java b/tests/plugins/fake-billing-plugin/src/main/java/FakeBillingValidations.java deleted file mode 100644 index 16c178c4233..00000000000 --- a/tests/plugins/fake-billing-plugin/src/main/java/FakeBillingValidations.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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. - */ - -/* - * SonarQube - * Copyright (C) 2009-2017 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. - */ - -import org.sonar.api.config.Settings; -import org.sonar.server.organization.BillingValidationsExtension; - -import static java.lang.String.format; - -public class FakeBillingValidations implements BillingValidationsExtension { - - private static final String PREVENT_PROJECT_ANALYSIS_SETTING = "sonar.billing.preventProjectAnalysis"; - private static final String PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING = "sonar.billing.preventUpdatingProjectsVisibilityToPrivate"; - - private final Settings settings; - - public FakeBillingValidations(Settings settings) { - this.settings = settings; - } - - @Override - public void checkOnProjectAnalysis(Organization organization) { - boolean preventProjectAnalysis = settings.getBoolean(PREVENT_PROJECT_ANALYSIS_SETTING); - if (preventProjectAnalysis) { - throw new BillingValidationsException(format("Organization %s cannot perform analysis", organization.getKey())); - } - } - - @Override - public void checkCanUpdateProjectVisibility(Organization organization, boolean updateToPrivate) { - boolean preventUpdatingProjectsToPrivate = settings.getBoolean(PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING); - if (preventUpdatingProjectsToPrivate) { - throw new BillingValidationsException(format("Organization %s cannot use private project", organization.getKey())); - } - } - - @Override - public boolean canUpdateProjectVisibilityToPrivate(Organization organization) { - if (!settings.hasKey(PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING)) { - return true; - } - return !settings.getBoolean(PREVENT_UPDATING_PROJECTS_VISIBILITY_TO_PRIVATE_SETTING); - } - - @Override - public void onDelete(Organization organization) { - // do nothing - } -} diff --git a/tests/plugins/fake-billing-plugin/src/main/resources/org/sonar/l10n/billing.properties b/tests/plugins/fake-billing-plugin/src/main/resources/org/sonar/l10n/billing.properties deleted file mode 100644 index f8ac8fcaef5..00000000000 --- a/tests/plugins/fake-billing-plugin/src/main/resources/org/sonar/l10n/billing.properties +++ /dev/null @@ -1,2 +0,0 @@ -billing.upgrade_box.header=The fake billing plugin is installed -billing.upgrade_box.text=It shows how to change the wording and hide the "Upgrade" button. \ No newline at end of file diff --git a/tests/src/test/java/org/sonarqube/tests/organization/BillingTest.java b/tests/src/test/java/org/sonarqube/tests/organization/BillingTest.java index 713762a7e2d..a66254b5ebd 100644 --- a/tests/src/test/java/org/sonarqube/tests/organization/BillingTest.java +++ b/tests/src/test/java/org/sonarqube/tests/organization/BillingTest.java @@ -25,7 +25,6 @@ import com.sonar.orchestrator.build.SonarScanner; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.sonarqube.qa.util.Tester; @@ -46,7 +45,6 @@ import static util.ItUtils.expectHttpError; import static util.ItUtils.newProjectKey; import static util.ItUtils.projectDir; -@Ignore("FIXME IT disabled because it relies on a privileged plugin (fake-billing-plugin)") public class BillingTest { private static final String PROPERTY_PREVENT_ANALYSIS = "sonar.billing.preventProjectAnalysis"; diff --git a/tests/src/test/java/org/sonarqube/tests/organization/OrganizationSuite.java b/tests/src/test/java/org/sonarqube/tests/organization/OrganizationSuite.java index a2b6c311e43..0d2d51231e2 100644 --- a/tests/src/test/java/org/sonarqube/tests/organization/OrganizationSuite.java +++ b/tests/src/test/java/org/sonarqube/tests/organization/OrganizationSuite.java @@ -24,6 +24,7 @@ import org.junit.ClassRule; import org.junit.runner.RunWith; import org.junit.runners.Suite; +import static util.ItUtils.installCoreExtension; import static util.ItUtils.newOrchestratorBuilder; import static util.ItUtils.pluginArtifact; import static util.ItUtils.xooPlugin; @@ -43,12 +44,8 @@ public class OrganizationSuite { @ClassRule public static final Orchestrator ORCHESTRATOR = newOrchestratorBuilder( builder -> builder - .addPlugin(xooPlugin()) - .addPlugin(pluginArtifact("fake-billing-plugin")) .addPlugin(pluginArtifact("ui-extensions-plugin")) - - .setServerProperty("sonar.sonarcloud.enabled", "true") - - ); + .setServerProperty("sonar.sonarcloud.enabled", "true"), + server -> installCoreExtension(server, "core-extension-fake-billing")); }