From 00a61114b3f0a16fab5b932cef6733542d5e9239 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 23 Aug 2017 15:00:55 +0200 Subject: [PATCH] SONAR-9616 Add BranchFeature to detect if branch is supported or not --- .../sonar/server/branch/BranchFeature.java | 27 ++++++++++ .../server/branch/BranchFeatureExtension.java | 31 ++++++++++++ .../server/branch/BranchFeatureProxy.java | 30 ++++++++++++ .../server/branch/BranchFeatureProxyImpl.java | 39 +++++++++++++++ .../org/sonar/server/branch/package-info.java | 23 +++++++++ .../platformlevel/PlatformLevel4.java | 4 ++ .../branch/BranchFeatureProxyImplTest.java | 49 +++++++++++++++++++ .../server/branch/BranchFeatureRule.java | 47 ++++++++++++++++++ 8 files changed, 250 insertions(+) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeature.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureExtension.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxy.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/branch/package-info.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureRule.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeature.java b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeature.java new file mode 100644 index 00000000000..2f5ceb756c0 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeature.java @@ -0,0 +1,27 @@ +/* + * 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. + */ + +package org.sonar.server.branch; + +interface BranchFeature { + + boolean isEnabled(); + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureExtension.java b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureExtension.java new file mode 100644 index 00000000000..60340cb47b7 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureExtension.java @@ -0,0 +1,31 @@ +/* + * 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. + */ + +package org.sonar.server.branch; + +import org.sonar.api.server.ServerSide; + +/** + * The branch plugin needs to implement this in order to know that the branch feature is supported + */ +@ServerSide +public interface BranchFeatureExtension extends BranchFeature { + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxy.java b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxy.java new file mode 100644 index 00000000000..6fa3d926b3c --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxy.java @@ -0,0 +1,30 @@ +/* + * 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. + */ + +package org.sonar.server.branch; + +/** + * The goal of this class is to handle the 2 different use case : + * - The branch plugin exists, the proxy will redirect method calls to the plugin + * - No branch plugin, feature is disabled + */ +public interface BranchFeatureProxy extends BranchFeature { + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java new file mode 100644 index 00000000000..6c1171110fa --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/branch/BranchFeatureProxyImpl.java @@ -0,0 +1,39 @@ +/* + * 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. + */ + +package org.sonar.server.branch; + +public class BranchFeatureProxyImpl implements BranchFeatureProxy { + + private final BranchFeatureExtension branchFeatureExtension; + + public BranchFeatureProxyImpl() { + this.branchFeatureExtension = null; + } + + public BranchFeatureProxyImpl(BranchFeatureExtension branchFeatureExtension) { + this.branchFeatureExtension = branchFeatureExtension; + } + + @Override + public boolean isEnabled() { + return branchFeatureExtension != null && branchFeatureExtension.isEnabled(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/branch/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/branch/package-info.java new file mode 100644 index 00000000000..89444c23cdc --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/branch/package-info.java @@ -0,0 +1,23 @@ +/* + * 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.branch; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 05a52047308..6786bec8638 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -35,6 +35,7 @@ import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.timemachine.Periods; import org.sonar.server.authentication.AuthenticationModule; import org.sonar.server.batch.BatchWsModule; +import org.sonar.server.branch.BranchFeatureProxyImpl; import org.sonar.server.ce.ws.CeWsModule; import org.sonar.server.component.ComponentCleanerService; import org.sonar.server.component.ComponentFinder; @@ -509,6 +510,9 @@ public class PlatformLevel4 extends PlatformLevel { CancelAllAction.class, PluginsWs.class, + // Branch + BranchFeatureProxyImpl.class, + // privileged plugins PrivilegedPluginsBootstraper.class, PrivilegedPluginsStopper.class, diff --git a/server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java new file mode 100644 index 00000000000..a72d399ed20 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureProxyImplTest.java @@ -0,0 +1,49 @@ +/* + * 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. + */ + +package org.sonar.server.branch; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class BranchFeatureProxyImplTest { + + private BranchFeatureExtension branchFeatureExtension = mock(BranchFeatureExtension.class); + + @Test + public void return_false_when_no_extension() { + assertThat(new BranchFeatureProxyImpl().isEnabled()).isFalse(); + } + + @Test + public void return_false_when_extension_returns_false() { + when(branchFeatureExtension.isEnabled()).thenReturn(false); + assertThat(new BranchFeatureProxyImpl(branchFeatureExtension).isEnabled()).isFalse(); + } + + @Test + public void return_true_when_extension_returns_ftrue() { + when(branchFeatureExtension.isEnabled()).thenReturn(true); + assertThat(new BranchFeatureProxyImpl(branchFeatureExtension).isEnabled()).isTrue(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureRule.java b/server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureRule.java new file mode 100644 index 00000000000..f0dd8c89b86 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/branch/BranchFeatureRule.java @@ -0,0 +1,47 @@ +/* + * 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. + */ + +package org.sonar.server.branch; + +import org.junit.rules.ExternalResource; + +public class BranchFeatureRule extends ExternalResource implements BranchFeatureProxy { + + private boolean enabled; + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override + protected void after() { + reset(); + } + + public void reset() { + this.enabled = false; + } + + @Override + public boolean isEnabled() { + return enabled; + } + +} -- 2.39.5