]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2094 Do not delete ACTIVE_RULES rows when rules are disabled
authorsimonbrandhof <simon.brandhof@gmail.com>
Thu, 3 Feb 2011 16:37:34 +0000 (17:37 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Thu, 3 Feb 2011 16:37:34 +0000 (17:37 +0100)
25 files changed:
sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
sonar-core/src/main/java/org/sonar/jpa/dao/RulesDao.java
sonar-core/src/test/java/org/sonar/jpa/dao/RulesDaoTest.java
sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldAddActiveRulesToProfile-result.xml [deleted file]
sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldAddActiveRulesToProfile.xml [deleted file]
sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldSynchronizeRuleOfActiveRule.xml [deleted file]
sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java [deleted file]
sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRule.java
sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java
sonar-server/src/main/java/org/sonar/server/startup/EnableProfiles.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
sonar-server/src/main/webapp/WEB-INF/app/helpers/profiles_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/models/active_rule_parameter.rb
sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/index.html.erb
sonar-server/src/test/java/org/sonar/server/configuration/ProfilesBackupTest.java
sonar-server/src/test/java/org/sonar/server/startup/EnableProfilesTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages-result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldDisableProfilesWithMissingLanguages.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages-result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/startup/EnableProfilesTest/shouldEnableProfilesWithKnownLanguages.xml [new file with mode: 0644]
tests/integration/tests/maven-projects/java-complexity/src/main/java/foo/AnonymousClass.java

index a0bb1c58dce2d20a65063b7139b718d1be5f9fc8..9f0e0c4ddc35ed886f439fde0a0e1c50875676c2 100644 (file)
@@ -78,7 +78,6 @@ public class ProjectBatch {
     batchContainer.as(Characteristics.CACHE)
         .addComponent(globalContainer.getComponent(DefaultResourcePersister.class).getSnapshot(project));
 
-    batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.RulesDao.class);
     batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.MeasuresDao.class);
     batchContainer.as(Characteristics.CACHE).addComponent(ProfilesDao.class);
     batchContainer.as(Characteristics.CACHE).addComponent(AsyncMeasuresDao.class);
index 8cb5151c7f59359d5f58ebd51923115739b03461..b90e8afe852b4b8a9d19874653e39516101c9aad 100644 (file)
@@ -57,60 +57,9 @@ public class RulesDao extends BaseDao {
     return getSession().getSingleResult(Rule.class, "key", ruleKey, "pluginName", repositoryKey, "enabled", true);\r
   }\r
 \r
-  public Long countRules(List<String> plugins) {\r
-    return (Long) getSession().createQuery(\r
-        "SELECT COUNT(r) FROM Rule r WHERE r.pluginName IN (:pluginNames) AND r.enabled=true").\r
-        setParameter("pluginNames", plugins).\r
-        getSingleResult();\r
-  }\r
-\r
-  public List<RuleParam> getRuleParams() {\r
-    return getSession().getResults(RuleParam.class);\r
-  }\r
 \r
   public RuleParam getRuleParam(Rule rule, String paramKey) {\r
     return getSession().getSingleResult(RuleParam.class, "rule", rule, "key", paramKey);\r
   }\r
 \r
