diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-24 09:10:45 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-24 09:22:47 +0200 |
commit | c31cbfe24543d7e7e35380ef941e92cef9aac77c (patch) | |
tree | a31ad27917377782e100409bc6fd25ec4af9bb2b /server | |
parent | 2659bc296bc643229ff65163aba71a4642362194 (diff) | |
download | sonarqube-c31cbfe24543d7e7e35380ef941e92cef9aac77c.tar.gz sonarqube-c31cbfe24543d7e7e35380ef941e92cef9aac77c.zip |
Fix use of RowNotFoundException in tests
Diffstat (limited to 'server')
9 files changed, 56 insertions, 48 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java index 036e62723d5..d67cc6d0c51 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java @@ -30,6 +30,7 @@ import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.Verifications; /** @@ -147,7 +148,10 @@ public class QProfileFactory { void setDefault(DbSession dbSession, String profileKey) { Verifications.check(StringUtils.isNotBlank(profileKey), "Profile key must be set"); - QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, profileKey); + QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, profileKey); + if (profile == null) { + throw new NotFoundException("Quality profile not found: " + profileKey); + } setDefault(dbSession, profile); dbSession.commit(); } @@ -201,7 +205,10 @@ public class QProfileFactory { Verifications.check(newName.length() < 100, String.format("Name is too long (>%d characters)", 100)); DbSession dbSession = db.openSession(false); try { - QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, key); + QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, key); + if (profile == null) { + throw new NotFoundException("Quality profile not found: " + key); + } if (!StringUtils.equals(newName, profile.getName())) { if (db.qualityProfileDao().selectByNameAndLanguage(newName, profile.getLanguage(), dbSession) != null) { throw new BadRequestException("Quality profile already exists: " + newName); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java index ef1bc03b96d..155bec15e8e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java @@ -38,10 +38,14 @@ import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.RowNotFoundException; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; +import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.db.measure.custom.CustomMeasureDto; +import org.sonar.db.metric.MetricDao; import org.sonar.db.metric.MetricDto; +import org.sonar.db.metric.MetricTesting; import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.ComponentTesting; import org.sonar.server.db.DbClient; @@ -50,9 +54,6 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.ServerException; -import org.sonar.db.measure.custom.CustomMeasureDao; -import org.sonar.db.metric.MetricDao; -import org.sonar.db.metric.MetricTesting; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.index.UserDoc; import org.sonar.server.user.index.UserIndex; @@ -361,7 +362,7 @@ public class CreateActionTest { dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto(DEFAULT_PROJECT_UUID)); dbSession.commit(); - expectedException.expect(IllegalStateException.class); + expectedException.expect(RowNotFoundException.class); expectedException.expectMessage("Metric id '42' not found"); newRequest() diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java index d9eb55bbcd9..17ed06dd7d1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java @@ -31,13 +31,14 @@ import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.RowNotFoundException; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; +import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.server.component.ComponentTesting; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; import org.sonar.test.DbTests; @@ -101,7 +102,7 @@ public class DeleteActionTest { @Test public void fail_when_not_found_in_db() throws Exception { - expectedException.expect(IllegalArgumentException.class); + expectedException.expect(RowNotFoundException.class); newRequest().setParam(PARAM_ID, "42").execute(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java index 420bafe9d8d..b7815f3dba7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java @@ -34,19 +34,20 @@ import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.RowNotFoundException; import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.db.measure.custom.CustomMeasureDto; +import org.sonar.db.metric.MetricDao; import org.sonar.db.metric.MetricDto; +import org.sonar.db.metric.MetricTesting; import org.sonar.server.component.ComponentTesting; import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.ServerException; import org.sonar.server.exceptions.UnauthorizedException; -import org.sonar.db.metric.MetricDao; -import org.sonar.db.metric.MetricTesting; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.index.UserDoc; import org.sonar.server.user.index.UserIndex; @@ -243,7 +244,7 @@ public class UpdateActionTest { @Test public void fail_if_not_in_db() throws Exception { - expectedException.expect(IllegalArgumentException.class); + expectedException.expect(RowNotFoundException.class); expectedException.expectMessage("Custom measure '42' not found."); ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java index 48913ec4dfa..8c79f28307f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java @@ -33,10 +33,12 @@ import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.api.server.rule.RuleParamType; import org.sonar.db.DbSession; +import org.sonar.db.RowNotFoundException; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleParamDto; @@ -53,6 +55,8 @@ public class QProfileBackuperMediumTest { @ClassRule public static ServerTester tester = new ServerTester(); @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); DbClient db; @@ -113,12 +117,10 @@ public class QProfileBackuperMediumTest { @Test public void fail_to_backup_unknown_profile() { - try { - tester.get(QProfileBackuper.class).backup("unknown", new StringWriter()); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Quality profile not found: unknown"); - } + thrown.expect(RowNotFoundException.class); + thrown.expectMessage("Quality profile not found: unknown"); + + tester.get(QProfileBackuper.class).backup("unknown", new StringWriter()); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java index d4f83316a34..2c19b0df97d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java @@ -24,21 +24,24 @@ import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.server.rule.RuleParamType; -import org.sonar.db.component.ComponentDto; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; +import org.sonar.db.RowNotFoundException; +import org.sonar.db.component.ComponentDto; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleParamDto; +import org.sonar.db.rule.RuleTesting; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.qualityprofile.index.ActiveRuleIndex; -import org.sonar.db.rule.RuleTesting; import org.sonar.server.search.IndexClient; +import org.sonar.server.tester.MockUserSession; import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.tester.MockUserSession; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -51,6 +54,8 @@ public class QProfileFactoryMediumTest { @ClassRule public static ServerTester tester = new ServerTester(); @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); DbClient db; @@ -112,7 +117,6 @@ public class QProfileFactoryMediumTest { } } - @Test public void fail_to_create_if_already_exists() { QProfileName name = new QProfileName("xoo", "P1"); @@ -175,12 +179,10 @@ public class QProfileFactoryMediumTest { @Test public void fail_renaming_if_profile_not_found() { - try { - factory.rename("unknown", "the new name"); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Quality profile not found: unknown"); - } + thrown.expect(NotFoundException.class); + thrown.expectMessage("Quality profile not found: unknown"); + + factory.rename("unknown", "the new name"); } @Test @@ -276,12 +278,10 @@ public class QProfileFactoryMediumTest { @Test public void fail_if_unknown_profile_to_be_deleted() { - try { - factory.delete(XOO_P1_KEY); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Quality profile not found: XOO_P1"); - } + thrown.expect(RowNotFoundException.class); + thrown.expectMessage("Quality profile not found: XOO_P1"); + + factory.delete(XOO_P1_KEY); } @Test @@ -300,13 +300,10 @@ public class QProfileFactoryMediumTest { @Test public void fail_if_unknown_profile_to_be_set_as_default() { - try { - // does not exist - factory.setDefault(XOO_P1_KEY); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Quality profile not found: " + XOO_P1_KEY); - } + thrown.expect(NotFoundException.class); + thrown.expectMessage("Quality profile not found: " + XOO_P1_KEY); + + factory.setDefault(XOO_P1_KEY); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangeParentActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangeParentActionMediumTest.java index 221738cd538..a4fd86d203c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangeParentActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangeParentActionMediumTest.java @@ -29,6 +29,7 @@ import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; +import org.sonar.db.RowNotFoundException; import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleDto; @@ -244,7 +245,7 @@ public class ChangeParentActionMediumTest { .execute(); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = RowNotFoundException.class) public void fail_if_profile_key_and_name_both_set() throws Exception { QualityProfileDto child = createProfile("xoo", "Child"); session.commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java index 3637e6acec6..743ec248c5e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java @@ -32,6 +32,7 @@ import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.qualityprofile.QProfileFactory; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -129,7 +130,7 @@ public class RenameActionTest { .execute(); } - @Test(expected = IllegalArgumentException.class) + @Test(expected = NotFoundException.class) public void fail_on_unknown_profile() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SetDefaultActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SetDefaultActionTest.java index e3562d129e7..77b68c9aec8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SetDefaultActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SetDefaultActionTest.java @@ -60,7 +60,6 @@ public class SetDefaultActionTest { private DbSession session; - @Before public void setUp() { dbTester.truncateTables(); @@ -86,7 +85,6 @@ public class SetDefaultActionTest { public void set_default_profile_using_key() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); - checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345"); checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567"); @@ -120,14 +118,13 @@ public class SetDefaultActionTest { try { tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "unknown-profile-666").execute(); Fail.failBecauseExceptionWasNotThrown(IllegalArgumentException.class); - } catch(IllegalArgumentException nfe) { + } catch (NotFoundException nfe) { assertThat(nfe).hasMessage("Quality profile not found: unknown-profile-666"); checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345"); checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567"); } } - @Test public void fail_to_set_default_profile_using_language_and_name() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); @@ -135,7 +132,7 @@ public class SetDefaultActionTest { try { tester.newPostRequest("api/qualityprofiles", "set_default").setParam("language", xoo2Key).setParam("profileName", "Unknown").execute(); Fail.failBecauseExceptionWasNotThrown(NotFoundException.class); - } catch(NotFoundException nfe) { + } catch (NotFoundException nfe) { assertThat(nfe).hasMessage("Unable to find a profile for language 'xoo2' with name 'Unknown'"); checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345"); checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567"); @@ -149,7 +146,7 @@ public class SetDefaultActionTest { try { tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "sonar-way-xoo2-23456").execute().assertNoContent(); Fail.failBecauseExceptionWasNotThrown(ForbiddenException.class); - } catch(ForbiddenException forbidden) { + } catch (ForbiddenException forbidden) { checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345"); checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567"); } |