From a6ab333f10d5b02b2293d525576a650d7201d03c Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 5 Jun 2014 17:48:12 +0200 Subject: [PATCH] SONAR-5007 rename DefaultProfileCache to BuiltInProfiles --- .../server/platform/ServerComponents.java | 4 +- .../qualityprofile/BuiltInProfiles.java | 53 ++++++++++++++++++ .../qualityprofile/BulkActivationResult.java | 29 ++++++++++ .../server/qualityprofile/QProfileReset.java | 6 +- .../qualityprofile/QProfileService.java | 2 +- .../RegisterQualityProfiles.java | 12 ++-- .../qualityprofile/BuiltInProfilesTest.java | 55 +++++++++++++++++++ 7 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java create mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkActivationResult.java create mode 100644 sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index c1f45620b10..359550f191e 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -175,7 +175,7 @@ import org.sonar.server.qualitygate.ws.QGatesShowAction; import org.sonar.server.qualitygate.ws.QGatesUnsetDefaultAction; import org.sonar.server.qualitygate.ws.QGatesUpdateConditionAction; import org.sonar.server.qualitygate.ws.QGatesWs; -import org.sonar.server.qualityprofile.DefaultProfilesCache; +import org.sonar.server.qualityprofile.BuiltInProfiles; import org.sonar.server.qualityprofile.ProfilesManager; import org.sonar.server.qualityprofile.QProfileBackuper; import org.sonar.server.qualityprofile.QProfileCopier; @@ -427,7 +427,7 @@ class ServerComponents { pico.addSingleton(QProfileProjectOperations.class); pico.addSingleton(QProfileProjectLookup.class); pico.addSingleton(QProfileRepositoryExporter.class); - pico.addSingleton(DefaultProfilesCache.class); + pico.addSingleton(BuiltInProfiles.class); pico.addSingleton(QProfileRecreateBuiltInAction.class); pico.addSingleton(QProfilesWs.class); pico.addSingleton(ProfilesWs.class); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java new file mode 100644 index 00000000000..76c5a00c7fe --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java @@ -0,0 +1,53 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.qualityprofile; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import org.sonar.api.ServerComponent; + +import java.util.Collection; + +/** + * Used to list default profile names. + * + * It should be removed as soon as a new API to define quality profiles is created. Currently loading all definitions + * just to get the profile names is too slow (see {@link org.sonar.api.profiles.ProfileDefinition}). + */ +public class BuiltInProfiles implements ServerComponent { + + // built-in profile names grouped by language + private final Multimap namesByLang; + + public BuiltInProfiles() { + this.namesByLang = ArrayListMultimap.create(); + } + + Collection byLanguage(String language) { + return namesByLang.get(language); + } + + BuiltInProfiles put(String language, String name) { + namesByLang.put(language, name); + return this; + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkActivationResult.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkActivationResult.java new file mode 100644 index 00000000000..a5f933d41a7 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkActivationResult.java @@ -0,0 +1,29 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.qualityprofile; + +import com.google.common.collect.Lists; + +import java.util.List; + +public class BulkActivationResult { + + private final List errors = Lists.newArrayList(); +} diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java index f2b016af6d8..c609da55f2e 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java @@ -50,17 +50,17 @@ public class QProfileReset implements ServerComponent { private final DbClient db; private final RuleActivator activator; - private final DefaultProfilesCache builtInProfiles; + private final BuiltInProfiles builtInProfiles; private final ProfileDefinition[] definitions; - public QProfileReset(DbClient db, RuleActivator activator, DefaultProfilesCache builtInProfiles, ProfileDefinition[] definitions) { + public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, ProfileDefinition[] definitions) { this.db = db; this.activator = activator; this.builtInProfiles = builtInProfiles; this.definitions = definitions; } - public QProfileReset(DbClient db, RuleActivator activator, DefaultProfilesCache builtInProfiles) { + public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles) { this(db, activator, builtInProfiles, new ProfileDefinition[0]); } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java index b30f97d08ed..20b358c7da2 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java @@ -99,7 +99,7 @@ public class QProfileService implements ServerComponent { /** * Deactivate a rule on a Quality profile. Does nothing if the rule is not activated, but - * fails (fast) if the rule or the profile does not exist. + * fails if the rule or the profile does not exist. */ public List deactivate(ActiveRuleKey key) { verifyAdminPermission(); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index 476b6386c38..3f8f6c755ce 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -59,7 +59,7 @@ public class RegisterQualityProfiles implements ServerComponent { private final PersistentSettings settings; private final List definitions; - private final DefaultProfilesCache defaultProfilesCache; + private final BuiltInProfiles builtInProfiles; private final DbClient dbClient; private final RuleActivator ruleActivator; @@ -67,19 +67,19 @@ public class RegisterQualityProfiles implements ServerComponent { * To be kept when no ProfileDefinition are injected */ public RegisterQualityProfiles(PersistentSettings settings, - DefaultProfilesCache defaultProfilesCache, + BuiltInProfiles builtInProfiles, DbClient dbClient, RuleActivator ruleActivator) { - this(settings, defaultProfilesCache, dbClient, ruleActivator, Collections.emptyList()); + this(settings, builtInProfiles, dbClient, ruleActivator, Collections.emptyList()); } public RegisterQualityProfiles(PersistentSettings settings, - DefaultProfilesCache defaultProfilesCache, + BuiltInProfiles builtInProfiles, DbClient dbClient, RuleActivator ruleActivator, List definitions) { this.settings = settings; - this.defaultProfilesCache = defaultProfilesCache; + this.builtInProfiles = builtInProfiles; this.dbClient = dbClient; this.ruleActivator = ruleActivator; this.definitions = definitions; @@ -101,7 +101,7 @@ public class RegisterQualityProfiles implements ServerComponent { if (shouldRegister(profileKey, session)) { register(profileKey, entry.getValue(), session); } - defaultProfilesCache.put(language, profileName); + builtInProfiles.put(language, profileName); } setDefault(language, profileDefs, session); } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java new file mode 100644 index 00000000000..a3d74116fde --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.qualityprofile; + +import org.junit.Before; +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + + +public class BuiltInProfilesTest { + + BuiltInProfiles cache; + + @Before + public void setUp() throws Exception { + cache = new BuiltInProfiles(); + } + + @Test + public void add_profiles() throws Exception { + cache.put("java", "Default"); + cache.put("java", "Sonar Way"); + cache.put("js", "Default"); + + assertThat(cache.byLanguage("java")).containsOnly("Default", "Sonar Way"); + assertThat(cache.byLanguage("js")).containsOnly("Default"); + } + + @Test + public void not_add_same_profile_name() throws Exception { + cache.put("java", "Default"); + cache.put("java", "Default"); + + assertThat(cache.byLanguage("java")).containsOnly("Default"); + } +} -- 2.39.5