diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-03 17:37:34 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-02-03 17:37:34 +0100 |
commit | 31af09b42f8aafb2a56522e8670b574603cd9eee (patch) | |
tree | 8ad623c3a6a658b4cb45a755367ad24adf995747 /sonar-server | |
parent | 68e772f2ce0b97b4c2cdb7f5d16c4bb6202b02d6 (diff) | |
download | sonarqube-31af09b42f8aafb2a56522e8670b574603cd9eee.tar.gz sonarqube-31af09b42f8aafb2a56522e8670b574603cd9eee.zip |
SONAR-2094 Do not delete ACTIVE_RULES rows when rules are disabled
Diffstat (limited to 'sonar-server')
15 files changed, 208 insertions, 40 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java index 96f55348b17..02564ab0475 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java @@ -163,7 +163,7 @@ public class ProfilesManager extends BaseDao { activeRule = (ActiveRule) parentActiveRule.clone(); activeRule.setRulesProfile(profile); activeRule.setInheritance(ActiveRule.INHERITED); - profile.getActiveRules().add(activeRule); + profile.addActiveRule(activeRule); getSession().saveWithoutFlush(activeRule); for (RulesProfile child : getChildren(profile)) { @@ -188,7 +188,7 @@ public class ProfilesManager extends BaseDao { activeRule = (ActiveRule) parentActiveRule.clone(); activeRule.setRulesProfile(profile); activeRule.setInheritance(ActiveRule.INHERITED); - profile.getActiveRules().add(activeRule); + profile.addActiveRule(activeRule); getSession().saveWithoutFlush(activeRule); for (RulesProfile child : getChildren(profile)) { @@ -227,7 +227,7 @@ public class ProfilesManager extends BaseDao { } private void removeActiveRule(RulesProfile profile, ActiveRule activeRule) { - profile.getActiveRules().remove(activeRule); + profile.removeActiveRule(activeRule); getSession().removeWithoutFlush(activeRule); } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 3850ae6d799..1d0ab614b67 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -163,7 +163,6 @@ public final class Platform { servicesContainer.as(Characteristics.CACHE).addComponent(Views.class); servicesContainer.as(Characteristics.CACHE).addComponent(CodeColorizers.class); servicesContainer.as(Characteristics.NO_CACHE).addComponent(RulesDao.class); - servicesContainer.as(Characteristics.NO_CACHE).addComponent(org.sonar.api.database.daos.RulesDao.class); servicesContainer.as(Characteristics.NO_CACHE).addComponent(MeasuresDao.class); servicesContainer.as(Characteristics.NO_CACHE).addComponent(org.sonar.api.database.daos.MeasuresDao.class); servicesContainer.as(Characteristics.NO_CACHE).addComponent(ProfilesDao.class); @@ -200,6 +199,7 @@ public final class Platform { startupContainer.as(Characteristics.CACHE).addComponent(RegisterMetrics.class); startupContainer.as(Characteristics.CACHE).addComponent(RegisterRules.class); startupContainer.as(Characteristics.CACHE).addComponent(RegisterProvidedProfiles.class); + startupContainer.as(Characteristics.CACHE).addComponent(EnableProfiles.class); startupContainer.as(Characteristics.CACHE).addComponent(ActivateDefaultProfiles.class); startupContainer.as(Characteristics.CACHE).addComponent(JdbcDriverDeployer.class); startupContainer.as(Characteristics.CACHE).addComponent(ServerMetadataPersister.class); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java index 38b87772f90..b608d35cbf0 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java @@ -62,7 +62,7 @@ public final class ActivateDefaultProfiles { Iterator<RulesProfile> iterator = profiles.iterator(); while (iterator.hasNext() && !oneProfileIsActivated) { RulesProfile profile = iterator.next(); - oneProfileIsActivated |= profile.getDefaultProfile(); + oneProfileIsActivated = profile.getDefaultProfile(); if (RulesProfile.SONAR_WAY_NAME.equals(profile.getName())) { profileToActivate = profile; } diff --git a/sonar-server/src/main/java/org/sonar/server/startup/EnableProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/EnableProfiles.java new file mode 100644 index 00000000000..9dc2fcccf81 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/startup/EnableProfiles.java @@ -0,0 +1,79 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.startup; + +import com.google.common.collect.Sets; +import org.sonar.api.database.DatabaseSession; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Language; +import org.sonar.api.utils.TimeProfiler; +import org.sonar.jpa.session.DatabaseSessionFactory; + +import javax.persistence.Query; +import java.util.Set; + +/** + * @since 2.6 + */ +public final class EnableProfiles { + + private Language[] languages; + private DatabaseSessionFactory sessionFactory; + + // NOSONAR the parameter registerProfilesBefore is used to define the execution order of startup components + public EnableProfiles(Language[] languages, DatabaseSessionFactory sessionFactory, RegisterProvidedProfiles registerProfilesBefore) { + this.languages = languages; + this.sessionFactory = sessionFactory; + } + + public void start() { + TimeProfiler profiler = new TimeProfiler().start("Enable profiles"); + DatabaseSession session = sessionFactory.getSession(); + Set<String> languages = getLanguageKeys(); + + enableProfilesOnKnownLanguages(languages, session); + disableProfilesOnMissingLanguages(languages, session); + + session.commit(); + profiler.stop(); + } + + private void enableProfilesOnKnownLanguages(Set<String> languages, DatabaseSession session) { + Query query = session.createQuery("update " + RulesProfile.class.getSimpleName() + " set enabled=:enabled where language in (:languages)"); + query.setParameter("enabled", true); + query.setParameter("languages", languages); + query.executeUpdate(); + } + + private void disableProfilesOnMissingLanguages(Set<String> languages, DatabaseSession session) { + Query query = session.createQuery("update " + RulesProfile.class.getSimpleName() + " set enabled=:enabled where language not in (:languages)"); + query.setParameter("enabled", false); + query.setParameter("languages", languages); + query.executeUpdate(); + } + + private Set<String> getLanguageKeys() { + Set<String> keys = Sets.newLinkedHashSet(); + for (Language language : languages) { + keys.add(language.getKey()); + } + return keys; + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java index 6f01506b6bc..2e2d31defed 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java @@ -19,6 +19,8 @@ */ package org.sonar.server.startup; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.database.DatabaseSession; import org.sonar.api.rules.*; @@ -61,15 +63,11 @@ public final class RegisterRules { disableDeprecatedUserRules(session); profiler.stop(); - profiler.start("Disable deprecated active rules"); - deleteDisabledActiveRules(session); - profiler.stop(); - session.commit(); } private void disableDeprecatedUserRules(DatabaseSession session) { - List<Integer> deprecatedUserRuleIds = new ArrayList<Integer>(); + List<Integer> deprecatedUserRuleIds = Lists.newLinkedList(); deprecatedUserRuleIds.addAll(session.createQuery( "SELECT r.id FROM " + Rule.class.getSimpleName() + " r WHERE r.parent IS NOT NULL AND NOT EXISTS(FROM " + Rule.class.getSimpleName() + " p WHERE r.parent=p)").getResultList()); @@ -91,7 +89,7 @@ public final class RegisterRules { } private void registerRepository(RuleRepository repository, DatabaseSession session) { - Map<String, Rule> rulesByKey = new HashMap<String, Rule>(); + Map<String, Rule> rulesByKey = Maps.newHashMap(); for (Rule rule : repository.createRules()) { rule.setRepositoryKey(repository.getKey()); rulesByKey.put(rule.getKey(), rule); @@ -110,16 +108,6 @@ public final class RegisterRules { saveNewRules(rulesByKey.values(), session); } - private void deleteDisabledActiveRules(DatabaseSession session) { - List<ActiveRule> deprecatedActiveRules = session - .createQuery("from " + ActiveRule.class.getSimpleName() + " where rule.enabled=:enabled") - .setParameter("enabled", false) - .getResultList(); - for (ActiveRule deprecatedActiveRule : deprecatedActiveRules) { - session.removeWithoutFlush(deprecatedActiveRule); - } - } - private void updateRule(Rule persistedRule, Rule rule, DatabaseSession session) { persistedRule.setName(rule.getName()); persistedRule.setConfigKey(rule.getConfigKey()); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb index bfd0fbcb3c1..ad7044b24a8 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb @@ -23,7 +23,7 @@ module ProfilesHelper end def label_for_rules_count(profile) - label="#{profile.active_rules.count} rules" + label="#{profile.count_active_rules} rules" count_overriding=profile.count_overriding_rules if count_overriding>0 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/active_rule_parameter.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/active_rule_parameter.rb index 85904b4ace9..6b47d8fd407 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/active_rule_parameter.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/active_rule_parameter.rb @@ -18,23 +18,23 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
#
class ActiveRuleParameter < ActiveRecord::Base
- belongs_to :active_rule
- belongs_to :rules_parameter
+ belongs_to :active_rule
+ belongs_to :rules_parameter
- def name
- rules_parameter.name
- end
+ def name
+ rules_parameter.name
+ end
- def parameter
- rules_parameter
- end
+ def parameter
+ rules_parameter
+ end
- def validate_on_update
- rules_parameter.validate_value(value, errors, "value" )
- end
-
- def copy
- ActiveRuleParameter.new(:rules_parameter => rules_parameter, :value => value)
- end
+ def validate_on_update
+ rules_parameter.validate_value(value, errors, "value" )
+ end
+
+ def copy
+ ActiveRuleParameter.new(:rules_parameter => rules_parameter, :value => value)
+ end
end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb index 493a56da9e0..52e986162b4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb @@ -126,6 +126,10 @@ class Profile < ActiveRecord::Base end end + def count_active_rules + active_rules.select{|ar| ar.rule.enabled}.size + end + def ancestors @ancestors ||= begin diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb index 0c75209fe57..48cb7947eb3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb @@ -86,7 +86,7 @@ <td><a href="<%= url_for :controller => 'rules_configuration', :action => 'index', :id => profile.id -%>" id="rules-<%= language.getKey() -%>-<%= u(profile.name) -%>"><%= h profile.name %></a></td> <td align="right"> - <span id="activated_rules_<%= u profile.key -%>"><%= profile.active_rules.count -%></span> + <span id="activated_rules_<%= u profile.key -%>"><%= profile.count_active_rules -%></span> </td> <td align="right"><span id="alerts_<%= u profile.key -%>"><%= profile.alerts.size -%></span></td> diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java index dfec8d7d3a1..2387aba138f 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java @@ -119,8 +119,8 @@ public class ProfilesBackupTest extends AbstractDbUnitTestCase { RulesProfile testProfile = new RulesProfile("testProfile", "lang", false, false); ActiveRule ar = new ActiveRule(null, new Rule("testPlugin", "testKey"), RulePriority.MAJOR); ar.getActiveRuleParams().add(new ActiveRuleParam(null, new RuleParam(null, "paramKey", null, null), "testValue")); - testProfile.getActiveRules().add(ar); - testProfile.getActiveRules().add(new ActiveRule(null, new Rule("testPlugin", "testKey2"), RulePriority.MINOR)); + testProfile.addActiveRule(ar); + testProfile.addActiveRule(new ActiveRule(null, new Rule("testPlugin", "testKey2"), RulePriority.MINOR)); testProfile.getAlerts().add(new Alert(null, new Metric("testKey"), Alert.OPERATOR_EQUALS, "10", "22")); testProfile.getAlerts().add(new Alert(null, new Metric("testKey2"), Alert.OPERATOR_GREATER, "10", "22")); diff --git a/sonar-server/src/test/java/org/sonar/server/startup/EnableProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/startup/EnableProfilesTest.java new file mode 100644 index 00000000000..c7ec1fec395 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/startup/EnableProfilesTest.java @@ -0,0 +1,63 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.startup; + +import org.junit.Test; +import org.sonar.api.resources.AbstractLanguage; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Language; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +public class EnableProfilesTest extends AbstractDbUnitTestCase { + + @Test + public void shouldDisableProfilesWithMissingLanguages() { + setupData("shouldDisableProfilesWithMissingLanguages"); + + Language[] languages = new Language[]{Java.INSTANCE, new Php()}; + EnableProfiles task = new EnableProfiles(languages, getSessionFactory(), null); + task.start(); + + checkTables("shouldDisableProfilesWithMissingLanguages", "rules_profiles"); + } + + @Test + public void shouldEnableProfilesWithKnownLanguages() { + setupData("shouldEnableProfilesWithKnownLanguages"); + + Language[] languages = new Language[]{Java.INSTANCE, new Php()}; + EnableProfiles task = new EnableProfiles(languages, getSessionFactory(), null); + task.start(); + + checkTables("shouldEnableProfilesWithKnownLanguages", "rules_profiles"); + } + + private static class Php extends AbstractLanguage { + + public Php() { + super("php"); + } + + public String[] getFileSuffixes() { + return new String[0]; + } + } +} + diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages-result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages-result.xml new file mode 100644 index 00000000000..62dce741d80 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages-result.xml @@ -0,0 +1,9 @@ +<dataset> + <rules_profiles id="1" provided="true" name="Java 1" default_profile="0" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="2" provided="false" name="Java 2" default_profile="1" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="3" provided="true" name="Php" default_profile="0" language="php" enabled="true" PARENT_NAME="[null]"/> + + <!-- disabled --> + <rules_profiles id="4" provided="true" name="Cobol 1" default_profile="1" language="cobol" enabled="false" PARENT_NAME="[null]"/> + <rules_profiles id="5" provided="false" name="Cobol 2" default_profile="0" language="cobol" enabled="false" PARENT_NAME="[null]"/> +</dataset>
\ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages.xml b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages.xml new file mode 100644 index 00000000000..f2c684b5735 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages.xml @@ -0,0 +1,7 @@ +<dataset> + <rules_profiles id="1" provided="true" name="Java 1" default_profile="0" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="2" provided="false" name="Java 2" default_profile="1" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="3" provided="true" name="Php" default_profile="0" language="php" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="4" provided="true" name="Cobol 1" default_profile="1" language="cobol" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="5" provided="false" name="Cobol 2" default_profile="0" language="cobol" enabled="true" PARENT_NAME="[null]"/> +</dataset>
\ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages-result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages-result.xml new file mode 100644 index 00000000000..d89acc7ae0b --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages-result.xml @@ -0,0 +1,11 @@ +<dataset> + <rules_profiles id="1" provided="true" name="Java 1" default_profile="0" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="2" provided="false" name="Java 2" default_profile="1" language="java" enabled="true" PARENT_NAME="[null]"/> + + <!-- enabled --> + <rules_profiles id="3" provided="true" name="Disabled Php 1" default_profile="0" language="php" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="4" provided="false" name="Disabled Php 2" default_profile="1" language="php" enabled="true" PARENT_NAME="[null]"/> + + <!-- keep disabled --> + <rules_profiles id="5" provided="true" name="Disabled Cobol" default_profile="1" language="cobol" enabled="false" PARENT_NAME="[null]"/> +</dataset>
\ No newline at end of file diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages.xml b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages.xml new file mode 100644 index 00000000000..c32281a33db --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages.xml @@ -0,0 +1,7 @@ +<dataset> + <rules_profiles id="1" provided="true" name="Java 1" default_profile="0" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="2" provided="false" name="Java 2" default_profile="1" language="java" enabled="true" PARENT_NAME="[null]"/> + <rules_profiles id="3" provided="true" name="Disabled Php 1" default_profile="0" language="php" enabled="false" PARENT_NAME="[null]"/> + <rules_profiles id="4" provided="false" name="Disabled Php 2" default_profile="1" language="php" enabled="false" PARENT_NAME="[null]"/> + <rules_profiles id="5" provided="true" name="Disabled Cobol" default_profile="1" language="cobol" enabled="false" PARENT_NAME="[null]"/> +</dataset>
\ No newline at end of file |