-  public void addActiveRulesToProfile(List<ActiveRule> activeRules, int profileId, String pluginKey) {\r
-    RulesProfile rulesProfile = getProfileById(profileId);\r
-    for (ActiveRule activeRule : activeRules) {\r
-      synchronizeRuleOfActiveRule(activeRule, pluginKey);\r
-      activeRule.setRulesProfile(rulesProfile);\r
-      getSession().save(activeRule);\r
-    }\r
-  }\r
-\r
-  public List<RuleFailureModel> getViolations(Snapshot snapshot) {\r
-    return getSession().getResults(RuleFailureModel.class, "snapshotId", snapshot.getId());\r
-  }\r
-\r
-  public void synchronizeRuleOfActiveRule(ActiveRule activeRule, String pluginKey) {\r
-    Rule rule = activeRule.getRule();\r
-    Rule ruleFromDataBase = getRuleByKey(pluginKey, rule.getKey());\r
-    activeRule.setRule(ruleFromDataBase);\r
-    List<RuleParam> ruleParamsFromDataBase = getRuleParams();\r
-    for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {\r
-      boolean found = false;\r
-      Iterator<RuleParam> iterator = ruleParamsFromDataBase.iterator();\r
-      while (iterator.hasNext() && !found) {\r
-        RuleParam ruleParamFromDataBase = iterator.next();\r
-        if (isRuleParamEqual(activeRuleParam.getRuleParam(), ruleParamFromDataBase, rule.getKey(), pluginKey)) {\r
-          activeRuleParam.setRuleParam(ruleParamFromDataBase);\r
-          found = true;\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  public boolean isRuleParamEqual(RuleParam ruleParam, RuleParam ruleParamFromDatabase, String ruleKey, String pluginKey) {\r
-    return ruleParam.getKey().equals(ruleParamFromDatabase.getKey()) &&\r
-        ruleKey.equals(ruleParamFromDatabase.getRule().getKey()) &&\r
-        ruleParamFromDatabase.getRule().getPluginName().equals(pluginKey);\r
-  }\r
-\r
-  public RulesProfile getProfileById(int profileId) {\r
-    return getSession().getEntityManager().getReference(RulesProfile.class, profileId);\r
-  }\r
-\r
 }\r
index 4656c20bb1bf22e1f1a92a0e7e93cbdf4fdac8df..7d1b0e97a64f0154c7ab789a49595605a4256a38 100644 (file)
@@ -65,55 +65,4 @@ public class RulesDaoTest extends AbstractDbUnitTestCase {
     assertThat(rule2, nullValue());\r
   }\r
 \r
-  @Test\r
-  public void shouldGetRuleParams() {\r
-    setupData("shouldGetRuleParams");\r
-\r
-    List<RuleParam> ruleParams = rulesDao.getRuleParams();\r
-\r
-    assertThat(ruleParams.size(), is(3));\r
-  }\r
-\r
-  @Test\r
-  public void shouldSynchronizeRuleOfActiveRule() {\r
-    setupData("shouldSynchronizeRuleOfActiveRule");\r
-    Rule rule = new Rule(null, "other key", (String) null, null, null);\r
-    RuleParam ruleParam = new RuleParam(null, "rule1_param1", null, null);\r
-    rule.setParams(Arrays.asList(ruleParam));\r
-    ActiveRule activeRule = new ActiveRule(null, rule, null);\r
-    ActiveRuleParam activeRuleParam = new ActiveRuleParam(activeRule, ruleParam, null);\r
-    activeRule.setActiveRuleParams(Arrays.asList(activeRuleParam));\r
-\r
-    rulesDao.synchronizeRuleOfActiveRule(activeRule, "plugin");\r
-\r
-    assertThat(activeRule.getRule().getId(), notNullValue());\r
-    assertThat(activeRule.getActiveRuleParams().size(), is(1));\r
-  }\r
-\r
-  @Test\r
-  public void shouldGetRulesProfileById() {\r
-    RulesProfile rulesProfile = new RulesProfile("profil", "java", true, true);\r
-    getSession().save(rulesProfile);\r
-\r
-    RulesProfile rulesProfileExpected = rulesDao.getProfileById(rulesProfile.getId());\r
-\r
-    assertThat(rulesProfileExpected, is(rulesProfile));\r
-  }\r
-\r
-  @Test\r
-  public void shouldAddActiveRulesToProfile() {\r
-    setupData("shouldAddActiveRulesToProfile");\r
-\r
-    Rule rule = new Rule("rule1", "key1", "config1", null, null);\r
-    RuleParam ruleParam = new RuleParam(null, "param1", null, null);\r
-    rule.setParams(Arrays.asList(ruleParam));\r
-\r
-    ActiveRule activeRule = new ActiveRule(null, rule, RulePriority.MAJOR);\r
-    ActiveRuleParam activeRuleParam = new ActiveRuleParam(activeRule, ruleParam, "20");\r
-    activeRule.setActiveRuleParams(Arrays.asList(activeRuleParam));\r
-    rulesDao.addActiveRulesToProfile(Arrays.asList(activeRule), 1, "plugin");\r
-\r
-    checkTables("shouldAddActiveRulesToProfile", "rules", "active_rules", "rules_profiles");\r
-  }\r
-\r
 }\r
diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldAddActiveRulesToProfile-result.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldAddActiveRulesToProfile-result.xml
deleted file mode 100644 (file)
index cecf13b..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-<dataset>
-
-  <rules_profiles id="1" parent_name="[null]" provided="true" name="profile" default_profile="1" language="java" enabled="true"/>
-
-  <rules_categories id="1" name="category one" description="[null]"/>
-
-  <rules id="1" name="rule1" description="desc" plugin_config_key="config1"
-         plugin_rule_key="key1" plugin_name="plugin" priority="1" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
-
-  <rules_parameters id="1" rule_id="1" name="param1" description="foo" param_type="r"/>
-
-  <!-- Active rule created -->
-  <active_rules id="1" profile_id="1" rule_id="1" failure_level="2" inheritance="[null]"/>
-
-  <!-- Active rule param created -->
-  <active_rule_parameters id="1" active_rule_id="1" rules_parameter_id="1" value="20"/>
-
-
-</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldAddActiveRulesToProfile.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldAddActiveRulesToProfile.xml
deleted file mode 100644 (file)
index 0851c74..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<dataset>
-
-  <rules_profiles id="1" provided="true" name="profile" default_profile="1" language="java" enabled="true"/>
-
-  <rules_categories id="1" name="category one" description="[null]"/>
-
-  <rules id="1" name="rule1" description="desc" plugin_config_key="config1"
-         plugin_rule_key="key1" plugin_name="plugin" priority="1" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
-
-  <rules_parameters id="1" rule_id="1" name="param1" description="foo" param_type="r"/>
-
-
-</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldSynchronizeRuleOfActiveRule.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldSynchronizeRuleOfActiveRule.xml
deleted file mode 100644 (file)
index 94227ff..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<dataset>
-
-  <rules_categories id="1" name="category one" description="[null]"/>
-
-  <rules id="1" name="other rule" description="desc" plugin_config_key="other config"
-         plugin_rule_key="other key" plugin_name="plugin" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
-  <rules id="2" name="rule" description="desc" plugin_config_key="config"
-         plugin_rule_key="key" plugin_name="other plugin" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
-
-  <rules_parameters id="1" rule_id="1" name="rule1_param1" description="rule1_desc1" param_type="r" />
-  <rules_parameters id="2" rule_id="2" name="rule2_param1" description="rule2_desc1" param_type="r" />
-
-</dataset>
\ No newline at end of file
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java b/sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java
deleted file mode 100644 (file)
index bc47708..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * 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.api.database.daos;
-
-import org.sonar.api.database.model.RuleFailureModel;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleParam;
-import org.sonar.api.rules.RulesCategory;
-
-import java.util.Collections;
-import java.util.List;
-
-/**
- * @deprecated since 2.3
- */
-@Deprecated
-public class RulesDao {
-
-  private org.sonar.jpa.dao.RulesDao target;
-
-  public RulesDao(org.sonar.jpa.dao.RulesDao target) {
-    this.target = target;
-  }
-
-  public List<Rule> getRules() {
-    return target.getRules();
-  }
-
-  public List<Rule> getRulesByPlugin(String pluginKey) {
-    return target.getRulesByPlugin(pluginKey);
-  }
-
-  public List<Rule> getRulesByCategory(RulesCategory categ) {
-    return Collections.emptyList();
-  }
-
-  public Rule getRuleByKey(String pluginKey, String ruleKey) {
-    return target.getRuleByKey(pluginKey, ruleKey);
-  }
-
-  public Long countRules(List<String> plugins, String categoryName) {
-    return target.countRules(plugins);
-  }
-
-  public List<RulesCategory> getCategories() {
-    return Collections.emptyList();
-  }
-
-  public RulesCategory getCategory(String key) {
-    return null;
-  }
-
-  public List<RuleParam> getRuleParams() {
-    return target.getRuleParams();
-  }
-
-  public RuleParam getRuleParam(Rule rule, String paramKey) {
-    return target.getRuleParam(rule, paramKey);
-  }
-
-  public void addActiveRulesToProfile(List<ActiveRule> activeRules, int profileId, String pluginKey) {
-    target.addActiveRulesToProfile(activeRules, profileId, pluginKey);
-  }
-
-  public List<RuleFailureModel> getViolations(Snapshot snapshot) {
-    return target.getViolations(snapshot);
-  }
-
-  public void synchronizeRuleOfActiveRule(ActiveRule activeRule, String pluginKey) {
-    target.synchronizeRuleOfActiveRule(activeRule, pluginKey);
-  }
-
-  public boolean isRuleParamEqual(RuleParam ruleParam, RuleParam ruleParamFromDatabase, String ruleKey, String pluginKey) {
-    return target.isRuleParamEqual(ruleParam, ruleParamFromDatabase, ruleKey, pluginKey);
-  }
-
-  public RulesProfile getProfileById(int profileId) {
-    return target.getProfileById(profileId);
-  }
-}
index 4a5cfa80b62ded1e15faf0f35a2eccbbbfbd88a3..9427f0c17655efa98c5f45df5e950b1420c95c90 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.api.profiles;
 
+import com.google.common.collect.Lists;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.Transformer;
 import org.apache.commons.lang.StringUtils;
@@ -78,14 +79,14 @@ public class RulesProfile implements Cloneable {
   @Column(name = "parent_name", updatable = true, nullable = true)
   private String parentName;
 
-  @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE })
-  private List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
+  @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
+  private List<ActiveRule> activeRules = Lists.newArrayList();
 
-  @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE })
-  private List<Alert> alerts = new ArrayList<Alert>();
+  @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
+  private List<Alert> alerts = Lists.newArrayList();
 
   @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY)
