]> source.dussan.org Git - sonarqube.git/commitdiff
Fix EntityNotFoundException when removing active rules
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 29 Oct 2013 15:53:02 +0000 (16:53 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 29 Oct 2013 15:53:55 +0000 (16:53 +0100)
(cherry picked from commit 1cf660f)

sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java
sonar-server/src/test/java/org/sonar/server/configuration/ProfilesManagerTest.java

index ea48a665e2f20136b62772e750a98fad20132979..b594dab10e01f6629c24fde691148d8f7d78e86c 100644 (file)
  */
 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();
index 82aa83f3897de2ddcf66f1e505fda03f76f1c5ce..5db986600c37186c29e2b36b3b4b0f0a6430d5f0 100644 (file)
  */
 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);
+  }
+
 }