From 8a1c7a7a1aa45781ec0266b01dd6f95f6c51be07 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 29 Oct 2013 16:53:02 +0100 Subject: [PATCH] Fix EntityNotFoundException when removing active rules (cherry picked from commit 1cf660f) --- .../server/configuration/ProfilesManager.java | 13 ++++------ .../configuration/ProfilesManagerTest.java | 26 ++++++++++++++++--- 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); + } + } -- 2.39.5