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);
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
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
+++ /dev/null
-<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
+++ /dev/null
-<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
+++ /dev/null
-<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
+++ /dev/null
-/*
- * 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);
- }
-}
*/
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;
@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()
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();
}
/**
* @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;
}
/**
}
public boolean isEnabled() {
- return enabled==Boolean.TRUE;
+ return enabled == Boolean.TRUE;
}
public RulesProfile setEnabled(Boolean b) {
/**
* For internal use only.
- *
+ *
* @since 2.5
*/
public String getParentName() {
/**
* For internal use only.
- *
+ *
* @since 2.5
*/
public void setParentName(String parentName) {
}
/**
- * @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);
}
}
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);
}
}
}
/**
+ * 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());
}
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();
}
return clone;
}
+ /**
+ * @since 2.6
+ */
+ public boolean isEnabled() {
+ return getRule().isEnabled();
+ }
}
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)) {
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)) {
}
private void removeActiveRule(RulesProfile profile, ActiveRule activeRule) {
- profile.getActiveRules().remove(activeRule);
+ profile.removeActiveRule(activeRule);
getSession().removeWithoutFlush(activeRule);
}
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);
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);
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;
}
--- /dev/null
+/*
+ * 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;
+ }
+}
*/
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.*;
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());
}
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);
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());
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
# 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
end
end
+ def count_active_rules
+ active_rules.select{|ar| ar.rule.enabled}.size
+ end
+
def ancestors
@ancestors ||=
begin
<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>
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"));
--- /dev/null
+/*
+ * 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];
+ }
+ }
+}
+
--- /dev/null
+<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
--- /dev/null
+<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
--- /dev/null
+<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
--- /dev/null
+<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
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");
}
// method complexity: 1
- public void anonymousClassWithZeroComplexity() {
+ public void hasEmptyAnonymousClass() {
Serializable serializable = new Serializable() {
};