]> source.dussan.org Git - sonarqube.git/commitdiff
RowNotFoundException is the default Exception at the DAO layer
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 23 Jul 2015 14:44:36 +0000 (16:44 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 24 Jul 2015 07:22:46 +0000 (09:22 +0200)
sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
sonar-db/src/main/java/org/sonar/db/measure/custom/CustomMeasureDao.java
sonar-db/src/main/java/org/sonar/db/metric/MetricDao.java
sonar-db/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
sonar-db/src/test/java/org/sonar/db/measure/custom/CustomMeasureDaoTest.java
sonar-db/src/test/java/org/sonar/db/metric/MetricDaoTest.java

index 78b517317c638f4bf99d04b6dedec5fe6ce5b103..cbfb688f965f42085bd7e9300446e657952b08da 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.api.resources.Scopes;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
+import org.sonar.db.util.RowNotFoundException;
 
 import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
 
@@ -43,7 +44,7 @@ public class ComponentDao implements Dao {
   public ComponentDto selectOrFailById(DbSession session, long id) {
     Optional<ComponentDto> componentDto = selectById(session, id);
     if (!componentDto.isPresent()) {
-      throw new IllegalArgumentException(String.format("Component id does not exist: %d", id));
+      throw new RowNotFoundException(String.format("Component id does not exist: %d", id));
     }
     return componentDto.get();
   }
@@ -59,7 +60,7 @@ public class ComponentDao implements Dao {
   public ComponentDto selectOrFailByUuid(DbSession session, String uuid) {
     Optional<ComponentDto> componentDto = selectByUuid(session, uuid);
     if (!componentDto.isPresent()) {
-      throw new IllegalArgumentException(String.format("Component with uuid '%s' not found", uuid));
+      throw new RowNotFoundException(String.format("Component with uuid '%s' not found", uuid));
     }
     return componentDto.get();
   }
@@ -133,10 +134,10 @@ public class ComponentDao implements Dao {
     return mapper(session).selectByKeys(keys);
   }
 
-  public ComponentDto selectNonNullByKey(DbSession session, String key) {
+  public ComponentDto selectOrFailByKey(DbSession session, String key) {
     Optional<ComponentDto> component = selectByKey(session, key);
     if (!component.isPresent()) {
-      throw new IllegalArgumentException(String.format("Component key '%s' not found", key));
+      throw new RowNotFoundException(String.format("Component key '%s' not found", key));
     }
     return component.get();
   }
index e5a09655ef57726f62ff1c8829781ded82c9d6cd..2f48fe901dc57225e750c5240b642abe5b406708 100644 (file)
@@ -29,6 +29,7 @@ import javax.annotation.Nullable;
 import org.sonar.api.resources.Scopes;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.util.RowNotFoundException;
 
 public class SnapshotDao implements Dao {
 
@@ -40,7 +41,7 @@ public class SnapshotDao implements Dao {
   public SnapshotDto selectOrFailById(DbSession session, long id) {
     SnapshotDto value = selectById(session, id);
     if (value == null) {
-      throw new IllegalArgumentException(String.format("Snapshot id does not exist: %d", id));
+      throw new RowNotFoundException(String.format("Snapshot id does not exist: %d", id));
     }
     return value;
   }
index 4abe75f49ef6a84d3028fd687916ece9a1dc60e3..9a6080344d29764b31fab6c18ea98e8a03032821 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.api.server.ServerSide;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
+import org.sonar.db.util.RowNotFoundException;
 
 @ServerSide
 public class CustomMeasureDao implements Dao {
@@ -62,7 +63,7 @@ public class CustomMeasureDao implements Dao {
   public CustomMeasureDto selectOrFail(DbSession session, long id) {
     CustomMeasureDto customMeasure = selectById(session, id);
     if (customMeasure == null) {
-      throw new IllegalArgumentException(String.format("Custom measure '%d' not found.", id));
+      throw new RowNotFoundException(String.format("Custom measure '%d' not found.", id));
     }
     return customMeasure;
   }
index 171eb8b1cecf54a65f59af2c924cdfd22355f835..d059eb1fd9f21503ae3dc7bb3e5902f5193861e8 100644 (file)
@@ -37,6 +37,7 @@ import org.apache.ibatis.session.RowBounds;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
+import org.sonar.db.util.RowNotFoundException;
 
 import static com.google.common.collect.Lists.newArrayList;
 
@@ -59,7 +60,7 @@ public class MetricDao implements Dao {
   public MetricDto selectOrFailByKey(DbSession session, String key) {
     MetricDto metric = selectByKey(session, key);
     if (metric == null) {
-      throw new IllegalStateException(String.format("Metric key '%s' not found", key));
+      throw new RowNotFoundException(String.format("Metric key '%s' not found", key));
     }
     return metric;
   }
@@ -151,7 +152,7 @@ public class MetricDao implements Dao {
   public MetricDto selectOrFailById(DbSession session, int id) {
     MetricDto metric = mapper(session).selectById(id);
     if (metric == null) {
-      throw new IllegalStateException(String.format("Metric id '%d' not found", id));
+      throw new RowNotFoundException(String.format("Metric id '%d' not found", id));
     }
     return metric;
   }
index 13301240367e636976512360230c63e4235225dc..665360558feee366399f431c221a4b75309c7d0e 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.util.RowNotFoundException;
 
 @ServerSide
 public class QualityProfileDao implements Dao {
@@ -54,7 +55,7 @@ public class QualityProfileDao implements Dao {
   public QualityProfileDto selectOrFailByKey(DbSession session, String key) {
     QualityProfileDto dto = selectByKey(session, key);
     if (dto == null) {
-      throw new IllegalArgumentException("Quality profile not found: " + key);
+      throw new RowNotFoundException("Quality profile not found: " + key);
     }
     return dto;
   }
index d2199aee6473c839d98b656d10460be8f2dc962f..e83c616ab6eb506d4a1f7c2b03a3e3c79f26dee2 100644 (file)
@@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
+import org.sonar.db.util.RowNotFoundException;
 import org.sonar.test.DbTests;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -102,7 +103,7 @@ public class ComponentDaoTest {
 
   @Test
   public void fail_to_get_by_uuid_when_component_not_found() {
-    thrown.expect(IllegalArgumentException.class);
+    thrown.expect(RowNotFoundException.class);
 
     db.prepareDbUnit(getClass(), "shared.xml");
 
@@ -131,7 +132,7 @@ public class ComponentDaoTest {
 
   @Test
   public void fail_to_get_by_key_when_component_not_found() {
-    thrown.expect(IllegalArgumentException.class);
+    thrown.expect(RowNotFoundException.class);
 
     db.prepareDbUnit(getClass(), "shared.xml");
 
@@ -270,7 +271,7 @@ public class ComponentDaoTest {
     assertThat(result.get().isEnabled()).isFalse();
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test(expected = RowNotFoundException.class)
   public void fail_to_get_by_id_when_project_not_found() {
     db.prepareDbUnit(getClass(), "shared.xml");
 
index 7a0805d11d675476ba89190c2ef2a14d792c4228..6090ddcf783b7824f232b3d8dafd3f3f1d7b60ed 100644 (file)
@@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.RowNotFoundException;
 import org.sonar.test.DbTests;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -151,7 +152,7 @@ public class CustomMeasureDaoTest {
 
   @Test
   public void select_by_id_fail_if_no_measure_found() {
-    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expect(RowNotFoundException.class);
 
     underTest.selectOrFail(session, 42L);
   }
index 1e1c17b7e62afc4a561cc421728fdc24dbbf0515..935c519b2d226b8cd0dfc730048e895857b2b8dc 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.experimental.categories.Category;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.util.RowNotFoundException;
 import org.sonar.test.DbTests;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -81,7 +82,7 @@ public class MetricDaoTest {
     assertThat(result.isEnabled()).isFalse();
   }
 
-  @Test(expected = IllegalStateException.class)
+  @Test(expected = RowNotFoundException.class)
   public void get_nullable_by_key() {
     dao.selectOrFailByKey(session, "unknown");
   }