aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2013-10-29 16:53:02 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2013-10-29 16:53:12 +0100
commit1cf660f5361e96f5282d9e32caf4047d2b9bdb60 (patch)
tree1f65fb6cd2d21758b384e2d6aa15b657eb4b5f18
parent2b6b6bd27a7d81280bc862cc7d48536c7a9c2bca (diff)
downloadsonarqube-1cf660f5361e96f5282d9e32caf4047d2b9bdb60.tar.gz
sonarqube-1cf660f5361e96f5282d9e32caf4047d2b9bdb60.zip
Fix EntityNotFoundException when removing active rules
-rw-r--r--sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java13
-rw-r--r--sonar-server/src/test/java/org/sonar/server/configuration/ProfilesManagerTest.java26
2 files changed, 28 insertions, 11 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
index ea48a665e2f..b594dab10e0 100644
--- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
+++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
@@ -19,19 +19,14 @@
*/
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;
@@ -121,8 +116,10 @@ public class ProfilesManager extends BaseDao {
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();
diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesManagerTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesManagerTest.java
index 82aa83f3897..5db986600c3 100644
--- a/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesManagerTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/configuration/ProfilesManagerTest.java
@@ -19,12 +19,15 @@
*/
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;
@@ -38,7 +41,7 @@ public class ProfilesManagerTest extends AbstractDbUnitTestCase {
}
@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");
@@ -52,4 +55,21 @@ public class ProfilesManagerTest extends AbstractDbUnitTestCase {
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);
+ }
+
}