--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.rule;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.codehaus.plexus.util.StringUtils;
+import org.picocontainer.injectors.ProviderAdapter;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.api.batch.rule.internal.NewActiveRule;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.RuleParam;
+import org.sonar.core.qualityprofile.db.ActiveRuleDao;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * Loads the rules that are activated on the Quality profiles
+ * used by the current module and build {@link org.sonar.api.batch.rule.ActiveRules}.
+ */
+public class ActiveRulesProvider extends ProviderAdapter {
+
+ private ActiveRules singleton = null;
+
+ public ActiveRules provide(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) {
+ if (singleton == null) {
+ singleton = load(qProfiles, dao, ruleFinder);
+ }
+ return singleton;
+ }
+
+ private ActiveRules load(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) {
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
+ for (ModuleQProfiles.QProfile qProfile : qProfiles.findAll()) {
+ ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId = ArrayListMultimap.create();
+ for (ActiveRuleParamDto dto : dao.selectParamsByProfileId(qProfile.id())) {
+ paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto);
+ }
+
+ for (ActiveRuleDto activeDto : dao.selectByProfileId(qProfile.id())) {
+ Rule rule = ruleFinder.findById(activeDto.getRulId());
+ if (rule != null) {
+ NewActiveRule newActiveRule = builder.activate(rule.ruleKey());
+ newActiveRule.setSeverity(activeDto.getSeverityString());
+ if (rule.getParent() != null) {
+ newActiveRule.setInternalKey(rule.getParent().getConfigKey());
+ } else {
+ newActiveRule.setInternalKey(rule.getConfigKey());
+ }
+
+ // load parameter values
+ for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
+ newActiveRule.setParam(paramDto.getKey(), paramDto.getValue());
+ }
+
+ // load default values
+ for (RuleParam param : rule.getParams()) {
+ if (!newActiveRule.params().containsKey(param.getKey())) {
+ newActiveRule.setParam(param.getKey(), param.getDefaultValue());
+ }
+ }
+ }
+ }
+ }
+ return builder.build();
+ }
+
+ @CheckForNull
+ private String defaultParamValue(Rule rule, String paramKey) {
+ RuleParam param = rule.getParam(paramKey);
+ return param != null ? param.getDefaultValue() : null;
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.rule;
-
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ListMultimap;
-import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.api.batch.rule.ModuleRules;
-import org.sonar.api.batch.rule.internal.ModuleRulesBuilder;
-import org.sonar.api.batch.rule.internal.NewModuleRule;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.core.qualityprofile.db.ActiveRuleDao;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-
-/**
- * Loads the rules that are activated on the Quality profiles
- * used by the current module and build {@link org.sonar.api.batch.rule.ModuleRules}.
- */
-public class ModuleRulesProvider extends ProviderAdapter {
-
- private ModuleRules singleton = null;
-
- public ModuleRules provide(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) {
- if (singleton == null) {
- singleton = load(qProfiles, dao, ruleFinder);
- }
- return singleton;
- }
-
- private ModuleRules load(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) {
- ModuleRulesBuilder builder = new ModuleRulesBuilder();
- for (ModuleQProfiles.QProfile qProfile : qProfiles.findAll()) {
- ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId = ArrayListMultimap.create();
- for (ActiveRuleParamDto dto : dao.selectParamsByProfileId(qProfile.id())) {
- paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto);
- }
-
- for (ActiveRuleDto activeDto : dao.selectByProfileId(qProfile.id())) {
- Rule rule = ruleFinder.findById(activeDto.getRulId());
- if (rule != null) {
- NewModuleRule newModuleRule = builder.activate(rule.ruleKey());
- newModuleRule.setSeverity(activeDto.getSeverityString());
- if (rule.getParent() != null) {
- newModuleRule.setEngineKey(rule.getParent().getConfigKey());
- } else {
- newModuleRule.setEngineKey(rule.getConfigKey());
- }
- for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
- newModuleRule.setParam(paramDto.getKey(), paramDto.getValue());
- }
- // TODO add default values declared on rule parameters
- }
- }
- }
- return builder.build();
- }
-}
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.batch.phases.PhaseExecutor;
import org.sonar.batch.phases.PhasesTimeProfiler;
+import org.sonar.batch.rule.ActiveRulesProvider;
import org.sonar.batch.rule.ModuleQProfiles;
-import org.sonar.batch.rule.ModuleRulesProvider;
import org.sonar.batch.rule.QProfileSensor;
import org.sonar.batch.rule.RulesProfileProvider;
import org.sonar.batch.scan.filesystem.*;
// rules
ModuleQProfiles.class,
- new ModuleRulesProvider(),
+ new ActiveRulesProvider(),
new RulesProfileProvider(),
QProfileSensor.class,
CheckFactory.class,
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.rule;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.qualityprofile.db.ActiveRuleDao;
+import org.sonar.core.qualityprofile.db.QualityProfileDao;
+
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ActiveRulesProviderTest extends AbstractDaoTestCase {
+
+ ModuleQProfiles qProfiles = mock(ModuleQProfiles.class);
+ RuleFinder ruleFinder = mock(RuleFinder.class);
+
+ @Before
+ public void init_rules() {
+ Rule squidRule = new Rule().setRepositoryKey("squid").setKey("S0001");
+ squidRule.createParameter("min").setDefaultValue("12");
+ when(ruleFinder.findById(10)).thenReturn(squidRule);
+ when(ruleFinder.findById(100)).thenReturn(new Rule().setRepositoryKey("phpunit").setKey("P1"));
+ }
+
+ @Test
+ public void build_active_rules() throws Exception {
+ setupData("shared");
+ QualityProfileDao profileDao = new QualityProfileDao(getMyBatis());
+ when(qProfiles.findAll()).thenReturn(Arrays.asList(
+ // 1 rule is enabled on java with severity INFO
+ new ModuleQProfiles.QProfile(profileDao.selectById(2)),
+ // 1 rule is enabled on php with severity BLOCKER
+ new ModuleQProfiles.QProfile(profileDao.selectById(3))
+ ));
+
+ ActiveRulesProvider provider = new ActiveRulesProvider();
+ ActiveRuleDao activeRuleDao = new ActiveRuleDao(getMyBatis());
+ ActiveRules activeRules = provider.provide(qProfiles, activeRuleDao, ruleFinder);
+
+ assertThat(activeRules.findAll()).hasSize(2);
+ assertThat(activeRules.findByRepository("squid")).hasSize(1);
+ assertThat(activeRules.findByRepository("phpunit")).hasSize(1);
+ assertThat(activeRules.findByRepository("unknown")).isEmpty();
+ ActiveRule squidRule = activeRules.find(RuleKey.of("squid", "S0001"));
+ assertThat(squidRule.severity()).isEqualTo(Severity.INFO);
+ assertThat(squidRule.internalKey()).isNull();
+ // "max" and "format" parameters are set in db, "min" is not set but has a default value
+ assertThat(squidRule.params()).hasSize(3);
+ assertThat(squidRule.param("min")).isEqualTo("12");
+ assertThat(squidRule.param("max")).isEqualTo("20");
+ assertThat(squidRule.param("format")).isEqualTo("html");
+
+ ActiveRule phpRule = activeRules.find(RuleKey.of("phpunit", "P1"));
+ assertThat(phpRule.severity()).isEqualTo(Severity.BLOCKER);
+ assertThat(phpRule.params()).isEmpty();
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.rule;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.batch.rule.ModuleRule;
-import org.sonar.api.batch.rule.ModuleRules;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-import org.sonar.core.qualityprofile.db.ActiveRuleDao;
-import org.sonar.core.qualityprofile.db.QualityProfileDao;
-
-import java.util.Arrays;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class ModuleRulesProviderTest extends AbstractDaoTestCase {
-
- ModuleQProfiles qProfiles = mock(ModuleQProfiles.class);
- RuleFinder ruleFinder = mock(RuleFinder.class);
-
- @Before
- public void init_rules() {
- when(ruleFinder.findById(10)).thenReturn(new Rule().setRepositoryKey("squid").setKey("S0001"));
- when(ruleFinder.findById(100)).thenReturn(new Rule().setRepositoryKey("phpunit").setKey("P1"));
- }
- @Test
- public void build_module_rules() throws Exception {
- setupData("shared");
- QualityProfileDao profileDao = new QualityProfileDao(getMyBatis());
- when(qProfiles.findAll()).thenReturn(Arrays.asList(
- // 1 rule is enabled on java with severity INFO
- new ModuleQProfiles.QProfile(profileDao.selectById(2)),
- // 1 rule is enabled on php with severity BLOCKER
- new ModuleQProfiles.QProfile(profileDao.selectById(3))
- ));
-
- ModuleRulesProvider provider = new ModuleRulesProvider();
- ActiveRuleDao activeRuleDao = new ActiveRuleDao(getMyBatis());
- ModuleRules moduleRules = provider.provide(qProfiles, activeRuleDao, ruleFinder);
-
- assertThat(moduleRules.findAll()).hasSize(2);
- assertThat(moduleRules.findByRepository("squid")).hasSize(1);
- assertThat(moduleRules.findByRepository("phpunit")).hasSize(1);
- assertThat(moduleRules.findByRepository("unknown")).isEmpty();
- ModuleRule squidRule = moduleRules.find(RuleKey.of("squid", "S0001"));
- assertThat(squidRule.severity()).isEqualTo(Severity.INFO);
- assertThat(squidRule.engineKey()).isNull();
- assertThat(squidRule.params()).hasSize(2);
- assertThat(squidRule.param("max")).isEqualTo("20");
- assertThat(squidRule.param("format")).isEqualTo("html");
- ModuleRule phpRule = moduleRules.find(RuleKey.of("phpunit", "P1"));
- assertThat(phpRule.severity()).isEqualTo(Severity.BLOCKER);
- assertThat(phpRule.params()).isEmpty();
- }
-
-}
--- /dev/null
+<dataset>
+
+ <rules_profiles id="1" name="Java One" language="java" parent_name="[null]" version="10"
+ used_profile="[false]"/>
+
+ <rules_profiles id="2" name="Java Two" language="java" parent_name="[null]" version="20"
+ used_profile="[false]"/>
+
+ <rules_profiles id="3" name="Php One" language="php" parent_name="[null]" version="30"
+ used_profile="[false]"/>
+
+ <rules_profiles id="4" name="Cobol One" language="cbl" parent_name="[null]" version="40"
+ used_profile="[false]"/>
+
+ <!-- java -->
+ <active_rules id="1" profile_id="2" rule_id="10" failure_level="0" inheritance="[null]"
+ note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/>
+
+ <active_rule_parameters id="1" active_rule_id="1" rules_parameter_id="1" rules_parameter_key="max" value="20"/>
+ <active_rule_parameters id="2" active_rule_id="1" rules_parameter_id="2" rules_parameter_key="format" value="html"/>
+
+ <!-- php -->
+ <active_rules id="2" profile_id="3" rule_id="100" failure_level="4" inheritance="[null]"
+ note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/>
+</dataset>
+++ /dev/null
-<dataset>
-
- <rules_profiles id="1" name="Java One" language="java" parent_name="[null]" version="10"
- used_profile="[false]"/>
-
- <rules_profiles id="2" name="Java Two" language="java" parent_name="[null]" version="20"
- used_profile="[false]"/>
-
- <rules_profiles id="3" name="Php One" language="php" parent_name="[null]" version="30"
- used_profile="[false]"/>
-
- <rules_profiles id="4" name="Cobol One" language="cbl" parent_name="[null]" version="40"
- used_profile="[false]"/>
-
- <!-- java -->
- <active_rules id="1" profile_id="2" rule_id="10" failure_level="0" inheritance="[null]"
- note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/>
-
- <active_rule_parameters id="1" active_rule_id="1" rules_parameter_id="1" rules_parameter_key="max" value="20"/>
- <active_rule_parameters id="2" active_rule_id="1" rules_parameter_id="2" rules_parameter_key="format" value="html"/>
-
- <!-- php -->
- <active_rules id="2" profile_id="3" rule_id="100" failure_level="4" inheritance="[null]"
- note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/>
-</dataset>
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule;
+
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.CheckForNull;
+import java.util.Map;
+
+/**
+ * Configuration of a rule activated on a Quality profile
+ * @since 4.2
+ */
+public interface ActiveRule {
+
+ RuleKey ruleKey();
+
+ /**
+ * Non-null severity.
+ * @see org.sonar.api.rule.Severity
+ */
+ String severity();
+
+ /**
+ * Value of given parameter. Returns <code>null</code> if the parameter key does not
+ * exist on the rule or if the parameter has no value nor default value.
+ */
+ @CheckForNull
+ String param(String key);
+
+ /**
+ * Immutable parameter values. Returns an empty map if no parameters are defined.
+ */
+ Map<String, String> params();
+
+ /**
+ * Optional key declared and used by the underlying rule engine. As an example
+ * the key of a Checkstyle rule looks like <code>com.puppycrawl.tools.checkstyle.checks.FooCheck</code>
+ * whereas its internal key can be <code>Checker/TreeWalker/Foo</code>.
+ */
+ @CheckForNull
+ String internalKey();
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule;
+
+import org.sonar.api.BatchComponent;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.CheckForNull;
+import java.util.Collection;
+
+/**
+ * The rules that are activated on the current module. Quality profiles are
+ * merged, so rules can relate to different repositories and languages.
+ * <p/>
+ * Use {@link org.sonar.api.batch.rule.internal.ActiveRulesBuilder} to instantiate
+ * this component in unit tests.
+ * @since 4.2
+ */
+public interface ActiveRules extends BatchComponent {
+
+ /**
+ * Find a {@link ActiveRule} by the associated rule key. <code>null</code>
+ * is returned if the rule does not exist or if the rule is not activated
+ * on any Quality profile associated with the module.
+ */
+ @CheckForNull
+ ActiveRule find(RuleKey ruleKey);
+
+ /**
+ * All the active rules, whatever their repository and related language.
+ */
+ Collection<ActiveRule> findAll();
+
+ /**
+ * The active rules for a given repository, like Findbugs
+ */
+ Collection<ActiveRule> findByRepository(String repository);
+
+}
*/
public class CheckFactory implements BatchComponent {
- private final ModuleRules moduleRules;
+ private final ActiveRules activeRules;
- public CheckFactory(ModuleRules moduleRules) {
- this.moduleRules = moduleRules;
+ public CheckFactory(ActiveRules activeRules) {
+ this.activeRules = activeRules;
}
public <C> Checks<C> create(String repository) {
- return new Checks<C>(moduleRules, repository);
+ return new Checks<C>(activeRules, repository);
}
}
* @since 4.2
*/
public class Checks<C> {
- private final ModuleRules moduleRules;
+ private final ActiveRules activeRules;
private final String repository;
private final Map<RuleKey, C> checkByRule = Maps.newHashMap();
private final Map<C, RuleKey> ruleByCheck = Maps.newIdentityHashMap();
- Checks(ModuleRules moduleRules, String repository) {
- this.moduleRules = moduleRules;
+ Checks(ActiveRules activeRules, String repository) {
+ this.activeRules = activeRules;
this.repository = repository;
}
}
}
- for (ModuleRule moduleRule : moduleRules.findByRepository(repository)) {
- String engineKey = StringUtils.defaultIfBlank(moduleRule.engineKey(), moduleRule.ruleKey().rule());
+ for (ActiveRule activeRule : activeRules.findByRepository(repository)) {
+ String engineKey = StringUtils.defaultIfBlank(activeRule.internalKey(), activeRule.ruleKey().rule());
Object checkClassesOrObject = checksByEngineKey.get(engineKey);
- Object obj = instantiate(moduleRule, checkClassesOrObject);
- add(moduleRule.ruleKey(), (C) obj);
+ Object obj = instantiate(activeRule, checkClassesOrObject);
+ add(activeRule.ruleKey(), (C) obj);
}
return this;
}
return StringUtils.defaultIfEmpty(key, clazz.getCanonicalName());
}
- private Object instantiate(ModuleRule moduleRule, Object checkClassOrInstance) {
+ private Object instantiate(ActiveRule activeRule, Object checkClassOrInstance) {
try {
Object check = checkClassOrInstance;
if (check instanceof Class) {
check = ((Class) checkClassOrInstance).newInstance();
}
- configureFields(moduleRule, check);
+ configureFields(activeRule, check);
return check;
} catch (InstantiationException e) {
- throw failToInstantiateCheck(moduleRule, checkClassOrInstance, e);
+ throw failToInstantiateCheck(activeRule, checkClassOrInstance, e);
} catch (IllegalAccessException e) {
- throw failToInstantiateCheck(moduleRule, checkClassOrInstance, e);
+ throw failToInstantiateCheck(activeRule, checkClassOrInstance, e);
}
}
- private RuntimeException failToInstantiateCheck(ModuleRule moduleRule, Object checkClassOrInstance, Exception e) {
- throw new IllegalStateException(String.format("Fail to instantiate class %s for rule %s", checkClassOrInstance, moduleRule.ruleKey()), e);
+ private RuntimeException failToInstantiateCheck(ActiveRule activeRule, Object checkClassOrInstance, Exception e) {
+ throw new IllegalStateException(String.format("Fail to instantiate class %s for rule %s", checkClassOrInstance, activeRule.ruleKey()), e);
}
- private void configureFields(ModuleRule moduleRule, Object check) {
- for (Map.Entry<String, String> param : moduleRule.params().entrySet()) {
+ private void configureFields(ActiveRule activeRule, Object check) {
+ for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
Field field = getField(check, param.getKey());
if (field == null) {
throw new IllegalStateException(
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule;
-
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.CheckForNull;
-import java.util.Map;
-
-/**
- * @since 4.2
- */
-public interface ModuleRule {
-
- RuleKey ruleKey();
-
- String severity();
-
- @CheckForNull
- String param(String key);
-
- Map<String, String> params();
-
- String engineKey();
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule;
-
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.CheckForNull;
-import java.util.Collection;
-
-/**
- * Lists all the rules that are activated on the current module. Quality profiles are
- * merged, so rules can relate to different repositories and languages.
- * @since 4.2
- */
-public interface ModuleRules {
-
- @CheckForNull
- ModuleRule find(RuleKey ruleKey);
-
- /**
- * All the rules that are enabled, whatever their repository and related language.
- */
- Collection<ModuleRule> findAll();
-
- Collection<ModuleRule> findByRepository(String repository);
-
-}
import java.util.Collection;
/**
- * Not used
+ * Not used yet
* @since 4.2
*/
@Beta
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule.internal;
+
+import com.google.common.collect.Maps;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Builds instances of {@link org.sonar.api.batch.rule.ActiveRules}.
+ * <b>For unit testing and internal use only</b>.
+ * @since 4.2
+ */
+public class ActiveRulesBuilder {
+
+ private final Map<RuleKey, NewActiveRule> map = Maps.newHashMap();
+
+ public NewActiveRule activate(RuleKey ruleKey) {
+ if (map.containsKey(ruleKey)) {
+ throw new IllegalStateException(String.format("Rule '%s' is already activated", ruleKey));
+ }
+ NewActiveRule newActiveRule = new NewActiveRule(ruleKey);
+ map.put(ruleKey, newActiveRule);
+ return newActiveRule;
+ }
+
+ public ActiveRules build() {
+ return new DefaultActiveRules(map.values());
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule.internal;
+
+import com.google.common.collect.ImmutableMap;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.concurrent.Immutable;
+import java.util.Map;
+
+@Immutable
+class DefaultActiveRule implements ActiveRule {
+ private final RuleKey ruleKey;
+ private final String severity, internalKey;
+ private final Map<String, String> params;
+
+ DefaultActiveRule(NewActiveRule newActiveRule) {
+ this.severity = newActiveRule.severity;
+ this.internalKey = newActiveRule.internalKey;
+ this.ruleKey = newActiveRule.ruleKey;
+ this.params = ImmutableMap.copyOf(newActiveRule.params);
+ }
+
+ @Override
+ public RuleKey ruleKey() {
+ return ruleKey;
+ }
+
+ @Override
+ public String severity() {
+ return severity;
+ }
+
+ @Override
+ public String param(String key) {
+ return params.get(key);
+ }
+
+ @Override
+ public Map<String, String> params() {
+ // already immutable
+ return params;
+ }
+
+ @Override
+ public String internalKey() {
+ return internalKey;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule.internal;
+
+import com.google.common.collect.ImmutableListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+
+import javax.annotation.concurrent.Immutable;
+import java.util.Collection;
+import java.util.List;
+
+@Immutable
+class DefaultActiveRules implements ActiveRules {
+
+ // TODO use disk-backed cache (persistit) instead of full in-memory cache ?
+ private final ListMultimap<String, ActiveRule> activeRulesByRepository;
+
+ public DefaultActiveRules(Collection<NewActiveRule> newActiveRules) {
+ ImmutableListMultimap.Builder<String, ActiveRule> builder = ImmutableListMultimap.builder();
+ for (NewActiveRule newAR : newActiveRules) {
+ DefaultActiveRule ar = new DefaultActiveRule(newAR);
+ builder.put(ar.ruleKey().repository(), ar);
+ }
+ activeRulesByRepository = builder.build();
+ }
+
+ @Override
+ public ActiveRule find(RuleKey ruleKey) {
+ List<ActiveRule> rules = activeRulesByRepository.get(ruleKey.repository());
+ for (ActiveRule rule : rules) {
+ if (StringUtils.equals(rule.ruleKey().rule(), ruleKey.rule())) {
+ return rule;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Collection<ActiveRule> findAll() {
+ return activeRulesByRepository.values();
+ }
+
+ @Override
+ public Collection<ActiveRule> findByRepository(String repository) {
+ return activeRulesByRepository.get(repository);
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import com.google.common.collect.ImmutableMap;
-import org.sonar.api.batch.rule.ModuleRule;
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.concurrent.Immutable;
-import java.util.Map;
-
-@Immutable
-class DefaultModuleRule implements ModuleRule {
- private final RuleKey ruleKey;
- private final String severity, engineKey;
- private final Map<String, String> params;
-
- DefaultModuleRule(NewModuleRule newModuleRule) {
- this.severity = newModuleRule.severity;
- this.engineKey = newModuleRule.engineKey;
- this.ruleKey = newModuleRule.ruleKey;
- this.params = ImmutableMap.copyOf(newModuleRule.params);
- }
-
- @Override
- public RuleKey ruleKey() {
- return ruleKey;
- }
-
- @Override
- public String severity() {
- return severity;
- }
-
- @Override
- public String param(String key) {
- return params.get(key);
- }
-
- @Override
- public Map<String, String> params() {
- // immutable
- return params;
- }
-
- @Override
- public String engineKey() {
- return engineKey;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import com.google.common.collect.ImmutableListMultimap;
-import com.google.common.collect.ListMultimap;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.batch.rule.ModuleRule;
-import org.sonar.api.batch.rule.ModuleRules;
-import org.sonar.api.rule.RuleKey;
-
-import javax.annotation.concurrent.Immutable;
-import java.util.Collection;
-import java.util.List;
-
-@Immutable
-class DefaultModuleRules implements ModuleRules {
-
- // TODO use disk-backed cache (persistit) instead of full in-memory cache ?
- private final ListMultimap<String, ModuleRule> moduleRulesByRepository;
-
- public DefaultModuleRules(Collection<NewModuleRule> newModuleRules) {
- ImmutableListMultimap.Builder<String, ModuleRule> builder = ImmutableListMultimap.builder();
- for (NewModuleRule newMr : newModuleRules) {
- DefaultModuleRule mr = new DefaultModuleRule(newMr);
- builder.put(mr.ruleKey().repository(), mr);
- }
- moduleRulesByRepository = builder.build();
- }
-
-
- @Override
- public ModuleRule find(RuleKey ruleKey) {
- List<ModuleRule> rules = moduleRulesByRepository.get(ruleKey.repository());
- for (ModuleRule rule : rules) {
- if (StringUtils.equals(rule.ruleKey().rule(), ruleKey.rule())) {
- return rule;
- }
- }
- return null;
- }
-
- @Override
- public Collection<ModuleRule> findAll() {
- return moduleRulesByRepository.values();
- }
-
- @Override
- public Collection<ModuleRule> findByRepository(String repository) {
- return moduleRulesByRepository.get(repository);
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import org.sonar.api.batch.rule.ModuleRules;
-import org.sonar.api.rule.RuleKey;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * For unit testing and internal use only.
- *
- * @since 4.2
- */
-public class ModuleRulesBuilder {
-
- private final Map<RuleKey, NewModuleRule> map = new HashMap<RuleKey, NewModuleRule>();
-
- public NewModuleRule activate(RuleKey ruleKey) {
- if (map.containsKey(ruleKey)) {
- throw new IllegalStateException(String.format("Rule '%s' is already activated", ruleKey));
- }
- NewModuleRule newModuleRule = new NewModuleRule(ruleKey);
- map.put(ruleKey, newModuleRule);
- return newModuleRule;
- }
-
- public ModuleRules build() {
- return new DefaultModuleRules(map.values());
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule.internal;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+
+import javax.annotation.Nullable;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @since 4.2
+ */
+public class NewActiveRule {
+ final RuleKey ruleKey;
+ String severity = Severity.defaultSeverity();
+ Map<String, String> params = new HashMap<String, String>();
+ String internalKey;
+
+ NewActiveRule(RuleKey ruleKey) {
+ this.ruleKey = ruleKey;
+ }
+
+ public NewActiveRule setSeverity(@Nullable String severity) {
+ this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
+ return this;
+ }
+
+ public NewActiveRule setInternalKey(@Nullable String internalKey) {
+ this.internalKey = internalKey;
+ return this;
+ }
+
+ public NewActiveRule setParam(String key, @Nullable String value) {
+ // possible improvement : check that the param key exists in rule definition
+ if (value == null) {
+ params.remove(key);
+ } else {
+ params.put(key, value);
+ }
+ return this;
+ }
+
+ public Map<String, String> params() {
+ return params;
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-
-import javax.annotation.Nullable;
-import java.util.HashMap;
-import java.util.Map;
-
-public class NewModuleRule {
- final RuleKey ruleKey;
- String severity = Severity.defaultSeverity();
- Map<String, String> params = new HashMap<String, String>();
- String engineKey;
-
- NewModuleRule(RuleKey ruleKey) {
- this.ruleKey = ruleKey;
- }
-
- public NewModuleRule setSeverity(@Nullable String severity) {
- this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
- return this;
- }
-
- public NewModuleRule setEngineKey(@Nullable String engineKey) {
- this.engineKey = engineKey;
- return this;
- }
-
- public NewModuleRule setParam(String key, @Nullable String value) {
- // possible improvement : check that the param key exists in rule definition
- if (value == null) {
- params.remove(key);
- } else {
- params.put(key, value);
- }
- return this;
- }
-}
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.rule.internal.ModuleRulesBuilder;
+import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.SonarException;
@org.junit.Rule
public ExpectedException thrown = ExpectedException.none();
- ModuleRulesBuilder builder = new ModuleRulesBuilder();
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
@Test
public void no_checks_are_enabled() {
@Test
public void use_engine_key() {
RuleKey ruleKey = RuleKey.of("squid", "One");
- builder.activate(ruleKey).setEngineKey("S0001");
+ builder.activate(ruleKey).setInternalKey("S0001");
CheckFactory checkFactory = new CheckFactory(builder.build());
Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.batch.rule.internal;
+
+import org.junit.Test;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+public class ActiveRulesBuilderTest {
+ @Test
+ public void no_rules() throws Exception {
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
+ ActiveRules rules = builder.build();
+ assertThat(rules.findAll()).isEmpty();
+ }
+
+ @Test
+ public void build_rules() throws Exception {
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
+ NewActiveRule newSquid1 = builder.activate(RuleKey.of("squid", "S0001"));
+ newSquid1.setSeverity(Severity.CRITICAL);
+ newSquid1.setInternalKey("__S0001__");
+ newSquid1.setParam("min", "20");
+ // most simple rule
+ builder.activate(RuleKey.of("squid", "S0002"));
+ builder.activate(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null);
+
+ ActiveRules activeRules = builder.build();
+
+ assertThat(activeRules.findAll()).hasSize(3);
+ assertThat(activeRules.findByRepository("squid")).hasSize(2);
+ assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
+ assertThat(activeRules.findByRepository("unknown")).isEmpty();
+
+ ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
+ assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
+ assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
+ assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
+ assertThat(squid1.internalKey()).isEqualTo("__S0001__");
+ assertThat(squid1.params()).hasSize(1);
+ assertThat(squid1.param("min")).isEqualTo("20");
+
+ ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
+ assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
+ assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
+ assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
+ assertThat(squid2.params()).isEmpty();
+
+ ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
+ assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
+ assertThat(findbugsRule.internalKey()).isNull();
+ assertThat(findbugsRule.params()).isEmpty();
+ }
+
+ @Test
+ public void fail_to_add_twice_the_same_rule() throws Exception {
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
+ builder.activate(RuleKey.of("squid", "S0001"));
+ try {
+ builder.activate(RuleKey.of("squid", "S0001"));
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Rule 'squid:S0001' is already activated");
+ }
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch.rule.internal;
-
-import org.junit.Test;
-import org.sonar.api.batch.rule.ModuleRule;
-import org.sonar.api.batch.rule.ModuleRules;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.Fail.fail;
-
-public class ModuleRulesBuilderTest {
- @Test
- public void no_rules() throws Exception {
- ModuleRulesBuilder builder = new ModuleRulesBuilder();
- ModuleRules rules = builder.build();
- assertThat(rules.findAll()).isEmpty();
- }
-
- @Test
- public void build_rules() throws Exception {
- ModuleRulesBuilder builder = new ModuleRulesBuilder();
- NewModuleRule newSquid1 = builder.activate(RuleKey.of("squid", "S0001"));
- newSquid1.setSeverity(Severity.CRITICAL);
- newSquid1.setEngineKey("__S0001__");
- newSquid1.setParam("min", "20");
- // most simple rule
- builder.activate(RuleKey.of("squid", "S0002"));
- builder.activate(RuleKey.of("findbugs", "NPE")).setEngineKey(null).setSeverity(null).setParam("foo", null);
-
- ModuleRules moduleRules = builder.build();
-
- assertThat(moduleRules.findAll()).hasSize(3);
- assertThat(moduleRules.findByRepository("squid")).hasSize(2);
- assertThat(moduleRules.findByRepository("findbugs")).hasSize(1);
- assertThat(moduleRules.findByRepository("unknown")).isEmpty();
-
- ModuleRule squid1 = moduleRules.find(RuleKey.of("squid", "S0001"));
- assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
- assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
- assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
- assertThat(squid1.engineKey()).isEqualTo("__S0001__");
- assertThat(squid1.params()).hasSize(1);
- assertThat(squid1.param("min")).isEqualTo("20");
-
- ModuleRule squid2 = moduleRules.find(RuleKey.of("squid", "S0002"));
- assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
- assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
- assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
- assertThat(squid2.params()).isEmpty();
-
- ModuleRule findbugsRule = moduleRules.find(RuleKey.of("findbugs", "NPE"));
- assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
- assertThat(findbugsRule.engineKey()).isNull();
- assertThat(findbugsRule.params()).isEmpty();
- }
-
- @Test
- public void fail_to_add_twice_the_same_rule() throws Exception {
- ModuleRulesBuilder builder = new ModuleRulesBuilder();
- builder.activate(RuleKey.of("squid", "S0001"));
- try {
- builder.activate(RuleKey.of("squid", "S0001"));
- fail();
- } catch (IllegalStateException e) {
- assertThat(e).hasMessage("Rule 'squid:S0001' is already activated");
- }
- }
-}