diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-06-03 15:09:42 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-06-03 15:09:42 +0200 |
commit | e4ff2f4bffd3c9d1abb7d5789106b238b1857031 (patch) | |
tree | b5c2b3636282af15049732689439aab6bec88717 /server | |
parent | d9add414c8a3ce8d34af21262d26313bd1dd03eb (diff) | |
download | sonarqube-e4ff2f4bffd3c9d1abb7d5789106b238b1857031.tar.gz sonarqube-e4ff2f4bffd3c9d1abb7d5789106b238b1857031.zip |
Sanitize SnapshotDao
- Rename all select methods to begin with 'select'
- Remove no more used method
Diffstat (limited to 'server')
6 files changed, 39 insertions, 78 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/db/SnapshotDao.java b/server/sonar-server/src/main/java/org/sonar/server/component/db/SnapshotDao.java index 99a04164bff..d86add58f0e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/db/SnapshotDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/db/SnapshotDao.java @@ -40,39 +40,25 @@ public class SnapshotDao implements DaoComponent { } @CheckForNull - public SnapshotDto getNullableByKey(DbSession session, Long id) { + public SnapshotDto selectNullableById(DbSession session, Long id) { return mapper(session).selectByKey(id); } - public SnapshotDto getByKey(DbSession session, Long key) { - SnapshotDto value = getNullableByKey(session, key); + public SnapshotDto selectById(DbSession session, Long key) { + SnapshotDto value = selectNullableById(session, key); if (value == null) { throw new NotFoundException(String.format("Key '%s' not found", key)); } - return value; } - public SnapshotDto insert(DbSession session, SnapshotDto item) { - if (item.getCreatedAt() == null) { - item.setCreatedAt(system2.now()); - } - mapper(session).insert(item); - return item; - } - - @CheckForNull - public SnapshotDto getLastSnapshot(DbSession session, SnapshotDto snapshot) { - return mapper(session).selectLastSnapshot(snapshot.getComponentId()); - } - @CheckForNull - public SnapshotDto getLastSnapshotOlderThan(DbSession session, SnapshotDto snapshot) { - return mapper(session).selectLastSnapshotOlderThan(snapshot.getComponentId(), snapshot.getCreatedAt()); + public SnapshotDto selectLastSnapshotByComponentId(DbSession session, long componentId) { + return mapper(session).selectLastSnapshot(componentId); } - public List<SnapshotDto> findSnapshotAndChildrenOfProjectScope(DbSession session, SnapshotDto snapshot) { - return mapper(session).selectSnapshotAndChildrenOfScope(snapshot.getId(), Scopes.PROJECT); + public List<SnapshotDto> selectSnapshotAndChildrenOfProjectScope(DbSession session, long snapshotId) { + return mapper(session).selectSnapshotAndChildrenOfScope(snapshotId, Scopes.PROJECT); } public int updateSnapshotAndChildrenLastFlagAndStatus(DbSession session, SnapshotDto snapshot, boolean isLast, String status) { @@ -91,10 +77,18 @@ public class SnapshotDao implements DaoComponent { return mapper(session).updateSnapshotAndChildrenLastFlag(rootId, pathRootId, path, isLast); } - public boolean isLast(SnapshotDto snapshotTested, @Nullable SnapshotDto previousLastSnapshot) { + public static boolean isLast(SnapshotDto snapshotTested, @Nullable SnapshotDto previousLastSnapshot) { return previousLastSnapshot == null || previousLastSnapshot.getCreatedAt() < snapshotTested.getCreatedAt(); } + public SnapshotDto insert(DbSession session, SnapshotDto item) { + if (item.getCreatedAt() == null) { + item.setCreatedAt(system2.now()); + } + mapper(session).insert(item); + return item; + } + private SnapshotMapper mapper(DbSession session) { return session.getMapper(SnapshotMapper.class); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/SwitchSnapshotStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/SwitchSnapshotStep.java index 8d180552170..d1bd3d2f85b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/SwitchSnapshotStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/SwitchSnapshotStep.java @@ -20,6 +20,7 @@ package org.sonar.server.computation.step; +import java.util.List; import org.sonar.core.component.SnapshotDto; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; @@ -27,8 +28,6 @@ import org.sonar.server.component.db.SnapshotDao; import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.db.DbClient; -import java.util.List; - public class SwitchSnapshotStep implements ComputationStep { private final DbClient dbClient; @@ -57,9 +56,9 @@ public class SwitchSnapshotStep implements ComputationStep { } private void disablePreviousSnapshot(DbSession session, long reportSnapshotId) { - List<SnapshotDto> snapshots = dbClient.snapshotDao().findSnapshotAndChildrenOfProjectScope(session, dbClient.snapshotDao().getByKey(session, reportSnapshotId)); + List<SnapshotDto> snapshots = dbClient.snapshotDao().selectSnapshotAndChildrenOfProjectScope(session, reportSnapshotId); for (SnapshotDto snapshot : snapshots) { - SnapshotDto previousLastSnapshot = dbClient.snapshotDao().getLastSnapshot(session, snapshot); + SnapshotDto previousLastSnapshot = dbClient.snapshotDao().selectLastSnapshotByComponentId(session, snapshot.getComponentId()); if (previousLastSnapshot != null) { dbClient.snapshotDao().updateSnapshotAndChildrenLastFlag(session, previousLastSnapshot, false); session.commit(); @@ -69,8 +68,8 @@ public class SwitchSnapshotStep implements ComputationStep { private void enableCurrentSnapshot(DbSession session, long reportSnapshotId) { SnapshotDao dao = dbClient.snapshotDao(); - SnapshotDto snapshot = dao.getByKey(session, reportSnapshotId); - SnapshotDto previousLastSnapshot = dao.getLastSnapshot(session, snapshot); + SnapshotDto snapshot = dao.selectById(session, reportSnapshotId); + SnapshotDto previousLastSnapshot = dao.selectLastSnapshotByComponentId(session, snapshot.getComponentId()); boolean isLast = dao.isLast(snapshot, previousLastSnapshot); dao.updateSnapshotAndChildrenLastFlagAndStatus(session, snapshot, isLast, SnapshotDto.STATUS_PROCESSED); diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java index fa4ebf8362f..8b11e056300 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentNavigationAction.java @@ -109,7 +109,7 @@ public class ComponentNavigationAction implements NavigationWsAction { userSession.checkProjectUuidPermission(UserRole.USER, component.projectUuid()); - SnapshotDto snapshot = dbClient.snapshotDao().getLastSnapshot(session, new SnapshotDto().setComponentId(component.getId())); + SnapshotDto snapshot = dbClient.snapshotDao().selectLastSnapshotByComponentId(session, component.getId()); JsonWriter json = response.newJsonWriter(); json.beginObject(); @@ -259,7 +259,7 @@ public class ComponentNavigationAction implements NavigationWsAction { if (snapshot != null) { SnapshotDto currentSnapshot = snapshot; while (currentSnapshot.getParentId() != null) { - currentSnapshot = dbClient.snapshotDao().getByKey(session, currentSnapshot.getParentId()); + currentSnapshot = dbClient.snapshotDao().selectById(session, currentSnapshot.getParentId()); componentPath.add(0, dbClient.componentDao().selectById(currentSnapshot.getComponentId(), session)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/db/SnapshotDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/db/SnapshotDaoTest.java index ae05766e634..019d376fdcb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/db/SnapshotDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/db/SnapshotDaoTest.java @@ -59,7 +59,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void get_by_key() { setupData("shared"); - SnapshotDto result = sut.getNullableByKey(session, 3L); + SnapshotDto result = sut.selectNullableById(session, 3L); assertThat(result).isNotNull(); assertThat(result.getId()).isEqualTo(3L); assertThat(result.getComponentId()).isEqualTo(3L); @@ -94,7 +94,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { assertThat(result.getCreatedAt()).isEqualTo(1228172400000L); assertThat(result.getBuildDate()).isEqualTo(1317247200000L); - assertThat(sut.getNullableByKey(session, 999L)).isNull(); + assertThat(sut.selectNullableById(session, 999L)).isNull(); } @Test @@ -116,7 +116,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void lastSnapshot_returns_null_when_no_last_snapshot() { setupData("empty"); - SnapshotDto snapshot = sut.getLastSnapshot(session, defaultSnapshot()); + SnapshotDto snapshot = sut.selectLastSnapshotByComponentId(session, 123L); assertThat(snapshot).isNull(); } @@ -125,7 +125,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void lastSnapshot_from_one_resource() { setupData("snapshots"); - SnapshotDto snapshot = sut.getLastSnapshot(session, defaultSnapshot().setComponentId(2L)); + SnapshotDto snapshot = sut.selectLastSnapshotByComponentId(session, 2L); assertThat(snapshot).isNotNull(); assertThat(snapshot.getId()).isEqualTo(4L); @@ -135,39 +135,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void lastSnapshot_from_one_resource_without_last_is_null() { setupData("snapshots"); - SnapshotDto snapshot = sut.getLastSnapshot(session, defaultSnapshot().setComponentId(5L)); - - assertThat(snapshot).isNull(); - } - - @Test - public void no_last_snapshot_older_than_another_one_in_a_empty_table() { - setupData("empty"); - - SnapshotDto snapshot = sut.getLastSnapshotOlderThan(session, defaultSnapshot()); - - assertThat(snapshot).isNull(); - } - - @Test - public void last_snapshot_older__than_a_reference() { - setupData("snapshots"); - - SnapshotDto referenceSnapshot = defaultSnapshot().setComponentId(1L); - referenceSnapshot.setCreatedAt(DateUtils.parseDate("2008-12-03").getTime()); - SnapshotDto snapshot = sut.getLastSnapshotOlderThan(session, referenceSnapshot); - - assertThat(snapshot).isNotNull(); - assertThat(snapshot.getId()).isEqualTo(1L); - } - - @Test - public void last_snapshot_earlier__than_a_reference() { - setupData("snapshots"); - - SnapshotDto referenceSnapshot = defaultSnapshot().setComponentId(1L); - referenceSnapshot.setCreatedAt(DateUtils.parseDate("2008-12-01").getTime()); - SnapshotDto snapshot = sut.getLastSnapshotOlderThan(session, referenceSnapshot); + SnapshotDto snapshot = sut.selectLastSnapshotByComponentId(session, 5L); assertThat(snapshot).isNull(); } @@ -176,7 +144,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void snapshot_and_child_retrieved() { setupData("snapshots"); - List<SnapshotDto> snapshots = sut.findSnapshotAndChildrenOfProjectScope(session, defaultSnapshot().setId(1L)); + List<SnapshotDto> snapshots = sut.selectSnapshotAndChildrenOfProjectScope(session, 1L); assertThat(snapshots).isNotEmpty(); assertThat(snapshots).extracting("id").containsOnly(1L, 6L); @@ -190,7 +158,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { sut.updateSnapshotAndChildrenLastFlagAndStatus(session, snapshot, false, SnapshotDto.STATUS_PROCESSED); session.commit(); - List<SnapshotDto> snapshots = sut.findSnapshotAndChildrenOfProjectScope(session, snapshot); + List<SnapshotDto> snapshots = sut.selectSnapshotAndChildrenOfProjectScope(session, 1L); assertThat(snapshots).hasSize(2); assertThat(snapshots).extracting("id").containsOnly(1L, 6L); assertThat(snapshots).extracting("last").containsOnly(false); @@ -205,7 +173,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { sut.updateSnapshotAndChildrenLastFlag(session, snapshot, false); session.commit(); - List<SnapshotDto> snapshots = sut.findSnapshotAndChildrenOfProjectScope(session, snapshot); + List<SnapshotDto> snapshots = sut.selectSnapshotAndChildrenOfProjectScope(session, 1L); assertThat(snapshots).hasSize(2); assertThat(snapshots).extracting("id").containsOnly(1L, 6L); assertThat(snapshots).extracting("last").containsOnly(false); diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java index 40385f5e2e6..f9f7c3c4166 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java @@ -75,8 +75,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.server.project.ws.BulkDeleteAction.PARAM_KEYS; import static org.sonar.server.project.ws.BulkDeleteAction.PARAM_IDS; +import static org.sonar.server.project.ws.BulkDeleteAction.PARAM_KEYS; @Category(DbTests.class) public class BulkDeleteActionTest { @@ -132,10 +132,10 @@ public class BulkDeleteActionTest { assertThat(dbClient.componentDao().selectByUuids(dbSession, Arrays.asList("project-uuid-1", "project-uuid-3", "project-uuid-4"))).isEmpty(); assertThat(dbClient.componentDao().selectByUuid(dbSession, "project-uuid-2")).isNotNull(); - assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshotId1)).isNull(); - assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshotId3)).isNull(); - assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshotId4)).isNull(); - assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshotId2)).isNotNull(); + assertThat(dbClient.snapshotDao().selectNullableById(dbSession, snapshotId1)).isNull(); + assertThat(dbClient.snapshotDao().selectNullableById(dbSession, snapshotId3)).isNull(); + assertThat(dbClient.snapshotDao().selectNullableById(dbSession, snapshotId4)).isNull(); + assertThat(dbClient.snapshotDao().selectNullableById(dbSession, snapshotId2)).isNotNull(); assertThat(dbClient.issueDao().selectByKeys(dbSession, Arrays.asList("issue-key-1", "issue-key-3", "issue-key-4"))).isEmpty(); assertThat(dbClient.issueDao().selectByKey(dbSession, "issue-key-2")).isNotNull(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java index 404e52eba11..1bdb9c6fb92 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java @@ -73,8 +73,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.server.project.ws.DeleteAction.PARAM_KEY; import static org.sonar.server.project.ws.DeleteAction.PARAM_ID; +import static org.sonar.server.project.ws.DeleteAction.PARAM_KEY; @Category(DbTests.class) public class DeleteActionTest { @@ -128,8 +128,8 @@ public class DeleteActionTest { assertThat(dbClient.componentDao().selectNullableByUuid(dbSession, "project-uuid-1")).isNull(); assertThat(dbClient.componentDao().selectByUuid(dbSession, "project-uuid-2")).isNotNull(); - assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshotId1)).isNull(); - assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshotId2)).isNotNull(); + assertThat(dbClient.snapshotDao().selectNullableById(dbSession, snapshotId1)).isNull(); + assertThat(dbClient.snapshotDao().selectNullableById(dbSession, snapshotId2)).isNotNull(); assertThat(dbClient.issueDao().selectNullableByKey(dbSession, "issue-key-1")).isNull(); assertThat(dbClient.issueDao().selectByKey(dbSession, "issue-key-2")).isNotNull(); } |