From e8edd1d603b0aa54d2f43e046780f7d6981d5d0e Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 27 Oct 2014 11:29:27 +0100 Subject: [PATCH] Improve code coverage --- .../org/sonar/server/component/db/SnapshotDao.java | 10 ++++++++-- .../org/sonar/server/component/db/SnapshotDaoTest.java | 9 +++++++++ 2 files changed, 17 insertions(+), 2 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 1bc35c43525..5b5d45d0d4b 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 @@ -20,6 +20,7 @@ package org.sonar.server.component.db; +import com.google.common.annotations.VisibleForTesting; import org.sonar.api.ServerComponent; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; @@ -68,7 +69,7 @@ public class SnapshotDao extends BaseDao impl public int updateSnapshotAndChildrenLastFlagAndStatus(DbSession session, SnapshotDto snapshot, boolean isLast, String status) { Long rootId = snapshot.getId(); String path = snapshot.getPath() + snapshot.getId() + ".%"; - Long pathRootId = snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId(); + Long pathRootId = componentRootIdOrSelfIfRootOf(snapshot); return mapper(session).updateSnapshotAndChildrenLastFlagAndStatus(rootId, pathRootId, path, isLast, status); } @@ -76,8 +77,13 @@ public class SnapshotDao extends BaseDao impl public int updateSnapshotAndChildrenLastFlag(DbSession session, SnapshotDto snapshot, boolean isLast) { Long rootId = snapshot.getId(); String path = snapshot.getPath() + snapshot.getId() + ".%"; - Long pathRootId = snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId(); + Long pathRootId = componentRootIdOrSelfIfRootOf(snapshot); return mapper(session).updateSnapshotAndChildrenLastFlag(rootId, pathRootId, path, isLast); } + + @VisibleForTesting + Long componentRootIdOrSelfIfRootOf(SnapshotDto snapshot) { + return snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId(); + } } 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 bd7f44380f5..c244a116f73 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 @@ -211,4 +211,13 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { assertThat(snapshots).onProperty("id").containsOnly(1L, 6L); assertThat(snapshots).onProperty("last").containsOnly(false); } + + @Test + public void root_id_of_a_component_is_himself_if_root_id_is_null_root_id_otherwise() { + SnapshotDto snapshotWithRootId = defaultSnapshot().setId(3L).setRootId(1L); + assertThat(dao.componentRootIdOrSelfIfRootOf(snapshotWithRootId)).isEqualTo(1L); + + SnapshotDto snapshotWithNullRootId = defaultSnapshot().setId(3L).setRootId(null); + assertThat(dao.componentRootIdOrSelfIfRootOf(snapshotWithNullRootId)).isEqualTo(3L); + } } -- 2.39.5