]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7318 Fail fast when more than one Language is declared with a specific key
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 24 Oct 2019 15:56:49 +0000 (10:56 -0500)
committerSonarTech <sonartech@sonarsource.com>
Fri, 25 Oct 2019 18:21:06 +0000 (20:21 +0200)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/language/LanguageValidationTest.java [new file with mode: 0644]
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java

diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/language/LanguageValidation.java
new file mode 100644 (file)
index 0000000..2d1ad97
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.language;
+
+import java.util.Arrays;
+import java.util.stream.Collectors;
+import org.picocontainer.Startable;
+import org.sonar.api.resources.Language;
+import org.sonar.server.plugins.ServerPluginRepository;
+
+public class LanguageValidation implements Startable {
+
+  private final ServerPluginRepository pluginRepository;
+  private final Language[] languages;
+
+  public LanguageValidation(ServerPluginRepository pluginRepository) {
+    this.pluginRepository = pluginRepository;
+    this.languages = new Language[0];
+  }
+
+  public LanguageValidation(ServerPluginRepository pluginRepository, Language... languages) {
+    this.pluginRepository = pluginRepository;
+    this.languages = languages;
+  }
+
+  public void start() {
+    Arrays.stream(languages).collect(Collectors.toMap(Language::getKey, x -> x, (x, y) -> {
+      String pluginX = pluginRepository.getPluginKey(x);
+      String pluginY = pluginRepository.getPluginKey(y);
+
+      throw new IllegalStateException(String.format("There are two languages declared with the same key '%s' declared "
+          + "by the plugins '%s' and '%s'. Please uninstall one of the conflicting plugins.",
+        x.getKey(), pluginX, pluginY));
+    }));
+  }
+
+  @Override public void stop() {
+    // nothing to do
+  }
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/language/LanguageValidationTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/language/LanguageValidationTest.java
new file mode 100644 (file)
index 0000000..654a341
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.language;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Language;
+import org.sonar.server.plugins.ServerPluginRepository;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class LanguageValidationTest {
+  @Rule
+  public ExpectedException exception = ExpectedException.none();
+
+  @Test
+  public void fail_if_conflicting_languages() {
+    Language lang1 = mock(Language.class);
+    Language lang2 = mock(Language.class);
+    when(lang1.getKey()).thenReturn("key");
+    when(lang2.getKey()).thenReturn("key");
+
+    ServerPluginRepository repo = mock(ServerPluginRepository.class);
+    when(repo.getPluginKey(lang1)).thenReturn("plugin1");
+    when(repo.getPluginKey(lang2)).thenReturn("plugin2");
+
+    exception.expect(IllegalStateException.class);
+    exception.expectMessage("There are two languages declared with the same key 'key' declared by the plugins 'plugin1' and 'plugin2'. "
+      + "Please uninstall one of the conflicting plugins.");
+    LanguageValidation languageValidation = new LanguageValidation(repo, lang1, lang2);
+    languageValidation.start();
+  }
+
+  @Test
+  public void succeed_if_no_language() {
+    ServerPluginRepository repo = mock(ServerPluginRepository.class);
+
+    LanguageValidation languageValidation = new LanguageValidation(repo);
+    languageValidation.start();
+    languageValidation.stop();
+  }
+
+  @Test
+  public void succeed_if_no_duplicated_language() {
+    Language lang1 = mock(Language.class);
+    Language lang2 = mock(Language.class);
+    when(lang1.getKey()).thenReturn("key1");
+    when(lang2.getKey()).thenReturn("key2");
+
+    ServerPluginRepository repo = mock(ServerPluginRepository.class);
+    when(repo.getPluginKey(lang1)).thenReturn("plugin1");
+    when(repo.getPluginKey(lang2)).thenReturn("plugin2");
+
+    LanguageValidation languageValidation = new LanguageValidation(repo);
+    languageValidation.start();
+    languageValidation.stop();
+  }
+}
index d62d0321a2cc3a79f30f6e94c28301e1a558136f..23601bc33b6c18a9026f45291324ae2b03746286 100644 (file)
@@ -93,6 +93,7 @@ import org.sonar.server.issue.notification.MyNewIssuesNotificationHandler;
 import org.sonar.server.issue.notification.NewIssuesEmailTemplate;
 import org.sonar.server.issue.notification.NewIssuesNotificationHandler;
 import org.sonar.server.issue.ws.IssueWsModule;
+import org.sonar.server.language.LanguageValidation;
 import org.sonar.server.language.ws.LanguageWs;
 import org.sonar.server.log.ServerLogging;
 import org.sonar.server.measure.custom.ws.CustomMeasuresWsModule;
@@ -325,6 +326,7 @@ public class PlatformLevel4 extends PlatformLevel {
       // languages
       Languages.class,
       LanguageWs.class,
+      LanguageValidation.class,
       org.sonar.server.language.ws.ListAction.class,
 
       // measure