*/
package org.sonar.server.configuration;
-import org.sonar.core.preview.PreviewCache;
-
import com.google.common.collect.Lists;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.ActiveRuleChange;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleParam;
-import org.sonar.api.rules.RulePriority;
+import org.sonar.api.rules.*;
import org.sonar.api.utils.ValidationMessages;
+import org.sonar.core.preview.PreviewCache;
import org.sonar.jpa.dao.BaseDao;
import org.sonar.jpa.dao.RulesDao;
activeRulesToRemove.add(activeRule);
}
}
+
for (ActiveRule activeRule : activeRulesToRemove) {
- ActiveRule activeRuleToRemove = getSession().getSingleResult(ActiveRule.class, "id", activeRule.getId());
+ // Do not use getSingleResult as it can generate an EntityNotFoundException
+ ActiveRule activeRuleToRemove = getSession().getEntity(ActiveRule.class, activeRule.getId());
removeActiveRule(activeRuleToRemove);
}
getSession().commit();
*/
package org.sonar.server.configuration;
-import org.sonar.core.preview.PreviewCache;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.rules.ActiveRule;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RulePriority;
+import org.sonar.core.preview.PreviewCache;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
}
@Test
- public void should_delete_all_profiles() {
+ public void delete_all_profiles() {
RulesProfile test1 = RulesProfile.create("test1", "java");
test1.setDefaultProfile(true);
RulesProfile test2 = RulesProfile.create("test2", "java");
assertThat(getHQLCount(RulesProfile.class)).isEqualTo(0);
}
+ @Test
+ public void remove_activated_rules() {
+ Rule rule1 = Rule.create("repo", "key");
+
+ RulesProfile profile1 = RulesProfile.create("profile1", "xoo");
+ ActiveRule activeRule1 = new ActiveRule(profile1, rule1, RulePriority.BLOCKER);
+
+ RulesProfile profile2 = RulesProfile.create("profile2", "foo");
+ ActiveRule activeRule2 = new ActiveRule(profile2, rule1, RulePriority.BLOCKER);
+
+ getSession().save(profile1, rule1, activeRule1, profile2, activeRule2);
+
+ manager.removeActivatedRules(rule1);
+
+ assertThat(getHQLCount(ActiveRule.class)).isEqualTo(0);
+ }
+
}