-  private List<ResourceModel> projects = new ArrayList<ResourceModel>();
+  private List<ResourceModel> projects = Lists.newArrayList();
 
   /**
    * @deprecated use the factory method create()
@@ -101,9 +102,9 @@ public class RulesProfile implements Cloneable {
   public RulesProfile(String name, String language) {
     this.name = name;
     this.language = language;
-    this.activeRules = new ArrayList<ActiveRule>();
-    this.alerts = new ArrayList<Alert>();
-    this.projects = new ArrayList<ResourceModel>();
+    this.activeRules = Lists.newArrayList();
+    this.alerts = Lists.newArrayList();
+    this.projects = Lists.newArrayList();
   }
 
   /**
@@ -139,7 +140,33 @@ public class RulesProfile implements Cloneable {
    * @return the list of active rules
    */
   public List<ActiveRule> getActiveRules() {
-    return activeRules;
+    return getActiveRules(false);
+  }
+
+  /**
+   * @return the list of active rules
+   */
+  public List<ActiveRule> getActiveRules(boolean acceptDisabledRules) {
+    if (acceptDisabledRules) {
+      return activeRules;
+    }
+    List<ActiveRule> result = Lists.newArrayList();
+    for (ActiveRule activeRule : activeRules) {
+      if (activeRule.isEnabled()) {
+        result.add(activeRule);
+      }
+    }
+    return result;
+  }
+
+  public RulesProfile removeActiveRule(ActiveRule activeRule) {
+    activeRules.remove(activeRule);
+    return this;
+  }
+
+  public RulesProfile addActiveRule(ActiveRule activeRule) {
+    activeRules.add(activeRule);
+    return this;
   }
 
   /**
@@ -184,7 +211,7 @@ public class RulesProfile implements Cloneable {
   }
 
   public boolean isEnabled() {
-    return enabled==Boolean.TRUE;
+    return enabled == Boolean.TRUE;
   }
 
   public RulesProfile setEnabled(Boolean b) {
@@ -209,7 +236,7 @@ public class RulesProfile implements Cloneable {
 
   /**
    * For internal use only.
-   * 
+   *
    * @since 2.5
    */
   public String getParentName() {
@@ -218,7 +245,7 @@ public class RulesProfile implements Cloneable {
 
   /**
    * For internal use only.
-   * 
+   *
    * @since 2.5
    */
   public void setParentName(String parentName) {
@@ -254,12 +281,13 @@ public class RulesProfile implements Cloneable {
   }
 
   /**
-   * @return the list of active rules for a given priority
+   * Note: disabled rules are excluded.
+   * @return the list of active rules for a given severity
    */
-  public List<ActiveRule> getActiveRules(RulePriority priority) {
-    List<ActiveRule> result = new ArrayList<ActiveRule>();
-    for (ActiveRule activeRule : getActiveRules()) {
-      if (activeRule.getSeverity().equals(priority)) {
+  public List<ActiveRule> getActiveRules(RulePriority severity) {
+    List<ActiveRule> result = Lists.newArrayList();
+    for (ActiveRule activeRule : activeRules) {
+      if (activeRule.getSeverity().equals(severity) && activeRule.isEnabled()) {
         result.add(activeRule);
       }
     }
@@ -274,10 +302,14 @@ public class RulesProfile implements Cloneable {
     return getActiveRulesByRepository(repositoryKey);
   }
 
+  /**
+   * Get the active rules of a specific repository.
+   * Only enabled rules are selected. Disabled rules are excluded.
+   */
   public List<ActiveRule> getActiveRulesByRepository(String repositoryKey) {
-    List<ActiveRule> result = new ArrayList<ActiveRule>();
-    for (ActiveRule activeRule : getActiveRules()) {
-      if (repositoryKey.equals(activeRule.getRepositoryKey())) {
+    List<ActiveRule> result = Lists.newArrayList();
+    for (ActiveRule activeRule : activeRules) {
+      if (repositoryKey.equals(activeRule.getRepositoryKey()) && activeRule.isEnabled()) {
         result.add(activeRule);
       }
     }
@@ -285,26 +317,34 @@ public class RulesProfile implements Cloneable {
   }
 
   /**
+   * Note: disabled rules are excluded.
    * @return an active rule from a plugin key and a rule key if the rule is activated, null otherwise
    */
   public ActiveRule getActiveRule(String repositoryKey, String ruleKey) {
-    for (ActiveRule activeRule : getActiveRules()) {
-      if (StringUtils.equals(activeRule.getRepositoryKey(), repositoryKey) && StringUtils.equals(activeRule.getRuleKey(), ruleKey)) {
+    for (ActiveRule activeRule : activeRules) {
+      if (StringUtils.equals(activeRule.getRepositoryKey(), repositoryKey) && StringUtils.equals(activeRule.getRuleKey(), ruleKey) && activeRule.isEnabled()) {
         return activeRule;
       }
     }
     return null;
   }
 
+  /**
+   * Note: disabled rules are excluded.
+   */
   public ActiveRule getActiveRuleByConfigKey(String repositoryKey, String configKey) {
-    for (ActiveRule activeRule : getActiveRules()) {
-      if (StringUtils.equals(activeRule.getRepositoryKey(), repositoryKey) && StringUtils.equals(activeRule.getConfigKey(), configKey)) {
+    for (ActiveRule activeRule : activeRules) {
+      if (StringUtils.equals(activeRule.getRepositoryKey(), repositoryKey) && StringUtils.equals(activeRule.getConfigKey(), configKey) && activeRule.isEnabled()) {
         return activeRule;
       }
     }
     return null;
   }
 
+  /**
+   * Note: disabled rules are excluded.
+   */
+
   public ActiveRule getActiveRule(Rule rule) {
     return getActiveRule(rule.getRepositoryKey(), rule.getKey());
   }
@@ -344,8 +384,8 @@ public class RulesProfile implements Cloneable {
     clone.setDefaultProfile(getDefaultProfile());
     clone.setProvided(getProvided());
     clone.setParentName(getParentName());
-    if (CollectionUtils.isNotEmpty(getActiveRules())) {
-      clone.setActiveRules(new ArrayList<ActiveRule>(CollectionUtils.collect(getActiveRules(), new Transformer() {
+    if (CollectionUtils.isNotEmpty(activeRules)) {
+      clone.setActiveRules(new ArrayList<ActiveRule>(CollectionUtils.collect(activeRules, new Transformer() {
         public Object transform(Object input) {
           return ((ActiveRule) input).clone();
         }
index 8fe4e0aea91a20cb5f06229142839daf3b3de2e4..5fe233feb0d9e54df41bc28eb2cc57a85a73d8d1 100644 (file)
@@ -284,4 +284,10 @@ public class ActiveRule implements Cloneable {
     return clone;
   }
 
+  /**
+   * @since 2.6
+   */
+  public boolean isEnabled() {
+    return getRule().isEnabled();
+  }
 }
index 96f55348b1727feb6f67157c6833c9599d64a61c..02564ab0475c2eab80ab8019f970425cc5d5bc43 100644 (file)
@@ -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);
   }
 
index 3850ae6d7991c7a547a378b719c770ee0d24db89..1d0ab614b67fb40d3274c0055a176b608705b873 100644 (file)
@@ -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);
index 38b87772f90a8406c2cb605a23c6b203fd9c227c..b608d35cbf092e421c22d7cb3383026e43965056 100644 (file)
@@ -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 (file)
index 0000000..9dc2fcc
--- /dev/null
@@ -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;
+  }
+}
index 6f01506b6bc9fc0299e058b8ce21b45bb0403ec0..2e2d31defedac91ffe930eeeed0e23554985477e 100644 (file)
@@ -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());
index bfd0fbcb3c166d29a7e4eab55ea9c0351ea45661..ad7044b24a8843b3b76022d443146d32bce90e86 100644 (file)
@@ -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
index 85904b4ace90f195926c447fc9e316545dd0fe77..6b47d8fd407e9a8456b92f05b6be38db3dc5aed0 100644 (file)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02\r
 #\r
  class ActiveRuleParameter < ActiveRecord::Base\r
- belongs_to :active_rule\r
- belongs_to :rules_parameter\r
  belongs_to :active_rule\r
  belongs_to :rules_parameter\r
 \r
- def name\r
-  rules_parameter.name  \r
- end\r
  def name\r
+    rules_parameter.name\r
  end\r
 \r
- def parameter\r
-   rules_parameter\r
- end\r
  def parameter\r
+     rules_parameter\r
  end\r
 \r
- def validate_on_update \r
-   rules_parameter.validate_value(value, errors, "value" )\r
- end\r
\r
- def copy\r
-   ActiveRuleParameter.new(:rules_parameter => rules_parameter, :value => value)\r
- end  \r
+   def validate_on_update\r
+     rules_parameter.validate_value(value, errors, "value" )\r
  end\r
+\r
  def copy\r
+     ActiveRuleParameter.new(:rules_parameter => rules_parameter, :value => value)\r
+   end\r
 \r
  end\r
index 493a56da9e0511dac60e792d73bea7d2c2f0c08f..52e986162b46fdc0cd8f0a80d848b79eca02e0fa 100644 (file)
@@ -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
index 0c75209fe57de14cb45dd6729ba80f478c13bcfa..48cb7947eb3b8f60f7bc4c7c237bd42aa0082c2b 100644 (file)
@@ -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>
index dfec8d7d3a168649ed2ad41f477d3b5268547b4f..2387aba138f3f37966125516d4c4b1218024e7fa 100644 (file)
@@ -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 (file)
index 0000000..c7ec1fe
--- /dev/null
@@ -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 (file)
index 0000000..62dce74
--- /dev/null
@@ -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 (file)
index 0000000..f2c684b
--- /dev/null
@@ -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 (file)
index 0000000..d89acc7
--- /dev/null
@@ -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 (file)
index 0000000..c32281a
--- /dev/null
@@ -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
index a7572a6d5a5811a4251f9bfbd1b2ada6c21e425a..61b755b16c631fd0d9d3b401841dc48ac98e8d7c 100644 (file)
@@ -3,12 +3,13 @@ package foo;
 import java.io.Serializable;
 import java.lang.Runnable;
 
-// class complexity: 4
 public class AnonymousClass {
 
-  // method complexity: 3
-  public void anonymousClassWithComplexity() {
+  // method complexity: 1 or 3 ?
+  public void hasComplexAnonymousClass() {
     Runnable runnable = new Runnable() {
+       
+         // method complexity: 2
       public void run() {
         if (true) {
           System.out.println("true");
@@ -18,7 +19,7 @@ public class AnonymousClass {
   }
 
   // method complexity: 1
-  public void anonymousClassWithZeroComplexity() {
+  public void hasEmptyAnonymousClass() {
     Serializable serializable = new Serializable() {
 
     };