diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2014-10-31 16:33:15 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2014-10-31 16:33:30 +0100 |
commit | 605455cb03c5cf67a8c2e4a1d8ba1b7b90f4c63b (patch) | |
tree | 45e2feb4b95629ca362af1c0e1e988085e07380c | |
parent | 0ffd9d683485c9a179e83ed4a466370c6036d1fe (diff) | |
download | sonarqube-605455cb03c5cf67a8c2e4a1d8ba1b7b90f4c63b.tar.gz sonarqube-605455cb03c5cf67a8c2e4a1d8ba1b7b90f4c63b.zip |
Improve branch code coverage on snapshot related classes
6 files changed, 92 insertions, 28 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 5b5d45d0d4b..5379396bcae 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,7 +20,6 @@ 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; @@ -31,6 +30,7 @@ import org.sonar.core.persistence.DbSession; import org.sonar.server.db.BaseDao; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import java.util.List; @@ -69,7 +69,7 @@ public class SnapshotDao extends BaseDao<SnapshotMapper, SnapshotDto, Long> impl public int updateSnapshotAndChildrenLastFlagAndStatus(DbSession session, SnapshotDto snapshot, boolean isLast, String status) { Long rootId = snapshot.getId(); String path = snapshot.getPath() + snapshot.getId() + ".%"; - Long pathRootId = componentRootIdOrSelfIfRootOf(snapshot); + Long pathRootId = snapshot.getRootIdOrSelf(); return mapper(session).updateSnapshotAndChildrenLastFlagAndStatus(rootId, pathRootId, path, isLast, status); } @@ -77,13 +77,12 @@ public class SnapshotDao extends BaseDao<SnapshotMapper, SnapshotDto, Long> impl public int updateSnapshotAndChildrenLastFlag(DbSession session, SnapshotDto snapshot, boolean isLast) { Long rootId = snapshot.getId(); String path = snapshot.getPath() + snapshot.getId() + ".%"; - Long pathRootId = componentRootIdOrSelfIfRootOf(snapshot); + Long pathRootId = snapshot.getRootIdOrSelf(); return mapper(session).updateSnapshotAndChildrenLastFlag(rootId, pathRootId, path, isLast); } - @VisibleForTesting - Long componentRootIdOrSelfIfRootOf(SnapshotDto snapshot) { - return snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId(); + public boolean isLast(SnapshotDto snapshotTested, @Nullable SnapshotDto previousLastSnapshot) { + return previousLastSnapshot == null || previousLastSnapshot.getCreatedAt().before(snapshotTested.getCreatedAt()); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java index c1109ed6310..23481749826 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/SwitchSnapshotStep.java @@ -70,7 +70,7 @@ public class SwitchSnapshotStep implements ComputationStep { SnapshotDto snapshot = dao.getByKey(session, report.getSnapshotId()); SnapshotDto previousLastSnapshot = dao.getLastSnapshot(session, snapshot); - boolean isLast = previousLastSnapshot == null || previousLastSnapshot.getCreatedAt().before(snapshot.getCreatedAt()); + boolean isLast = dao.isLast(snapshot, previousLastSnapshot); dao.updateSnapshotAndChildrenLastFlagAndStatus(session, snapshot, isLast, SnapshotDto.STATUS_PROCESSED); session.commit(); } 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 c244a116f73..f799082745f 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 @@ -28,7 +28,9 @@ import org.sonar.api.utils.System2; import org.sonar.core.component.SnapshotDto; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.persistence.DbSession; +import org.sonar.server.component.SnapshotTesting; +import java.util.Date; import java.util.List; import static org.fest.assertions.Assertions.assertThat; @@ -40,7 +42,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { DbSession session; - SnapshotDao dao; + SnapshotDao sut; System2 system2; @@ -48,7 +50,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void createDao() throws Exception { session = getMyBatis().openSession(false); system2 = mock(System2.class); - dao = new SnapshotDao(system2); + sut = new SnapshotDao(system2); } @After @@ -60,7 +62,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void get_by_key() { setupData("shared"); - SnapshotDto result = dao.getNullableByKey(session, 3L); + SnapshotDto result = sut.getNullableByKey(session, 3L); assertThat(result).isNotNull(); assertThat(result.getId()).isEqualTo(3L); assertThat(result.getResourceId()).isEqualTo(3L); @@ -95,7 +97,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { assertThat(result.getCreatedAt()).isEqualTo(DateUtils.parseDate("2008-12-02")); assertThat(result.getBuildDate()).isEqualTo(DateUtils.parseDate("2011-09-29")); - assertThat(dao.getNullableByKey(session, 999L)).isNull(); + assertThat(sut.getNullableByKey(session, 999L)).isNull(); } @Test @@ -106,7 +108,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { SnapshotDto dto = defaultSnapshot(); - dao.insert(session, dto); + sut.insert(session, dto); session.commit(); assertThat(dto.getId()).isNotNull(); @@ -117,7 +119,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void lastSnapshot_returns_null_when_no_last_snapshot() { setupData("empty"); - SnapshotDto snapshot = dao.getLastSnapshot(session, defaultSnapshot()); + SnapshotDto snapshot = sut.getLastSnapshot(session, defaultSnapshot()); assertThat(snapshot).isNull(); } @@ -126,7 +128,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void lastSnapshot_from_one_resource() { setupData("snapshots"); - SnapshotDto snapshot = dao.getLastSnapshot(session, defaultSnapshot().setResourceId(2L)); + SnapshotDto snapshot = sut.getLastSnapshot(session, defaultSnapshot().setResourceId(2L)); assertThat(snapshot).isNotNull(); assertThat(snapshot.getId()).isEqualTo(4L); @@ -136,7 +138,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void lastSnapshot_from_one_resource_without_last_is_null() { setupData("snapshots"); - SnapshotDto snapshot = dao.getLastSnapshot(session, defaultSnapshot().setResourceId(5L)); + SnapshotDto snapshot = sut.getLastSnapshot(session, defaultSnapshot().setResourceId(5L)); assertThat(snapshot).isNull(); } @@ -145,7 +147,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void no_last_snapshot_older_than_another_one_in_a_empty_table() { setupData("empty"); - SnapshotDto snapshot = dao.getLastSnapshotOlderThan(session, defaultSnapshot()); + SnapshotDto snapshot = sut.getLastSnapshotOlderThan(session, defaultSnapshot()); assertThat(snapshot).isNull(); } @@ -156,7 +158,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { SnapshotDto referenceSnapshot = defaultSnapshot().setResourceId(1L); referenceSnapshot.setCreatedAt(DateUtils.parseDate("2008-12-03")); - SnapshotDto snapshot = dao.getLastSnapshotOlderThan(session, referenceSnapshot); + SnapshotDto snapshot = sut.getLastSnapshotOlderThan(session, referenceSnapshot); assertThat(snapshot).isNotNull(); assertThat(snapshot.getId()).isEqualTo(1L); @@ -168,7 +170,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { SnapshotDto referenceSnapshot = defaultSnapshot().setResourceId(1L); referenceSnapshot.setCreatedAt(DateUtils.parseDate("2008-12-01")); - SnapshotDto snapshot = dao.getLastSnapshotOlderThan(session, referenceSnapshot); + SnapshotDto snapshot = sut.getLastSnapshotOlderThan(session, referenceSnapshot); assertThat(snapshot).isNull(); } @@ -177,7 +179,7 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { public void snapshot_and_child_retrieved() { setupData("snapshots"); - List<SnapshotDto> snapshots = dao.findSnapshotAndChildrenOfProjectScope(session, defaultSnapshot().setId(1L)); + List<SnapshotDto> snapshots = sut.findSnapshotAndChildrenOfProjectScope(session, defaultSnapshot().setId(1L)); assertThat(snapshots).isNotEmpty(); assertThat(snapshots).onProperty("id").containsOnly(1L, 6L); @@ -188,10 +190,10 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { setupData("snapshots"); SnapshotDto snapshot = defaultSnapshot().setId(1L); - dao.updateSnapshotAndChildrenLastFlagAndStatus(session, snapshot, false, SnapshotDto.STATUS_PROCESSED); + sut.updateSnapshotAndChildrenLastFlagAndStatus(session, snapshot, false, SnapshotDto.STATUS_PROCESSED); session.commit(); - List<SnapshotDto> snapshots = dao.findSnapshotAndChildrenOfProjectScope(session, snapshot); + List<SnapshotDto> snapshots = sut.findSnapshotAndChildrenOfProjectScope(session, snapshot); assertThat(snapshots).hasSize(2); assertThat(snapshots).onProperty("id").containsOnly(1L, 6L); assertThat(snapshots).onProperty("last").containsOnly(false); @@ -203,21 +205,47 @@ public class SnapshotDaoTest extends AbstractDaoTestCase { setupData("snapshots"); SnapshotDto snapshot = defaultSnapshot().setId(1L); - dao.updateSnapshotAndChildrenLastFlag(session, snapshot, false); + sut.updateSnapshotAndChildrenLastFlag(session, snapshot, false); session.commit(); - List<SnapshotDto> snapshots = dao.findSnapshotAndChildrenOfProjectScope(session, snapshot); + List<SnapshotDto> snapshots = sut.findSnapshotAndChildrenOfProjectScope(session, snapshot); assertThat(snapshots).hasSize(2); 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); + public void is_last_snapshot_when_no_previous_snapshot() { + SnapshotDto snapshot = SnapshotTesting.defaultSnapshot(); - SnapshotDto snapshotWithNullRootId = defaultSnapshot().setId(3L).setRootId(null); - assertThat(dao.componentRootIdOrSelfIfRootOf(snapshotWithNullRootId)).isEqualTo(3L); + boolean isLast = sut.isLast(snapshot, null); + + assertThat(isLast).isTrue(); + } + + @Test + public void is_last_snapshot_when_previous_snapshot_is_older() { + Date today = new Date(); + Date yesterday = DateUtils.addDays(today, -1); + + SnapshotDto snapshot = SnapshotTesting.defaultSnapshot().setCreatedAt(today); + SnapshotDto previousLastSnapshot = SnapshotTesting.defaultSnapshot().setCreatedAt(yesterday); + + boolean isLast = sut.isLast(snapshot, previousLastSnapshot); + + assertThat(isLast).isTrue(); + } + + @Test + public void is_not_last_snapshot_when_previous_snapshot_is_newer() { + Date today = new Date(); + Date yesterday = DateUtils.addDays(today, -1); + + SnapshotDto snapshot = SnapshotTesting.defaultSnapshot().setCreatedAt(yesterday); + SnapshotDto previousLastSnapshot = SnapshotTesting.defaultSnapshot().setCreatedAt(today); + + boolean isLast = sut.isLast(snapshot, previousLastSnapshot); + + assertThat(isLast).isFalse(); } } diff --git a/sonar-core/src/main/java/org/sonar/core/component/SnapshotDto.java b/sonar-core/src/main/java/org/sonar/core/component/SnapshotDto.java index 3cd77466dd8..a9b5aaa5a56 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/SnapshotDto.java +++ b/sonar-core/src/main/java/org/sonar/core/component/SnapshotDto.java @@ -317,4 +317,20 @@ public final class SnapshotDto extends Dto<Long> { public Long getKey() { return id; } + + @Override + public SnapshotDto setCreatedAt(Date datetime) { + super.setCreatedAt(datetime); + return this; + } + + @Override + public SnapshotDto setUpdatedAt(Date datetime) { + super.setUpdatedAt(datetime); + return this; + } + + public Long getRootIdOrSelf() { + return getRootId() == null ? getId() : getRootId(); + } } diff --git a/sonar-core/src/test/java/org/sonar/core/component/SnapshotDtoTest.java b/sonar-core/src/test/java/org/sonar/core/component/SnapshotDtoTest.java index 4ba5618578f..ac13ac5a2b8 100644 --- a/sonar-core/src/test/java/org/sonar/core/component/SnapshotDtoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/component/SnapshotDtoTest.java @@ -88,4 +88,21 @@ public class SnapshotDtoTest { assertThat(snapshotDto.getPeriodDate(5)).isEqualTo(DateUtils.parseDate("2014-06-05")); } + @Test + public void get_root_id_if_when_it_is_not_null() { + SnapshotDto snapshot = new SnapshotDto().setRootId(123L).setId(456L); + + Long rootIdOrSelf = snapshot.getRootIdOrSelf(); + + assertThat(rootIdOrSelf).isEqualTo(123L); + } + + @Test + public void getRootIdOrSelf_return_own_id_when_root_id_is_null() { + SnapshotDto snapshot = new SnapshotDto().setRootId(null).setId(456L); + + Long rootIdOrSelf = snapshot.getRootIdOrSelf(); + + assertThat(rootIdOrSelf).isEqualTo(456L); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java index 3985ddafeb9..a36ce0a44ea 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/DateUtils.java @@ -130,6 +130,10 @@ public final class DateUtils { return datetime; } + public static Date addDays(Date date, int numberOfDays) { + return org.apache.commons.lang.time.DateUtils.addDays(date, numberOfDays); + } + static class ThreadSafeDateFormat extends DateFormat { private final String format; private final ThreadLocal<Reference<DateFormat>> cache = new ThreadLocal<Reference<DateFormat>>() { |