*/
package org.sonar.server.rule;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.*;
private final RuleTagOperations ruleTagOperations;
private final ActiveRuleDao activeRuleDao;
private final CharacteristicDao characteristicDao;
- private final System2 system = System2.INSTANCE;
+ private final System2 system;
/**
- * @param registerTechnicalDebtModel used only to be started after init of the technical debt model
+ * @param registerDebtModel used only to be started after init of the technical debt model
*/
public RuleRegistration(RuleDefinitionsLoader defLoader, ProfilesManager profilesManager,
RuleRegistry ruleRegistry, ESRuleTags esRuleTags, RuleTagOperations ruleTagOperations,
MyBatis myBatis, RuleDao ruleDao, RuleTagDao ruleTagDao, ActiveRuleDao activeRuleDao, CharacteristicDao characteristicDao,
- RegisterDebtModel registerTechnicalDebtModel) {
+ RegisterDebtModel registerDebtModel) {
+ this(defLoader, profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, System2.INSTANCE);
+ }
+
+ @VisibleForTesting
+ RuleRegistration(RuleDefinitionsLoader defLoader, ProfilesManager profilesManager,
+ RuleRegistry ruleRegistry, ESRuleTags esRuleTags, RuleTagOperations ruleTagOperations,
+ MyBatis myBatis, RuleDao ruleDao, RuleTagDao ruleTagDao, ActiveRuleDao activeRuleDao, CharacteristicDao characteristicDao, System2 system) {
this.defLoader = defLoader;
this.profilesManager = profilesManager;
this.ruleRegistry = ruleRegistry;
this.ruleTagDao = ruleTagDao;
this.activeRuleDao = activeRuleDao;
this.characteristicDao = characteristicDao;
+ this.system = system;
}
@Override
// Update copy of template rules from template
if (ruleDto.getParentId() != null) {
RuleDto parent = buffer.rulesById.get(ruleDto.getParentId());
- if (parent != null && !parent.getStatus().equals(Rule.STATUS_REMOVED)) {
+ if (parent != null && !Rule.STATUS_REMOVED.equals(parent.getStatus())) {
// TODO merge params and tags ?
ruleDto.setLanguage(parent.getLanguage());
ruleDto.setStatus(parent.getStatus());
toBeRemoved = false;
}
}
- if (toBeRemoved) {
+ if (toBeRemoved && !Rule.STATUS_REMOVED.equals(ruleDto.getStatus())) {
// TODO log repository key
LOG.info("Disable rule " + ruleDto.getRuleKey());
ruleDto.setStatus(Rule.STATUS_REMOVED);
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition;
+import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.System2;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.qualityprofile.db.ActiveRuleDao;
import org.sonar.server.qualityprofile.ProfilesManager;
import org.sonar.server.startup.RegisterDebtModel;
+import java.util.Date;
+
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.*;
RuleTagDao ruleTagDao;
ActiveRuleDao activeRuleDao;
CharacteristicDao characteristicDao;
+ System2 system;
+ Date date = DateUtils.parseDateTime("2014-03-17T19:10:03+0100");
@Before
public void before() {
+ system = mock(System2.class);
+ when(system.now()).thenReturn(date.getTime());
myBatis = getMyBatis();
ruleDao = new RuleDao(myBatis);
ruleTagDao = new RuleTagDao(myBatis);
ruleTagOperations = new RuleTagOperations(ruleTagDao, esRuleTags);
characteristicDao = new CharacteristicDao(myBatis);
task = new RuleRegistration(new RuleDefinitionsLoader(mock(RuleRepositories.class), new RulesDefinition[]{new FakeRepository()}),
- profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, mock(RegisterDebtModel.class));
+ profilesManager, ruleRegistry, esRuleTags, ruleTagOperations, myBatis, ruleDao, ruleTagDao, activeRuleDao, characteristicDao, system);
}
@Test
checkTables("disable_deprecated_rules", EXCLUDED_COLUMN_NAMES_INCLUDING_DEBT, "rules", "rules_parameters", "rules_rule_tags", "rule_tags");
}
+ @Test
+ public void not_disable_already_disabled_rules() {
+ setupData("not_disable_already_disabled_rules");
+ task.start();
+
+ checkTables("not_disable_already_disabled_rules", new String[]{"created_at", "note_data", "note_user_login", "note_created_at", "note_updated_at"}, "rules");
+ }
+
@Test
public void update_rule_fields() {
setupData("update_rule_fields");
--- /dev/null
+<dataset>
+
+ <rules id="1" plugin_rule_key="deprecated-key" plugin_name="deprecated-repo" plugin_config_key="[null]" name="Deprecated" description="[null]"
+ status="REMOVED" priority="4" cardinality="SINGLE" parent_id="[null]" language="[null]"
+ characteristic_id="[null]" default_characteristic_id="[null]"
+ remediation_function="[null]" default_remediation_function="[null]"
+ remediation_factor="[null]" default_remediation_factor="[null]"
+ remediation_offset="[null]" default_remediation_offset="[null]"
+ effort_to_fix_l10n_key="[null]"
+ updated_at="2014-03-16"/>
+
+ <rules id="2" plugin_rule_key="deprecated" plugin_name="fake" plugin_config_key="[null]" name="Deprecated fake" description="[null]"
+ status="REMOVED" priority="4" cardinality="SINGLE" parent_id="[null]" language="[null]"
+ characteristic_id="[null]" default_characteristic_id="[null]"
+ remediation_function="[null]" default_remediation_function="[null]"
+ remediation_factor="[null]" default_remediation_factor="[null]"
+ remediation_offset="[null]" default_remediation_offset="[null]"
+ effort_to_fix_l10n_key="[null]"
+ updated_at="2014-03-16"/>
+
+ <!-- New rules -->
+ <rules id="3" plugin_rule_key="rule1" plugin_name="fake" plugin_config_key="config1" name="One" description="Description of One"
+ status="READY" priority="4" cardinality="SINGLE" parent_id="[null]" language="java"
+ characteristic_id="[null]" default_characteristic_id="2"
+ remediation_function="[null]" default_remediation_function="LINEAR_OFFSET"
+ remediation_factor="[null]" default_remediation_factor="5d"
+ remediation_offset="[null]" default_remediation_offset="10h"
+ effort_to_fix_l10n_key="squid.S115.effortToFix"
+ updated_at="2014-03-17 19:10:03.0"/>
+
+ <rules id="4" plugin_rule_key="rule2" plugin_name="fake" plugin_config_key="[null]" name="Two" description="Description of Two"
+ status="DEPRECATED" priority="0" cardinality="SINGLE" parent_id="[null]" language="java"
+ characteristic_id="[null]" default_characteristic_id="[null]"
+ remediation_function="[null]" default_remediation_function="[null]"
+ remediation_factor="[null]" default_remediation_factor="[null]"
+ remediation_offset="[null]" default_remediation_offset="[null]"
+ effort_to_fix_l10n_key="[null]"
+ updated_at="2014-03-17 19:10:03.0"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <characteristics id="2" kee="MEMORY_EFFICIENCY" name="Memory Efficiency" parent_id="1" characteristic_order="1" enabled="[true]"/>
+
+ <rules id="1" plugin_rule_key="deprecated-key" plugin_name="deprecated-repo" plugin_config_key="[null]" name="Deprecated" description="[null]"
+ status="REMOVED" priority="4" cardinality="SINGLE" parent_id="[null]"
+ characteristic_id="[null]" default_characteristic_id="[null]"
+ remediation_function="[null]" default_remediation_function="[null]"
+ remediation_factor="[null]" default_remediation_factor="[null]"
+ remediation_offset="[null]" default_remediation_offset="[null]"
+ effort_to_fix_l10n_key="[null]"
+ updated_at="2014-03-16"/>
+
+ <rules id="2" plugin_rule_key="deprecated" plugin_name="fake" plugin_config_key="[null]" name="Deprecated fake" description="[null]"
+ status="REMOVED" priority="4" cardinality="SINGLE" parent_id="[null]"
+ characteristic_id="[null]" default_characteristic_id="[null]"
+ remediation_function="[null]" default_remediation_function="[null]"
+ remediation_factor="[null]" default_remediation_factor="[null]"
+ remediation_offset="[null]" default_remediation_offset="[null]"
+ effort_to_fix_l10n_key="[null]"
+ updated_at="2014-03-16"/>
+
+</dataset>